guiEditCtrl.cpp 88 KB

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