guiWindowCtrl.cpp 67 KB

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