guiWindowCtrl.cpp 66 KB

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