guiWindowCtrl.cpp 67 KB

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