guiWindowCtrl.cpp 66 KB

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