guiWindowCtrl.cpp 66 KB

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