guiWindowCtrl.cpp 66 KB

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