forestEditorCtrl.cpp
Engine/source/forest/editor/forestEditorCtrl.cpp
Public Variables
Public Functions
ConsoleDocClass(ForestEditorCtrl , "@brief The actual <a href="/coding/class/classforest/">Forest</a> Editor <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control\n\n</a>" "Editor use only, should not be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">modified.\n\n</a>" " @internal" )
DefineEngineMethod(ForestEditorCtrl , deleteMeshSafe , void , (const char *obj) , "( ForestItemData obj )" )
DefineEngineMethod(ForestEditorCtrl , getActiveTool , S32 , () , "()" )
DefineEngineMethod(ForestEditorCtrl , isDirty , bool , () , "" )
DefineEngineMethod(ForestEditorCtrl , setActiveForest , void , (const char *obj) , "( Forest obj )" )
DefineEngineMethod(ForestEditorCtrl , setActiveTool , void , (const char *toolName) , "( ForestTool tool )" )
DefineEngineMethod(ForestEditorCtrl , updateActiveForest , void , () , "()" )
FindDirtyForests(SceneObject * obj, void * key)
bool
findMeshReferences(SimObject * obj)
Detailed Description
Public Variables
ForestItemData * sKey
Public Functions
ConsoleDocClass(ForestEditorCtrl , "@brief The actual <a href="/coding/class/classforest/">Forest</a> Editor <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control\n\n</a>" "Editor use only, should not be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">modified.\n\n</a>" " @internal" )
DefineEngineMethod(ForestEditorCtrl , deleteMeshSafe , void , (const char *obj) , "( ForestItemData obj )" )
DefineEngineMethod(ForestEditorCtrl , getActiveTool , S32 , () , "()" )
DefineEngineMethod(ForestEditorCtrl , isDirty , bool , () , "" )
DefineEngineMethod(ForestEditorCtrl , setActiveForest , void , (const char *obj) , "( Forest obj )" )
DefineEngineMethod(ForestEditorCtrl , setActiveTool , void , (const char *toolName) , "( ForestTool tool )" )
DefineEngineMethod(ForestEditorCtrl , updateActiveForest , void , () , "()" )
FindDirtyForests(SceneObject * obj, void * key)
findMeshReferences(SimObject * obj)
IMPLEMENT_CONOBJECT(ForestEditorCtrl )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "forest/editor/forestEditorCtrl.h" 26 27#include "forest/editor/forestBrushTool.h" 28#include "console/consoleTypes.h" 29#include "console/engineAPI.h" 30#include "gui/core/guiCanvas.h" 31#include "windowManager/platformCursorController.h" 32#include "forest/editor/forestUndo.h" 33#include "gui/worldEditor/undoActions.h" 34 35 36IMPLEMENT_CONOBJECT( ForestEditorCtrl ); 37 38ConsoleDocClass( ForestEditorCtrl, 39 "@brief The actual Forest Editor control\n\n" 40 "Editor use only, should not be modified.\n\n" 41 "@internal" 42); 43 44ForestEditorCtrl::ForestEditorCtrl() 45{ 46 dMemset( &mLastEvent, 0, sizeof(Gui3DMouseEvent) ); 47} 48 49ForestEditorCtrl::~ForestEditorCtrl() 50{ 51} 52 53bool ForestEditorCtrl::onAdd() 54{ 55 if ( !Parent::onAdd() ) 56 return false; 57 58 return true; 59} 60 61void ForestEditorCtrl::initPersistFields() 62{ 63 Parent::initPersistFields(); 64} 65 66bool ForestEditorCtrl::onWake() 67{ 68 if ( !Parent::onWake() ) 69 return false; 70 71 // Push our default cursor on here once. 72 GuiCanvas *root = getRoot(); 73 if ( root ) 74 { 75 S32 currCursor = PlatformCursorController::curArrow; 76 77 PlatformWindow *window = root->getPlatformWindow(); 78 PlatformCursorController *controller = window->getCursorController(); 79 controller->pushCursor( currCursor ); 80 } 81 82 return true; 83} 84 85void ForestEditorCtrl::onSleep() 86{ 87 // Pop our default cursor off. 88 GuiCanvas *root = getRoot(); 89 if ( root ) 90 { 91 PlatformWindow *window = root->getPlatformWindow(); 92 PlatformCursorController *controller = window->getCursorController(); 93 controller->popCursor(); 94 } 95 96 Parent::onSleep(); 97} 98 99bool ForestEditorCtrl::updateActiveForest( bool createNew ) 100{ 101 Con::executef( this, "onActiveForestUpdated", mForest ? mForest->getIdString() : "", createNew ? "1" : "0" ); 102 103 if ( mTool ) 104 mTool->setActiveForest( mForest ); 105 106 return mForest; 107} 108 109void ForestEditorCtrl::setActiveTool( ForestTool *tool ) 110{ 111 if ( mTool ) 112 { 113 mTool->onDeactivated(); 114 } 115 116 mTool = tool; 117 118 if ( mTool ) 119 { 120 mTool->setActiveForest( mForest ); 121 mTool->setParentEditor( this ); 122 mTool->onActivated( mLastEvent ); 123 } 124} 125 126void ForestEditorCtrl::onMouseUp( const GuiEvent &event_ ) 127{ 128 Parent::onMouseUp( event_ ); 129} 130 131void ForestEditorCtrl::get3DCursor( GuiCursor *&cursor, 132 bool &visible, 133 const Gui3DMouseEvent &event_ ) 134{ 135 cursor = NULL; 136 visible = false; 137 138 GuiCanvas *root = getRoot(); 139 if ( !root ) 140 return; 141 142 S32 currCursor = PlatformCursorController::curArrow; 143 144 if ( root->mCursorChanged == currCursor ) 145 return; 146 147 PlatformWindow *window = root->getPlatformWindow(); 148 PlatformCursorController *controller = window->getCursorController(); 149 150 // We've already changed the cursor, 151 // so set it back before we change it again. 152 if( root->mCursorChanged != -1) 153 controller->popCursor(); 154 155 // Now change the cursor shape 156 controller->pushCursor(currCursor); 157 root->mCursorChanged = currCursor; 158} 159 160void ForestEditorCtrl::on3DMouseDown( const Gui3DMouseEvent &evt ) 161{ 162 if ( !mForest && !updateActiveForest( true ) ) 163 return; 164 165 if ( mTool ) 166 mTool->on3DMouseDown( evt ); 167 168 mouseLock(); 169} 170 171void ForestEditorCtrl::on3DMouseUp( const Gui3DMouseEvent &evt ) 172{ 173 if ( !isMouseLocked() ) 174 return; 175 176 if ( mTool ) 177 mTool->on3DMouseUp( evt ); 178 179 mouseUnlock(); 180} 181 182void ForestEditorCtrl::on3DMouseMove( const Gui3DMouseEvent &evt ) 183{ 184 if ( !mForest ) 185 updateActiveForest( false ); 186 187 if ( mTool ) 188 mTool->on3DMouseMove( evt ); 189} 190 191void ForestEditorCtrl::on3DMouseDragged( const Gui3DMouseEvent &evt ) 192{ 193 if ( mTool ) 194 mTool->on3DMouseDragged( evt ); 195} 196 197void ForestEditorCtrl::on3DMouseEnter( const Gui3DMouseEvent &evt ) 198{ 199 if ( mTool ) 200 mTool->on3DMouseEnter( evt ); 201} 202 203void ForestEditorCtrl::on3DMouseLeave( const Gui3DMouseEvent &evt ) 204{ 205 if ( mTool ) 206 mTool->on3DMouseLeave( evt ); 207} 208 209void ForestEditorCtrl::on3DRightMouseDown( const Gui3DMouseEvent &evt ) 210{ 211} 212 213void ForestEditorCtrl::on3DRightMouseUp( const Gui3DMouseEvent &evt ) 214{ 215} 216 217bool ForestEditorCtrl::onMouseWheelUp(const GuiEvent &event_) 218{ 219 if ( mTool ) 220 return mTool->onMouseWheel( event_ ); 221 222 return Parent::onMouseWheelUp( event_ ); 223} 224 225bool ForestEditorCtrl::onMouseWheelDown(const GuiEvent &event_) 226{ 227 if ( mTool ) 228 return mTool->onMouseWheel( event_ ); 229 230 return Parent::onMouseWheelDown( event_ ); 231} 232 233void ForestEditorCtrl::updateGuiInfo() 234{ 235 // Note: This is intended to be used for updating 236 // GuiControls with info before they are rendered. 237 238 SimObject *statusbar; 239 Sim::findObject( "EditorGuiStatusBar", statusbar ); 240 241 SimObject *selectionBar; 242 Sim::findObject( "EWorldEditorStatusBarSelection", selectionBar ); 243 244 String text; 245 246 if ( !mForest ) 247 { 248 if ( statusbar ) 249 Con::executef( statusbar, "setInfo", "Forest Editor. You have no Forest in your level; click anywhere in the scene to create one." ); 250 if ( selectionBar ) 251 Con::executef( selectionBar, "setInfo", "" ); 252 return; 253 } 254 255 if ( mTool ) 256 { 257 if ( mTool->updateGuiInfo() ) 258 return; 259 } 260 261 // Tool did not handle the update so we will. 262 263 if ( statusbar ) 264 Con::executef( statusbar, "setInfo", "Forest Editor." ); 265 if ( selectionBar ) 266 Con::executef( selectionBar, "setInfo", "" ); 267} 268 269void ForestEditorCtrl::renderScene( const RectI &updateRect ) 270{ 271 if ( mTool ) 272 mTool->onRender3D(); 273} 274 275void ForestEditorCtrl::updateGizmo() 276{ 277 if ( mTool ) 278 mTool->updateGizmo(); 279} 280 281void ForestEditorCtrl::renderGui( Point2I offset, const RectI &updateRect ) 282{ 283 if ( mTool ) 284 mTool->onRender2D(); 285} 286 287static ForestItemData* sKey = NULL; 288 289bool findMeshReferences( SimObject *obj ) 290{ 291 ForestBrushElement *element = dynamic_cast<ForestBrushElement*>(obj); 292 293 if ( element && element->mData == sKey ) 294 return true; 295 296 return false; 297} 298 299void ForestEditorCtrl::onUndoAction() 300{ 301 if ( mTool ) 302 mTool->onUndoAction(); 303 304 updateCollision(); 305} 306 307void ForestEditorCtrl::deleteMeshSafe( ForestItemData *mesh ) 308{ 309 UndoManager *undoMan = NULL; 310 if ( !Sim::findObject( "EUndoManager", undoMan ) ) 311 { 312 Con::errorf( "ForestEditorCtrl::deleteMeshSafe() - EUndoManager not found." ); 313 return; 314 } 315 316 // CompoundUndoAction which will delete the ForestItemData, ForestItem(s), and ForestBrushElement(s). 317 CompoundUndoAction *compoundAction = new CompoundUndoAction( "Delete Forest Mesh" ); 318 319 // Find ForestItem(s) referencing this datablock and add their deletion 320 // to the undo action. 321 if ( mForest ) 322 { 323 Vector<ForestItem> foundItems; 324 mForest->getData()->getItems( mesh, &foundItems ); 325 326 ForestDeleteUndoAction *itemAction = new ForestDeleteUndoAction( mForest->getData(), this ); 327 itemAction->removeItem( foundItems ); 328 compoundAction->addAction( itemAction ); 329 } 330 331 // Find ForestBrushElement(s) referencing this datablock. 332 SimGroup *brushGroup = ForestBrush::getGroup(); 333 sKey = mesh; 334 Vector<SimObject*> foundElements; 335 brushGroup->findObjectByCallback( &findMeshReferences, foundElements ); 336 337 // Add UndoAction to delete the ForestBrushElement(s) and the ForestItemData. 338 MEDeleteUndoAction *elementAction = new MEDeleteUndoAction(); 339 elementAction->deleteObject( foundElements ); 340 elementAction->deleteObject( mesh ); 341 342 // Add compound action to the UndoManager. Done. 343 undoMan->addAction( compoundAction ); 344 345 updateCollision(); 346} 347 348void ForestEditorCtrl::updateCollision() 349{ 350 if ( mForest ) 351 { 352 mForest->updateCollision(); 353 354 if ( mForest->getClientObject() ) 355 ((Forest*)(mForest->getClientObject()))->updateCollision(); 356 } 357} 358 359void FindDirtyForests( SceneObject *obj, void *key ) 360{ 361 Forest *forest = dynamic_cast<Forest*>(obj); 362 if ( forest && forest->getData()->isDirty() ) 363 *((bool*)(key)) = true; 364} 365 366bool ForestEditorCtrl::isDirty() 367{ 368 bool foundDirty = false; 369 gServerContainer.findObjects( EnvironmentObjectType, FindDirtyForests, (void*)&foundDirty ); 370 371 return foundDirty; 372} 373 374DefineEngineMethod( ForestEditorCtrl, updateActiveForest, void, (), , "()" ) 375{ 376 object->updateActiveForest( true ); 377} 378 379DefineEngineMethod( ForestEditorCtrl, setActiveTool, void, ( const char * toolName ), , "( ForestTool tool )" ) 380{ 381 ForestTool *tool = dynamic_cast<ForestTool*>( Sim::findObject( toolName ) ); 382 object->setActiveTool( tool ); 383} 384 385DefineEngineMethod( ForestEditorCtrl, getActiveTool, S32, (), , "()" ) 386{ 387 ForestTool *tool = object->getActiveTool(); 388 return tool ? tool->getId() : 0; 389} 390 391DefineEngineMethod( ForestEditorCtrl, deleteMeshSafe, void, ( const char * obj ), , "( ForestItemData obj )" ) 392{ 393 ForestItemData *db; 394 if ( !Sim::findObject( obj, db ) ) 395 return; 396 397 object->deleteMeshSafe( db ); 398} 399 400DefineEngineMethod( ForestEditorCtrl, isDirty, bool, (), , "" ) 401{ 402 return object->isDirty(); 403} 404 405DefineEngineMethod(ForestEditorCtrl, setActiveForest, void, (const char * obj), , "( Forest obj )") 406{ 407 Forest *forestObject; 408 if (!Sim::findObject(obj, forestObject)) 409 return; 410 411 object->setActiveForest(forestObject); 412} 413