guiEditCtrl.cpp 88 KB

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