guiWindowCtrl.cpp 65 KB

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