guiEditCtrl.cpp 88 KB

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