guiWindowCtrl.cpp 66 KB

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