guiWindowCtrl.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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/containers/guiWindowCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "console/engineAPI.h"
  27. #include "gui/core/guiCanvas.h"
  28. #include "gui/core/guiDefaultControlRender.h"
  29. #include "gfx/gfxDevice.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "gui/containers/guiRolloutCtrl.h"
  32. IMPLEMENT_CONOBJECT( GuiWindowCtrl );
  33. ConsoleDocClass( GuiWindowCtrl,
  34. "@brief A window with a title bar and an optional set of buttons.\n\n"
  35. "The GuiWindowCtrl class implements windows that can be freely placed within the render window. Additionally, "
  36. "the windows can be resized and maximized/minimized.\n\n"
  37. "@tsexample\n"
  38. "new GuiWindowCtrl( MyWindow )\n"
  39. "{\n"
  40. " text = \"My Window\"; // The text that is displayed on the title bar.\n"
  41. " resizeWidth = true; // Allow horizontal resizing by user via mouse.\n"
  42. " resizeHeight = true; // Allow vertical resizing by user via mouse.\n"
  43. " canClose = true; // Display a close button in the title bar.\n"
  44. " canMinimize = true; // Display a minimize button in the title bar.\n"
  45. " canMaximize = true; // Display a maximize button in the title bar.\n"
  46. "};\n"
  47. "@endtsexample\n\n"
  48. "@ingroup GuiContainers"
  49. );
  50. IMPLEMENT_CALLBACK( GuiWindowCtrl, onClose, void, (), (),
  51. "Called when the close button has been pressed." );
  52. IMPLEMENT_CALLBACK( GuiWindowCtrl, onMinimize, void, (), (),
  53. "Called when the window has been minimized." );
  54. IMPLEMENT_CALLBACK( GuiWindowCtrl, onMaximize, void, (), (),
  55. "Called when the window has been maximized." );
  56. IMPLEMENT_CALLBACK( GuiWindowCtrl, onCollapse, void, (), (),
  57. "Called when the window is collapsed by clicking its title bar." );
  58. IMPLEMENT_CALLBACK( GuiWindowCtrl, onRestore, void, (), (),
  59. "Called when the window is restored from minimized, maximized, or collapsed state." );
  60. //-----------------------------------------------------------------------------
  61. GuiWindowCtrl::GuiWindowCtrl()
  62. : mResizeEdge(edgeNone),
  63. mResizeWidth(true),
  64. mResizeHeight(true),
  65. mResizeMargin(2.f),
  66. mCanMove(true),
  67. mCanClose(true),
  68. mCanMinimize(true),
  69. mCanMaximize(true),
  70. mCanDock(false),
  71. mCanCollapse(false),
  72. mEdgeSnap(true),
  73. mCollapseGroup(-1),
  74. mCollapseGroupNum(-1),
  75. mIsCollapsed(false),
  76. mIsMouseResizing(false)
  77. {
  78. // mTitleHeight will change in instanciation most likely...
  79. mTitleHeight = 24;
  80. mIsContainer = true;
  81. mCloseCommand = StringTable->EmptyString();
  82. mMinimized = false;
  83. mMaximized = false;
  84. mMouseMovingWin = false;
  85. mMouseResizeWidth = false;
  86. mMouseResizeHeight = false;
  87. mMinimizeIndex = -1;
  88. mTabIndex = -1;
  89. mBitmapBounds = NULL;
  90. setExtent(100, 200);
  91. RectI closeRect(80, 2, 16, 16);
  92. mCloseButton = closeRect;
  93. closeRect.point.x -= 18;
  94. mMaximizeButton = closeRect;
  95. closeRect.point.x -= 18;
  96. mMinimizeButton = closeRect;
  97. // Other defaults
  98. mActive = true;
  99. mCloseButtonPressed = false;
  100. mMaximizeButtonPressed = false;
  101. mMinimizeButtonPressed = false;
  102. mText = "New Window";
  103. }
  104. //-----------------------------------------------------------------------------
  105. void GuiWindowCtrl::initPersistFields()
  106. {
  107. addGroup( "Window" );
  108. addField( "text", TypeRealString, Offset( mText, GuiWindowCtrl ),
  109. "Text label to display in titlebar." );
  110. addField( "resizeWidth", TypeBool, Offset( mResizeWidth, GuiWindowCtrl ),
  111. "Whether the window can be resized horizontally." );
  112. addField( "resizeHeight", TypeBool, Offset( mResizeHeight, GuiWindowCtrl ),
  113. "Whether the window can be resized vertically." );
  114. addField( "canMove", TypeBool, Offset( mCanMove, GuiWindowCtrl ),
  115. "Whether the window can be moved by dragging its titlebar." );
  116. addField( "canClose", TypeBool, Offset( mCanClose, GuiWindowCtrl ),
  117. "Whether the window has a close button." );
  118. addField( "canMinimize", TypeBool, Offset( mCanMinimize, GuiWindowCtrl ),
  119. "Whether the window has a minimize button." );
  120. addField( "canMaximize", TypeBool, Offset( mCanMaximize, GuiWindowCtrl ),
  121. "Whether the window has a maximize button." );
  122. addField( "canCollapse", TypeBool, Offset( mCanCollapse, GuiWindowCtrl ),
  123. "Whether the window can be collapsed by clicking its title bar." );
  124. addField( "closeCommand", TypeString, Offset( mCloseCommand, GuiWindowCtrl ),
  125. "Script code to execute when the window is closed." );
  126. addField( "edgeSnap", TypeBool, Offset( mEdgeSnap,GuiWindowCtrl ),
  127. "If true, the window will snap to the edges of other windows when moved close to them." );
  128. endGroup( "Window" );
  129. Parent::initPersistFields();
  130. }
  131. //=============================================================================
  132. // Collapsing.
  133. //=============================================================================
  134. // MARK: ---- Collapsing ----
  135. //-----------------------------------------------------------------------------
  136. void GuiWindowCtrl::moveFromCollapseGroup()
  137. {
  138. GuiControl *parent = getParent();
  139. if( !parent )
  140. return;
  141. S32 groupVec = mCollapseGroup;
  142. S32 vecPos = mCollapseGroupNum;
  143. S32 groupVecCount = parent->mCollapseGroupVec[groupVec].size() - 1;
  144. CollapseGroupNumVec collapseGroupNumVec;
  145. if( groupVecCount > vecPos )
  146. {
  147. if (vecPos == 1)
  148. {
  149. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[groupVec].begin();
  150. for(; iter != parent->mCollapseGroupVec[groupVec].end(); iter++ )
  151. {
  152. if((*iter)->mCollapseGroupNum >= vecPos)
  153. collapseGroupNumVec.push_back((*iter));
  154. }
  155. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroup = -1;
  156. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroupNum = -1;
  157. parent->mCollapseGroupVec[groupVec].erase(U32(0));
  158. parent->mCollapseGroupVec[groupVec].setSize(groupVecCount - 1);
  159. parent->mCollapseGroupVec.erase(groupVec);
  160. if(groupVec > 0)
  161. parent->mCollapseGroupVec.setSize(groupVec);
  162. parent->mCollapseGroupVec.push_back( collapseGroupNumVec );
  163. }
  164. else
  165. {
  166. // Iterate through the group i was once in, gather myself and the controls below me and store them in an array
  167. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[groupVec].begin();
  168. for(; iter != parent->mCollapseGroupVec[groupVec].end(); iter++ )
  169. {
  170. if((*iter)->mCollapseGroupNum >= vecPos)
  171. collapseGroupNumVec.push_back((*iter));
  172. }
  173. // Iterate through the newly created array; delete my references in the old group, create a new group and organize me accord.
  174. S32 assignWindowNumber = 0;
  175. CollapseGroupNumVec::iterator iter2 = collapseGroupNumVec.begin();
  176. for(; iter2 != collapseGroupNumVec.end(); iter2++ )
  177. {
  178. parent->mCollapseGroupVec[groupVec].pop_back();
  179. parent->mCollapseGroupVec[groupVec].setSize(groupVecCount);
  180. (*iter2)->mCollapseGroup = (parent->mCollapseGroupVec.size());
  181. (*iter2)->mCollapseGroupNum = assignWindowNumber;
  182. assignWindowNumber++;
  183. groupVecCount--;
  184. }
  185. parent->mCollapseGroupVec.push_back( collapseGroupNumVec );
  186. }
  187. }
  188. else
  189. {
  190. parent->mCollapseGroupVec[groupVec].erase(mCollapseGroupNum);
  191. parent->mCollapseGroupVec[groupVec].setSize(groupVecCount);
  192. mCollapseGroup = -1;
  193. mCollapseGroupNum = -1;
  194. if(groupVecCount <= 1)
  195. {
  196. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroup = -1;
  197. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroupNum = -1;
  198. parent->mCollapseGroupVec[groupVec].erase(U32(0));
  199. parent->mCollapseGroupVec[groupVec].setSize(groupVecCount - 1);
  200. parent->mCollapseGroupVec.erase(groupVec);
  201. }
  202. }
  203. // Re id collapse groups
  204. refreshCollapseGroups();
  205. }
  206. //-----------------------------------------------------------------------------
  207. void GuiWindowCtrl::moveToCollapseGroup(GuiWindowCtrl* hitWindow, bool orientation )
  208. {
  209. // Orientation 0 - window in question is being connected to top of another window
  210. // Orientation 1 - window in question is being connected to bottom of another window
  211. GuiControl *parent = getParent();
  212. if( !parent )
  213. return;
  214. S32 groupVec = mCollapseGroup;
  215. S32 attatchedGroupVec = hitWindow->mCollapseGroup;
  216. S32 vecPos = mCollapseGroupNum;
  217. if(mCollapseGroup == attatchedGroupVec && vecPos != -1)
  218. return;
  219. CollapseGroupNumVec collapseGroupNumVec;
  220. // Window colliding with is not in a collapse group
  221. if(hitWindow->mCollapseGroup < 0)
  222. {
  223. // We(the collider) are in a group of windows
  224. if(mCollapseGroup >= 0)
  225. {
  226. S32 groupVecCount = parent->mCollapseGroupVec[groupVec].size() - 1;
  227. // Copy pointer window data in my array, and store in a temp array
  228. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[groupVec].begin();
  229. for(; iter != parent->mCollapseGroupVec[groupVec].end(); iter++ )
  230. {
  231. if((*iter)->mCollapseGroupNum >= vecPos)
  232. {
  233. collapseGroupNumVec.push_back((*iter));
  234. groupVecCount--;
  235. }
  236. }
  237. // Kill my old array group and erase its footprints
  238. if( vecPos <= 1 || groupVecCount == 0 )
  239. {
  240. // Isn't covered in the renumbering of groups, so we it here
  241. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroup = -1;
  242. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroupNum = -1;
  243. parent->mCollapseGroupVec[groupVec].clear();
  244. parent->mCollapseGroupVec[groupVec].setSize(U32(0));
  245. parent->mCollapseGroupVec.erase(groupVec);
  246. if(groupVec > 0)
  247. parent->mCollapseGroupVec.setSize(groupVec);
  248. }
  249. // Push the collided window
  250. if(orientation == 0)
  251. collapseGroupNumVec.push_back(hitWindow);
  252. else
  253. collapseGroupNumVec.push_front(hitWindow);
  254. // Push the temp array in the guiControl array holder
  255. parent->mCollapseGroupVec.push_back( collapseGroupNumVec );
  256. }
  257. else
  258. {
  259. if(orientation == 0)
  260. {
  261. collapseGroupNumVec.push_front(hitWindow);
  262. collapseGroupNumVec.push_front(this);
  263. }
  264. else
  265. {
  266. collapseGroupNumVec.push_front(this);
  267. collapseGroupNumVec.push_front(hitWindow);
  268. }
  269. parent->mCollapseGroupVec.push_back( collapseGroupNumVec );
  270. }
  271. }
  272. else // Window colliding with *IS* in a collapse group
  273. {
  274. if(mCollapseGroup >= 0)
  275. {
  276. S32 groupVecCount = parent->mCollapseGroupVec[groupVec].size() - 1;
  277. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[groupVec].begin();
  278. for(; iter != parent->mCollapseGroupVec[groupVec].end(); iter++ )
  279. {
  280. if((*iter)->mCollapseGroupNum >= vecPos)
  281. {
  282. // Push back the pointer window controls to the collided array
  283. parent->mCollapseGroupVec[attatchedGroupVec].push_back((*iter));
  284. groupVecCount--;
  285. }
  286. }
  287. if( vecPos <= 1 || groupVecCount == 0 )
  288. {
  289. // Isn't covered in the renumbering of groups, so we it here
  290. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroup = -1;
  291. parent->mCollapseGroupVec[groupVec].first()->mCollapseGroupNum = -1;
  292. parent->mCollapseGroupVec[groupVec].clear();
  293. parent->mCollapseGroupVec[groupVec].setSize(U32(0));
  294. parent->mCollapseGroupVec.erase(groupVec);
  295. if(groupVec > 0)
  296. parent->mCollapseGroupVec.setSize(groupVec);
  297. }
  298. // Since we killed my old array group, run in case the guiControl array moved me down a notch
  299. if(attatchedGroupVec > groupVec )
  300. attatchedGroupVec--;
  301. }
  302. else
  303. {
  304. S32 groupVec = hitWindow->mCollapseGroup;
  305. if(orientation == 0)
  306. parent->mCollapseGroupVec[groupVec].push_front(this);
  307. else
  308. parent->mCollapseGroupVec[groupVec].push_back(this);
  309. }
  310. }
  311. // Re id collapse groups
  312. refreshCollapseGroups();
  313. // Select the current window and its collapse group
  314. selectWindow();
  315. }
  316. //-----------------------------------------------------------------------------
  317. void GuiWindowCtrl::refreshCollapseGroups()
  318. {
  319. GuiControl *parent = getParent();
  320. if( !parent )
  321. return;
  322. CollapseGroupNumVec collapseGroupNumVec;
  323. // iterate through the collided array, renumbering the windows pointers
  324. S32 assignGroupNum = 0;
  325. CollapseGroupVec::iterator iter = parent->mCollapseGroupVec.begin();
  326. for(; iter != parent->mCollapseGroupVec.end(); iter++ )
  327. {
  328. S32 assignWindowNumber = 0;
  329. CollapseGroupNumVec::iterator iter2 = parent->mCollapseGroupVec[assignGroupNum].begin();
  330. for(; iter2 != parent->mCollapseGroupVec[assignGroupNum].end(); iter2++ )
  331. {
  332. (*iter2)->mCollapseGroup = assignGroupNum;
  333. (*iter2)->mCollapseGroupNum = assignWindowNumber;
  334. assignWindowNumber++;
  335. }
  336. assignGroupNum++;
  337. }
  338. }
  339. //-----------------------------------------------------------------------------
  340. void GuiWindowCtrl::moveWithCollapseGroup(Point2I windowPosition)
  341. {
  342. GuiControl *parent = getParent();
  343. if( !parent )
  344. return;
  345. Point2I newChildPosition(0, 0);
  346. S32 addedPosition = getExtent().y;
  347. // Iterate through windows below this. Move them according to my position.
  348. CollapseGroupNumVec collapseGroupNumVec;
  349. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[mCollapseGroup].begin();
  350. for(; iter != parent->mCollapseGroupVec[mCollapseGroup].end(); iter++ )
  351. {
  352. if((*iter)->mCollapseGroupNum > mCollapseGroupNum)
  353. {
  354. newChildPosition.x = windowPosition.x;
  355. newChildPosition.y = windowPosition.y + addedPosition;
  356. (*iter)->resize(newChildPosition, (*iter)->getExtent());
  357. addedPosition += (*iter)->getExtent().y;
  358. }
  359. }
  360. }
  361. //-----------------------------------------------------------------------------
  362. void GuiWindowCtrl::setCollapseGroup(bool state)
  363. {
  364. GuiControl *parent = getParent();
  365. if( !parent )
  366. return;
  367. if( mIsCollapsed != state )
  368. {
  369. mIsCollapsed = state;
  370. handleCollapseGroup();
  371. }
  372. }
  373. //-----------------------------------------------------------------------------
  374. void GuiWindowCtrl::toggleCollapseGroup()
  375. {
  376. GuiControl *parent = getParent();
  377. if( !parent )
  378. return;
  379. mIsCollapsed = !mIsCollapsed;
  380. handleCollapseGroup();
  381. }
  382. //-----------------------------------------------------------------------------
  383. void GuiWindowCtrl::handleCollapseGroup()
  384. {
  385. GuiControl *parent = getParent();
  386. if( !parent )
  387. return;
  388. CollapseGroupNumVec collapseGroupNumVec;
  389. if( mIsCollapsed ) // minimize window up to its header bar
  390. {
  391. //save settings
  392. mPreCollapsedYExtent = getExtent().y;
  393. mPreCollapsedYMinExtent = getMinExtent().y;
  394. //create settings for collapsed window to abide by
  395. mResizeHeight = false;
  396. setMinExtent( Point2I( getMinExtent().x, 24 ) );
  397. iterator i;
  398. for(i = begin(); i != end(); i++)
  399. {
  400. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  401. ctrl->setVisible(false);
  402. ctrl->mCanResize = false;
  403. }
  404. resize( getPosition(), Point2I( getExtent().x, 24 ) );
  405. if(mCollapseGroup >= 0)
  406. {
  407. S32 moveChildYBy = (mPreCollapsedYExtent - 24);
  408. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[mCollapseGroup].begin();
  409. for(; iter != parent->mCollapseGroupVec[mCollapseGroup].end(); iter++ )
  410. {
  411. if((*iter)->mCollapseGroupNum > mCollapseGroupNum)
  412. {
  413. Point2I newChildPosition = (*iter)->getPosition();
  414. newChildPosition.y -= moveChildYBy;
  415. (*iter)->resize(newChildPosition, (*iter)->getExtent());
  416. }
  417. }
  418. }
  419. onCollapse_callback();
  420. }
  421. else // maximize the window to its previous position
  422. {
  423. //create and load settings
  424. mResizeHeight = true;
  425. setMinExtent( Point2I( getMinExtent().x, mPreCollapsedYMinExtent ) );
  426. resize( getPosition(), Point2I( getExtent().x, mPreCollapsedYExtent ) );
  427. iterator i;
  428. for(i = begin(); i != end(); i++)
  429. {
  430. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  431. ctrl->setVisible(true);
  432. ctrl->mCanResize = true;
  433. }
  434. if(mCollapseGroup >= 0)
  435. {
  436. S32 moveChildYBy = (mPreCollapsedYExtent - 24);
  437. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[mCollapseGroup].begin();
  438. for(; iter != parent->mCollapseGroupVec[mCollapseGroup].end(); iter++ )
  439. {
  440. if((*iter)->mCollapseGroupNum > mCollapseGroupNum)
  441. {
  442. Point2I newChildPosition = (*iter)->getPosition();
  443. newChildPosition.y += moveChildYBy;
  444. (*iter)->resize(newChildPosition, (*iter)->getExtent());
  445. }
  446. }
  447. }
  448. onRestore_callback();
  449. }
  450. }
  451. //-----------------------------------------------------------------------------
  452. bool GuiWindowCtrl::resizeCollapseGroup(bool resizeX, bool resizeY, Point2I resizePos, Point2I resizeExtent)
  453. {
  454. GuiControl *parent = getParent();
  455. if( !parent )
  456. return false;
  457. CollapseGroupNumVec collapseGroupNumVec;
  458. bool canResize = true;
  459. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[mCollapseGroup].begin();
  460. for(; iter != parent->mCollapseGroupVec[mCollapseGroup].end(); iter++ )
  461. {
  462. if((*iter) == this)
  463. continue;
  464. Point2I newChildPosition = (*iter)->getPosition();
  465. Point2I newChildExtent = (*iter)->getExtent();
  466. if( resizeX == true )
  467. {
  468. newChildPosition.x -= resizePos.x;
  469. newChildExtent.x -= resizeExtent.x;
  470. }
  471. if( resizeY == true )
  472. {
  473. if((*iter)->mCollapseGroupNum > mCollapseGroupNum)
  474. {
  475. newChildPosition.y -= resizeExtent.y;
  476. newChildPosition.y -= resizePos.y;
  477. }
  478. else if((*iter)->mCollapseGroupNum == mCollapseGroupNum - 1)
  479. newChildExtent.y -= resizePos.y;
  480. }
  481. // check is done for normal extent of windows. if false, check again in case its just giving false
  482. // due to being a collapsed window. if your truly being forced passed your extent, return false
  483. if( !(*iter)->mIsCollapsed && newChildExtent.y >= (*iter)->getMinExtent().y )
  484. (*iter)->resize( newChildPosition, newChildExtent);
  485. else
  486. {
  487. if( (*iter)->mIsCollapsed )
  488. (*iter)->resize( newChildPosition, newChildExtent);
  489. else
  490. canResize = false;
  491. }
  492. }
  493. return canResize;
  494. }
  495. //-----------------------------------------------------------------------------
  496. // Mouse Methods
  497. //-----------------------------------------------------------------------------
  498. S32 GuiWindowCtrl::findHitEdges( const Point2I &globalPoint )
  499. {
  500. // No Edges
  501. S32 edgeMask = edgeNone;
  502. GuiControl *parent = getParent();
  503. if( !parent )
  504. return edgeMask;
  505. RectI bounds( getGlobalBounds() );
  506. // Create an EdgeRectI structure that has four edges
  507. // Left/Right/Top/Bottom
  508. // Each Edge structure has a hit operation that will take
  509. // another edge and test for a hit on the edge with a margin
  510. // specified by the .margin scalar
  511. EdgeRectI edges = EdgeRectI(bounds, mResizeMargin);
  512. // Get Cursor Edges
  513. Edge cursorVertEdge = Edge( globalPoint, Point2F( 1.f, 0.f ) );
  514. Edge cursorHorzEdge = Edge( globalPoint, Point2F( 0.f, 1.f ) );
  515. if( edges.left.hit( cursorVertEdge ) )
  516. edgeMask |= edgeLeft;
  517. else if( edges.right.hit( cursorVertEdge ) )
  518. edgeMask |= edgeRight;
  519. if( edges.bottom.hit( cursorHorzEdge ) )
  520. edgeMask |= edgeBottom;
  521. else if( edges.top.hit( cursorHorzEdge ) )
  522. {
  523. // Only the top window in a collapse group can be extended from the top
  524. if( mCanCollapse && mCollapseGroup >= 0 )
  525. {
  526. if( parent->mCollapseGroupVec[mCollapseGroup].first() != this )
  527. return edgeMask;
  528. }
  529. edgeMask |= edgeTop;
  530. }
  531. // Return the resulting mask
  532. return edgeMask;
  533. }
  534. void GuiWindowCtrl::getSnappableWindows( Vector<GuiWindowCtrl*> &windowOutVector, bool canCollapse )
  535. {
  536. GuiControl *parent = getParent();
  537. if( !parent )
  538. return;
  539. S32 parentSize = parent->size();
  540. for( S32 i = 0; i < parentSize; i++ )
  541. {
  542. GuiWindowCtrl *childWindow = dynamic_cast<GuiWindowCtrl*>(parent->at(i));
  543. if( !childWindow || !childWindow->isVisible() || childWindow == this || childWindow->mEdgeSnap == false)
  544. continue;
  545. if( canCollapse && !childWindow->mCanCollapse )
  546. continue;
  547. windowOutVector.push_back(childWindow);
  548. }
  549. }
  550. //=============================================================================
  551. // Events.
  552. //=============================================================================
  553. // MARK: ---- Events ----
  554. //-----------------------------------------------------------------------------
  555. bool GuiWindowCtrl::onWake()
  556. {
  557. if (! Parent::onWake())
  558. return false;
  559. //get the texture for the close, minimize, and maximize buttons
  560. mTextureObject = mProfile->mTextureObject;
  561. bool result = mProfile->constructBitmapArray() >= NumBitmaps;
  562. if( !result )
  563. {
  564. Con::errorf( "GuiWindowCtrl::onWake - failed to create bitmap array from profile bitmap." );
  565. return false;
  566. }
  567. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  568. S32 buttonHeight = mBitmapBounds[BmpStates * BmpClose].extent.y;
  569. mTitleHeight = buttonHeight + 4;
  570. //set the button coords
  571. positionButtons();
  572. //set the tab index
  573. mTabIndex = -1;
  574. GuiControl *parent = getParent();
  575. if (parent && mFirstResponder)
  576. {
  577. mTabIndex = 0;
  578. //count the number of windows preceeding this one
  579. iterator i;
  580. for (i = parent->begin(); i != parent->end(); i++)
  581. {
  582. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  583. if (ctrl)
  584. {
  585. if (ctrl == this) break;
  586. else if (ctrl->mFirstResponder) mTabIndex++;
  587. }
  588. }
  589. }
  590. return true;
  591. }
  592. //-----------------------------------------------------------------------------
  593. void GuiWindowCtrl::onSleep()
  594. {
  595. mTextureObject = NULL;
  596. mMousePosition = Point2I(0,0);
  597. Parent::onSleep();
  598. }
  599. //-----------------------------------------------------------------------------
  600. void GuiWindowCtrl::onMouseDown(const GuiEvent &event)
  601. {
  602. setUpdate();
  603. mOrigBounds = getBounds();
  604. mMouseDownPosition = event.mousePoint;
  605. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  606. // Select this window - move it to the front, and set the first responder
  607. selectWindow();
  608. mMouseMovingWin = false;
  609. S32 hitEdges = findHitEdges( event.mousePoint );
  610. mResizeEdge = edgeNone;
  611. // Set flag by default so we only clear it
  612. // if we don't match either edge
  613. mMouseResizeHeight = true;
  614. // Check Bottom/Top edges (Mutually Exclusive)
  615. if( mResizeHeight && hitEdges & edgeBottom )
  616. mResizeEdge |= edgeBottom;
  617. else if( mResizeHeight && hitEdges & edgeTop )
  618. mResizeEdge |= edgeTop;
  619. else
  620. mMouseResizeHeight = false;
  621. // Set flag by default so we only clear it
  622. // if we don't match either edge
  623. mMouseResizeWidth = true;
  624. // Check Left/Right edges (Mutually Exclusive)
  625. if( mResizeWidth && hitEdges & edgeLeft )
  626. mResizeEdge |= edgeLeft;
  627. else if( mResizeWidth && hitEdges & edgeRight )
  628. mResizeEdge |= edgeRight;
  629. else
  630. mMouseResizeWidth = false;
  631. // If we clicked within the title bar
  632. if ( !(mResizeEdge & ( edgeTop | edgeLeft | edgeRight ) ) && localPoint.y < mTitleHeight)
  633. {
  634. if (mCanClose && mCloseButton.pointInRect(localPoint))
  635. {
  636. mCloseButtonPressed = mCanClose;
  637. }
  638. else if (mCanMaximize && mMaximizeButton.pointInRect(localPoint))
  639. {
  640. mMaximizeButtonPressed = mCanMaximize;
  641. }
  642. else if (mCanMinimize && mMinimizeButton.pointInRect(localPoint))
  643. {
  644. mMinimizeButtonPressed = mCanMinimize;
  645. }
  646. else // We clicked anywhere else within the title
  647. {
  648. S32 docking = getDocking();
  649. if( docking == Docking::dockInvalid || docking == Docking::dockNone )
  650. mMouseMovingWin = mCanMove;
  651. mMouseResizeWidth = false;
  652. mMouseResizeHeight = false;
  653. }
  654. }
  655. if (mMouseMovingWin || mResizeEdge != edgeNone ||
  656. mCloseButtonPressed || mMaximizeButtonPressed || mMinimizeButtonPressed)
  657. {
  658. mouseLock();
  659. }
  660. else
  661. {
  662. GuiControl *ctrl = findHitControl(localPoint);
  663. if (ctrl && ctrl != this)
  664. ctrl->onMouseDown(event);
  665. }
  666. }
  667. //-----------------------------------------------------------------------------
  668. void GuiWindowCtrl::onMouseDragged(const GuiEvent &event)
  669. {
  670. GuiControl *parent = getParent();
  671. GuiCanvas *root = getRoot();
  672. if ( !root )
  673. return;
  674. mMousePosition = globalToLocalCoord(event.mousePoint);
  675. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  676. Point2I newPosition = getPosition();
  677. Point2I newExtent = getExtent();
  678. bool resizeX = false;
  679. bool resizeY = false;
  680. mResizeWindow = false;
  681. mRepositionWindow = false;
  682. if (mMouseMovingWin && parent)
  683. {
  684. if( parent != root )
  685. {
  686. newPosition.x = mOrigBounds.point.x + deltaMousePosition.x;
  687. newPosition.y = getMax(0, mOrigBounds.point.y + deltaMousePosition.y );
  688. mRepositionWindow = true;
  689. }
  690. else
  691. {
  692. newPosition.x = getMax(0, getMin(parent->getWidth() - getWidth(), mOrigBounds.point.x + deltaMousePosition.x));
  693. newPosition.y = getMax(0, getMin(parent->getHeight() - getHeight(), mOrigBounds.point.y + deltaMousePosition.y));
  694. }
  695. // Check snapping to other windows
  696. if( mEdgeSnap )
  697. {
  698. RectI bounds = getGlobalBounds();
  699. bounds.point = mOrigBounds.point + deltaMousePosition;
  700. EdgeRectI edges = EdgeRectI( bounds, mResizeMargin );
  701. // Create a global-space rectangle that covers the snapping
  702. // zone of this window. Double the space in which snapping occurs
  703. // for top and bottom.
  704. RectI snapZone = bounds;
  705. snapZone.point.x -= SnapDistance;
  706. snapZone.point.y -= SnapDistance;
  707. snapZone.extent.x += SnapDistance + SnapDistance;
  708. snapZone.extent.y += SnapDistance + SnapDistance;
  709. // Build valid snap and window vectors to compare against
  710. Vector< GuiWindowCtrl* > windowList;
  711. getSnappableWindows( windowList );
  712. for( S32 i =0; i < windowList.size(); i++ )
  713. {
  714. // Make sure the window is both horizontally and vertically
  715. // within the snap zone for this window.
  716. if( !snapZone.overlaps( windowList[i]->getGlobalBounds() ) )
  717. continue;
  718. // Build edges for snap detection
  719. EdgeRectI snapRect( windowList[i]->getGlobalBounds(), mResizeMargin );
  720. if( snapRect.right.position.x <= edges.left.position.x + SnapDistance &&
  721. snapRect.right.position.x >= edges.left.position.x - SnapDistance )
  722. {
  723. newPosition.x = snapRect.right.position.x;
  724. }
  725. else if( snapRect.left.position.x <= edges.right.position.x + SnapDistance &&
  726. snapRect.left.position.x >= edges.right.position.x - SnapDistance )
  727. {
  728. newPosition.x = snapRect.left.position.x - bounds.extent.x;
  729. }
  730. else if( snapRect.top.position.y <= edges.bottom.position.y + SnapDistance + SnapDistance &&
  731. snapRect.top.position.y >= edges.bottom.position.y - SnapDistance - SnapDistance )
  732. {
  733. // Ensure that we're not snapping to the middle of collapse groups
  734. if( (windowList[i]->mCanCollapse && windowList[i]->mCollapseGroup >= 0) ||
  735. (mCanCollapse && mCollapseGroup >= 0) )
  736. continue;
  737. newPosition.x = snapRect.left.position.x;
  738. newPosition.y = snapRect.top.position.y - bounds.extent.y;
  739. }
  740. else if( snapRect.bottom.position.y <= edges.top.position.y + SnapDistance + SnapDistance &&
  741. snapRect.bottom.position.y >= edges.top.position.y - SnapDistance - SnapDistance)
  742. {
  743. // Ensure that we're not snapping to the middle of collapse groups
  744. // We are not in a group, or we are not in the same group
  745. if( mCollapseGroup == -1 || ( mCollapseGroup >= 0 && mCollapseGroup != windowList[i]->mCollapseGroup ) )
  746. {
  747. // If the window checked is in a group, if its anything but the last, its n/a
  748. if( windowList[i]->mCollapseGroup >= 0 &&
  749. windowList[i] != parent->mCollapseGroupVec[windowList[i]->mCollapseGroup].last() )
  750. continue;
  751. }
  752. else // We are in the same group, we can't obviously be [0]
  753. {
  754. // If we are [-1/0] we have a serious problem. Also, we only allow connection to the window directly above us
  755. if( mCollapseGroupNum <= 0 ||
  756. windowList[i] != parent->mCollapseGroupVec[mCollapseGroup][mCollapseGroupNum-1] )
  757. continue;
  758. }
  759. newPosition.x = snapRect.left.position.x;
  760. newPosition.y = snapRect.bottom.position.y;
  761. }
  762. }
  763. }
  764. }
  765. else if(mCloseButtonPressed || mMaximizeButtonPressed || mMinimizeButtonPressed)
  766. {
  767. setUpdate();
  768. return;
  769. }
  770. else
  771. {
  772. if( ( !mMouseResizeHeight && !mMouseResizeWidth ) || !parent )
  773. return;
  774. mResizeWindow = true;
  775. if( mResizeEdge & edgeBottom )
  776. {
  777. newExtent.y = getMin(parent->getHeight(), mOrigBounds.extent.y + deltaMousePosition.y);
  778. resizeY = true;
  779. }
  780. else if ( mResizeEdge & edgeTop )
  781. {
  782. newExtent.y = getMin(parent->getHeight(), mOrigBounds.extent.y - deltaMousePosition.y);
  783. if ( newExtent.y >= mMinExtent.y )
  784. {
  785. // Standard reposition as we're not travelling into the min extent range
  786. newPosition.y = mOrigBounds.point.y + deltaMousePosition.y;
  787. }
  788. else
  789. {
  790. // We're into the min extent, so adjust the position up to the min extent
  791. // so the window doesn't appear to jump
  792. newPosition.y = mOrigBounds.point.y + (mOrigBounds.extent.y - mMinExtent.y);
  793. }
  794. resizeY = true;
  795. }
  796. if( mResizeEdge & edgeRight )
  797. {
  798. newExtent.x = getMin(parent->getWidth(), mOrigBounds.extent.x + deltaMousePosition.x);
  799. resizeX = true;
  800. }
  801. else if( mResizeEdge & edgeLeft )
  802. {
  803. newExtent.x = getMin(parent->getWidth(), mOrigBounds.extent.x - deltaMousePosition.x);
  804. if ( newExtent.x >= mMinExtent.x )
  805. {
  806. // Standard reposition as we're not travelling into the min extent range
  807. newPosition.x = mOrigBounds.point.x + deltaMousePosition.x;
  808. }
  809. else
  810. {
  811. // We're into the min extent, so adjust the position up to the min extent
  812. // so the window doesn't appear to jump
  813. newPosition.x = mOrigBounds.point.x + (mOrigBounds.extent.x - mMinExtent.x);
  814. }
  815. resizeX = true;
  816. }
  817. }
  818. // Resize this
  819. Point2I pos = parent->localToGlobalCoord(getPosition());
  820. root->addUpdateRegion(pos, getExtent());
  821. if(mCanCollapse && mCollapseGroup >= 0 && mRepositionWindow == true)
  822. moveWithCollapseGroup(newPosition);
  823. if(mCanCollapse && mCollapseGroup >= 0 && mResizeWindow == true )
  824. {
  825. // Resize the window if allowed
  826. if( newExtent.y >= getMinExtent().y && newExtent.x >= getMinExtent().x)
  827. {
  828. mIsMouseResizing = true;
  829. if( resizeCollapseGroup( resizeX, resizeY, (getPosition() - newPosition), (getExtent() - newExtent) ) )
  830. resize(newPosition, newExtent);
  831. }
  832. }
  833. else // Normal window sizing functionality
  834. resize(newPosition, newExtent);
  835. }
  836. //-----------------------------------------------------------------------------
  837. void GuiWindowCtrl::onMouseUp(const GuiEvent &event)
  838. {
  839. bool closing = mCloseButtonPressed;
  840. bool maximizing = mMaximizeButtonPressed;
  841. bool minimizing = mMinimizeButtonPressed;
  842. mCloseButtonPressed = false;
  843. mMaximizeButtonPressed = false;
  844. mMinimizeButtonPressed = false;
  845. TORQUE_UNUSED(event);
  846. mouseUnlock();
  847. mMouseMovingWin = false;
  848. mMouseResizeWidth = false;
  849. mMouseResizeHeight = false;
  850. GuiControl *parent = getParent();
  851. if (! parent)
  852. return;
  853. if( mIsMouseResizing )
  854. {
  855. mIsMouseResizing = false;
  856. return;
  857. }
  858. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  859. if (closing && mCloseButton.pointInRect(localPoint))
  860. {
  861. // Here is where were going to put our other if statement
  862. // if the window closes, and there were only 2 windows in the array, then just delete the array and default there params
  863. // if not, delete the window from the array, default its params, and renumber the windows
  864. if( engineAPI::gUseConsoleInterop )
  865. evaluate( mCloseCommand );
  866. onClose_callback();
  867. }
  868. else if (maximizing && mMaximizeButton.pointInRect(localPoint))
  869. {
  870. if (mMaximized)
  871. {
  872. // Resize to the previous position and extent, bounded by the parent
  873. resize(Point2I(getMax(0, getMin(parent->getWidth() - mStandardBounds.extent.x, mStandardBounds.point.x)),
  874. getMax(0, getMin(parent->getHeight() - mStandardBounds.extent.y, mStandardBounds.point.y))),
  875. mStandardBounds.extent);
  876. // Set the flag
  877. mMaximized = false;
  878. onRestore_callback();
  879. }
  880. else
  881. {
  882. // Only save the position if we're not minimized
  883. if (! mMinimized)
  884. mStandardBounds = getBounds();
  885. else
  886. mMinimized = false;
  887. // Resize to fit the parent
  888. resize(Point2I(0, 0), parent->getExtent());
  889. // Set the flag
  890. mMaximized = true;
  891. onMaximize_callback();
  892. }
  893. }
  894. else if (minimizing && mMinimizeButton.pointInRect(localPoint))
  895. {
  896. if (mMinimized)
  897. {
  898. // Resize to the previous position and extent, bounded by the parent
  899. resize(Point2I(getMax(0, getMin(parent->getWidth() - mStandardBounds.extent.x, mStandardBounds.point.x)),
  900. getMax(0, getMin(parent->getHeight() - mStandardBounds.extent.y, mStandardBounds.point.y))),
  901. mStandardBounds.extent);
  902. // Set the flag
  903. mMinimized = false;
  904. onRestore_callback();
  905. }
  906. else
  907. {
  908. if (parent->getWidth() < 100 || parent->getHeight() < mTitleHeight + 3)
  909. return;
  910. // Only save the position if we're not maximized
  911. if (! mMaximized)
  912. {
  913. mStandardBounds = getBounds();
  914. }
  915. else
  916. {
  917. mMaximized = false;
  918. }
  919. // First find the lowest unused minimized index up to 32 minimized windows
  920. U32 indexMask = 0;
  921. iterator i;
  922. S32 count = 0;
  923. for (i = parent->begin(); i != parent->end() && count < 32; i++)
  924. {
  925. count++;
  926. S32 index;
  927. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  928. if (ctrl && ctrl->isMinimized(index))
  929. {
  930. indexMask |= (1 << index);
  931. }
  932. }
  933. // Now find the first unused bit
  934. for (count = 0; count < 32; count++)
  935. {
  936. if (! (indexMask & (1 << count))) break;
  937. }
  938. // If we have more than 32 minimized windows, use the first position
  939. count = getMax(0, count);
  940. // This algorithm assumes all window have the same title height, and will minimize to 98 pix
  941. Point2I newExtent(98, mTitleHeight);
  942. // First, how many can fit across
  943. S32 numAcross = getMax(1, (parent->getWidth() / newExtent.x + 2));
  944. // Find the new "mini position"
  945. Point2I newPosition;
  946. newPosition.x = (count % numAcross) * (newExtent.x + 2) + 2;
  947. newPosition.y = parent->getHeight() - (((count / numAcross) + 1) * (newExtent.y + 2)) - 2;
  948. // Find the minimized position and extent
  949. resize(newPosition, newExtent);
  950. // Set the index so other windows will not try to minimize to the same location
  951. mMinimizeIndex = count;
  952. // Set the flag
  953. mMinimized = true;
  954. onMinimize_callback();
  955. }
  956. }
  957. else if ( !(mResizeEdge & edgeTop) && localPoint.y < mTitleHeight && event.mousePoint == mMouseDownPosition)
  958. {
  959. if (mCanClose && mCloseButton.pointInRect(localPoint))
  960. return;
  961. else if (mCanMaximize && mMaximizeButton.pointInRect(localPoint))
  962. return;
  963. else if (mCanMinimize && mMinimizeButton.pointInRect(localPoint))
  964. return;
  965. else if( mCanCollapse ) // If we clicked anywhere else on the title bar
  966. toggleCollapseGroup();
  967. }
  968. else if( mEdgeSnap && mCanCollapse )
  969. {
  970. // Create storage pointer
  971. GuiWindowCtrl* hitWindow = NULL;
  972. S32 snapType = -1;
  973. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  974. Point2I newExtent = getExtent();
  975. RectI bounds = getGlobalBounds();
  976. bounds.point = mOrigBounds.point + deltaMousePosition;
  977. EdgeRectI edges = EdgeRectI( bounds, mResizeMargin );
  978. RectI snapZone = bounds;
  979. snapZone.point.x -= SnapDistance;
  980. snapZone.point.y -= SnapDistance;
  981. snapZone.extent.x += SnapDistance + SnapDistance;
  982. snapZone.extent.y += SnapDistance + SnapDistance;
  983. Vector<EdgeRectI> snapList;
  984. Vector<GuiWindowCtrl*> windowList;
  985. getSnappableWindows( windowList, true );
  986. for( S32 i =0; i < windowList.size(); i++ )
  987. {
  988. if( !snapZone.overlaps( windowList[i]->getGlobalBounds() ) )
  989. continue;
  990. // Build edges for snap detection
  991. EdgeRectI snapRect( windowList[i]->getGlobalBounds(), mResizeMargin );
  992. if( windowList[i]->mCollapseGroupNum == -1 )
  993. {
  994. // BECOMES "PARENT"
  995. if( snapRect.top.position.y <= edges.bottom.position.y + SnapDistance + SnapDistance &&
  996. snapRect.top.position.y >= edges.bottom.position.y - SnapDistance - SnapDistance )
  997. {
  998. hitWindow = windowList[i];
  999. snapType = 0;
  1000. newExtent.x = snapRect.right.position.x - snapRect.left.position.x;
  1001. break;
  1002. }
  1003. }
  1004. if( (windowList[i]->mCollapseGroupNum == -1) || (windowList[i]->mCollapseGroupNum == mCollapseGroupNum - 1) ||
  1005. (!parent->mCollapseGroupVec.empty() && parent->mCollapseGroupVec[windowList[i]->mCollapseGroup].last() == windowList[i]) )
  1006. {
  1007. // BECOMES "CHILD"
  1008. if( snapRect.bottom.position.y <= edges.top.position.y + SnapDistance + SnapDistance &&
  1009. snapRect.bottom.position.y >= edges.top.position.y - SnapDistance - SnapDistance)
  1010. {
  1011. hitWindow = windowList[i];
  1012. snapType = 1;
  1013. newExtent.x = snapRect.right.position.x - snapRect.left.position.x;
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. // We're either moving out of a collapse group or moving to another one
  1019. // Not valid for windows not previously in a group
  1020. if( mCollapseGroup >= 0 &&
  1021. (snapType == -1 || (hitWindow && snapType >= 0 && mCollapseGroup != hitWindow->mCollapseGroup)))
  1022. moveFromCollapseGroup();
  1023. // No window to connect to
  1024. if( !hitWindow )
  1025. return;
  1026. moveToCollapseGroup( hitWindow, snapType );
  1027. resize( getPosition(), newExtent );
  1028. }
  1029. }
  1030. //-----------------------------------------------------------------------------
  1031. void GuiWindowCtrl::onMouseMove(const GuiEvent &event)
  1032. {
  1033. mMousePosition = globalToLocalCoord(event.mousePoint);
  1034. }
  1035. bool GuiWindowCtrl::onKeyDown(const GuiEvent &event)
  1036. {
  1037. // If this control is a dead end, kill the event
  1038. if ((! mVisible) || (! mActive) || (! mAwake)) return true;
  1039. if ((event.keyCode == KEY_TAB) && (event.modifier & SI_PRIMARY_CTRL))
  1040. {
  1041. // Find the next sibling window, and select it
  1042. GuiControl *parent = getParent();
  1043. if (parent)
  1044. {
  1045. GuiWindowCtrl *firstWindow = NULL;
  1046. iterator i;
  1047. for (i = parent->begin(); i != parent->end(); i++)
  1048. {
  1049. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  1050. if (ctrl && ctrl->getTabIndex() == mTabIndex + 1)
  1051. {
  1052. ctrl->selectWindow();
  1053. return true;
  1054. }
  1055. else if (ctrl && ctrl->getTabIndex() == 0)
  1056. {
  1057. firstWindow = ctrl;
  1058. }
  1059. }
  1060. // Recycle from the beginning
  1061. if (firstWindow != this)
  1062. {
  1063. firstWindow->selectWindow();
  1064. return true;
  1065. }
  1066. }
  1067. }
  1068. return Parent::onKeyDown(event);
  1069. }
  1070. //-----------------------------------------------------------------------------
  1071. void GuiWindowCtrl::onRender(Point2I offset, const RectI &updateRect)
  1072. {
  1073. if( !mProfile || mProfile->mFont == NULL || mProfile->mBitmapArrayRects.size() < NumBitmaps )
  1074. return Parent::onRender( offset, updateRect );
  1075. // Draw the outline
  1076. RectI winRect;
  1077. winRect.point = offset;
  1078. winRect.extent = getExtent();
  1079. GuiCanvas *root = getRoot();
  1080. GuiControl *firstResponder = root ? root->getFirstResponder() : NULL;
  1081. bool isKey = (!firstResponder || controlIsChild(firstResponder));
  1082. U32 topBase = isKey ? BorderTopLeftKey : BorderTopLeftNoKey;
  1083. winRect.point.x += mBitmapBounds[BorderLeft].extent.x;
  1084. winRect.point.y += mBitmapBounds[topBase + 2].extent.y;
  1085. winRect.extent.x -= mBitmapBounds[BorderLeft].extent.x + mBitmapBounds[BorderRight].extent.x;
  1086. winRect.extent.y -= mBitmapBounds[topBase + 2].extent.y + mBitmapBounds[BorderBottom].extent.y;
  1087. winRect.extent.x += 1;
  1088. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  1089. drawUtil->drawRectFill(winRect, mProfile->mFillColor);
  1090. drawUtil->clearBitmapModulation();
  1091. drawUtil->drawBitmapSR(mTextureObject, offset, mBitmapBounds[topBase]);
  1092. drawUtil->drawBitmapSR(mTextureObject, Point2I(offset.x + getWidth() - mBitmapBounds[topBase+1].extent.x, offset.y),
  1093. mBitmapBounds[topBase + 1]);
  1094. RectI destRect;
  1095. destRect.point.x = offset.x + mBitmapBounds[topBase].extent.x;
  1096. destRect.point.y = offset.y;
  1097. destRect.extent.x = getWidth() - mBitmapBounds[topBase].extent.x - mBitmapBounds[topBase + 1].extent.x;
  1098. destRect.extent.y = mBitmapBounds[topBase + 2].extent.y;
  1099. RectI stretchRect = mBitmapBounds[topBase + 2];
  1100. stretchRect.inset(1,0);
  1101. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  1102. destRect.point.x = offset.x;
  1103. destRect.point.y = offset.y + mBitmapBounds[topBase].extent.y;
  1104. destRect.extent.x = mBitmapBounds[BorderLeft].extent.x;
  1105. destRect.extent.y = getHeight() - mBitmapBounds[topBase].extent.y - mBitmapBounds[BorderBottomLeft].extent.y;
  1106. stretchRect = mBitmapBounds[BorderLeft];
  1107. stretchRect.inset(0,1);
  1108. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  1109. destRect.point.x = offset.x + getWidth() - mBitmapBounds[BorderRight].extent.x;
  1110. destRect.extent.x = mBitmapBounds[BorderRight].extent.x;
  1111. destRect.point.y = offset.y + mBitmapBounds[topBase + 1].extent.y;
  1112. destRect.extent.y = getHeight() - mBitmapBounds[topBase + 1].extent.y - mBitmapBounds[BorderBottomRight].extent.y;
  1113. stretchRect = mBitmapBounds[BorderRight];
  1114. stretchRect.inset(0,1);
  1115. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  1116. drawUtil->drawBitmapSR(mTextureObject, offset + Point2I(0, getHeight() - mBitmapBounds[BorderBottomLeft].extent.y), mBitmapBounds[BorderBottomLeft]);
  1117. drawUtil->drawBitmapSR(mTextureObject, offset + getExtent() - mBitmapBounds[BorderBottomRight].extent, mBitmapBounds[BorderBottomRight]);
  1118. destRect.point.x = offset.x + mBitmapBounds[BorderBottomLeft].extent.x;
  1119. destRect.extent.x = getWidth() - mBitmapBounds[BorderBottomLeft].extent.x - mBitmapBounds[BorderBottomRight].extent.x;
  1120. destRect.point.y = offset.y + getHeight() - mBitmapBounds[BorderBottom].extent.y;
  1121. destRect.extent.y = mBitmapBounds[BorderBottom].extent.y;
  1122. stretchRect = mBitmapBounds[BorderBottom];
  1123. stretchRect.inset(1,0);
  1124. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  1125. // Draw the title
  1126. // dhc addition: copied/modded from renderJustifiedText, since we enforce a
  1127. // different color usage here. NOTE: it currently CAN overdraw the controls
  1128. // if mis-positioned or 'scrunched' in a small width.
  1129. drawUtil->setBitmapModulation(mProfile->mFontColor);
  1130. S32 textWidth = mProfile->mFont->getStrWidth((const UTF8 *)mText);
  1131. Point2I start(0,0);
  1132. // Align the horizontal
  1133. if ( mProfile->mAlignment == GuiControlProfile::RightJustify )
  1134. start.set( winRect.extent.x - textWidth, 0 );
  1135. else if ( mProfile->mAlignment == GuiControlProfile::CenterJustify )
  1136. start.set( ( winRect.extent.x - textWidth) / 2, 0 );
  1137. else // GuiControlProfile::LeftJustify or garbage... ;)
  1138. start.set( 0, 0 );
  1139. // If the text is longer then the box size, (it'll get clipped) so force Left Justify
  1140. if( textWidth > winRect.extent.x ) start.set( 0, 0 );
  1141. // center the vertical
  1142. // start.y = ( winRect.extent.y - ( font->getHeight() - 2 ) ) / 2;
  1143. drawUtil->drawText( mProfile->mFont, start + offset + mProfile->mTextOffset, mText );
  1144. // Deal with rendering the titlebar controls
  1145. AssertFatal(root, "Unable to get the root GuiCanvas.");
  1146. // Draw the close button
  1147. Point2I tempUL;
  1148. Point2I tempLR;
  1149. S32 bmp = BmpStates * BmpClose;
  1150. if( mCanClose ) {
  1151. if( mCloseButton.pointInRect( mMousePosition ) )
  1152. {
  1153. if( mCloseButtonPressed )
  1154. bmp += BmpDown;
  1155. else
  1156. bmp += BmpHilite;
  1157. }
  1158. drawUtil->clearBitmapModulation();
  1159. drawUtil->drawBitmapSR(mTextureObject, offset + mCloseButton.point, mBitmapBounds[bmp]);
  1160. }
  1161. // Draw the maximize button
  1162. if( mMaximized )
  1163. bmp = BmpStates * BmpNormal;
  1164. else
  1165. bmp = BmpStates * BmpMaximize;
  1166. if( mCanMaximize ) {
  1167. if( mMaximizeButton.pointInRect( mMousePosition ) )
  1168. {
  1169. if( mMaximizeButtonPressed )
  1170. bmp += BmpDown;
  1171. else
  1172. bmp += BmpHilite;
  1173. }
  1174. drawUtil->clearBitmapModulation();
  1175. drawUtil->drawBitmapSR( mTextureObject, offset + mMaximizeButton.point, mBitmapBounds[bmp] );
  1176. }
  1177. // Draw the minimize button
  1178. if( mMinimized )
  1179. bmp = BmpStates * BmpNormal;
  1180. else
  1181. bmp = BmpStates * BmpMinimize;
  1182. if( mCanMinimize ) {
  1183. if( mMinimizeButton.pointInRect( mMousePosition ) )
  1184. {
  1185. if( mMinimizeButtonPressed )
  1186. bmp += BmpDown;
  1187. else
  1188. bmp += BmpHilite;
  1189. }
  1190. drawUtil->clearBitmapModulation();
  1191. drawUtil->drawBitmapSR( mTextureObject, offset + mMinimizeButton.point, mBitmapBounds[bmp] );
  1192. }
  1193. if( !mMinimized )
  1194. {
  1195. // Render the children
  1196. renderChildControls( offset, updateRect );
  1197. }
  1198. }
  1199. //=============================================================================
  1200. // Misc.
  1201. //=============================================================================
  1202. // MARK: ---- Misc ----
  1203. //-----------------------------------------------------------------------------
  1204. const RectI GuiWindowCtrl::getClientRect()
  1205. {
  1206. if( !mProfile || mProfile->mBitmapArrayRects.size() < NumBitmaps )
  1207. return Parent::getClientRect();
  1208. if( !mBitmapBounds )
  1209. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  1210. RectI winRect;
  1211. winRect.point.x = mBitmapBounds[BorderLeft].extent.x;
  1212. winRect.point.y = mBitmapBounds[BorderTopKey].extent.y;
  1213. winRect.extent.y = getHeight() - ( winRect.point.y + mBitmapBounds[BorderBottom].extent.y );
  1214. winRect.extent.x = getWidth() - ( winRect.point.x + mBitmapBounds[BorderRight].extent.x );
  1215. // Finally, inset it by padding
  1216. // Inset by padding. margin is specified for all t/b/l/r but
  1217. // uses only pointx pointy uniformly on both ends. This should be fixed. - JDD
  1218. // winRect.inset( mSizingOptions.mPadding.point.x, mSizingOptions.mPadding.point.y );
  1219. return winRect;
  1220. }
  1221. //-----------------------------------------------------------------------------
  1222. bool GuiWindowCtrl::isMinimized(S32 &index)
  1223. {
  1224. index = mMinimizeIndex;
  1225. return mMinimized && mVisible;
  1226. }
  1227. //-----------------------------------------------------------------------------
  1228. void GuiWindowCtrl::positionButtons(void)
  1229. {
  1230. if( !mBitmapBounds || !mAwake )
  1231. return;
  1232. S32 buttonWidth = mBitmapBounds[BmpStates * BmpClose].extent.x;
  1233. S32 buttonHeight = mBitmapBounds[BmpStates * BmpClose].extent.y;
  1234. Point2I mainOff = mProfile->mTextOffset;
  1235. // Until a pref, if alignment is LEFT, put buttons RIGHT justified.
  1236. // ELSE, put buttons LEFT justified.
  1237. S32 closeLeft = mainOff.x, closeTop = mainOff.y, closeOff = buttonWidth + 2;
  1238. if ( mProfile->mAlignment == GuiControlProfile::LeftJustify )
  1239. {
  1240. closeOff = -closeOff;
  1241. closeLeft = getWidth() - buttonWidth - mainOff.x;
  1242. }
  1243. RectI closeRect(closeLeft, closeTop, buttonHeight, buttonWidth);
  1244. mCloseButton = closeRect;
  1245. // Always put Minimize on left side of Maximize.
  1246. closeRect.point.x += closeOff;
  1247. if (closeOff>0)
  1248. {
  1249. mMinimizeButton = closeRect;
  1250. closeRect.point.x += closeOff;
  1251. mMaximizeButton = closeRect;
  1252. }
  1253. else
  1254. {
  1255. mMaximizeButton = closeRect;
  1256. closeRect.point.x += closeOff;
  1257. mMinimizeButton = closeRect;
  1258. }
  1259. }
  1260. //-----------------------------------------------------------------------------
  1261. void GuiWindowCtrl::setCloseCommand(const char *newCmd)
  1262. {
  1263. if (newCmd)
  1264. mCloseCommand = StringTable->insert(newCmd);
  1265. else
  1266. mCloseCommand = StringTable->insert("");
  1267. }
  1268. //-----------------------------------------------------------------------------
  1269. GuiControl* GuiWindowCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  1270. {
  1271. if (! mMinimized)
  1272. return Parent::findHitControl(pt, initialLayer);
  1273. else
  1274. return this;
  1275. }
  1276. //-----------------------------------------------------------------------------
  1277. bool GuiWindowCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  1278. {
  1279. if( !Parent::resize(newPosition, newExtent) )
  1280. return false;
  1281. // Set the button coords
  1282. positionButtons();
  1283. return true;
  1284. }
  1285. //-----------------------------------------------------------------------------
  1286. GuiControl *GuiWindowCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  1287. {
  1288. // Set the global if this is the first call (directly from the canvas)
  1289. if (firstCall)
  1290. {
  1291. GuiControl::smCurResponder = NULL;
  1292. }
  1293. // If the window does not already contain the first responder, return false
  1294. // ie. Can't tab into or out of a window
  1295. if (! controlIsChild(curResponder))
  1296. {
  1297. return NULL;
  1298. }
  1299. // Loop through, checking each child to see if it is the one that follows the firstResponder
  1300. GuiControl *tabCtrl = NULL;
  1301. iterator i;
  1302. for (i = begin(); i != end(); i++)
  1303. {
  1304. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1305. tabCtrl = ctrl->findNextTabable(curResponder, false);
  1306. if (tabCtrl) break;
  1307. }
  1308. // To ensure the tab cycles within the current window...
  1309. if (! tabCtrl)
  1310. {
  1311. tabCtrl = findFirstTabable();
  1312. }
  1313. mFirstResponder = tabCtrl;
  1314. return tabCtrl;
  1315. }
  1316. //-----------------------------------------------------------------------------
  1317. GuiControl *GuiWindowCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  1318. {
  1319. if (firstCall)
  1320. {
  1321. GuiControl::smPrevResponder = NULL;
  1322. }
  1323. // If the window does not already contain the first responder, return false
  1324. // ie. Can't tab into or out of a window
  1325. if (! controlIsChild(curResponder))
  1326. {
  1327. return NULL;
  1328. }
  1329. // Loop through, checking each child to see if it is the one that follows the firstResponder
  1330. GuiControl *tabCtrl = NULL;
  1331. iterator i;
  1332. for (i = begin(); i != end(); i++)
  1333. {
  1334. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  1335. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  1336. if (tabCtrl) break;
  1337. }
  1338. // To ensure the tab cycles within the current window...
  1339. if (! tabCtrl)
  1340. {
  1341. tabCtrl = findLastTabable();
  1342. }
  1343. mFirstResponder = tabCtrl;
  1344. return tabCtrl;
  1345. }
  1346. //-----------------------------------------------------------------------------
  1347. void GuiWindowCtrl::selectWindow(void)
  1348. {
  1349. // First make sure this window is the front most of its siblings
  1350. GuiControl *parent = getParent();
  1351. if (parent && *parent->end() != this )
  1352. {
  1353. // Valid collapse groups have to be selected together
  1354. if( mCanCollapse && mCollapseGroup >= 0 )
  1355. {
  1356. CollapseGroupNumVec::iterator iter = parent->mCollapseGroupVec[mCollapseGroup].begin();
  1357. for(; iter != parent->mCollapseGroupVec[mCollapseGroup].end(); iter++ )
  1358. {
  1359. parent->pushObjectToBack( (*iter) );
  1360. }
  1361. }
  1362. else
  1363. {
  1364. parent->pushObjectToBack( this );
  1365. }
  1366. }
  1367. // Also set the first responder to be the one within this window
  1368. setFirstResponder(mFirstResponder);
  1369. }
  1370. //-----------------------------------------------------------------------------
  1371. void GuiWindowCtrl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  1372. {
  1373. GuiCanvas *pRoot = getRoot();
  1374. if( !pRoot )
  1375. return;
  1376. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  1377. AssertFatal(pWindow != NULL,"GuiControl without owning platform window! This should not be possible.");
  1378. PlatformCursorController *pController = pWindow->getCursorController();
  1379. AssertFatal(pController != NULL,"PlatformWindow without an owned CursorController!");
  1380. S32 desiredCursor = PlatformCursorController::curArrow;
  1381. S32 hitEdges = findHitEdges( lastGuiEvent.mousePoint );
  1382. if( hitEdges & edgeBottom && hitEdges & edgeLeft && mResizeHeight )
  1383. desiredCursor = PlatformCursorController::curResizeNESW;
  1384. else if( hitEdges & edgeBottom && hitEdges & edgeRight && mResizeHeight )
  1385. desiredCursor = PlatformCursorController::curResizeNWSE;
  1386. else if( hitEdges & edgeBottom && mResizeHeight )
  1387. desiredCursor = PlatformCursorController::curResizeHorz;
  1388. else if( hitEdges & edgeTop && hitEdges & edgeLeft && mResizeHeight )
  1389. desiredCursor = PlatformCursorController::curResizeNWSE;
  1390. else if( hitEdges & edgeTop && hitEdges & edgeRight && mResizeHeight )
  1391. desiredCursor = PlatformCursorController::curResizeNESW;
  1392. else if( hitEdges & edgeTop && mResizeHeight )
  1393. desiredCursor = PlatformCursorController::curResizeHorz;
  1394. else if ( hitEdges & edgeLeft && mResizeWidth )
  1395. desiredCursor = PlatformCursorController::curResizeVert;
  1396. else if( hitEdges & edgeRight && mResizeWidth )
  1397. desiredCursor = PlatformCursorController::curResizeVert;
  1398. else
  1399. desiredCursor = PlatformCursorController::curArrow;
  1400. // Bail if we're already at the desired cursor
  1401. if(pRoot->mCursorChanged == desiredCursor )
  1402. return;
  1403. // Now change the cursor shape
  1404. pController->popCursor();
  1405. pController->pushCursor(desiredCursor);
  1406. pRoot->mCursorChanged = desiredCursor;
  1407. }
  1408. //-----------------------------------------------------------------------------
  1409. void GuiWindowCtrl::parentResized(const RectI &oldParentRect, const RectI &newParentRect)
  1410. {
  1411. if(!mCanResize)
  1412. return;
  1413. GuiControl *parent = getParent();
  1414. if( !parent )
  1415. return;
  1416. // Bail if were not sized both by windowrelative
  1417. if( mHorizSizing != horizResizeWindowRelative || mHorizSizing != vertResizeWindowRelative )
  1418. return Parent::parentResized( oldParentRect, newParentRect );
  1419. Point2I newPosition = getPosition();
  1420. Point2I newExtent = getExtent();
  1421. bool doCollapse = false;
  1422. S32 deltaX = newParentRect.extent.x - oldParentRect.extent.x;
  1423. S32 deltaY = newParentRect.extent.y - oldParentRect.extent.y;// + mProfile->mYPositionOffset;
  1424. if( newPosition.x > ( oldParentRect.extent.x / 2 ) )
  1425. newPosition.x = newPosition.x + deltaX;
  1426. if (oldParentRect.extent.y != 0)
  1427. {
  1428. // Only if were apart of a group
  1429. if ( mCanCollapse && mCollapseGroup >= 0 )
  1430. {
  1431. // Setup parsing mechanisms
  1432. CollapseGroupNumVec collapseGroupNumVec;
  1433. // Lets grab the information we need (this should probably be already stored on each individual window object)
  1434. S32 groupNum = mCollapseGroup;
  1435. S32 groupMax = parent->mCollapseGroupVec[ groupNum ].size() - 1;
  1436. // Set up vars that we're going to be using
  1437. S32 groupPos = 0;
  1438. S32 groupExtent = 0;
  1439. S32 tempGroupExtent = 0;
  1440. // Set up the vector/iterator combo we need
  1441. collapseGroupNumVec = parent->mCollapseGroupVec[ groupNum ];
  1442. CollapseGroupNumVec::iterator iter = collapseGroupNumVec.begin();
  1443. // Grab some more information we need later ( this info should also be located on each ind. window object )
  1444. for( ; iter != collapseGroupNumVec.end(); iter++ )
  1445. {
  1446. if((*iter)->getCollapseGroupNum() == 0)
  1447. {
  1448. groupPos = (*iter)->getPosition().y;
  1449. }
  1450. groupExtent += (*iter)->getExtent().y;
  1451. }
  1452. // Use the information we just gatherered; only enter this block if we need to
  1453. tempGroupExtent = groupPos + groupExtent;
  1454. if( tempGroupExtent > ( newParentRect.extent.y / 2 ) )
  1455. {
  1456. // Lets size the collapse group
  1457. S32 windowParser = groupMax;
  1458. bool secondLoop = false;
  1459. while( tempGroupExtent >= newParentRect.extent.y )
  1460. {
  1461. if( windowParser == -1)
  1462. {
  1463. if( !secondLoop )
  1464. {
  1465. secondLoop = true;
  1466. windowParser = groupMax;
  1467. }
  1468. else
  1469. break;
  1470. }
  1471. GuiWindowCtrl *tempWindow = collapseGroupNumVec[windowParser];
  1472. if(tempWindow->mIsCollapsed)
  1473. {
  1474. windowParser--;
  1475. continue;
  1476. }
  1477. // Resizing block for the loop... if we can get away with just resizing the bottom window do it before
  1478. // resizing the whole block. else, go through the group and start setting min extents. if that doesnt work
  1479. // on the second go around, start collpsing the windows.
  1480. if( tempGroupExtent - ( tempWindow->getExtent().y - tempWindow->getMinExtent().y ) <= newParentRect.extent.y )
  1481. {
  1482. if( this == tempWindow )
  1483. newExtent.y = newExtent.y - ( tempGroupExtent - newParentRect.extent.y );
  1484. tempGroupExtent = tempGroupExtent - newParentRect.extent.y;
  1485. }
  1486. else
  1487. {
  1488. if( secondLoop )
  1489. {
  1490. tempGroupExtent = tempGroupExtent - (tempWindow->getExtent().y + 32);
  1491. if( this == tempWindow )
  1492. doCollapse = true;
  1493. }
  1494. else
  1495. {
  1496. tempGroupExtent = tempGroupExtent - (tempWindow->getExtent().y - tempWindow->getMinExtent().y);
  1497. if( this == tempWindow )
  1498. newExtent.y = tempWindow->getMinExtent().y;
  1499. }
  1500. }
  1501. windowParser--;
  1502. }
  1503. }
  1504. }
  1505. else if( newPosition.y > ( oldParentRect.extent.y / 2 ) )
  1506. {
  1507. newPosition.y = newPosition.y + deltaY - 1;
  1508. }
  1509. }
  1510. if( newExtent.x >= getMinExtent().x && newExtent.y >= getMinExtent().y )
  1511. {
  1512. // If we are already outside the reach of the main window, lets not place ourselves
  1513. // further out; but if were trying to improve visibility, go for it
  1514. // note: small tolerance (4) added to keep guis that are very slightly outside
  1515. // the main window (like all of the editor windows!) from appearing to 'undock'
  1516. // when the resolution is changed.
  1517. if( (newPosition.x + newExtent.x) > newParentRect.extent.x + 4 )
  1518. {
  1519. if( (newPosition.x + newExtent.x) > (getPosition().x + getExtent().x) )
  1520. {
  1521. newPosition.x = getPosition().x;
  1522. newExtent.x = getExtent().x;
  1523. }
  1524. }
  1525. if( (newPosition.y + newExtent.y) > newParentRect.extent.y + 4)
  1526. {
  1527. if( (newPosition.y + newExtent.y) > (getPosition().y + getExtent().y) )
  1528. {
  1529. newPosition.y = getPosition().y;
  1530. newExtent.y = getExtent().y;
  1531. }
  1532. }
  1533. // Only for collpasing groups, if were not, then do it like normal windows
  1534. if( mCanCollapse && mCollapseGroup >= 0 )
  1535. {
  1536. bool resizeMe = false;
  1537. // Only the group window should control positioning
  1538. if( mCollapseGroupNum == 0 )
  1539. {
  1540. resizeMe = resizeCollapseGroup( true, true, (getPosition() - newPosition), (getExtent() - newExtent) );
  1541. if(resizeMe == true)
  1542. resize(newPosition, newExtent);
  1543. }
  1544. else if( getExtent() != newExtent)
  1545. {
  1546. resizeMe = resizeCollapseGroup( false, true, (getPosition() - newPosition), (getExtent() - newExtent) );
  1547. if(resizeMe == true)
  1548. resize(getPosition(), newExtent);
  1549. }
  1550. }
  1551. else
  1552. {
  1553. resize(newPosition, newExtent);
  1554. }
  1555. }
  1556. if( mCanCollapse && !mIsCollapsed && doCollapse )
  1557. toggleCollapseGroup();
  1558. // If docking is invalid on this control, then bail out here
  1559. if( getDocking() & Docking::dockInvalid || getDocking() & Docking::dockNone)
  1560. return;
  1561. // Update Self
  1562. RectI oldThisRect = getBounds();
  1563. anchorControl( this, Point2I( deltaX, deltaY ) );
  1564. RectI newThisRect = getBounds();
  1565. // Update Deltas to pass on to children
  1566. deltaX = newThisRect.extent.x - oldThisRect.extent.x;
  1567. deltaY = newThisRect.extent.y - oldThisRect.extent.y;
  1568. // Iterate over all children and update their anchors
  1569. iterator nI = begin();
  1570. for( ; nI != end(); nI++ )
  1571. {
  1572. // Sanity
  1573. GuiControl *control = dynamic_cast<GuiControl*>( (*nI) );
  1574. if( control )
  1575. control->parentResized( oldThisRect, newThisRect );
  1576. }
  1577. }
  1578. //=============================================================================
  1579. // Console Methods.
  1580. //=============================================================================
  1581. // MARK: ---- Console Methods ----
  1582. //-----------------------------------------------------------------------------
  1583. DefineEngineMethod( GuiWindowCtrl, selectWindow, void, (),,
  1584. "Bring the window to the front." )
  1585. {
  1586. object->selectWindow();
  1587. }
  1588. //-----------------------------------------------------------------------------
  1589. DefineEngineMethod( GuiWindowCtrl, setCollapseGroup, void, ( bool state ),,
  1590. "Set the window's collapsing state." )
  1591. {
  1592. object->setCollapseGroup(state);
  1593. }
  1594. //-----------------------------------------------------------------------------
  1595. DefineEngineMethod( GuiWindowCtrl, toggleCollapseGroup, void, (),,
  1596. "Toggle the window collapsing." )
  1597. {
  1598. object->toggleCollapseGroup();
  1599. }
  1600. //-----------------------------------------------------------------------------
  1601. DefineEngineMethod( GuiWindowCtrl, attachTo, void, ( GuiWindowCtrl* window ),,
  1602. "" )
  1603. {
  1604. object->moveToCollapseGroup( window, 1 );
  1605. }
  1606. //-----------------------------------------------------------------------------
  1607. DefineEngineStaticMethod( GuiWindowCtrl, attach, void, ( GuiWindowCtrl* bottomWindow, GuiWindowCtrl* topWindow ),,
  1608. "Attach @a bottomWindow to @topWindow so that @a bottomWindow moves along with @a topWindow when it is dragged.\n\n"
  1609. "@param bottomWindow \n"
  1610. "@param topWindow " )
  1611. {
  1612. if(bottomWindow == NULL || topWindow == NULL)
  1613. {
  1614. Con::warnf("Warning: AttachWindows - could not find windows");
  1615. return;
  1616. }
  1617. bottomWindow->moveToCollapseGroup( topWindow, 1 );
  1618. }