guiEditCtrl.cpp 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/editor/guiEditCtrl.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/stream/memStream.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "gui/containers/guiScrollCtrl.h"
  30. #include "core/strings/stringUnit.h"
  31. #include "console/engineAPI.h"
  32. IMPLEMENT_CONOBJECT( GuiEditCtrl );
  33. ConsoleDocClass( GuiEditCtrl,
  34. "@brief Native side of the GUI editor.\n\n"
  35. "Editor use only.\n\n"
  36. "@internal"
  37. );
  38. IMPLEMENT_CALLBACK( GuiEditCtrl, onHierarchyChanged, void, (), (),
  39. "" );
  40. IMPLEMENT_CALLBACK( GuiEditCtrl, onDelete, void, (), (),
  41. "" );
  42. IMPLEMENT_CALLBACK( GuiEditCtrl, onPreEdit, void, ( SimSet* selection ), ( selection ),
  43. "" );
  44. IMPLEMENT_CALLBACK( GuiEditCtrl, onPostEdit, void, ( SimSet* selection ), ( selection ),
  45. "" );
  46. IMPLEMENT_CALLBACK( GuiEditCtrl, onClearSelected, void, (), (),
  47. "" )
  48. IMPLEMENT_CALLBACK( GuiEditCtrl, onSelect, void, ( GuiControl* control ), ( control ),
  49. "" );
  50. IMPLEMENT_CALLBACK( GuiEditCtrl, onAddSelected, void, ( GuiControl* control ), ( control ),
  51. "" );
  52. IMPLEMENT_CALLBACK( GuiEditCtrl, onRemoveSelected, void, ( GuiControl* control ), ( control ),
  53. "" );
  54. IMPLEMENT_CALLBACK( GuiEditCtrl, onPreSelectionNudged, void, ( SimSet* selection ), ( selection ),
  55. "" );
  56. IMPLEMENT_CALLBACK( GuiEditCtrl, onPostSelectionNudged, void, ( SimSet* selection ), ( selection ),
  57. "" );
  58. IMPLEMENT_CALLBACK( GuiEditCtrl, onSelectionMoved, void, ( GuiControl* control ), ( control ),
  59. "" );
  60. IMPLEMENT_CALLBACK( GuiEditCtrl, onSelectionCloned, void, ( SimSet* selection ), ( selection ),
  61. "" );
  62. IMPLEMENT_CALLBACK( GuiEditCtrl, onTrashSelection, void, ( SimSet* selection ), ( selection ),
  63. "" );
  64. IMPLEMENT_CALLBACK( GuiEditCtrl, onAddNewCtrl, void, ( GuiControl* control ), ( control ),
  65. "" );
  66. IMPLEMENT_CALLBACK( GuiEditCtrl, onAddNewCtrlSet, void, ( SimSet* set ), ( set ),
  67. "" );
  68. IMPLEMENT_CALLBACK( GuiEditCtrl, onSelectionResized, void, ( GuiControl* control ), ( control ),
  69. "" );
  70. IMPLEMENT_CALLBACK( GuiEditCtrl, onFitIntoParent, void, ( bool width, bool height ), ( width, height ),
  71. "" );
  72. IMPLEMENT_CALLBACK( GuiEditCtrl, onMouseModeChange, void, (), (),
  73. "" );
  74. IMPLEMENT_CALLBACK( GuiEditCtrl, onControlInspectPreApply, void, ( GuiControl* control ), ( control ),
  75. "" );
  76. IMPLEMENT_CALLBACK( GuiEditCtrl, onControlInspectPostApply, void, ( GuiControl* control ), ( control ),
  77. "" );
  78. StringTableEntry GuiEditCtrl::smGuidesPropertyName[ 2 ];
  79. //-----------------------------------------------------------------------------
  80. GuiEditCtrl::GuiEditCtrl()
  81. : mCurrentAddSet( NULL ),
  82. mContentControl( NULL ),
  83. mGridSnap( 0, 0 ),
  84. mDragBeginPoint( -1, -1 ),
  85. mSnapToControls( true ),
  86. mSnapToEdges( true ),
  87. mSnapToCenters( true ),
  88. mSnapToGuides( true ),
  89. mSnapToCanvas( true ),
  90. mSnapSensitivity( 2 ),
  91. mFullBoxSelection( false ),
  92. mDrawBorderLines( true ),
  93. mDrawGuides( true )
  94. {
  95. VECTOR_SET_ASSOCIATION( mSelectedControls );
  96. VECTOR_SET_ASSOCIATION( mDragBeginPoints );
  97. VECTOR_SET_ASSOCIATION( mSnapHits[ 0 ] );
  98. VECTOR_SET_ASSOCIATION( mSnapHits[ 1 ] );
  99. mActive = true;
  100. mDotSB = NULL;
  101. mSnapped[ SnapVertical ] = false;
  102. mSnapped[ SnapHorizontal ] = false;
  103. mDragGuide[ GuideVertical ] = false;
  104. mDragGuide[ GuideHorizontal ] = false;
  105. if( !smGuidesPropertyName[ GuideVertical ] )
  106. smGuidesPropertyName[ GuideVertical ] = StringTable->insert( "guidesVertical" );
  107. if( !smGuidesPropertyName[ GuideHorizontal ] )
  108. smGuidesPropertyName[ GuideHorizontal ] = StringTable->insert( "guidesHorizontal" );
  109. }
  110. //-----------------------------------------------------------------------------
  111. void GuiEditCtrl::initPersistFields()
  112. {
  113. addGroup( "Snapping" );
  114. addField( "snapToControls", TypeBool, Offset( mSnapToControls, GuiEditCtrl ),
  115. "If true, edge and center snapping will work against controls." );
  116. addField( "snapToGuides", TypeBool, Offset( mSnapToGuides, GuiEditCtrl ),
  117. "If true, edge and center snapping will work against guides." );
  118. addField( "snapToCanvas", TypeBool, Offset( mSnapToCanvas, GuiEditCtrl ),
  119. "If true, edge and center snapping will work against canvas (toplevel control)." );
  120. addField( "snapToEdges", TypeBool, Offset( mSnapToEdges, GuiEditCtrl ),
  121. "If true, selection edges will snap into alignment when moved or resized." );
  122. addField( "snapToCenters", TypeBool, Offset( mSnapToCenters, GuiEditCtrl ),
  123. "If true, selection centers will snap into alignment when moved or resized." );
  124. addField( "snapSensitivity", TypeS32, Offset( mSnapSensitivity, GuiEditCtrl ),
  125. "Distance in pixels that edge and center snapping will work across." );
  126. endGroup( "Snapping" );
  127. addGroup( "Selection" );
  128. addField( "fullBoxSelection", TypeBool, Offset( mFullBoxSelection, GuiEditCtrl ),
  129. "If true, rectangle selection will only select controls fully inside the drag rectangle." );
  130. endGroup( "Selection" );
  131. addGroup( "Rendering" );
  132. addField( "drawBorderLines", TypeBool, Offset( mDrawBorderLines, GuiEditCtrl ),
  133. "If true, lines will be drawn extending along the edges of selected objects." );
  134. addField( "drawGuides", TypeBool, Offset( mDrawGuides, GuiEditCtrl ),
  135. "If true, guides will be included in rendering." );
  136. endGroup( "Rendering" );
  137. Parent::initPersistFields();
  138. }
  139. //=============================================================================
  140. // Events.
  141. //=============================================================================
  142. // MARK: ---- Events ----
  143. //-----------------------------------------------------------------------------
  144. bool GuiEditCtrl::onAdd()
  145. {
  146. if( !Parent::onAdd() )
  147. return false;
  148. mTrash = new SimGroup();
  149. mSelectedSet = new SimSet();
  150. if( !mTrash->registerObject() )
  151. return false;
  152. if( !mSelectedSet->registerObject() )
  153. return false;
  154. return true;
  155. }
  156. //-----------------------------------------------------------------------------
  157. void GuiEditCtrl::onRemove()
  158. {
  159. Parent::onRemove();
  160. mDotSB = NULL;
  161. mTrash->deleteObject();
  162. mSelectedSet->deleteObject();
  163. mTrash = NULL;
  164. mSelectedSet = NULL;
  165. }
  166. //-----------------------------------------------------------------------------
  167. bool GuiEditCtrl::onWake()
  168. {
  169. if (! Parent::onWake())
  170. return false;
  171. // Set GUI Controls to DesignTime mode
  172. GuiControl::smDesignTime = true;
  173. GuiControl::smEditorHandle = this;
  174. setEditMode(true);
  175. return true;
  176. }
  177. //-----------------------------------------------------------------------------
  178. void GuiEditCtrl::onSleep()
  179. {
  180. // Set GUI Controls to run time mode
  181. GuiControl::smDesignTime = false;
  182. GuiControl::smEditorHandle = NULL;
  183. Parent::onSleep();
  184. }
  185. //-----------------------------------------------------------------------------
  186. bool GuiEditCtrl::onKeyDown(const GuiEvent &event)
  187. {
  188. if (! mActive)
  189. return Parent::onKeyDown(event);
  190. if (!(event.modifier & SI_PRIMARY_CTRL))
  191. {
  192. switch(event.keyCode)
  193. {
  194. case KEY_BACKSPACE:
  195. case KEY_DELETE:
  196. deleteSelection();
  197. onDelete_callback();
  198. return true;
  199. default:
  200. break;
  201. }
  202. }
  203. return false;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void GuiEditCtrl::onMouseDown(const GuiEvent &event)
  207. {
  208. if (! mActive)
  209. {
  210. Parent::onMouseDown(event);
  211. return;
  212. }
  213. if(!mContentControl)
  214. return;
  215. setFirstResponder();
  216. mouseLock();
  217. mLastMousePos = globalToLocalCoord( event.mousePoint );
  218. // Check whether we've hit a guide. If so, start a guide drag.
  219. // Don't do this if SHIFT is down.
  220. if( !( event.modifier & SI_SHIFT ) )
  221. {
  222. for( U32 axis = 0; axis < 2; ++ axis )
  223. {
  224. const S32 guide = findGuide( ( guideAxis ) axis, event.mousePoint, 1 );
  225. if( guide != -1 )
  226. {
  227. setMouseMode( DragGuide );
  228. mDragGuide[ axis ] = true;
  229. mDragGuideIndex[ axis ] = guide;
  230. }
  231. }
  232. if( mMouseDownMode == DragGuide )
  233. return;
  234. }
  235. // Check whether we have hit a sizing knob on any of the currently selected
  236. // controls.
  237. for( U32 i = 0, num = mSelectedControls.size(); i < num; ++ i )
  238. {
  239. GuiControl* ctrl = mSelectedControls[ i ];
  240. Point2I cext = ctrl->getExtent();
  241. Point2I ctOffset = globalToLocalCoord( ctrl->localToGlobalCoord( Point2I( 0, 0 ) ) );
  242. RectI box( ctOffset.x, ctOffset.y, cext.x, cext.y );
  243. if( ( mSizingMode = ( GuiEditCtrl::sizingModes ) getSizingHitKnobs( mLastMousePos, box ) ) != 0 )
  244. {
  245. setMouseMode( SizingSelection );
  246. mLastDragPos = event.mousePoint;
  247. // undo
  248. onPreEdit_callback( getSelectedSet() );
  249. return;
  250. }
  251. }
  252. // Find the control we have hit.
  253. GuiControl* ctrl = mContentControl->findHitControl( mLastMousePos, getCurrentAddSet()->mLayer );
  254. // Give the control itself the opportunity to handle the event
  255. // to implement custom editing logic.
  256. bool handledEvent = ctrl->onMouseDownEditor( event, localToGlobalCoord( Point2I(0,0) ) );
  257. if( handledEvent == true )
  258. {
  259. // The Control handled the event and requested the edit ctrl
  260. // *NOT* act on it.
  261. return;
  262. }
  263. else if( event.modifier & SI_SHIFT )
  264. {
  265. // Shift is down. Start rectangle selection in add mode
  266. // no matter what we have hit.
  267. startDragRectangle( event.mousePoint );
  268. mDragAddSelection = true;
  269. }
  270. else if( selectionContains( ctrl ) )
  271. {
  272. // We hit a selected control. If the multiselect key is pressed,
  273. // deselect the control. Otherwise start a drag move.
  274. if( event.modifier & SI_MULTISELECT )
  275. {
  276. removeSelection( ctrl );
  277. //set the mode
  278. setMouseMode( Selecting );
  279. }
  280. else if( event.modifier & SI_PRIMARY_ALT )
  281. {
  282. // Alt is down. Start a drag clone.
  283. startDragClone( event.mousePoint );
  284. }
  285. else
  286. {
  287. startDragMove( event.mousePoint );
  288. }
  289. }
  290. else
  291. {
  292. // We clicked an unselected control.
  293. if( ctrl == getContentControl() )
  294. {
  295. // Clicked in toplevel control. Start a rectangle selection.
  296. startDragRectangle( event.mousePoint );
  297. mDragAddSelection = false;
  298. }
  299. else if( event.modifier & SI_PRIMARY_ALT && ctrl != getContentControl() )
  300. {
  301. // Alt is down. Start a drag clone.
  302. clearSelection();
  303. addSelection( ctrl );
  304. startDragClone( event.mousePoint );
  305. }
  306. else if( event.modifier & SI_MULTISELECT )
  307. addSelection( ctrl );
  308. else
  309. {
  310. // Clicked on child control. Start move.
  311. clearSelection();
  312. addSelection( ctrl );
  313. startDragMove( event.mousePoint );
  314. }
  315. }
  316. }
  317. //-----------------------------------------------------------------------------
  318. void GuiEditCtrl::onMouseUp(const GuiEvent &event)
  319. {
  320. if (! mActive || !mContentControl || !getCurrentAddSet() )
  321. {
  322. Parent::onMouseUp(event);
  323. return;
  324. }
  325. //find the control we clicked
  326. GuiControl *ctrl = mContentControl->findHitControl(mLastMousePos, getCurrentAddSet()->mLayer);
  327. bool handledEvent = ctrl->onMouseUpEditor( event, localToGlobalCoord( Point2I(0,0) ) );
  328. if( handledEvent == true )
  329. {
  330. // The Control handled the event and requested the edit ctrl
  331. // *NOT* act on it. The dude abides.
  332. return;
  333. }
  334. //unlock the mouse
  335. mouseUnlock();
  336. // Reset Drag Axis Alignment Information
  337. mDragBeginPoint.set(-1,-1);
  338. mDragBeginPoints.clear();
  339. mLastMousePos = globalToLocalCoord(event.mousePoint);
  340. if( mMouseDownMode == DragGuide )
  341. {
  342. // Check to see if the mouse has moved off the canvas. If so,
  343. // remove the guides being dragged.
  344. for( U32 axis = 0; axis < 2; ++ axis )
  345. if( mDragGuide[ axis ] && !getContentControl()->getGlobalBounds().pointInRect( event.mousePoint ) )
  346. mGuides[ axis ].erase( mDragGuideIndex[ axis ] );
  347. }
  348. else if( mMouseDownMode == DragSelecting )
  349. {
  350. // If not multiselecting, clear the current selection.
  351. if( !( event.modifier & SI_MULTISELECT ) && !mDragAddSelection )
  352. clearSelection();
  353. RectI rect;
  354. getDragRect( rect );
  355. // If the region is somewhere less than at least 2x2, count this as a
  356. // normal, non-rectangular selection.
  357. if( rect.extent.x <= 2 && rect.extent.y <= 2 )
  358. addSelectControlAt( rect.point );
  359. else
  360. {
  361. // Use HIT_AddParentHits by default except if ALT is pressed.
  362. // Use HIT_ParentPreventsChildHit if ALT+CTRL is pressed.
  363. U32 hitFlags = 0;
  364. if( !( event.modifier & SI_PRIMARY_ALT ) )
  365. hitFlags |= HIT_AddParentHits;
  366. if( event.modifier & SI_PRIMARY_ALT && event.modifier & SI_CTRL )
  367. hitFlags |= HIT_ParentPreventsChildHit;
  368. addSelectControlsInRegion( rect, hitFlags );
  369. }
  370. }
  371. else if( ctrl == getContentControl() && mMouseDownMode == Selecting )
  372. setCurrentAddSet( NULL, true );
  373. // deliver post edit event if we've been editing
  374. // note: paxorr: this may need to be moved earlier, if the selection has changed.
  375. // undo
  376. if( mMouseDownMode == SizingSelection || ( mMouseDownMode == MovingSelection && mDragMoveUndo ) )
  377. onPostEdit_callback( getSelectedSet() );
  378. //reset the mouse mode
  379. setFirstResponder();
  380. setMouseMode( Selecting );
  381. mSizingMode = sizingNone;
  382. // Clear snapping state.
  383. mSnapped[ SnapVertical ] = false;
  384. mSnapped[ SnapHorizontal ] = false;
  385. mSnapTargets[ SnapVertical ] = NULL;
  386. mSnapTargets[ SnapHorizontal ] = NULL;
  387. // Clear guide drag state.
  388. mDragGuide[ GuideVertical ] = false;
  389. mDragGuide[ GuideHorizontal ] = false;
  390. }
  391. //-----------------------------------------------------------------------------
  392. void GuiEditCtrl::onMouseDragged( const GuiEvent &event )
  393. {
  394. if( !mActive || !mContentControl || !getCurrentAddSet() )
  395. {
  396. Parent::onMouseDragged(event);
  397. return;
  398. }
  399. Point2I mousePoint = globalToLocalCoord( event.mousePoint );
  400. //find the control we clicked
  401. GuiControl *ctrl = mContentControl->findHitControl( mousePoint, getCurrentAddSet()->mLayer );
  402. bool handledEvent = ctrl->onMouseDraggedEditor( event, localToGlobalCoord( Point2I(0,0) ) );
  403. if( handledEvent == true )
  404. {
  405. // The Control handled the event and requested the edit ctrl
  406. // *NOT* act on it. The dude abides.
  407. return;
  408. }
  409. // If we're doing a drag clone, see if we have crossed the move threshold. If so,
  410. // clone the selection and switch to move mode.
  411. if( mMouseDownMode == DragClone )
  412. {
  413. // If we haven't yet crossed the mouse delta to actually start the
  414. // clone, check if we have now.
  415. S32 delta = mAbs( ( mousePoint - mDragBeginPoint ).len() );
  416. if( delta >= 4 )
  417. {
  418. cloneSelection();
  419. mLastMousePos = mDragBeginPoint;
  420. mDragMoveUndo = false;
  421. setMouseMode( MovingSelection );
  422. }
  423. }
  424. if( mMouseDownMode == DragGuide )
  425. {
  426. for( U32 axis = 0; axis < 2; ++ axis )
  427. if( mDragGuide[ axis ] )
  428. {
  429. // Set the guide to the coordinate of the mouse cursor
  430. // on the guide's axis.
  431. Point2I point = event.mousePoint;
  432. point -= localToGlobalCoord( Point2I( 0, 0 ) );
  433. point[ axis ] = mClamp( point[ axis ], 0, getExtent()[ axis ] - 1 );
  434. mGuides[ axis ][ mDragGuideIndex[ axis ] ] = point[ axis ];
  435. }
  436. }
  437. else if( mMouseDownMode == SizingSelection )
  438. {
  439. // Snap the mouse cursor to grid if active. Do this on the mouse cursor so that we handle
  440. // incremental drags correctly.
  441. Point2I mousePoint = event.mousePoint;
  442. snapToGrid( mousePoint );
  443. Point2I delta = mousePoint - mLastDragPos;
  444. // If CTRL is down, apply smart snapping.
  445. if( event.modifier & SI_CTRL )
  446. {
  447. RectI selectionBounds = getSelectionBounds();
  448. doSnapping( event, selectionBounds, delta );
  449. }
  450. else
  451. {
  452. mSnapped[ SnapVertical ] = false;
  453. mSnapped[ SnapHorizontal ] = false;
  454. }
  455. // If ALT is down, do a move instead of a resize on the control
  456. // knob's axis. Otherwise resize.
  457. if( event.modifier & SI_PRIMARY_ALT )
  458. {
  459. if( !( mSizingMode & sizingLeft ) && !( mSizingMode & sizingRight ) )
  460. {
  461. mSnapped[ SnapVertical ] = false;
  462. delta.x = 0;
  463. }
  464. if( !( mSizingMode & sizingTop ) && !( mSizingMode & sizingBottom ) )
  465. {
  466. mSnapped[ SnapHorizontal ] = false;
  467. delta.y = 0;
  468. }
  469. moveSelection( delta );
  470. }
  471. else
  472. resizeControlsInSelectionBy( delta, mSizingMode );
  473. // Remember drag point.
  474. mLastDragPos = mousePoint;
  475. }
  476. else if (mMouseDownMode == MovingSelection && mSelectedControls.size())
  477. {
  478. Point2I delta = mousePoint - mLastMousePos;
  479. RectI selectionBounds = getSelectionBounds();
  480. // Apply snaps.
  481. doSnapping( event, selectionBounds, delta );
  482. //RDTODO: to me seems to be in need of revision
  483. // Do we want to align this drag to the X and Y axes within a certain threshold?
  484. if( event.modifier & SI_SHIFT && !( event.modifier & SI_PRIMARY_ALT ) )
  485. {
  486. Point2I dragTotalDelta = event.mousePoint - localToGlobalCoord( mDragBeginPoint );
  487. if( dragTotalDelta.y < 10 && dragTotalDelta.y > -10 )
  488. {
  489. for(S32 i = 0; i < mSelectedControls.size(); i++)
  490. {
  491. Point2I selCtrlPos = mSelectedControls[i]->getPosition();
  492. Point2I snapBackPoint( selCtrlPos.x, mDragBeginPoints[i].y);
  493. // This is kind of nasty but we need to snap back if we're not at origin point with selection - JDD
  494. if( selCtrlPos.y != mDragBeginPoints[i].y )
  495. mSelectedControls[i]->setPosition( snapBackPoint );
  496. }
  497. delta.y = 0;
  498. }
  499. if( dragTotalDelta.x < 10 && dragTotalDelta.x > -10 )
  500. {
  501. for(S32 i = 0; i < mSelectedControls.size(); i++)
  502. {
  503. Point2I selCtrlPos = mSelectedControls[i]->getPosition();
  504. Point2I snapBackPoint( mDragBeginPoints[i].x, selCtrlPos.y);
  505. // This is kind of nasty but we need to snap back if we're not at origin point with selection - JDD
  506. if( selCtrlPos.x != mDragBeginPoints[i].x )
  507. mSelectedControls[i]->setPosition( snapBackPoint );
  508. }
  509. delta.x = 0;
  510. }
  511. }
  512. if( delta.x || delta.y )
  513. moveSelection( delta, mDragMoveUndo );
  514. // find the current control under the mouse
  515. canHitSelectedControls( false );
  516. GuiControl *inCtrl = mContentControl->findHitControl(mousePoint, getCurrentAddSet()->mLayer);
  517. canHitSelectedControls( true );
  518. // find the nearest control up the heirarchy from the control the mouse is in
  519. // that is flagged as a container.
  520. while( !inCtrl->mIsContainer )
  521. inCtrl = inCtrl->getParent();
  522. // if the control under the mouse is not our parent, move the selected controls
  523. // into the new parent.
  524. if(mSelectedControls[0]->getParent() != inCtrl && inCtrl->mIsContainer)
  525. {
  526. moveSelectionToCtrl( inCtrl, mDragMoveUndo );
  527. setCurrentAddSet( inCtrl, false );
  528. }
  529. mLastMousePos += delta;
  530. }
  531. else
  532. mLastMousePos = mousePoint;
  533. }
  534. //-----------------------------------------------------------------------------
  535. void GuiEditCtrl::onRightMouseDown(const GuiEvent &event)
  536. {
  537. if (! mActive || !mContentControl)
  538. {
  539. Parent::onRightMouseDown(event);
  540. return;
  541. }
  542. setFirstResponder();
  543. //search for the control hit in any layer below the edit layer
  544. GuiControl *hitCtrl = mContentControl->findHitControl(globalToLocalCoord(event.mousePoint), mLayer - 1);
  545. if (hitCtrl != getCurrentAddSet())
  546. {
  547. setCurrentAddSet( hitCtrl );
  548. }
  549. // select the parent if we right-click on the current add set
  550. else if( getCurrentAddSet() != mContentControl)
  551. {
  552. setCurrentAddSet( hitCtrl->getParent() );
  553. select(hitCtrl);
  554. }
  555. //Design time mouse events
  556. GuiEvent designEvent = event;
  557. designEvent.mousePoint = mLastMousePos;
  558. hitCtrl->onRightMouseDownEditor( designEvent, localToGlobalCoord( Point2I(0,0) ) );
  559. }
  560. //=============================================================================
  561. // Rendering.
  562. //=============================================================================
  563. // MARK: ---- Rendering ----
  564. //-----------------------------------------------------------------------------
  565. void GuiEditCtrl::onPreRender()
  566. {
  567. setUpdate();
  568. }
  569. //-----------------------------------------------------------------------------
  570. void GuiEditCtrl::onRender(Point2I offset, const RectI &updateRect)
  571. {
  572. Point2I ctOffset;
  573. Point2I cext;
  574. bool keyFocused = isFirstResponder();
  575. GFXDrawUtil *drawer = GFX->getDrawUtil();
  576. if (mActive)
  577. {
  578. if( getCurrentAddSet() != getContentControl() )
  579. {
  580. // draw a white frame inset around the current add set.
  581. cext = getCurrentAddSet()->getExtent();
  582. ctOffset = getCurrentAddSet()->localToGlobalCoord(Point2I(0,0));
  583. RectI box(ctOffset.x, ctOffset.y, cext.x, cext.y);
  584. box.inset( -5, -5 );
  585. drawer->drawRect( box, ColorI( 50, 101, 152, 128 ) );
  586. box.inset( 1, 1 );
  587. drawer->drawRect( box, ColorI( 50, 101, 152, 128 ) );
  588. box.inset( 1, 1 );
  589. drawer->drawRect( box, ColorI( 50, 101, 152, 128 ) );
  590. box.inset( 1, 1 );
  591. drawer->drawRect( box, ColorI( 50, 101, 152, 128 ) );
  592. box.inset( 1, 1 );
  593. drawer->drawRect( box, ColorI( 50, 101, 152, 128 ) );
  594. }
  595. Vector<GuiControl *>::iterator i;
  596. bool multisel = mSelectedControls.size() > 1;
  597. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  598. {
  599. GuiControl *ctrl = (*i);
  600. cext = ctrl->getExtent();
  601. ctOffset = ctrl->localToGlobalCoord(Point2I(0,0));
  602. RectI box(ctOffset.x,ctOffset.y, cext.x, cext.y);
  603. ColorI nutColor = multisel ? ColorI( 255, 255, 255, 100 ) : ColorI( 0, 0, 0, 100 );
  604. ColorI outlineColor = multisel ? ColorI( 0, 0, 0, 100 ) : ColorI( 255, 255, 255, 100 );
  605. if(!keyFocused)
  606. nutColor.set( 128, 128, 128, 100 );
  607. drawNuts(box, outlineColor, nutColor);
  608. }
  609. }
  610. renderChildControls(offset, updateRect);
  611. // Draw selection rectangle.
  612. if( mActive && mMouseDownMode == DragSelecting )
  613. {
  614. RectI b;
  615. getDragRect(b);
  616. b.point += offset;
  617. // Draw outline.
  618. drawer->drawRect( b, ColorI( 100, 100, 100, 128 ) );
  619. // Draw fill.
  620. b.inset( 1, 1 );
  621. drawer->drawRectFill( b, ColorI( 150, 150, 150, 128 ) );
  622. }
  623. // Draw grid.
  624. if( mActive &&
  625. ( mMouseDownMode == MovingSelection || mMouseDownMode == SizingSelection ) &&
  626. ( mGridSnap.x || mGridSnap.y ) )
  627. {
  628. Point2I cext = getContentControl()->getExtent();
  629. Point2I coff = getContentControl()->localToGlobalCoord(Point2I(0,0));
  630. // create point-dots
  631. const Point2I& snap = mGridSnap;
  632. U32 maxdot = (U32)(mCeil(cext.x / (F32)snap.x) - 1) * (U32)(mCeil(cext.y / (F32)snap.y) - 1);
  633. if( mDots.isNull() || maxdot != mDots->mNumVerts)
  634. {
  635. mDots.set(GFX, maxdot, GFXBufferTypeStatic);
  636. U32 ndot = 0;
  637. mDots.lock();
  638. for(U32 ix = snap.x; ix < cext.x; ix += snap.x)
  639. {
  640. for(U32 iy = snap.y; ndot < maxdot && iy < cext.y; iy += snap.y)
  641. {
  642. mDots[ndot].color.set( 50, 50, 254, 100 );
  643. mDots[ndot].point.x = F32(ix + coff.x);
  644. mDots[ndot].point.y = F32(iy + coff.y);
  645. mDots[ndot].point.z = 0.0f;
  646. ndot++;
  647. }
  648. }
  649. mDots.unlock();
  650. AssertFatal(ndot <= maxdot, "dot overflow");
  651. AssertFatal(ndot == maxdot, "dot underflow");
  652. }
  653. if (!mDotSB)
  654. {
  655. GFXStateBlockDesc dotdesc;
  656. dotdesc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  657. dotdesc.setCullMode( GFXCullNone );
  658. mDotSB = GFX->createStateBlock( dotdesc );
  659. }
  660. GFX->setStateBlock(mDotSB);
  661. // draw the points.
  662. GFX->setVertexBuffer( mDots );
  663. GFX->drawPrimitive( GFXPointList, 0, mDots->mNumVerts );
  664. }
  665. // Draw snapping lines.
  666. if( mActive && getContentControl() )
  667. {
  668. RectI bounds = getContentControl()->getGlobalBounds();
  669. // Draw guide lines.
  670. if( mDrawGuides )
  671. {
  672. for( U32 axis = 0; axis < 2; ++ axis )
  673. {
  674. for( U32 i = 0, num = mGuides[ axis ].size(); i < num; ++ i )
  675. drawCrossSection( axis, mGuides[ axis ][ i ] + bounds.point[ axis ],
  676. bounds, ColorI( 0, 255, 0, 100 ), drawer );
  677. }
  678. }
  679. // Draw smart snap lines.
  680. for( U32 axis = 0; axis < 2; ++ axis )
  681. {
  682. if( mSnapped[ axis ] )
  683. {
  684. // Draw the snap line.
  685. drawCrossSection( axis, mSnapOffset[ axis ],
  686. bounds, ColorI( 0, 0, 255, 100 ), drawer );
  687. // Draw a border around the snap target control.
  688. if( mSnapTargets[ axis ] )
  689. {
  690. RectI bounds = mSnapTargets[ axis ]->getGlobalBounds();
  691. drawer->drawRect( bounds, ColorF( .5, .5, .5, .5 ) );
  692. }
  693. }
  694. }
  695. }
  696. }
  697. //-----------------------------------------------------------------------------
  698. void GuiEditCtrl::drawNuts(RectI &box, ColorI &outlineColor, ColorI &nutColor)
  699. {
  700. GFXDrawUtil *drawer = GFX->getDrawUtil();
  701. S32 lx = box.point.x, rx = box.point.x + box.extent.x - 1;
  702. S32 cx = (lx + rx) >> 1;
  703. S32 ty = box.point.y, by = box.point.y + box.extent.y - 1;
  704. S32 cy = (ty + by) >> 1;
  705. if( mDrawBorderLines )
  706. {
  707. ColorF lineColor( 0.7f, 0.7f, 0.7f, 0.25f );
  708. ColorF lightLineColor( 0.5f, 0.5f, 0.5f, 0.1f );
  709. if(lx > 0 && ty > 0)
  710. {
  711. drawer->drawLine(0, ty, lx, ty, lineColor); // Left edge to top-left corner.
  712. drawer->drawLine(lx, 0, lx, ty, lineColor); // Top edge to top-left corner.
  713. }
  714. if(lx > 0 && by > 0)
  715. drawer->drawLine(0, by, lx, by, lineColor); // Left edge to bottom-left corner.
  716. if(rx > 0 && ty > 0)
  717. drawer->drawLine(rx, 0, rx, ty, lineColor); // Top edge to top-right corner.
  718. Point2I extent = localToGlobalCoord(getExtent());
  719. if(lx < extent.x && by < extent.y)
  720. drawer->drawLine(lx, by, lx, extent.y, lightLineColor); // Bottom-left corner to bottom edge.
  721. if(rx < extent.x && by < extent.y)
  722. {
  723. drawer->drawLine(rx, by, rx, extent.y, lightLineColor); // Bottom-right corner to bottom edge.
  724. drawer->drawLine(rx, by, extent.x, by, lightLineColor); // Bottom-right corner to right edge.
  725. }
  726. if(rx < extent.x && ty < extent.y)
  727. drawer->drawLine(rx, ty, extent.x, ty, lightLineColor); // Top-right corner to right edge.
  728. }
  729. // Adjust nuts, so they dont straddle the controls.
  730. lx -= NUT_SIZE + 1;
  731. ty -= NUT_SIZE + 1;
  732. rx += 1;
  733. by += 1;
  734. // Draw nuts.
  735. drawNut( Point2I( lx - NUT_SIZE, ty - NUT_SIZE ), outlineColor, nutColor ); // Top left
  736. drawNut( Point2I( lx - NUT_SIZE, cy - NUT_SIZE / 2 ), outlineColor, nutColor ); // Mid left
  737. drawNut( Point2I( lx - NUT_SIZE, by ), outlineColor, nutColor ); // Bottom left
  738. drawNut( Point2I( rx, ty - NUT_SIZE ), outlineColor, nutColor ); // Top right
  739. drawNut( Point2I( rx, cy - NUT_SIZE / 2 ), outlineColor, nutColor ); // Mid right
  740. drawNut( Point2I( rx, by ), outlineColor, nutColor ); // Bottom right
  741. drawNut( Point2I( cx - NUT_SIZE / 2, ty - NUT_SIZE ), outlineColor, nutColor ); // Mid top
  742. drawNut( Point2I( cx - NUT_SIZE / 2, by ), outlineColor, nutColor ); // Mid bottom
  743. }
  744. //-----------------------------------------------------------------------------
  745. void GuiEditCtrl::drawNut(const Point2I &nut, ColorI &outlineColor, ColorI &nutColor)
  746. {
  747. RectI r( nut.x, nut.y, NUT_SIZE * 2, NUT_SIZE * 2 );
  748. GFX->getDrawUtil()->drawRect( r, outlineColor );
  749. r.inset( 1, 1 );
  750. GFX->getDrawUtil()->drawRectFill( r, nutColor );
  751. }
  752. //=============================================================================
  753. // Selections.
  754. //=============================================================================
  755. // MARK: ---- Selections ----
  756. //-----------------------------------------------------------------------------
  757. void GuiEditCtrl::clearSelection(void)
  758. {
  759. mSelectedControls.clear();
  760. onClearSelected_callback();
  761. }
  762. //-----------------------------------------------------------------------------
  763. void GuiEditCtrl::setSelection(GuiControl *ctrl, bool inclusive)
  764. {
  765. //sanity check
  766. if( !ctrl )
  767. return;
  768. if( mSelectedControls.size() == 1 && mSelectedControls[ 0 ] == ctrl )
  769. return;
  770. if( !inclusive )
  771. clearSelection();
  772. if( mContentControl == ctrl )
  773. setCurrentAddSet( ctrl, false );
  774. else
  775. addSelection( ctrl );
  776. }
  777. //-----------------------------------------------------------------------------
  778. void GuiEditCtrl::addSelection(S32 id)
  779. {
  780. GuiControl * ctrl;
  781. if( Sim::findObject( id, ctrl ) )
  782. addSelection( ctrl );
  783. }
  784. //-----------------------------------------------------------------------------
  785. void GuiEditCtrl::addSelection( GuiControl* ctrl )
  786. {
  787. // Only add if this isn't the content control and the
  788. // control isn't yet in the selection.
  789. if( ctrl != getContentControl() && !selectionContains( ctrl ) )
  790. {
  791. mSelectedControls.push_back( ctrl );
  792. if( mSelectedControls.size() == 1 )
  793. {
  794. // Update the add set.
  795. if( ctrl->mIsContainer )
  796. setCurrentAddSet( ctrl, false );
  797. else
  798. setCurrentAddSet( ctrl->getParent(), false );
  799. // Notify script.
  800. onSelect_callback( ctrl );
  801. }
  802. else
  803. {
  804. // Notify script.
  805. onAddSelected_callback( ctrl );
  806. }
  807. }
  808. }
  809. //-----------------------------------------------------------------------------
  810. void GuiEditCtrl::removeSelection( S32 id )
  811. {
  812. GuiControl * ctrl;
  813. if ( Sim::findObject( id, ctrl ) )
  814. removeSelection( ctrl );
  815. }
  816. //-----------------------------------------------------------------------------
  817. void GuiEditCtrl::removeSelection( GuiControl* ctrl )
  818. {
  819. if( selectionContains( ctrl ) )
  820. {
  821. Vector< GuiControl* >::iterator i = ::find( mSelectedControls.begin(), mSelectedControls.end(), ctrl );
  822. if ( i != mSelectedControls.end() )
  823. mSelectedControls.erase( i );
  824. onRemoveSelected_callback( ctrl );
  825. }
  826. }
  827. //-----------------------------------------------------------------------------
  828. void GuiEditCtrl::canHitSelectedControls( bool state )
  829. {
  830. for( U32 i = 0, num = mSelectedControls.size(); i < num; ++ i )
  831. mSelectedControls[ i ]->setCanHit( state );
  832. }
  833. //-----------------------------------------------------------------------------
  834. void GuiEditCtrl::moveSelectionToCtrl( GuiControl *newParent, bool callback )
  835. {
  836. for( U32 i = 0; i < mSelectedControls.size(); ++ i )
  837. {
  838. GuiControl* ctrl = mSelectedControls[i];
  839. if( ctrl->getParent() == newParent
  840. || ctrl->isLocked()
  841. || selectionContainsParentOf( ctrl ) )
  842. continue;
  843. Point2I globalpos = ctrl->localToGlobalCoord(Point2I(0,0));
  844. newParent->addObject(ctrl);
  845. Point2I newpos = ctrl->globalToLocalCoord(globalpos) + ctrl->getPosition();
  846. ctrl->setPosition(newpos);
  847. }
  848. onHierarchyChanged_callback();
  849. //TODO: undo
  850. }
  851. //-----------------------------------------------------------------------------
  852. static Point2I snapPoint(Point2I point, Point2I delta, Point2I gridSnap)
  853. {
  854. S32 snap;
  855. if(gridSnap.x && delta.x)
  856. {
  857. snap = point.x % gridSnap.x;
  858. point.x -= snap;
  859. if(delta.x > 0 && snap != 0)
  860. point.x += gridSnap.x;
  861. }
  862. if(gridSnap.y && delta.y)
  863. {
  864. snap = point.y % gridSnap.y;
  865. point.y -= snap;
  866. if(delta.y > 0 && snap != 0)
  867. point.y += gridSnap.y;
  868. }
  869. return point;
  870. }
  871. void GuiEditCtrl::moveAndSnapSelection( const Point2I &delta, bool callback )
  872. {
  873. // move / nudge gets a special callback so that multiple small moves can be
  874. // coalesced into one large undo action.
  875. // undo
  876. if( callback )
  877. onPreSelectionNudged_callback( getSelectedSet() );
  878. Vector<GuiControl *>::iterator i;
  879. Point2I newPos;
  880. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  881. {
  882. GuiControl* ctrl = *i;
  883. if( !ctrl->isLocked() && !selectionContainsParentOf( ctrl ) )
  884. {
  885. newPos = ctrl->getPosition() + delta;
  886. newPos = snapPoint( newPos, delta, mGridSnap );
  887. ctrl->setPosition( newPos );
  888. }
  889. }
  890. // undo
  891. if( callback )
  892. onPostSelectionNudged_callback( getSelectedSet() );
  893. // allow script to update the inspector
  894. if( callback && mSelectedControls.size() > 0 )
  895. onSelectionMoved_callback( mSelectedControls[ 0 ] );
  896. }
  897. //-----------------------------------------------------------------------------
  898. void GuiEditCtrl::moveSelection( const Point2I &delta, bool callback )
  899. {
  900. Vector<GuiControl *>::iterator i;
  901. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  902. {
  903. GuiControl* ctrl = *i;
  904. if( !ctrl->isLocked() && !selectionContainsParentOf( ctrl ) )
  905. ctrl->setPosition( ctrl->getPosition() + delta );
  906. }
  907. // allow script to update the inspector
  908. if( callback )
  909. onSelectionMoved_callback( mSelectedControls[ 0 ] );
  910. }
  911. //-----------------------------------------------------------------------------
  912. void GuiEditCtrl::justifySelection( Justification j )
  913. {
  914. S32 minX, maxX;
  915. S32 minY, maxY;
  916. S32 extentX, extentY;
  917. if (mSelectedControls.size() < 2)
  918. return;
  919. Vector<GuiControl *>::iterator i = mSelectedControls.begin();
  920. minX = (*i)->getLeft();
  921. maxX = minX + (*i)->getWidth();
  922. minY = (*i)->getTop();
  923. maxY = minY + (*i)->getHeight();
  924. extentX = (*i)->getWidth();
  925. extentY = (*i)->getHeight();
  926. i++;
  927. for(;i != mSelectedControls.end(); i++)
  928. {
  929. minX = getMin(minX, (*i)->getLeft());
  930. maxX = getMax(maxX, (*i)->getLeft() + (*i)->getWidth());
  931. minY = getMin(minY, (*i)->getTop());
  932. maxY = getMax(maxY, (*i)->getTop() + (*i)->getHeight());
  933. extentX += (*i)->getWidth();
  934. extentY += (*i)->getHeight();
  935. }
  936. S32 deltaX = maxX - minX;
  937. S32 deltaY = maxY - minY;
  938. switch(j)
  939. {
  940. case JUSTIFY_LEFT:
  941. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  942. if( !( *i )->isLocked() )
  943. (*i)->setLeft( minX );
  944. break;
  945. case JUSTIFY_TOP:
  946. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  947. if( !( *i )->isLocked() )
  948. (*i)->setTop( minY );
  949. break;
  950. case JUSTIFY_RIGHT:
  951. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  952. if( !( *i )->isLocked() )
  953. (*i)->setLeft( maxX - (*i)->getWidth() + 1 );
  954. break;
  955. case JUSTIFY_BOTTOM:
  956. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  957. if( !( *i )->isLocked() )
  958. (*i)->setTop( maxY - (*i)->getHeight() + 1 );
  959. break;
  960. case JUSTIFY_CENTER_VERTICAL:
  961. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  962. if( !( *i )->isLocked() )
  963. (*i)->setLeft( minX + ((deltaX - (*i)->getWidth()) >> 1 ));
  964. break;
  965. case JUSTIFY_CENTER_HORIZONTAL:
  966. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  967. if( !( *i )->isLocked() )
  968. (*i)->setTop( minY + ((deltaY - (*i)->getHeight()) >> 1 ));
  969. break;
  970. case SPACING_VERTICAL:
  971. {
  972. Vector<GuiControl *> sortedList;
  973. Vector<GuiControl *>::iterator k;
  974. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  975. {
  976. for(k = sortedList.begin(); k != sortedList.end(); k++)
  977. {
  978. if ((*i)->getTop() < (*k)->getTop())
  979. break;
  980. }
  981. sortedList.insert(k, *i);
  982. }
  983. S32 space = (deltaY - extentY) / (mSelectedControls.size() - 1);
  984. S32 curY = minY;
  985. for(k = sortedList.begin(); k != sortedList.end(); k++)
  986. {
  987. if( !( *k )->isLocked() )
  988. (*k)->setTop( curY );
  989. curY += (*k)->getHeight() + space;
  990. }
  991. }
  992. break;
  993. case SPACING_HORIZONTAL:
  994. {
  995. Vector<GuiControl *> sortedList;
  996. Vector<GuiControl *>::iterator k;
  997. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  998. {
  999. for(k = sortedList.begin(); k != sortedList.end(); k++)
  1000. {
  1001. if ((*i)->getLeft() < (*k)->getLeft())
  1002. break;
  1003. }
  1004. sortedList.insert(k, *i);
  1005. }
  1006. S32 space = (deltaX - extentX) / (mSelectedControls.size() - 1);
  1007. S32 curX = minX;
  1008. for(k = sortedList.begin(); k != sortedList.end(); k++)
  1009. {
  1010. if( !( *k )->isLocked() )
  1011. (*k)->setLeft( curX );
  1012. curX += (*k)->getWidth() + space;
  1013. }
  1014. }
  1015. break;
  1016. }
  1017. }
  1018. //-----------------------------------------------------------------------------
  1019. void GuiEditCtrl::cloneSelection()
  1020. {
  1021. Vector< GuiControl* > newSelection;
  1022. // Clone the controls in the current selection.
  1023. const U32 numOldControls = mSelectedControls.size();
  1024. for( U32 i = 0; i < numOldControls; ++ i )
  1025. {
  1026. GuiControl* ctrl = mSelectedControls[ i ];
  1027. // If parent is in selection, too, skip to prevent multiple clones.
  1028. if( ctrl->getParent() && selectionContains( ctrl->getParent() ) )
  1029. continue;
  1030. // Clone and add to set.
  1031. GuiControl* clone = dynamic_cast< GuiControl* >( ctrl->deepClone() );
  1032. if( clone )
  1033. newSelection.push_back( clone );
  1034. }
  1035. // Exchange the selection set.
  1036. clearSelection();
  1037. const U32 numNewControls = newSelection.size();
  1038. for( U32 i = 0; i < numNewControls; ++ i )
  1039. addSelection( newSelection[ i ] );
  1040. // Callback for undo.
  1041. onSelectionCloned_callback( getSelectedSet() );
  1042. }
  1043. //-----------------------------------------------------------------------------
  1044. void GuiEditCtrl::deleteSelection()
  1045. {
  1046. // Notify script for undo.
  1047. onTrashSelection_callback( getSelectedSet() );
  1048. // Move all objects in selection to trash.
  1049. Vector< GuiControl* >::iterator i;
  1050. for( i = mSelectedControls.begin(); i != mSelectedControls.end(); i ++ )
  1051. {
  1052. if( ( *i ) == getCurrentAddSet() )
  1053. setCurrentAddSet( getContentControl(), false );
  1054. mTrash->addObject( *i );
  1055. }
  1056. clearSelection();
  1057. // Notify script it needs to update its views.
  1058. onHierarchyChanged_callback();
  1059. }
  1060. //-----------------------------------------------------------------------------
  1061. void GuiEditCtrl::loadSelection( const char* filename )
  1062. {
  1063. // Set redefine behavior to rename.
  1064. const char* oldRedefineBehavior = Con::getVariable( "$Con::redefineBehavior" );
  1065. Con::setVariable( "$Con::redefineBehavior", "renameNew" );
  1066. // Exec the file or clipboard contents with the saved selection set.
  1067. if( filename )
  1068. Con::executef( "exec", filename );
  1069. else
  1070. Con::evaluate( Platform::getClipboard() );
  1071. SimSet* set;
  1072. if( !Sim::findObject( "guiClipboard", set ) )
  1073. {
  1074. if( filename )
  1075. Con::errorf( "GuiEditCtrl::loadSelection() - could not find 'guiClipboard' in '%s'", filename );
  1076. else
  1077. Con::errorf( "GuiEditCtrl::loadSelection() - could not find 'guiClipboard'" );
  1078. return;
  1079. }
  1080. // Restore redefine behavior.
  1081. Con::setVariable( "$Con::redefineBehavior", oldRedefineBehavior );
  1082. // Add the objects in the set.
  1083. if( set->size() )
  1084. {
  1085. clearSelection();
  1086. GuiControlVector ctrls;
  1087. for( U32 i = 0, num = set->size(); i < num; ++ i )
  1088. {
  1089. GuiControl *ctrl = dynamic_cast< GuiControl* >( ( *set )[ i ] );
  1090. if( ctrl )
  1091. {
  1092. getCurrentAddSet()->addObject( ctrl );
  1093. ctrls.push_back( ctrl );
  1094. }
  1095. }
  1096. // Select all controls. We need to perform this here rather than in the
  1097. // loop above as addSelection() will modify the current add set.
  1098. for( U32 i = 0; i < ctrls.size(); ++ i )
  1099. {
  1100. addSelection( ctrls[i] );
  1101. }
  1102. // Undo
  1103. onAddNewCtrlSet_callback( getSelectedSet() );
  1104. // Notify the script it needs to update its treeview.
  1105. onHierarchyChanged_callback();
  1106. }
  1107. set->deleteObject();
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. void GuiEditCtrl::saveSelection( const char* filename )
  1111. {
  1112. // If there are no selected objects, then don't save.
  1113. if( mSelectedControls.size() == 0 )
  1114. return;
  1115. // Open the stream.
  1116. Stream* stream;
  1117. if( filename )
  1118. {
  1119. stream = FileStream::createAndOpen( filename, Torque::FS::File::Write );
  1120. if( !stream )
  1121. {
  1122. Con::errorf( "GuiEditCtrl::saveSelection - could not open '%s' for writing", filename );
  1123. return;
  1124. }
  1125. }
  1126. else
  1127. stream = new MemStream( 4096 );
  1128. // Create a temporary SimSet.
  1129. SimSet* clipboardSet = new SimSet;
  1130. clipboardSet->registerObject();
  1131. Sim::getRootGroup()->addObject( clipboardSet, "guiClipboard" );
  1132. // Add the selected controls to the set.
  1133. for( Vector< GuiControl* >::iterator i = mSelectedControls.begin();
  1134. i != mSelectedControls.end(); ++ i )
  1135. {
  1136. GuiControl* ctrl = *i;
  1137. if( !selectionContainsParentOf( ctrl ) )
  1138. clipboardSet->addObject( ctrl );
  1139. }
  1140. // Write the SimSet. Use the IgnoreCanSave to ensure the controls
  1141. // get actually written out (also disables the default parent inheritance
  1142. // behavior for the flag).
  1143. clipboardSet->write( *stream, 0, IgnoreCanSave );
  1144. clipboardSet->deleteObject();
  1145. // If we were writing to a memory stream, copy to clipboard
  1146. // now.
  1147. if( !filename )
  1148. {
  1149. MemStream* memStream = static_cast< MemStream* >( stream );
  1150. memStream->write( U8( 0 ) );
  1151. Platform::setClipboard( ( const char* ) memStream->getBuffer() );
  1152. }
  1153. delete stream;
  1154. }
  1155. //-----------------------------------------------------------------------------
  1156. void GuiEditCtrl::selectAll()
  1157. {
  1158. GuiControl::iterator i;
  1159. clearSelection();
  1160. for(i = getCurrentAddSet()->begin(); i != getCurrentAddSet()->end(); i++)
  1161. {
  1162. GuiControl *ctrl = dynamic_cast<GuiControl *>(*i);
  1163. addSelection( ctrl );
  1164. }
  1165. }
  1166. //-----------------------------------------------------------------------------
  1167. void GuiEditCtrl::bringToFront()
  1168. {
  1169. if( getNumSelected() != 1 )
  1170. return;
  1171. GuiControl* ctrl = mSelectedControls.first();
  1172. ctrl->getParent()->pushObjectToBack( ctrl );
  1173. }
  1174. //-----------------------------------------------------------------------------
  1175. void GuiEditCtrl::pushToBack()
  1176. {
  1177. if( getNumSelected() != 1 )
  1178. return;
  1179. GuiControl* ctrl = mSelectedControls.first();
  1180. ctrl->getParent()->bringObjectToFront( ctrl );
  1181. }
  1182. //-----------------------------------------------------------------------------
  1183. RectI GuiEditCtrl::getSelectionBounds() const
  1184. {
  1185. Vector<GuiControl *>::const_iterator i = mSelectedControls.begin();
  1186. Point2I minPos = (*i)->localToGlobalCoord( Point2I( 0, 0 ) );
  1187. Point2I maxPos = minPos;
  1188. for(; i != mSelectedControls.end(); i++)
  1189. {
  1190. Point2I iPos = (**i).localToGlobalCoord( Point2I( 0 , 0 ) );
  1191. minPos.x = getMin( iPos.x, minPos.x );
  1192. minPos.y = getMin( iPos.y, minPos.y );
  1193. Point2I iExt = ( **i ).getExtent();
  1194. iPos.x += iExt.x;
  1195. iPos.y += iExt.y;
  1196. maxPos.x = getMax( iPos.x, maxPos.x );
  1197. maxPos.y = getMax( iPos.y, maxPos.y );
  1198. }
  1199. minPos = getContentControl()->globalToLocalCoord( minPos );
  1200. maxPos = getContentControl()->globalToLocalCoord( maxPos );
  1201. return RectI( minPos.x, minPos.y, ( maxPos.x - minPos.x ), ( maxPos.y - minPos.y ) );
  1202. }
  1203. //-----------------------------------------------------------------------------
  1204. RectI GuiEditCtrl::getSelectionGlobalBounds() const
  1205. {
  1206. Point2I minb( S32_MAX, S32_MAX );
  1207. Point2I maxb( S32_MIN, S32_MIN );
  1208. for( U32 i = 0, num = mSelectedControls.size(); i < num; ++ i )
  1209. {
  1210. // Min.
  1211. Point2I pos = mSelectedControls[ i ]->localToGlobalCoord( Point2I( 0, 0 ) );
  1212. minb.x = getMin( minb.x, pos.x );
  1213. minb.y = getMin( minb.y, pos.y );
  1214. // Max.
  1215. const Point2I extent = mSelectedControls[ i ]->getExtent();
  1216. maxb.x = getMax( maxb.x, pos.x + extent.x );
  1217. maxb.y = getMax( maxb.y, pos.y + extent.y );
  1218. }
  1219. RectI bounds( minb.x, minb.y, maxb.x - minb.x, maxb.y - minb.y );
  1220. return bounds;
  1221. }
  1222. //-----------------------------------------------------------------------------
  1223. bool GuiEditCtrl::selectionContains( GuiControl *ctrl )
  1224. {
  1225. Vector<GuiControl *>::iterator i;
  1226. for (i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  1227. if (ctrl == *i) return true;
  1228. return false;
  1229. }
  1230. //-----------------------------------------------------------------------------
  1231. bool GuiEditCtrl::selectionContainsParentOf( GuiControl* ctrl )
  1232. {
  1233. GuiControl* parent = ctrl->getParent();
  1234. while( parent && parent != getContentControl() )
  1235. {
  1236. if( selectionContains( parent ) )
  1237. return true;
  1238. parent = parent->getParent();
  1239. }
  1240. return false;
  1241. }
  1242. //-----------------------------------------------------------------------------
  1243. void GuiEditCtrl::select( GuiControl* ctrl )
  1244. {
  1245. clearSelection();
  1246. addSelection( ctrl );
  1247. }
  1248. //-----------------------------------------------------------------------------
  1249. void GuiEditCtrl::updateSelectedSet()
  1250. {
  1251. mSelectedSet->clear();
  1252. Vector<GuiControl*>::iterator i;
  1253. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  1254. {
  1255. mSelectedSet->addObject(*i);
  1256. }
  1257. }
  1258. //-----------------------------------------------------------------------------
  1259. void GuiEditCtrl::addSelectControlsInRegion( const RectI& rect, U32 flags )
  1260. {
  1261. // Do a hit test on the content control.
  1262. canHitSelectedControls( false );
  1263. Vector< GuiControl* > hits;
  1264. if( mFullBoxSelection )
  1265. flags |= GuiControl::HIT_FullBoxOnly;
  1266. getContentControl()->findHitControls( rect, hits, flags );
  1267. canHitSelectedControls( true );
  1268. // Add all controls that got hit.
  1269. for( U32 i = 0, num = hits.size(); i < num; ++ i )
  1270. addSelection( hits[ i ] );
  1271. }
  1272. //-----------------------------------------------------------------------------
  1273. void GuiEditCtrl::addSelectControlAt( const Point2I& pos )
  1274. {
  1275. // Run a hit test.
  1276. canHitSelectedControls( false );
  1277. GuiControl* hit = getContentControl()->findHitControl( pos );
  1278. canHitSelectedControls( true );
  1279. // Add to selection.
  1280. if( hit )
  1281. addSelection( hit );
  1282. }
  1283. //-----------------------------------------------------------------------------
  1284. void GuiEditCtrl::resizeControlsInSelectionBy( const Point2I& delta, U32 mode )
  1285. {
  1286. for( U32 i = 0, num = mSelectedControls.size(); i < num; ++ i )
  1287. {
  1288. GuiControl *ctrl = mSelectedControls[ i ];
  1289. if( ctrl->isLocked() )
  1290. continue;
  1291. Point2I minExtent = ctrl->getMinExtent();
  1292. Point2I newPosition = ctrl->getPosition();
  1293. Point2I newExtent = ctrl->getExtent();
  1294. if( mSizingMode & sizingLeft )
  1295. {
  1296. newPosition.x += delta.x;
  1297. newExtent.x -= delta.x;
  1298. if( newExtent.x < minExtent.x )
  1299. {
  1300. newPosition.x -= minExtent.x - newExtent.x;
  1301. newExtent.x = minExtent.x;
  1302. }
  1303. }
  1304. else if( mSizingMode & sizingRight )
  1305. {
  1306. newExtent.x += delta.x;
  1307. if( newExtent.x < minExtent.x )
  1308. newExtent.x = minExtent.x;
  1309. }
  1310. if( mSizingMode & sizingTop )
  1311. {
  1312. newPosition.y += delta.y;
  1313. newExtent.y -= delta.y;
  1314. if( newExtent.y < minExtent.y )
  1315. {
  1316. newPosition.y -= minExtent.y - newExtent.y;
  1317. newExtent.y = minExtent.y;
  1318. }
  1319. }
  1320. else if( mSizingMode & sizingBottom )
  1321. {
  1322. newExtent.y += delta.y;
  1323. if( newExtent.y < minExtent.y )
  1324. newExtent.y = minExtent.y;
  1325. }
  1326. ctrl->resize( newPosition, newExtent );
  1327. }
  1328. if( mSelectedControls.size() == 1 )
  1329. onSelectionResized_callback( mSelectedControls[ 0 ] );
  1330. }
  1331. //-----------------------------------------------------------------------------
  1332. void GuiEditCtrl::fitIntoParents( bool width, bool height )
  1333. {
  1334. // Record undo.
  1335. onFitIntoParent_callback( width, height );
  1336. // Fit.
  1337. for( U32 i = 0; i < mSelectedControls.size(); ++ i )
  1338. {
  1339. GuiControl* ctrl = mSelectedControls[ i ];
  1340. GuiControl* parent = ctrl->getParent();
  1341. Point2I position = ctrl->getPosition();
  1342. if( width )
  1343. position.x = 0;
  1344. if( height )
  1345. position.y = 0;
  1346. Point2I extents = ctrl->getExtent();
  1347. if( width )
  1348. extents.x = parent->getWidth();
  1349. if( height )
  1350. extents.y = parent->getHeight();
  1351. ctrl->resize( position, extents );
  1352. }
  1353. }
  1354. //-----------------------------------------------------------------------------
  1355. void GuiEditCtrl::selectParents( bool addToSelection )
  1356. {
  1357. Vector< GuiControl* > parents;
  1358. // Collect all parents.
  1359. for( U32 i = 0; i < mSelectedControls.size(); ++ i )
  1360. {
  1361. GuiControl* ctrl = mSelectedControls[ i ];
  1362. if( ctrl != mContentControl && ctrl->getParent() != mContentControl )
  1363. parents.push_back( mSelectedControls[ i ]->getParent() );
  1364. }
  1365. // If there's no parents to select, don't
  1366. // change the selection.
  1367. if( parents.empty() )
  1368. return;
  1369. // Blast selection if need be.
  1370. if( !addToSelection )
  1371. clearSelection();
  1372. // Add the parents.
  1373. for( U32 i = 0; i < parents.size(); ++ i )
  1374. addSelection( parents[ i ] );
  1375. }
  1376. //-----------------------------------------------------------------------------
  1377. void GuiEditCtrl::selectChildren( bool addToSelection )
  1378. {
  1379. Vector< GuiControl* > children;
  1380. // Collect all children.
  1381. for( U32 i = 0; i < mSelectedControls.size(); ++ i )
  1382. {
  1383. GuiControl* parent = mSelectedControls[ i ];
  1384. for( GuiControl::iterator iter = parent->begin(); iter != parent->end(); ++ iter )
  1385. {
  1386. GuiControl* child = dynamic_cast< GuiControl* >( *iter );
  1387. if( child )
  1388. children.push_back( child );
  1389. }
  1390. }
  1391. // If there's no children to select, don't
  1392. // change the selection.
  1393. if( children.empty() )
  1394. return;
  1395. // Blast selection if need be.
  1396. if( !addToSelection )
  1397. clearSelection();
  1398. // Add the children.
  1399. for( U32 i = 0; i < children.size(); ++ i )
  1400. addSelection( children[ i ] );
  1401. }
  1402. //=============================================================================
  1403. // Guides.
  1404. //=============================================================================
  1405. // MARK: ---- Guides ----
  1406. //-----------------------------------------------------------------------------
  1407. void GuiEditCtrl::readGuides( guideAxis axis, GuiControl* ctrl )
  1408. {
  1409. // Read the guide indices from the vector stored on the respective dynamic
  1410. // property of the control.
  1411. const char* guideIndices = ctrl->getDataField( smGuidesPropertyName[ axis ], NULL );
  1412. if( guideIndices && guideIndices[ 0 ] )
  1413. {
  1414. U32 index = 0;
  1415. while( true )
  1416. {
  1417. const char* posStr = StringUnit::getUnit( guideIndices, index, " \t" );
  1418. if( !posStr[ 0 ] )
  1419. break;
  1420. mGuides[ axis ].push_back( dAtoi( posStr ) );
  1421. index ++;
  1422. }
  1423. }
  1424. }
  1425. //-----------------------------------------------------------------------------
  1426. void GuiEditCtrl::writeGuides( guideAxis axis, GuiControl* ctrl )
  1427. {
  1428. // Store the guide indices of the given axis in a vector on the respective
  1429. // dynamic property of the control.
  1430. StringBuilder str;
  1431. bool isFirst = true;
  1432. for( U32 i = 0, num = mGuides[ axis ].size(); i < num; ++ i )
  1433. {
  1434. if( !isFirst )
  1435. str.append( ' ' );
  1436. char buffer[ 32 ];
  1437. dSprintf( buffer, sizeof( buffer ), "%i", mGuides[ axis ][ i ] );
  1438. str.append( buffer );
  1439. isFirst = false;
  1440. }
  1441. String value = str.end();
  1442. ctrl->setDataField( smGuidesPropertyName[ axis ], NULL, value );
  1443. }
  1444. //-----------------------------------------------------------------------------
  1445. S32 GuiEditCtrl::findGuide( guideAxis axis, const Point2I& point, U32 tolerance )
  1446. {
  1447. const S32 p = ( point - localToGlobalCoord( Point2I( 0, 0 ) ) )[ axis ];
  1448. for( U32 i = 0, num = mGuides[ axis ].size(); i < num; ++ i )
  1449. {
  1450. const S32 g = mGuides[ axis ][ i ];
  1451. if( p >= ( g - tolerance ) &&
  1452. p <= ( g + tolerance ) )
  1453. return i;
  1454. }
  1455. return -1;
  1456. }
  1457. //=============================================================================
  1458. // Snapping.
  1459. //=============================================================================
  1460. // MARK: ---- Snapping ----
  1461. //-----------------------------------------------------------------------------
  1462. RectI GuiEditCtrl::getSnapRegion( snappingAxis axis, const Point2I& center ) const
  1463. {
  1464. RectI rootBounds = getContentControl()->getBounds();
  1465. RectI rect;
  1466. if( axis == SnapHorizontal )
  1467. rect = RectI( rootBounds.point.x,
  1468. center.y - mSnapSensitivity,
  1469. rootBounds.extent.x,
  1470. mSnapSensitivity * 2 );
  1471. else // SnapVertical
  1472. rect = RectI( center.x - mSnapSensitivity,
  1473. rootBounds.point.y,
  1474. mSnapSensitivity * 2,
  1475. rootBounds.extent.y );
  1476. // Clip against root bounds.
  1477. rect.intersect( rootBounds );
  1478. return rect;
  1479. }
  1480. //-----------------------------------------------------------------------------
  1481. void GuiEditCtrl::registerSnap( snappingAxis axis, const Point2I& mousePoint, const Point2I& point, snappingEdges edge, GuiControl* ctrl )
  1482. {
  1483. bool takeNewSnap = false;
  1484. const Point2I globalPoint = getContentControl()->localToGlobalCoord( point );
  1485. // If we have no snap yet, just take this one.
  1486. if( !mSnapped[ axis ] )
  1487. takeNewSnap = true;
  1488. // Otherwise see if this snap is the better one.
  1489. else
  1490. {
  1491. // Compare deltas to pointer.
  1492. S32 deltaCurrent = mAbs( mSnapOffset[ axis ] - mousePoint[ axis ] );
  1493. S32 deltaNew = mAbs( globalPoint[ axis ] - mousePoint[ axis ] );
  1494. if( deltaCurrent > deltaNew )
  1495. takeNewSnap = true;
  1496. }
  1497. if( takeNewSnap )
  1498. {
  1499. mSnapped[ axis ] = true;
  1500. mSnapOffset[ axis ] = globalPoint[ axis ];
  1501. mSnapEdge[ axis ] = edge;
  1502. mSnapTargets[ axis ] = ctrl;
  1503. }
  1504. }
  1505. //-----------------------------------------------------------------------------
  1506. void GuiEditCtrl::findSnaps( snappingAxis axis, const Point2I& mousePoint, const RectI& minRegion, const RectI& midRegion, const RectI& maxRegion )
  1507. {
  1508. // Find controls with edge in either minRegion, midRegion, or maxRegion
  1509. // (depending on snap settings).
  1510. for( U32 i = 0, num = mSnapHits[ axis ].size(); i < num; ++ i )
  1511. {
  1512. GuiControl* ctrl = mSnapHits[ axis ][ i ];
  1513. if( ctrl == getContentControl() && !mSnapToCanvas )
  1514. continue;
  1515. RectI bounds = ctrl->getGlobalBounds();
  1516. bounds.point = getContentControl()->globalToLocalCoord( bounds.point );
  1517. // Compute points on min, mid, and max lines of control.
  1518. Point2I min = bounds.point;
  1519. Point2I max = min + bounds.extent - Point2I( 1, 1 );
  1520. Point2I mid = min;
  1521. mid.x += bounds.extent.x / 2;
  1522. mid.y += bounds.extent.y / 2;
  1523. // Test edge snap cases.
  1524. if( mSnapToEdges )
  1525. {
  1526. // Min to min.
  1527. if( minRegion.pointInRect( min ) )
  1528. registerSnap( axis, mousePoint, min, SnapEdgeMin, ctrl );
  1529. // Max to max.
  1530. if( maxRegion.pointInRect( max ) )
  1531. registerSnap( axis, mousePoint, max, SnapEdgeMax, ctrl );
  1532. // Min to max.
  1533. if( minRegion.pointInRect( max ) )
  1534. registerSnap( axis, mousePoint, max, SnapEdgeMin, ctrl );
  1535. // Max to min.
  1536. if( maxRegion.pointInRect( min ) )
  1537. registerSnap( axis, mousePoint, min, SnapEdgeMax, ctrl );
  1538. }
  1539. // Test center snap cases.
  1540. if( mSnapToCenters )
  1541. {
  1542. // Mid to mid.
  1543. if( midRegion.pointInRect( mid ) )
  1544. registerSnap( axis, mousePoint, mid, SnapEdgeMid, ctrl );
  1545. }
  1546. // Test combined center+edge snap cases.
  1547. if( mSnapToEdges && mSnapToCenters )
  1548. {
  1549. // Min to mid.
  1550. if( minRegion.pointInRect( mid ) )
  1551. registerSnap( axis, mousePoint, mid, SnapEdgeMin, ctrl );
  1552. // Max to mid.
  1553. if( maxRegion.pointInRect( mid ) )
  1554. registerSnap( axis, mousePoint, mid, SnapEdgeMax, ctrl );
  1555. // Mid to min.
  1556. if( midRegion.pointInRect( min ) )
  1557. registerSnap( axis, mousePoint, min, SnapEdgeMid, ctrl );
  1558. // Mid to max.
  1559. if( midRegion.pointInRect( max ) )
  1560. registerSnap( axis, mousePoint, max, SnapEdgeMid, ctrl );
  1561. }
  1562. }
  1563. }
  1564. //-----------------------------------------------------------------------------
  1565. void GuiEditCtrl::doControlSnap( const GuiEvent& event, const RectI& selectionBounds, const RectI& selectionBoundsGlobal, Point2I& delta )
  1566. {
  1567. if( !mSnapToControls || ( !mSnapToEdges && !mSnapToCenters ) )
  1568. return;
  1569. // Allow restricting to just vertical (ALT+SHIFT) or just horizontal (ALT+CTRL)
  1570. // snaps.
  1571. bool snapAxisEnabled[ 2 ];
  1572. if( event.modifier & SI_PRIMARY_ALT && event.modifier & SI_SHIFT )
  1573. snapAxisEnabled[ SnapHorizontal ] = false;
  1574. else
  1575. snapAxisEnabled[ SnapHorizontal ] = true;
  1576. if( event.modifier & SI_PRIMARY_ALT && event.modifier & SI_CTRL )
  1577. snapAxisEnabled[ SnapVertical ] = false;
  1578. else
  1579. snapAxisEnabled[ SnapVertical ] = true;
  1580. // Compute snap regions. There is one region centered on and aligned with
  1581. // each of the selection bounds edges plus two regions aligned on the selection
  1582. // bounds center. For the selection bounds origin, we use the point that the
  1583. // selection would be at, if we had already done the mouse drag.
  1584. RectI snapRegions[ 2 ][ 3 ];
  1585. Point2I projectedOrigin( selectionBounds.point + delta );
  1586. dMemset( snapRegions, 0, sizeof( snapRegions ) );
  1587. for( U32 axis = 0; axis < 2; ++ axis )
  1588. {
  1589. if( !snapAxisEnabled[ axis ] )
  1590. continue;
  1591. if( mSizingMode == sizingNone ||
  1592. ( axis == 0 && mSizingMode & sizingLeft ) ||
  1593. ( axis == 1 && mSizingMode & sizingTop ) )
  1594. snapRegions[ axis ][ 0 ] = getSnapRegion( ( snappingAxis ) axis, projectedOrigin );
  1595. if( mSizingMode == sizingNone )
  1596. snapRegions[ axis ][ 1 ] = getSnapRegion( ( snappingAxis ) axis, projectedOrigin + Point2I( selectionBounds.extent.x / 2, selectionBounds.extent.y / 2 ) );
  1597. if( mSizingMode == sizingNone ||
  1598. ( axis == 0 && mSizingMode & sizingRight ) ||
  1599. ( axis == 1 && mSizingMode & sizingBottom ) )
  1600. snapRegions[ axis ][ 2 ] = getSnapRegion( ( snappingAxis ) axis, projectedOrigin + selectionBounds.extent - Point2I( 1, 1 ) );
  1601. }
  1602. // Find hit controls.
  1603. canHitSelectedControls( false );
  1604. if( mSnapToEdges )
  1605. {
  1606. for( U32 axis = 0; axis < 2; ++ axis )
  1607. if( snapAxisEnabled[ axis ] )
  1608. {
  1609. getContentControl()->findHitControls( snapRegions[ axis ][ 0 ], mSnapHits[ axis ], HIT_NoCanHitNoRecurse );
  1610. getContentControl()->findHitControls( snapRegions[ axis ][ 2 ], mSnapHits[ axis ], HIT_NoCanHitNoRecurse );
  1611. }
  1612. }
  1613. if( mSnapToCenters && mSizingMode == sizingNone )
  1614. {
  1615. for( U32 axis = 0; axis < 2; ++ axis )
  1616. if( snapAxisEnabled[ axis ] )
  1617. getContentControl()->findHitControls( snapRegions[ axis ][ 1 ], mSnapHits[ axis ], HIT_NoCanHitNoRecurse );
  1618. }
  1619. canHitSelectedControls( true );
  1620. // Add the content control itself to the hit controls
  1621. // so we can always get a snap on it.
  1622. if( mSnapToCanvas )
  1623. {
  1624. mSnapHits[ 0 ].push_back( mContentControl );
  1625. mSnapHits[ 1 ].push_back( mContentControl );
  1626. }
  1627. // Find snaps.
  1628. for( U32 i = 0; i < 2; ++ i )
  1629. if( snapAxisEnabled[ i ] )
  1630. findSnaps( ( snappingAxis ) i,
  1631. event.mousePoint,
  1632. snapRegions[ i ][ 0 ],
  1633. snapRegions[ i ][ 1 ],
  1634. snapRegions[ i ][ 2 ] );
  1635. // Clean up.
  1636. mSnapHits[ 0 ].clear();
  1637. mSnapHits[ 1 ].clear();
  1638. }
  1639. //-----------------------------------------------------------------------------
  1640. void GuiEditCtrl::doGridSnap( const GuiEvent& event, const RectI& selectionBounds, const RectI& selectionBoundsGlobal, Point2I& delta )
  1641. {
  1642. delta += selectionBounds.point;
  1643. if( mGridSnap.x )
  1644. delta.x -= delta.x % mGridSnap.x;
  1645. if( mGridSnap.y )
  1646. delta.y -= delta.y % mGridSnap.y;
  1647. delta -= selectionBounds.point;
  1648. }
  1649. //-----------------------------------------------------------------------------
  1650. void GuiEditCtrl::doGuideSnap( const GuiEvent& event, const RectI& selectionBounds, const RectI& selectionBoundsGlobal, Point2I& delta )
  1651. {
  1652. if( !mSnapToGuides )
  1653. return;
  1654. Point2I min = getContentControl()->localToGlobalCoord( selectionBounds.point + delta );
  1655. Point2I mid = min + selectionBounds.extent / 2;
  1656. Point2I max = min + selectionBounds.extent - Point2I( 1, 1 );
  1657. for( U32 axis = 0; axis < 2; ++ axis )
  1658. {
  1659. if( mSnapToEdges )
  1660. {
  1661. S32 guideMin = -1;
  1662. S32 guideMax = -1;
  1663. if( mSizingMode == sizingNone ||
  1664. ( axis == 0 && mSizingMode & sizingLeft ) ||
  1665. ( axis == 1 && mSizingMode & sizingTop ) )
  1666. guideMin = findGuide( ( guideAxis ) axis, min, mSnapSensitivity );
  1667. if( mSizingMode == sizingNone ||
  1668. ( axis == 0 && mSizingMode & sizingRight ) ||
  1669. ( axis == 1 && mSizingMode & sizingBottom ) )
  1670. guideMax = findGuide( ( guideAxis ) axis, max, mSnapSensitivity );
  1671. Point2I pos( 0, 0 );
  1672. if( guideMin != -1 )
  1673. {
  1674. pos[ axis ] = mGuides[ axis ][ guideMin ];
  1675. registerSnap( ( snappingAxis ) axis, event.mousePoint, pos, SnapEdgeMin );
  1676. }
  1677. if( guideMax != -1 )
  1678. {
  1679. pos[ axis ] = mGuides[ axis ][ guideMax ];
  1680. registerSnap( ( snappingAxis ) axis, event.mousePoint, pos, SnapEdgeMax );
  1681. }
  1682. }
  1683. if( mSnapToCenters && mSizingMode == sizingNone )
  1684. {
  1685. const S32 guideMid = findGuide( ( guideAxis ) axis, mid, mSnapSensitivity );
  1686. if( guideMid != -1 )
  1687. {
  1688. Point2I pos( 0, 0 );
  1689. pos[ axis ] = mGuides[ axis ][ guideMid ];
  1690. registerSnap( ( snappingAxis ) axis, event.mousePoint, pos, SnapEdgeMid );
  1691. }
  1692. }
  1693. }
  1694. }
  1695. //-----------------------------------------------------------------------------
  1696. void GuiEditCtrl::doSnapping( const GuiEvent& event, const RectI& selectionBounds, Point2I& delta )
  1697. {
  1698. // Clear snapping. If we have snapping on, we want to find a new best snap.
  1699. mSnapped[ SnapVertical ] = false;
  1700. mSnapped[ SnapHorizontal ] = false;
  1701. // Compute global bounds.
  1702. RectI selectionBoundsGlobal = selectionBounds;
  1703. selectionBoundsGlobal.point = getContentControl()->localToGlobalCoord( selectionBoundsGlobal.point );
  1704. // Apply the snaps.
  1705. doGridSnap( event, selectionBounds, selectionBoundsGlobal, delta );
  1706. doGuideSnap( event, selectionBounds, selectionBoundsGlobal, delta );
  1707. doControlSnap( event, selectionBounds, selectionBoundsGlobal, delta );
  1708. // If we have a horizontal snap, compute a delta.
  1709. if( mSnapped[ SnapVertical ] )
  1710. snapDelta( SnapVertical, mSnapEdge[ SnapVertical ], mSnapOffset[ SnapVertical ], selectionBoundsGlobal, delta );
  1711. // If we have a vertical snap, compute a delta.
  1712. if( mSnapped[ SnapHorizontal ] )
  1713. snapDelta( SnapHorizontal, mSnapEdge[ SnapHorizontal ], mSnapOffset[ SnapHorizontal ], selectionBoundsGlobal, delta );
  1714. }
  1715. //-----------------------------------------------------------------------------
  1716. void GuiEditCtrl::snapToGrid( Point2I& point )
  1717. {
  1718. if( mGridSnap.x )
  1719. point.x -= point.x % mGridSnap.x;
  1720. if( mGridSnap.y )
  1721. point.y -= point.y % mGridSnap.y;
  1722. }
  1723. //=============================================================================
  1724. // Misc.
  1725. //=============================================================================
  1726. // MARK: ---- Misc ----
  1727. //-----------------------------------------------------------------------------
  1728. void GuiEditCtrl::setContentControl(GuiControl *root)
  1729. {
  1730. mContentControl = root;
  1731. if( root != NULL )
  1732. root->mIsContainer = true;
  1733. setCurrentAddSet( root );
  1734. }
  1735. //-----------------------------------------------------------------------------
  1736. void GuiEditCtrl::setEditMode(bool value)
  1737. {
  1738. mActive = value;
  1739. clearSelection();
  1740. if( mActive && mAwake )
  1741. mCurrentAddSet = NULL;
  1742. }
  1743. //-----------------------------------------------------------------------------
  1744. void GuiEditCtrl::setMouseMode( mouseModes mode )
  1745. {
  1746. if( mMouseDownMode != mode )
  1747. {
  1748. mMouseDownMode = mode;
  1749. onMouseModeChange_callback();
  1750. }
  1751. }
  1752. //-----------------------------------------------------------------------------
  1753. void GuiEditCtrl::setCurrentAddSet(GuiControl *ctrl, bool doclearSelection)
  1754. {
  1755. if (ctrl != mCurrentAddSet)
  1756. {
  1757. if(doclearSelection)
  1758. clearSelection();
  1759. mCurrentAddSet = ctrl;
  1760. }
  1761. }
  1762. //-----------------------------------------------------------------------------
  1763. GuiControl* GuiEditCtrl::getCurrentAddSet()
  1764. {
  1765. if( !mCurrentAddSet )
  1766. setCurrentAddSet( mContentControl, false );
  1767. return mCurrentAddSet;
  1768. }
  1769. //-----------------------------------------------------------------------------
  1770. void GuiEditCtrl::addNewControl(GuiControl *ctrl)
  1771. {
  1772. getCurrentAddSet()->addObject(ctrl);
  1773. select( ctrl );
  1774. // undo
  1775. onAddNewCtrl_callback( ctrl );
  1776. }
  1777. //-----------------------------------------------------------------------------
  1778. S32 GuiEditCtrl::getSizingHitKnobs(const Point2I &pt, const RectI &box)
  1779. {
  1780. S32 lx = box.point.x, rx = box.point.x + box.extent.x - 1;
  1781. S32 cx = (lx + rx) >> 1;
  1782. S32 ty = box.point.y, by = box.point.y + box.extent.y - 1;
  1783. S32 cy = (ty + by) >> 1;
  1784. // adjust nuts, so they dont straddle the controls
  1785. lx -= NUT_SIZE;
  1786. ty -= NUT_SIZE;
  1787. rx += NUT_SIZE;
  1788. by += NUT_SIZE;
  1789. if (inNut(pt, lx, ty))
  1790. return sizingLeft | sizingTop;
  1791. if (inNut(pt, cx, ty))
  1792. return sizingTop;
  1793. if (inNut(pt, rx, ty))
  1794. return sizingRight | sizingTop;
  1795. if (inNut(pt, lx, by))
  1796. return sizingLeft | sizingBottom;
  1797. if (inNut(pt, cx, by))
  1798. return sizingBottom;
  1799. if (inNut(pt, rx, by))
  1800. return sizingRight | sizingBottom;
  1801. if (inNut(pt, lx, cy))
  1802. return sizingLeft;
  1803. if (inNut(pt, rx, cy))
  1804. return sizingRight;
  1805. return sizingNone;
  1806. }
  1807. //-----------------------------------------------------------------------------
  1808. void GuiEditCtrl::getDragRect(RectI &box)
  1809. {
  1810. box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
  1811. box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
  1812. box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
  1813. box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
  1814. }
  1815. //-----------------------------------------------------------------------------
  1816. void GuiEditCtrl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1817. {
  1818. GuiCanvas *pRoot = getRoot();
  1819. if( !pRoot )
  1820. return;
  1821. showCursor = false;
  1822. cursor = NULL;
  1823. Point2I ctOffset;
  1824. Point2I cext;
  1825. GuiControl *ctrl;
  1826. Point2I mousePos = globalToLocalCoord(lastGuiEvent.mousePoint);
  1827. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  1828. AssertFatal(pWindow != NULL,"GuiControl without owning platform window! This should not be possible.");
  1829. PlatformCursorController *pController = pWindow->getCursorController();
  1830. AssertFatal(pController != NULL,"PlatformWindow without an owned CursorController!");
  1831. S32 desiredCursor = PlatformCursorController::curArrow;
  1832. // first see if we hit a sizing knob on the currently selected control...
  1833. if (mSelectedControls.size() == 1 )
  1834. {
  1835. ctrl = mSelectedControls.first();
  1836. cext = ctrl->getExtent();
  1837. ctOffset = globalToLocalCoord(ctrl->localToGlobalCoord(Point2I(0,0)));
  1838. RectI box(ctOffset.x,ctOffset.y,cext.x, cext.y);
  1839. GuiEditCtrl::sizingModes sizeMode = (GuiEditCtrl::sizingModes)getSizingHitKnobs(mousePos, box);
  1840. if( mMouseDownMode == SizingSelection )
  1841. {
  1842. if ( ( mSizingMode == ( sizingBottom | sizingRight ) ) || ( mSizingMode == ( sizingTop | sizingLeft ) ) )
  1843. desiredCursor = PlatformCursorController::curResizeNWSE;
  1844. else if ( ( mSizingMode == ( sizingBottom | sizingLeft ) ) || ( mSizingMode == ( sizingTop | sizingRight ) ) )
  1845. desiredCursor = PlatformCursorController::curResizeNESW;
  1846. else if ( mSizingMode == sizingLeft || mSizingMode == sizingRight )
  1847. desiredCursor = PlatformCursorController::curResizeVert;
  1848. else if (mSizingMode == sizingTop || mSizingMode == sizingBottom )
  1849. desiredCursor = PlatformCursorController::curResizeHorz;
  1850. }
  1851. else
  1852. {
  1853. // Check for current mouse position after checking for actual sizing mode
  1854. if ( ( sizeMode == ( sizingBottom | sizingRight ) ) || ( sizeMode == ( sizingTop | sizingLeft ) ) )
  1855. desiredCursor = PlatformCursorController::curResizeNWSE;
  1856. else if ( ( sizeMode == ( sizingBottom | sizingLeft ) ) || ( sizeMode == ( sizingTop | sizingRight ) ) )
  1857. desiredCursor = PlatformCursorController::curResizeNESW;
  1858. else if (sizeMode == sizingLeft || sizeMode == sizingRight )
  1859. desiredCursor = PlatformCursorController::curResizeVert;
  1860. else if (sizeMode == sizingTop || sizeMode == sizingBottom )
  1861. desiredCursor = PlatformCursorController::curResizeHorz;
  1862. }
  1863. }
  1864. if( mMouseDownMode == MovingSelection && cursor == NULL )
  1865. desiredCursor = PlatformCursorController::curResizeAll;
  1866. if( pRoot->mCursorChanged != desiredCursor )
  1867. {
  1868. // We've already changed the cursor,
  1869. // so set it back before we change it again.
  1870. if(pRoot->mCursorChanged != -1)
  1871. pController->popCursor();
  1872. // Now change the cursor shape
  1873. pController->pushCursor(desiredCursor);
  1874. pRoot->mCursorChanged = desiredCursor;
  1875. }
  1876. }
  1877. //-----------------------------------------------------------------------------
  1878. void GuiEditCtrl::setSnapToGrid(U32 gridsize)
  1879. {
  1880. if( gridsize != 0 )
  1881. gridsize = getMax( gridsize, ( U32 ) MIN_GRID_SIZE );
  1882. mGridSnap.set( gridsize, gridsize );
  1883. }
  1884. //-----------------------------------------------------------------------------
  1885. void GuiEditCtrl::controlInspectPreApply(GuiControl* object)
  1886. {
  1887. // undo
  1888. onControlInspectPreApply_callback( object );
  1889. }
  1890. //-----------------------------------------------------------------------------
  1891. void GuiEditCtrl::controlInspectPostApply(GuiControl* object)
  1892. {
  1893. // undo
  1894. onControlInspectPostApply_callback( object );
  1895. }
  1896. //-----------------------------------------------------------------------------
  1897. void GuiEditCtrl::startDragMove( const Point2I& startPoint )
  1898. {
  1899. mDragMoveUndo = true;
  1900. // For calculating mouse delta
  1901. mDragBeginPoint = globalToLocalCoord( startPoint );
  1902. // Allocate enough space for our selected controls
  1903. mDragBeginPoints.reserve( mSelectedControls.size() );
  1904. // For snapping to origin
  1905. Vector<GuiControl *>::iterator i;
  1906. for(i = mSelectedControls.begin(); i != mSelectedControls.end(); i++)
  1907. mDragBeginPoints.push_back( (*i)->getPosition() );
  1908. // Set Mouse Mode
  1909. setMouseMode( MovingSelection );
  1910. // undo
  1911. onPreEdit_callback( getSelectedSet() );
  1912. }
  1913. //-----------------------------------------------------------------------------
  1914. void GuiEditCtrl::startDragRectangle( const Point2I& startPoint )
  1915. {
  1916. mSelectionAnchor = globalToLocalCoord( startPoint );
  1917. setMouseMode( DragSelecting );
  1918. }
  1919. //-----------------------------------------------------------------------------
  1920. void GuiEditCtrl::startDragClone( const Point2I& startPoint )
  1921. {
  1922. mDragBeginPoint = globalToLocalCoord( startPoint );
  1923. setMouseMode( DragClone );
  1924. }
  1925. //-----------------------------------------------------------------------------
  1926. void GuiEditCtrl::startMouseGuideDrag( guideAxis axis, U32 guideIndex, bool lockMouse )
  1927. {
  1928. mDragGuideIndex[ axis ] = guideIndex;
  1929. mDragGuide[ axis ] = true;
  1930. setMouseMode( DragGuide );
  1931. // Grab the mouse.
  1932. if( lockMouse )
  1933. mouseLock();
  1934. }
  1935. //=============================================================================
  1936. // Console Methods.
  1937. //=============================================================================
  1938. // MARK: ---- Console Methods ----
  1939. //-----------------------------------------------------------------------------
  1940. ConsoleMethod( GuiEditCtrl, getContentControl, S32, 2, 2, "() - Return the toplevel control edited inside the GUI editor." )
  1941. {
  1942. GuiControl* ctrl = object->getContentControl();
  1943. if( ctrl )
  1944. return ctrl->getId();
  1945. else
  1946. return 0;
  1947. }
  1948. //-----------------------------------------------------------------------------
  1949. ConsoleMethod( GuiEditCtrl, setContentControl, void, 3, 3, "( GuiControl ctrl ) - Set the toplevel control to edit in the GUI editor." )
  1950. {
  1951. GuiControl *ctrl;
  1952. if(!Sim::findObject(argv[2], ctrl))
  1953. return;
  1954. object->setContentControl(ctrl);
  1955. }
  1956. //-----------------------------------------------------------------------------
  1957. ConsoleMethod( GuiEditCtrl, addNewCtrl, void, 3, 3, "(GuiControl ctrl)")
  1958. {
  1959. GuiControl *ctrl;
  1960. if(!Sim::findObject(argv[2], ctrl))
  1961. return;
  1962. object->addNewControl(ctrl);
  1963. }
  1964. //-----------------------------------------------------------------------------
  1965. ConsoleMethod( GuiEditCtrl, addSelection, void, 3, 3, "selects a control.")
  1966. {
  1967. S32 id = dAtoi(argv[2]);
  1968. object->addSelection(id);
  1969. }
  1970. //-----------------------------------------------------------------------------
  1971. ConsoleMethod( GuiEditCtrl, removeSelection, void, 3, 3, "deselects a control.")
  1972. {
  1973. S32 id = dAtoi(argv[2]);
  1974. object->removeSelection(id);
  1975. }
  1976. //-----------------------------------------------------------------------------
  1977. ConsoleMethod( GuiEditCtrl, clearSelection, void, 2, 2, "Clear selected controls list.")
  1978. {
  1979. object->clearSelection();
  1980. }
  1981. //-----------------------------------------------------------------------------
  1982. ConsoleMethod( GuiEditCtrl, select, void, 3, 3, "(GuiControl ctrl)")
  1983. {
  1984. GuiControl *ctrl;
  1985. if(!Sim::findObject(argv[2], ctrl))
  1986. return;
  1987. object->setSelection(ctrl, false);
  1988. }
  1989. //-----------------------------------------------------------------------------
  1990. ConsoleMethod( GuiEditCtrl, setCurrentAddSet, void, 3, 3, "(GuiControl ctrl)")
  1991. {
  1992. GuiControl *addSet;
  1993. if (!Sim::findObject(argv[2], addSet))
  1994. {
  1995. Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
  1996. return;
  1997. }
  1998. object->setCurrentAddSet(addSet);
  1999. }
  2000. //-----------------------------------------------------------------------------
  2001. ConsoleMethod( GuiEditCtrl, getCurrentAddSet, S32, 2, 2, "Returns the set to which new controls will be added")
  2002. {
  2003. const GuiControl* add = object->getCurrentAddSet();
  2004. return add ? add->getId() : 0;
  2005. }
  2006. //-----------------------------------------------------------------------------
  2007. ConsoleMethod( GuiEditCtrl, toggle, void, 2, 2, "Toggle activation.")
  2008. {
  2009. object->setEditMode( !object->isActive() );
  2010. }
  2011. //-----------------------------------------------------------------------------
  2012. ConsoleMethod( GuiEditCtrl, justify, void, 3, 3, "(int mode)" )
  2013. {
  2014. object->justifySelection((GuiEditCtrl::Justification)dAtoi(argv[2]));
  2015. }
  2016. //-----------------------------------------------------------------------------
  2017. ConsoleMethod( GuiEditCtrl, bringToFront, void, 2, 2, "")
  2018. {
  2019. object->bringToFront();
  2020. }
  2021. //-----------------------------------------------------------------------------
  2022. ConsoleMethod( GuiEditCtrl, pushToBack, void, 2, 2, "")
  2023. {
  2024. object->pushToBack();
  2025. }
  2026. //-----------------------------------------------------------------------------
  2027. ConsoleMethod( GuiEditCtrl, deleteSelection, void, 2, 2, "() - Delete the selected controls.")
  2028. {
  2029. object->deleteSelection();
  2030. }
  2031. //-----------------------------------------------------------------------------
  2032. ConsoleMethod( GuiEditCtrl, moveSelection, void, 4, 4, "(int dx, int dy) - Move all controls in the selection by (dx,dy) pixels.")
  2033. {
  2034. object->moveAndSnapSelection(Point2I(dAtoi(argv[2]), dAtoi(argv[3])));
  2035. }
  2036. //-----------------------------------------------------------------------------
  2037. ConsoleMethod( GuiEditCtrl, saveSelection, void, 2, 3, "( string fileName=null ) - Save selection to file or clipboard.")
  2038. {
  2039. const char* filename = NULL;
  2040. if( argc > 2 )
  2041. filename = argv[ 2 ];
  2042. object->saveSelection( filename );
  2043. }
  2044. //-----------------------------------------------------------------------------
  2045. ConsoleMethod( GuiEditCtrl, loadSelection, void, 2, 3, "( string fileName=null ) - Load selection from file or clipboard.")
  2046. {
  2047. const char* filename = NULL;
  2048. if( argc > 2 )
  2049. filename = argv[ 2 ];
  2050. object->loadSelection( filename );
  2051. }
  2052. //-----------------------------------------------------------------------------
  2053. ConsoleMethod( GuiEditCtrl, selectAll, void, 2, 2, "()")
  2054. {
  2055. object->selectAll();
  2056. }
  2057. //-----------------------------------------------------------------------------
  2058. DefineEngineMethod( GuiEditCtrl, getSelection, SimSet*, (),,
  2059. "Gets the set of GUI controls currently selected in the editor." )
  2060. {
  2061. return object->getSelectedSet();
  2062. }
  2063. //-----------------------------------------------------------------------------
  2064. ConsoleMethod( GuiEditCtrl, getNumSelected, S32, 2, 2, "() - Return the number of controls currently selected." )
  2065. {
  2066. return object->getNumSelected();
  2067. }
  2068. //-----------------------------------------------------------------------------
  2069. ConsoleMethod( GuiEditCtrl, getSelectionGlobalBounds, const char*, 2, 2, "() - Returns global bounds of current selection as vector 'x y width height'." )
  2070. {
  2071. RectI bounds = object->getSelectionGlobalBounds();
  2072. String str = String::ToString( "%i %i %i %i", bounds.point.x, bounds.point.y, bounds.extent.x, bounds.extent.y );
  2073. char* buffer = Con::getReturnBuffer( str.length() );
  2074. dStrcpy( buffer, str.c_str() );
  2075. return buffer;
  2076. }
  2077. //-----------------------------------------------------------------------------
  2078. ConsoleMethod( GuiEditCtrl, selectParents, void, 2, 3, "( bool addToSelection=false ) - Select parents of currently selected controls." )
  2079. {
  2080. bool addToSelection = false;
  2081. if( argc > 2 )
  2082. addToSelection = dAtob( argv[ 2 ] );
  2083. object->selectParents( addToSelection );
  2084. }
  2085. //-----------------------------------------------------------------------------
  2086. ConsoleMethod( GuiEditCtrl, selectChildren, void, 2, 3, "( bool addToSelection=false ) - Select children of currently selected controls." )
  2087. {
  2088. bool addToSelection = false;
  2089. if( argc > 2 )
  2090. addToSelection = dAtob( argv[ 2 ] );
  2091. object->selectChildren( addToSelection );
  2092. }
  2093. //-----------------------------------------------------------------------------
  2094. DefineEngineMethod( GuiEditCtrl, getTrash, SimGroup*, (),,
  2095. "Gets the GUI controls(s) that are currently in the trash.")
  2096. {
  2097. return object->getTrash();
  2098. }
  2099. //-----------------------------------------------------------------------------
  2100. ConsoleMethod(GuiEditCtrl, setSnapToGrid, void, 3, 3, "GuiEditCtrl.setSnapToGrid(gridsize)")
  2101. {
  2102. U32 gridsize = dAtoi(argv[2]);
  2103. object->setSnapToGrid(gridsize);
  2104. }
  2105. //-----------------------------------------------------------------------------
  2106. ConsoleMethod( GuiEditCtrl, readGuides, void, 3, 4, "( GuiControl ctrl [, int axis ] ) - Read the guides from the given control." )
  2107. {
  2108. // Find the control.
  2109. GuiControl* ctrl;
  2110. if( !Sim::findObject( argv[ 2 ], ctrl ) )
  2111. {
  2112. Con::errorf( "GuiEditCtrl::readGuides - no control '%s'", argv[ 2 ] );
  2113. return;
  2114. }
  2115. // Read the guides.
  2116. if( argc > 3 )
  2117. {
  2118. S32 axis = dAtoi( argv[ 3 ] );
  2119. if( axis < 0 || axis > 1 )
  2120. {
  2121. Con::errorf( "GuiEditCtrl::readGuides - invalid axis '%s'", argv[ 3 ] );
  2122. return;
  2123. }
  2124. object->readGuides( ( GuiEditCtrl::guideAxis ) axis, ctrl );
  2125. }
  2126. else
  2127. {
  2128. object->readGuides( GuiEditCtrl::GuideHorizontal, ctrl );
  2129. object->readGuides( GuiEditCtrl::GuideVertical, ctrl );
  2130. }
  2131. }
  2132. //-----------------------------------------------------------------------------
  2133. ConsoleMethod( GuiEditCtrl, writeGuides, void, 3, 4, "( GuiControl ctrl [, int axis ] ) - Write the guides to the given control." )
  2134. {
  2135. // Find the control.
  2136. GuiControl* ctrl;
  2137. if( !Sim::findObject( argv[ 2 ], ctrl ) )
  2138. {
  2139. Con::errorf( "GuiEditCtrl::writeGuides - no control '%i'", argv[ 2 ] );
  2140. return;
  2141. }
  2142. // Write the guides.
  2143. if( argc > 3 )
  2144. {
  2145. S32 axis = dAtoi( argv[ 3 ] );
  2146. if( axis < 0 || axis > 1 )
  2147. {
  2148. Con::errorf( "GuiEditCtrl::writeGuides - invalid axis '%s'", argv[ 3 ] );
  2149. return;
  2150. }
  2151. object->writeGuides( ( GuiEditCtrl::guideAxis ) axis, ctrl );
  2152. }
  2153. else
  2154. {
  2155. object->writeGuides( GuiEditCtrl::GuideHorizontal, ctrl );
  2156. object->writeGuides( GuiEditCtrl::GuideVertical, ctrl );
  2157. }
  2158. }
  2159. //-----------------------------------------------------------------------------
  2160. ConsoleMethod( GuiEditCtrl, clearGuides, void, 2, 3, "( [ int axis ] ) - Clear all currently set guide lines." )
  2161. {
  2162. if( argc > 2 )
  2163. {
  2164. S32 axis = dAtoi( argv[ 2 ] );
  2165. if( axis < 0 || axis > 1 )
  2166. {
  2167. Con::errorf( "GuiEditCtrl::clearGuides - invalid axis '%i'", axis );
  2168. return;
  2169. }
  2170. object->clearGuides( ( GuiEditCtrl::guideAxis ) axis );
  2171. }
  2172. else
  2173. {
  2174. object->clearGuides( GuiEditCtrl::GuideHorizontal );
  2175. object->clearGuides( GuiEditCtrl::GuideVertical );
  2176. }
  2177. }
  2178. //-----------------------------------------------------------------------------
  2179. ConsoleMethod( GuiEditCtrl, fitIntoParents, void, 2, 4, "( bool width=true, bool height=true ) - Fit selected controls into their parents." )
  2180. {
  2181. bool width = true;
  2182. bool height = true;
  2183. if( argc > 2 )
  2184. width = dAtob( argv[ 2 ] );
  2185. if( argc > 3 )
  2186. height = dAtob( argv[ 3 ] );
  2187. object->fitIntoParents( width, height );
  2188. }
  2189. //-----------------------------------------------------------------------------
  2190. ConsoleMethod( GuiEditCtrl, getMouseMode, const char*, 2, 2, "() - Return the current mouse mode." )
  2191. {
  2192. switch( object->getMouseMode() )
  2193. {
  2194. case GuiEditCtrl::Selecting:
  2195. return "Selecting";
  2196. case GuiEditCtrl::DragSelecting:
  2197. return "DragSelecting";
  2198. case GuiEditCtrl::MovingSelection:
  2199. return "MovingSelection";
  2200. case GuiEditCtrl::SizingSelection:
  2201. return "SizingSelection";
  2202. case GuiEditCtrl::DragGuide:
  2203. return "DragGuide";
  2204. case GuiEditCtrl::DragClone:
  2205. return "DragClone";
  2206. default:
  2207. return "";
  2208. }
  2209. }
  2210. //=============================================================================
  2211. // GuiEditorRuler.
  2212. //=============================================================================
  2213. class GuiEditorRuler : public GuiControl
  2214. {
  2215. public:
  2216. typedef GuiControl Parent;
  2217. protected:
  2218. String mRefCtrlName;
  2219. String mEditCtrlName;
  2220. GuiScrollCtrl* mRefCtrl;
  2221. GuiEditCtrl* mEditCtrl;
  2222. public:
  2223. enum EOrientation
  2224. {
  2225. ORIENTATION_Horizontal,
  2226. ORIENTATION_Vertical
  2227. };
  2228. GuiEditorRuler()
  2229. : mRefCtrl( 0 ),
  2230. mEditCtrl( 0 )
  2231. {
  2232. }
  2233. EOrientation getOrientation() const
  2234. {
  2235. if( getWidth() > getHeight() )
  2236. return ORIENTATION_Horizontal;
  2237. else
  2238. return ORIENTATION_Vertical;
  2239. }
  2240. bool onWake()
  2241. {
  2242. if( !Parent::onWake() )
  2243. return false;
  2244. if( !mEditCtrlName.isEmpty() && !Sim::findObject( mEditCtrlName, mEditCtrl ) )
  2245. Con::errorf( "GuiEditorRuler::onWake() - no GuiEditCtrl '%s'", mEditCtrlName.c_str() );
  2246. if( !mRefCtrlName.isEmpty() && !Sim::findObject( mRefCtrlName, mRefCtrl ) )
  2247. Con::errorf( "GuiEditorRuler::onWake() - no GuiScrollCtrl '%s'", mRefCtrlName.c_str() );
  2248. return true;
  2249. }
  2250. void onPreRender()
  2251. {
  2252. setUpdate();
  2253. }
  2254. void onMouseDown( const GuiEvent& event )
  2255. {
  2256. if( !mEditCtrl )
  2257. return;
  2258. // Determine the guide axis.
  2259. GuiEditCtrl::guideAxis axis;
  2260. if( getOrientation() == ORIENTATION_Horizontal )
  2261. axis = GuiEditCtrl::GuideHorizontal;
  2262. else
  2263. axis = GuiEditCtrl::GuideVertical;
  2264. // Start dragging a new guide out in the editor.
  2265. U32 guideIndex = mEditCtrl->addGuide( axis, 0 );
  2266. mEditCtrl->startMouseGuideDrag( axis, guideIndex );
  2267. }
  2268. void onRender(Point2I offset, const RectI &updateRect)
  2269. {
  2270. GFX->getDrawUtil()->drawRectFill(updateRect, ColorF(1,1,1,1));
  2271. Point2I choffset(0,0);
  2272. if( mRefCtrl != NULL )
  2273. choffset = mRefCtrl->getChildPos();
  2274. if( getOrientation() == ORIENTATION_Horizontal )
  2275. {
  2276. // it's horizontal.
  2277. for(U32 i = 0; i < getWidth(); i++)
  2278. {
  2279. S32 x = offset.x + i;
  2280. S32 pos = i - choffset.x;
  2281. if(!(pos % 10))
  2282. {
  2283. S32 start = 6;
  2284. if(!(pos %20))
  2285. start = 4;
  2286. if(!(pos % 100))
  2287. start = 1;
  2288. GFX->getDrawUtil()->drawLine(x, offset.y + start, x, offset.y + 10, ColorF(0,0,0,1));
  2289. }
  2290. }
  2291. }
  2292. else
  2293. {
  2294. // it's vertical.
  2295. for(U32 i = 0; i < getHeight(); i++)
  2296. {
  2297. S32 y = offset.y + i;
  2298. S32 pos = i - choffset.y;
  2299. if(!(pos % 10))
  2300. {
  2301. S32 start = 6;
  2302. if(!(pos %20))
  2303. start = 4;
  2304. if(!(pos % 100))
  2305. start = 1;
  2306. GFX->getDrawUtil()->drawLine(offset.x + start, y, offset.x + 10, y, ColorF(0,0,0,1));
  2307. }
  2308. }
  2309. }
  2310. }
  2311. static void initPersistFields()
  2312. {
  2313. addField( "refCtrl", TypeRealString, Offset( mRefCtrlName, GuiEditorRuler ) );
  2314. addField( "editCtrl", TypeRealString, Offset( mEditCtrlName, GuiEditorRuler ) );
  2315. Parent::initPersistFields();
  2316. }
  2317. DECLARE_CONOBJECT(GuiEditorRuler);
  2318. DECLARE_CATEGORY( "Gui Editor" );
  2319. };
  2320. IMPLEMENT_CONOBJECT(GuiEditorRuler);
  2321. ConsoleDocClass( GuiEditorRuler,
  2322. "@brief Visual representation of markers on top and left sides of GUI Editor\n\n"
  2323. "Editor use only.\n\n"
  2324. "@internal"
  2325. );