guiWindowCtrl.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/containers/guiWindowCtrl.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "guiWindowCtrl_ScriptBinding.h"
  29. IMPLEMENT_CONOBJECT(GuiWindowCtrl);
  30. GuiWindowCtrl::GuiWindowCtrl(void)
  31. {
  32. mResizeWidth = true;
  33. mResizeHeight = true;
  34. mCanMove = true;
  35. mCanClose = true;
  36. mCanMinimize = true;
  37. mCanMaximize = true;
  38. mTitleHeight = 20;
  39. mResizeRightWidth = 10;
  40. mResizeBottomHeight = 10;
  41. mIsContainer = true;
  42. mDepressed = false;
  43. curHitRegion = None;
  44. mActive = true;
  45. mMinimized = false;
  46. mMaximized = false;
  47. mMouseMovingWin = false;
  48. mMouseResizeWidth = false;
  49. mMouseResizeHeight = false;
  50. mBounds.extent.set(300, 240);
  51. mMinimizeIndex = -1;
  52. mTabIndex = -1;
  53. //other defaults
  54. mActive = true;
  55. mPressClose = false;
  56. mPressMaximize = false;
  57. mPressMinimize = false;
  58. mContentProfile = NULL;
  59. mCloseButtonProfile = NULL;
  60. mMinButtonProfile = NULL;
  61. mMaxButtonProfile = NULL;
  62. setField("contentProfile", "GuiWindowContentProfile");
  63. setField("closeButtonProfile", "GuiWindowCloseButtonProfile");
  64. setField("minButtonProfile", "GuiWindowMinButtonProfile");
  65. setField("maxButtonProfile", "GuiWindowMaxButtonProfile");
  66. setField("profile", "GuiWindowProfile");
  67. mLeftRightCursor = NULL;
  68. mUpDownCursor = NULL;
  69. mNWSECursor = NULL;
  70. }
  71. void GuiWindowCtrl::initPersistFields()
  72. {
  73. Parent::initPersistFields();
  74. addField("resizeWidth", TypeBool, Offset(mResizeWidth, GuiWindowCtrl));
  75. addField("resizeHeight", TypeBool, Offset(mResizeHeight, GuiWindowCtrl));
  76. addField("canMove", TypeBool, Offset(mCanMove, GuiWindowCtrl));
  77. addField("canClose", TypeBool, Offset(mCanClose, GuiWindowCtrl));
  78. addField("canMinimize", TypeBool, Offset(mCanMinimize, GuiWindowCtrl));
  79. addField("canMaximize", TypeBool, Offset(mCanMaximize, GuiWindowCtrl));
  80. addField("titleHeight", TypeS32, Offset(mTitleHeight, GuiWindowCtrl));
  81. addField("resizeRightWidth", TypeS32, Offset(mResizeRightWidth, GuiWindowCtrl));
  82. addField("resizeBottomHeight", TypeS32, Offset(mResizeBottomHeight, GuiWindowCtrl));
  83. addField("contentProfile", TypeGuiProfile, Offset(mContentProfile, GuiWindowCtrl));
  84. addField("closeButtonProfile", TypeGuiProfile, Offset(mCloseButtonProfile, GuiWindowCtrl));
  85. addField("minButtonProfile", TypeGuiProfile, Offset(mMinButtonProfile, GuiWindowCtrl));
  86. addField("maxButtonProfile", TypeGuiProfile, Offset(mMaxButtonProfile, GuiWindowCtrl));
  87. addField("leftRightCursor", TypeGuiCursor, Offset(mLeftRightCursor, GuiWindowCtrl));
  88. addField("upDownCursor", TypeGuiCursor, Offset(mUpDownCursor, GuiWindowCtrl));
  89. addField("nWSECursor", TypeGuiCursor, Offset(mNWSECursor, GuiWindowCtrl));
  90. }
  91. bool GuiWindowCtrl::isMinimized(S32 &index)
  92. {
  93. index = mMinimizeIndex;
  94. return mMinimized && mVisible;
  95. }
  96. bool GuiWindowCtrl::onWake()
  97. {
  98. if (!Parent::onWake())
  99. return false;
  100. if (mContentProfile != NULL)
  101. mContentProfile->incRefCount();
  102. if (mCloseButtonProfile != NULL)
  103. mCloseButtonProfile->incRefCount();
  104. if (mMinButtonProfile != NULL)
  105. mMinButtonProfile->incRefCount();
  106. if (mMaxButtonProfile != NULL)
  107. mMaxButtonProfile->incRefCount();
  108. //set the tab index
  109. mTabIndex = -1;
  110. GuiControl *parent = getParent();
  111. if (parent && mFirstResponder)
  112. {
  113. mTabIndex = 0;
  114. //count the number of windows preceeding this one
  115. iterator i;
  116. for (i = parent->begin(); i != parent->end(); i++)
  117. {
  118. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  119. if (ctrl)
  120. {
  121. if (ctrl == this) break;
  122. else if (ctrl->mFirstResponder) mTabIndex++;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. void GuiWindowCtrl::onSleep()
  129. {
  130. Parent::onSleep();
  131. if (mContentProfile != NULL)
  132. mContentProfile->decRefCount();
  133. if (mCloseButtonProfile != NULL)
  134. mCloseButtonProfile->decRefCount();
  135. if (mMinButtonProfile != NULL)
  136. mMinButtonProfile->decRefCount();
  137. if (mMaxButtonProfile != NULL)
  138. mMaxButtonProfile->decRefCount();
  139. }
  140. GuiControl* GuiWindowCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  141. {
  142. if (! mMinimized)
  143. return Parent::findHitControl(pt, initialLayer);
  144. else
  145. return this;
  146. }
  147. void GuiWindowCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  148. {
  149. Parent::resize(newPosition, newExtent);
  150. }
  151. void GuiWindowCtrl::onTouchMove(const GuiEvent &event)
  152. {
  153. curHitRegion = findHitRegion(globalToLocalCoord(event.mousePoint));
  154. }
  155. void GuiWindowCtrl::onTouchLeave(const GuiEvent &event)
  156. {
  157. curHitRegion = None;
  158. }
  159. void GuiWindowCtrl::onTouchDown(const GuiEvent &event)
  160. {
  161. setUpdate();
  162. mOrigBounds = mBounds;
  163. mMouseDownPosition = event.mousePoint;
  164. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  165. curHitRegion = findHitRegion(localPoint);
  166. mDepressed = true;
  167. //select this window - move it to the front, and set the first responder
  168. selectWindow();
  169. //if we clicked within the title bar
  170. if (localPoint.y < mTitleHeight)
  171. {
  172. //if we clicked on the close button
  173. if (curHitRegion == CloseButton)
  174. {
  175. mPressClose = true;
  176. }
  177. else if (curHitRegion == MaxButton)
  178. {
  179. mPressMaximize = true;
  180. }
  181. else if (curHitRegion == MinButton)
  182. {
  183. mPressMinimize = true;
  184. }
  185. else if (curHitRegion == TitleBar)
  186. {
  187. mMouseMovingWin = mCanMove;
  188. ResizeComplete();
  189. }
  190. }
  191. else
  192. {
  193. MoveComplete();
  194. //see if we clicked on the right edge
  195. if (mResizeWidth && (localPoint.x > mBounds.extent.x - mResizeRightWidth))
  196. {
  197. mMouseResizeWidth = true;
  198. }
  199. //see if we clicked on the bottom edge (as well)
  200. if (mResizeHeight && (localPoint.y > mBounds.extent.y - mResizeBottomHeight))
  201. {
  202. mMouseResizeHeight = true;
  203. }
  204. }
  205. if (mMouseMovingWin || mMouseResizeWidth || mMouseResizeHeight ||
  206. mPressClose || mPressMaximize || mPressMinimize)
  207. {
  208. mouseLock();
  209. }
  210. else
  211. {
  212. GuiControl *ctrl = findHitControl(localPoint);
  213. if (ctrl && ctrl != this)
  214. ctrl->onTouchDown(event);
  215. }
  216. }
  217. void GuiWindowCtrl::onTouchDragged(const GuiEvent &event)
  218. {
  219. GuiControl *parent = getParent();
  220. GuiCanvas *root = getRoot();
  221. if (! root) return;
  222. curHitRegion = findHitRegion(globalToLocalCoord(event.mousePoint));
  223. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  224. Point2I newPosition = mBounds.point;
  225. Point2I newExtent = mBounds.extent;
  226. bool update = false;
  227. if (mMouseMovingWin && parent)
  228. {
  229. newPosition.x = getMax(0, getMin(parent->mBounds.extent.x - mBounds.extent.x, mOrigBounds.point.x + deltaMousePosition.x));
  230. newPosition.y = getMax(0, getMin(parent->mBounds.extent.y - mBounds.extent.y, mOrigBounds.point.y + deltaMousePosition.y));
  231. update = true;
  232. }
  233. else if(mPressClose || mPressMaximize || mPressMinimize)
  234. {
  235. setUpdate();
  236. }
  237. else
  238. {
  239. Point2I minExtent = getMinExtent();
  240. if (mMouseResizeWidth && parent)
  241. {
  242. newExtent.x = getMax(0, getMax(minExtent.x, getMin(parent->getWidth(), mOrigBounds.extent.x + deltaMousePosition.x)));
  243. update = true;
  244. }
  245. if (mMouseResizeHeight && parent)
  246. {
  247. newExtent.y = getMax(0, getMax(minExtent.y, getMin(parent->getHeight(), mOrigBounds.extent.y + deltaMousePosition.y)));
  248. update = true;
  249. }
  250. }
  251. if (update)
  252. {
  253. Point2I pos = parent->localToGlobalCoord(getPosition());
  254. root->addUpdateRegion(pos, getExtent());
  255. resize(newPosition, newExtent);
  256. }
  257. }
  258. void GuiWindowCtrl::onTouchUp(const GuiEvent &event)
  259. {
  260. bool closing = mPressClose;
  261. bool maximizing = mPressMaximize;
  262. bool minimizing = mPressMinimize;
  263. mPressClose = false;
  264. mPressMaximize = false;
  265. mPressMinimize = false;
  266. mDepressed = false;
  267. mouseUnlock();
  268. setUpdate();
  269. MoveComplete();
  270. ResizeComplete();
  271. GuiControl *parent = getParent();
  272. if (! parent)
  273. return;
  274. //see if we take an action
  275. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  276. if (closing && mCloseButton.pointInRect(localPoint))
  277. {
  278. if(isMethod("onClose"))
  279. {
  280. Con::executef(this, 1, "onClose");
  281. }
  282. }
  283. else if (maximizing && mMaximizeButton.pointInRect(localPoint))
  284. {
  285. if (mMaximized)
  286. {
  287. //resize to the previous position and extent, bounded by the parent
  288. resize(Point2I(getMax(0, getMin(parent->mBounds.extent.x - mStandardBounds.extent.x, mStandardBounds.point.x)),
  289. getMax(0, getMin(parent->mBounds.extent.y - mStandardBounds.extent.y, mStandardBounds.point.y))),
  290. mStandardBounds.extent);
  291. //set the flag
  292. mMaximized = false;
  293. if (isMethod("onRestore"))
  294. {
  295. Con::executef(this, 1, "onRestore");
  296. }
  297. }
  298. else
  299. {
  300. //only save the position if we're not minimized
  301. if (! mMinimized)
  302. {
  303. mStandardBounds = mBounds;
  304. }
  305. else
  306. {
  307. mMinimized = false;
  308. }
  309. //resize to fit the parent
  310. resize(Point2I(0, 0), parent->mBounds.extent);
  311. //set the flag
  312. mMaximized = true;
  313. if (isMethod("onMaximize"))
  314. {
  315. Con::executef(this, 1, "onMaximize");
  316. }
  317. }
  318. }
  319. else if (minimizing && mMinimizeButton.pointInRect(localPoint))
  320. {
  321. if (mMinimized)
  322. {
  323. //resize to the previous position and extent, bounded by the parent
  324. resize(Point2I(getMax(0, getMin(parent->mBounds.extent.x - mStandardBounds.extent.x, mStandardBounds.point.x)),
  325. getMax(0, getMin(parent->mBounds.extent.y - mStandardBounds.extent.y, mStandardBounds.point.y))),
  326. mStandardBounds.extent);
  327. //set the flag
  328. mMinimized = false;
  329. if (isMethod("onRestore"))
  330. {
  331. Con::executef(this, 1, "onRestore");
  332. }
  333. }
  334. else
  335. {
  336. if (parent->mBounds.extent.x < 100 || parent->mBounds.extent.y < mTitleHeight + 3)
  337. return;
  338. //only save the position if we're not maximized
  339. if (! mMaximized)
  340. {
  341. mStandardBounds = mBounds;
  342. }
  343. else
  344. {
  345. mMaximized = false;
  346. }
  347. //first find the lowest unused minimized index up to 32 minimized windows
  348. U32 indexMask = 0;
  349. iterator i;
  350. S32 count = 0;
  351. for (i = parent->begin(); i != parent->end() && count < 32; i++)
  352. {
  353. count++;
  354. S32 index;
  355. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  356. if (ctrl && ctrl->isMinimized(index))
  357. {
  358. indexMask |= (1 << index);
  359. }
  360. }
  361. //now find the first unused bit
  362. for (count = 0; count < 32; count++)
  363. {
  364. if (! (indexMask & (1 << count))) break;
  365. }
  366. //if we have more than 32 minimized windows, use the first position
  367. count = getMax(0, count);
  368. //this algorithm assumes all window have the same title height, and will minimize to 148 pix
  369. Point2I newExtent(148, mTitleHeight);
  370. //first, how many can fit across
  371. S32 numAcross = getMax(1, (parent->mBounds.extent.x / newExtent.x + 2));
  372. //find the new "mini position"
  373. Point2I newPosition;
  374. newPosition.x = (count % numAcross) * (newExtent.x + 2) + 2;
  375. newPosition.y = parent->mBounds.extent.y - (((count / numAcross) + 1) * (newExtent.y + 2)) - 2;
  376. //find the minimized position and extent
  377. resize(newPosition, newExtent);
  378. //set the index so other windows will not try to minimize to the same location
  379. mMinimizeIndex = count;
  380. //set the flag
  381. mMinimized = true;
  382. if (isMethod("onMinimize"))
  383. {
  384. Con::executef(this, 1, "onMinimize");
  385. }
  386. }
  387. }
  388. }
  389. GuiControl *GuiWindowCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  390. {
  391. //set the global if this is the first call (directly from the canvas)
  392. if (firstCall)
  393. {
  394. GuiControl::smCurResponder = NULL;
  395. }
  396. //if the window does not already contain the first responder, return false
  397. //ie. Can't tab into or out of a window
  398. if (! ControlIsChild(curResponder))
  399. {
  400. return NULL;
  401. }
  402. //loop through, checking each child to see if it is the one that follows the firstResponder
  403. GuiControl *tabCtrl = NULL;
  404. iterator i;
  405. for (i = begin(); i != end(); i++)
  406. {
  407. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  408. tabCtrl = ctrl->findNextTabable(curResponder, false);
  409. if (tabCtrl) break;
  410. }
  411. //to ensure the tab cycles within the current window...
  412. if (! tabCtrl)
  413. {
  414. tabCtrl = findFirstTabable();
  415. }
  416. mFirstResponder = tabCtrl;
  417. return tabCtrl;
  418. }
  419. GuiControl *GuiWindowCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  420. {
  421. if (firstCall)
  422. {
  423. GuiControl::smPrevResponder = NULL;
  424. }
  425. //if the window does not already contain the first responder, return false
  426. //ie. Can't tab into or out of a window
  427. if (! ControlIsChild(curResponder))
  428. {
  429. return NULL;
  430. }
  431. //loop through, checking each child to see if it is the one that follows the firstResponder
  432. GuiControl *tabCtrl = NULL;
  433. iterator i;
  434. for (i = begin(); i != end(); i++)
  435. {
  436. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  437. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  438. if (tabCtrl) break;
  439. }
  440. //to ensure the tab cycles within the current window...
  441. if (! tabCtrl)
  442. {
  443. tabCtrl = findLastTabable();
  444. }
  445. mFirstResponder = tabCtrl;
  446. return tabCtrl;
  447. }
  448. GuiWindowCtrl::Region GuiWindowCtrl::findHitRegion(const Point2I& pt)
  449. {
  450. if (mCanClose && mCloseButton.pointInRect(pt))
  451. {
  452. return CloseButton;
  453. }
  454. else if (mCanMaximize && mMaximizeButton.pointInRect(pt))
  455. {
  456. return MaxButton;
  457. }
  458. else if (mCanMinimize && mMinimizeButton.pointInRect(pt))
  459. {
  460. return MinButton;
  461. }
  462. else if (mTitleBar.pointInRect(pt))
  463. {
  464. return TitleBar;
  465. }
  466. return None;
  467. }
  468. bool GuiWindowCtrl::onKeyDown(const GuiEvent &event)
  469. {
  470. //if this control is a dead end, kill the event
  471. if ((! mVisible) || (! mActive) || (! mAwake)) return true;
  472. if ((event.keyCode == KEY_TAB) && (event.modifier & SI_CTRL))
  473. {
  474. //find the next sibling window, and select it
  475. GuiControl *parent = getParent();
  476. if (parent)
  477. {
  478. GuiWindowCtrl *firstWindow = NULL;
  479. iterator i;
  480. for (i = parent->begin(); i != parent->end(); i++)
  481. {
  482. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  483. if (ctrl && ctrl->getTabIndex() == mTabIndex + 1)
  484. {
  485. ctrl->selectWindow();
  486. return true;
  487. }
  488. else if (ctrl && ctrl->getTabIndex() == 0)
  489. {
  490. firstWindow = ctrl;
  491. }
  492. }
  493. //recycle from the beginning
  494. if (firstWindow != this)
  495. {
  496. firstWindow->selectWindow();
  497. return true;
  498. }
  499. }
  500. }
  501. return Parent::onKeyDown(event);
  502. }
  503. void GuiWindowCtrl::onFocus()
  504. {
  505. //bubble the focus up
  506. GuiControl *parent = getParent();
  507. if (parent)
  508. {
  509. parent->pushObjectToBack(this);
  510. parent->onFocus();
  511. }
  512. setFirstResponder(mFirstResponder);
  513. }
  514. void GuiWindowCtrl::selectWindow(void)
  515. {
  516. //first make sure this window is the front most of its siblings
  517. GuiControl *parent = getParent();
  518. if (parent)
  519. {
  520. parent->pushObjectToBack(this);
  521. }
  522. //also set the first responder to be the one within this window
  523. setFirstResponder(mFirstResponder);
  524. }
  525. void GuiWindowCtrl::ResizeComplete()
  526. {
  527. if ((mMouseResizeWidth || mMouseResizeHeight) && isMethod("onResize"))
  528. {
  529. Con::executef(this, 1, "onResize");
  530. }
  531. mMouseResizeWidth = false;
  532. mMouseResizeHeight = false;
  533. }
  534. void GuiWindowCtrl::MoveComplete()
  535. {
  536. if (mMouseMovingWin && isMethod("onMove"))
  537. {
  538. Con::executef(this, 1, "onMove");
  539. }
  540. mMouseMovingWin = false;
  541. }
  542. void GuiWindowCtrl::onRender(Point2I offset, const RectI &updateRect)
  543. {
  544. //Does this window have focus (does it or a child receive key events)?
  545. GuiCanvas *root = getRoot();
  546. GuiControl *firstResponder = root ? root->getFirstResponder() : NULL;
  547. bool hasFocus = (firstResponder && ControlIsChild(firstResponder));
  548. GuiControlState currentState = NormalState;
  549. if (mMinimized)
  550. {
  551. currentState = DisabledState;
  552. }
  553. else if (hasFocus)
  554. {
  555. currentState = SelectedState;
  556. }
  557. else if (curHitRegion == TitleBar)
  558. {
  559. currentState = HighlightState;
  560. }
  561. //Render the title bar
  562. RectI ctrlRectTitle = applyMargins(offset, Point2I(mBounds.extent.x, mTitleHeight), currentState, mProfile);
  563. if (!ctrlRectTitle.isValidRect())
  564. {
  565. return;
  566. }
  567. mTitleBar.set(Point2I(ctrlRectTitle.point.x - offset.x, ctrlRectTitle.point.y - offset.y), ctrlRectTitle.extent);
  568. renderUniversalRect(ctrlRectTitle, mProfile, currentState);
  569. //Render Text and buttons
  570. dglSetBitmapModulation(mProfile->mFontColor);
  571. RectI fillRectTitle = applyBorders(ctrlRectTitle.point, ctrlRectTitle.extent, currentState, mProfile);
  572. RectI contentRectTitle = applyPadding(fillRectTitle.point, fillRectTitle.extent, currentState, mProfile);
  573. if (contentRectTitle.isValidRect())
  574. {
  575. RectI textRect = renderButtons(offset, contentRectTitle);
  576. renderText(textRect.point, textRect.extent, mText, mProfile);
  577. }
  578. //Render window contents
  579. if (!mMinimized)
  580. {
  581. currentState = currentState != SelectedState ? NormalState : SelectedState;
  582. RectI ctrlRectWindow = applyMargins(Point2I(offset.x, offset.y + mTitleHeight), Point2I(mBounds.extent.x, mBounds.extent.y - mTitleHeight), currentState, mContentProfile);
  583. if (!ctrlRectWindow.isValidRect())
  584. {
  585. return;
  586. }
  587. renderUniversalRect(ctrlRectWindow, mContentProfile, currentState);
  588. RectI fillRectWindow = applyBorders(ctrlRectWindow.point, ctrlRectWindow.extent, currentState, mContentProfile);
  589. RectI contentRectWindow = applyPadding(fillRectWindow.point, fillRectWindow.extent, currentState, mContentProfile);
  590. if (contentRectWindow.isValidRect())
  591. {
  592. //render the children
  593. renderChildControls(offset, contentRectWindow, updateRect);
  594. }
  595. }
  596. }
  597. RectI GuiWindowCtrl::renderButtons(const Point2I &offset, const RectI &contentRect)
  598. {
  599. S32 distanceFromEdge = 0;
  600. if (mCanClose)
  601. {
  602. GuiControlState state = getRegionCurrentState(Region::CloseButton);
  603. RectI content = renderButton(contentRect, distanceFromEdge, state, mCloseButtonProfile, Icon::Close);
  604. mCloseButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  605. distanceFromEdge += content.extent.x;
  606. GuiBorderProfile *leftProfile = mCloseButtonProfile->getLeftBorder();
  607. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  608. distanceFromEdge += leftSize;
  609. GuiBorderProfile *rightProfile = mCloseButtonProfile->getRightBorder();
  610. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  611. distanceFromEdge += rightSize;
  612. }
  613. if (mCanMaximize)
  614. {
  615. GuiControlState state = getRegionCurrentState(Region::MaxButton);
  616. RectI content = renderButton(contentRect, distanceFromEdge, state, mMaxButtonProfile, Icon::Max);
  617. mMaximizeButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  618. distanceFromEdge += content.extent.x;
  619. GuiBorderProfile *leftProfile = mMaxButtonProfile->getLeftBorder();
  620. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  621. distanceFromEdge += leftSize;
  622. GuiBorderProfile *rightProfile = mMaxButtonProfile->getRightBorder();
  623. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  624. distanceFromEdge += rightSize;
  625. }
  626. if (mCanMinimize)
  627. {
  628. GuiControlState state = getRegionCurrentState(Region::MinButton);
  629. RectI content = renderButton(contentRect, distanceFromEdge, state, mMinButtonProfile, Icon::Min);
  630. mMinimizeButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  631. distanceFromEdge += content.extent.x;
  632. GuiBorderProfile *leftProfile = mMinButtonProfile->getLeftBorder();
  633. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  634. distanceFromEdge += leftSize;
  635. GuiBorderProfile *rightProfile = mMinButtonProfile->getRightBorder();
  636. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  637. distanceFromEdge += rightSize;
  638. }
  639. if (mProfile->mAlignment != GuiControlProfile::AlignmentType::RightAlign)
  640. {
  641. return RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.x - distanceFromEdge, contentRect.extent.y);
  642. }
  643. else
  644. {
  645. return RectI(contentRect.point.x + distanceFromEdge, contentRect.point.y, contentRect.extent.x - distanceFromEdge, contentRect.extent.y);
  646. }
  647. }
  648. RectI GuiWindowCtrl::renderButton(const RectI &contentRect, S32 distanceFromEdge, GuiControlState buttonState, GuiControlProfile *profile, Icon defaultIcon)
  649. {
  650. RectI buttonContent = applyMargins(Point2I(contentRect.point), Point2I(contentRect.extent), buttonState, profile);
  651. S32 horizMarginSize = contentRect.extent.x - buttonContent.extent.x;
  652. RectI finalButtonRect = RectI(contentRect.point, Point2I(buttonContent.extent.y + horizMarginSize, contentRect.extent.y));
  653. if (mProfile->mAlignment != GuiControlProfile::AlignmentType::RightAlign)
  654. {
  655. //get the right margin and add it to the distance from the edge
  656. GuiBorderProfile *rightProfile = profile->getRightBorder();
  657. S32 rightSize = (rightProfile) ? rightProfile->getMargin(buttonState) : 0;
  658. finalButtonRect.point.x = contentRect.point.x + (contentRect.extent.x - finalButtonRect.extent.x) - distanceFromEdge;
  659. distanceFromEdge += rightSize;
  660. }
  661. else
  662. {
  663. //get the left margin and add it to the disance from the edge
  664. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  665. S32 leftSize = (leftProfile) ? leftProfile->getMargin(buttonState) : 0;
  666. finalButtonRect.point.x = contentRect.point.x + distanceFromEdge;
  667. distanceFromEdge += leftSize;
  668. }
  669. RectI finalButtonContent = applyMargins(finalButtonRect.point, finalButtonRect.extent, buttonState, profile);
  670. renderUniversalRect(finalButtonContent, profile, buttonState);
  671. //now draw an icon if default rendering was used.
  672. if ((profile->mImageAsset == NULL || !mCloseButtonProfile->mImageAsset->isAssetValid()) &&
  673. profile->mBitmapName == NULL)
  674. {
  675. RectI fillRect = applyBorders(finalButtonContent.point, finalButtonContent.extent, buttonState, profile);
  676. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, buttonState, profile);
  677. //draw the icon
  678. if (defaultIcon == Icon::Close)
  679. {
  680. Point2I p1 = Point2I(contentRect.point.x + 1, contentRect.point.y);
  681. Point2I p2 = Point2I(contentRect.point.x, contentRect.point.y + 1);
  682. Point2I p3 = Point2I(contentRect.point.x + contentRect.extent.x - 1, contentRect.point.y + contentRect.extent.y);
  683. Point2I p4 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + contentRect.extent.y - 1);
  684. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  685. Point2I p5 = Point2I(contentRect.point.x, contentRect.point.y + contentRect.extent.y - 1);
  686. Point2I p6 = Point2I(contentRect.point.x + 1, contentRect.point.y + contentRect.extent.y);
  687. Point2I p7 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + 1);
  688. Point2I p8 = Point2I(contentRect.point.x + contentRect.extent.x - 1, contentRect.point.y);
  689. dglDrawQuadFill(p5, p6, p7, p8, profile->getFontColor(buttonState));
  690. }
  691. else if (defaultIcon == Icon::Min)
  692. {
  693. S32 h = (contentRect.len_y() / 2) - 1;
  694. Point2I p1 = Point2I(contentRect.point.x, contentRect.point.y + h);
  695. Point2I p2 = Point2I(contentRect.point.x, contentRect.point.y + h + 2);
  696. Point2I p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + h + 2);
  697. Point2I p4 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + h);
  698. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  699. }
  700. else if (defaultIcon == Icon::Max)
  701. {
  702. //left side
  703. S32 h = (contentRect.len_y() / 2) - 1;
  704. Point2I p1 = Point2I(contentRect.point.x, contentRect.point.y);
  705. Point2I p2 = Point2I(contentRect.point.x, contentRect.point.y + contentRect.extent.y);
  706. Point2I p3 = Point2I(contentRect.point.x + 2, contentRect.point.y + contentRect.extent.y);
  707. Point2I p4 = Point2I(contentRect.point.x + 2, contentRect.point.y);
  708. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  709. //right side
  710. p1.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y);
  711. p2.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y + contentRect.extent.y);
  712. p3.set(contentRect.point.x + contentRect.extent.x, contentRect.point.y + contentRect.extent.y);
  713. p4.set(contentRect.point.x + contentRect.extent.x, contentRect.point.y);
  714. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  715. //top
  716. p1.set(contentRect.point.x + 2, contentRect.point.y);
  717. p2.set(contentRect.point.x + 2, contentRect.point.y + 2);
  718. p3.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y + 2);
  719. p4.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y);
  720. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  721. //bottom
  722. p1.set(contentRect.point.x + 2, contentRect.point.y + contentRect.extent.y - 2);
  723. p2.set(contentRect.point.x + 2, contentRect.point.y + contentRect.extent.y);
  724. p3.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y + contentRect.extent.y);
  725. p4.set(contentRect.point.x + contentRect.extent.x - 2, contentRect.point.y + contentRect.extent.y - 2);
  726. dglDrawQuadFill(p1, p2, p3, p4, profile->getFontColor(buttonState));
  727. }
  728. }
  729. return finalButtonContent;
  730. }
  731. GuiControlState GuiWindowCtrl::getRegionCurrentState(GuiWindowCtrl::Region region)
  732. {
  733. GuiControlState currentState = GuiControlState::NormalState;
  734. if (!mActive)
  735. {
  736. currentState = GuiControlState::DisabledState;
  737. }
  738. else if (curHitRegion == region && mDepressed)
  739. {
  740. currentState = GuiControlState::SelectedState;
  741. }
  742. else if (curHitRegion == region)
  743. {
  744. currentState = GuiControlState::HighlightState;
  745. }
  746. return currentState;
  747. }
  748. void GuiWindowCtrl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent)
  749. {
  750. Point2I mousePos = lastGuiEvent.mousePoint;
  751. RectI winRect = mBounds;
  752. Point2I offset = getParent()->localToGlobalCoord(Point2I(0,0));
  753. RectI rightRect = RectI( ( ( winRect.extent.x + winRect.point.x ) - mResizeRightWidth + offset.x), winRect.point.y + mTitleHeight + offset.y, mResizeRightWidth, winRect.extent.y );
  754. RectI bottomRect = RectI( winRect.point.x + offset.x, ( ( winRect.point.y + winRect.extent.y ) - mResizeBottomHeight) + offset.y, winRect.extent.x , mResizeBottomHeight );
  755. bool resizeRight = rightRect.pointInRect( mousePos );
  756. bool resizeBottom = bottomRect.pointInRect( mousePos );
  757. if ( resizeRight && resizeBottom && mResizeHeight && mResizeWidth )
  758. {
  759. if (mNWSECursor == NULL)
  760. {
  761. SimObject *obj;
  762. obj = Sim::findObject("NWSECursor");
  763. mNWSECursor = dynamic_cast<GuiCursor*>(obj);
  764. }
  765. if(mNWSECursor != NULL)
  766. {
  767. cursor = mNWSECursor;
  768. }
  769. }
  770. else if ( resizeBottom && mResizeHeight )
  771. {
  772. if(mUpDownCursor == NULL)
  773. {
  774. SimObject *obj;
  775. obj = Sim::findObject("UpDownCursor");
  776. mUpDownCursor = dynamic_cast<GuiCursor*>(obj);
  777. }
  778. if (mUpDownCursor != NULL)
  779. {
  780. cursor = mUpDownCursor;
  781. }
  782. }
  783. else if ( resizeRight && mResizeWidth )
  784. {
  785. if (mLeftRightCursor == NULL)
  786. {
  787. SimObject *obj;
  788. obj = Sim::findObject("LeftRightCursor");
  789. mLeftRightCursor = dynamic_cast<GuiCursor*>(obj);
  790. }
  791. if (mLeftRightCursor != NULL)
  792. {
  793. cursor = mLeftRightCursor;
  794. }
  795. }
  796. }
  797. void GuiWindowCtrl::setControlContentProfile(GuiControlProfile* prof)
  798. {
  799. AssertFatal(prof, "GuiWindowCtrl::setControlContentProfile: invalid content profile");
  800. if (prof == mContentProfile)
  801. return;
  802. if (mAwake)
  803. mContentProfile->decRefCount();
  804. mContentProfile = prof;
  805. if (mAwake)
  806. mContentProfile->incRefCount();
  807. }
  808. void GuiWindowCtrl::setControlCloseButtonProfile(GuiControlProfile* prof)
  809. {
  810. AssertFatal(prof, "GuiWindowCtrl::setControlCloseButtonProfile: invalid close button profile");
  811. if (prof == mCloseButtonProfile)
  812. return;
  813. if (mAwake)
  814. mCloseButtonProfile->decRefCount();
  815. mCloseButtonProfile = prof;
  816. if (mAwake)
  817. mCloseButtonProfile->incRefCount();
  818. }
  819. void GuiWindowCtrl::setControlMinButtonProfile(GuiControlProfile* prof)
  820. {
  821. AssertFatal(prof, "GuiWindowCtrl::setControlMinButtonProfile: invalid minimize button profile");
  822. if (prof == mMinButtonProfile)
  823. return;
  824. if (mAwake)
  825. mMinButtonProfile->decRefCount();
  826. mMinButtonProfile = prof;
  827. if (mAwake)
  828. mMinButtonProfile->incRefCount();
  829. }
  830. void GuiWindowCtrl::setControlMaxButtonProfile(GuiControlProfile* prof)
  831. {
  832. AssertFatal(prof, "GuiWindowCtrl::setControlMaxButtonProfile: invalid maximize button profile");
  833. if (prof == mMaxButtonProfile)
  834. return;
  835. if (mAwake)
  836. mMaxButtonProfile->decRefCount();
  837. mMaxButtonProfile = prof;
  838. if (mAwake)
  839. mMaxButtonProfile->incRefCount();
  840. }
  841. void GuiWindowCtrl::setControlLeftRightCursor(GuiCursor* cursor)
  842. {
  843. AssertFatal(cursor, "GuiWindowCtrl::setControlLeftRightCursor: invalid cursor");
  844. if (cursor == mLeftRightCursor)
  845. return;
  846. mLeftRightCursor = cursor;
  847. }
  848. void GuiWindowCtrl::setControlUpDownCursor(GuiCursor* cursor)
  849. {
  850. AssertFatal(cursor, "GuiWindowCtrl::setControlUpDownCursor: invalid cursor");
  851. if (cursor == mUpDownCursor)
  852. return;
  853. mUpDownCursor = cursor;
  854. }
  855. void GuiWindowCtrl::setControlNWSECursor(GuiCursor* cursor)
  856. {
  857. AssertFatal(cursor, "GuiWindowCtrl::setControlNWSECursor: invalid cursor");
  858. if (cursor == mNWSECursor)
  859. return;
  860. mNWSECursor = cursor;
  861. }