guiWindowCtrl.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. Parent::onTouchMove(event);
  155. }
  156. void GuiWindowCtrl::onTouchLeave(const GuiEvent &event)
  157. {
  158. curHitRegion = None;
  159. }
  160. void GuiWindowCtrl::onTouchDown(const GuiEvent &event)
  161. {
  162. setUpdate();
  163. mOrigBounds = mBounds;
  164. mMouseDownPosition = event.mousePoint;
  165. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  166. curHitRegion = findHitRegion(localPoint);
  167. mDepressed = true;
  168. //select this window - move it to the front, and set the first responder
  169. selectWindow();
  170. //if we clicked within the title bar
  171. if (localPoint.y < mTitleHeight)
  172. {
  173. //if we clicked on the close button
  174. if (curHitRegion == CloseButton)
  175. {
  176. mPressClose = true;
  177. }
  178. else if (curHitRegion == MaxButton)
  179. {
  180. mPressMaximize = true;
  181. }
  182. else if (curHitRegion == MinButton)
  183. {
  184. mPressMinimize = true;
  185. }
  186. else if (curHitRegion == TitleBar)
  187. {
  188. mMouseMovingWin = mCanMove;
  189. ResizeComplete();
  190. }
  191. }
  192. else
  193. {
  194. MoveComplete();
  195. //see if we clicked on the right edge
  196. if (mResizeWidth && (localPoint.x > mBounds.extent.x - mResizeRightWidth))
  197. {
  198. mMouseResizeWidth = true;
  199. }
  200. //see if we clicked on the bottom edge (as well)
  201. if (mResizeHeight && (localPoint.y > mBounds.extent.y - mResizeBottomHeight))
  202. {
  203. mMouseResizeHeight = true;
  204. }
  205. }
  206. if (mMouseMovingWin || mMouseResizeWidth || mMouseResizeHeight ||
  207. mPressClose || mPressMaximize || mPressMinimize)
  208. {
  209. mouseLock();
  210. }
  211. }
  212. void GuiWindowCtrl::onTouchDragged(const GuiEvent &event)
  213. {
  214. GuiControl *parent = getParent();
  215. GuiCanvas *root = getRoot();
  216. if (! root) return;
  217. curHitRegion = findHitRegion(globalToLocalCoord(event.mousePoint));
  218. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  219. Point2I newPosition = mBounds.point;
  220. Point2I newExtent = mBounds.extent;
  221. bool update = false;
  222. if (mMouseMovingWin && parent)
  223. {
  224. newPosition.x = getMax(0, getMin(parent->mBounds.extent.x - mBounds.extent.x, mOrigBounds.point.x + deltaMousePosition.x));
  225. newPosition.y = getMax(0, getMin(parent->mBounds.extent.y - mBounds.extent.y, mOrigBounds.point.y + deltaMousePosition.y));
  226. update = true;
  227. }
  228. else if(mPressClose || mPressMaximize || mPressMinimize)
  229. {
  230. setUpdate();
  231. }
  232. else
  233. {
  234. Point2I minExtent = getMinExtent();
  235. if (mMouseResizeWidth && parent)
  236. {
  237. newExtent.x = getMax(0, getMax(minExtent.x, getMin(parent->getWidth(), mOrigBounds.extent.x + deltaMousePosition.x)));
  238. update = true;
  239. }
  240. if (mMouseResizeHeight && parent)
  241. {
  242. newExtent.y = getMax(0, getMax(minExtent.y, getMin(parent->getHeight(), mOrigBounds.extent.y + deltaMousePosition.y)));
  243. update = true;
  244. }
  245. }
  246. if (update)
  247. {
  248. Point2I pos = parent->localToGlobalCoord(getPosition());
  249. root->addUpdateRegion(pos, getExtent());
  250. resize(newPosition, newExtent);
  251. }
  252. }
  253. void GuiWindowCtrl::onTouchUp(const GuiEvent &event)
  254. {
  255. bool closing = mPressClose;
  256. bool maximizing = mPressMaximize;
  257. bool minimizing = mPressMinimize;
  258. mPressClose = false;
  259. mPressMaximize = false;
  260. mPressMinimize = false;
  261. mDepressed = false;
  262. mouseUnlock();
  263. setUpdate();
  264. MoveComplete();
  265. ResizeComplete();
  266. GuiControl *parent = getParent();
  267. if (! parent)
  268. return;
  269. //see if we take an action
  270. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  271. if (closing && mCloseButton.pointInRect(localPoint))
  272. {
  273. if(isMethod("onClose"))
  274. {
  275. Con::executef(this, 1, "onClose");
  276. }
  277. }
  278. else if (maximizing && mMaximizeButton.pointInRect(localPoint))
  279. {
  280. if (mMaximized)
  281. {
  282. //resize to the previous position and extent, bounded by the parent
  283. resize(Point2I(getMax(0, getMin(parent->mBounds.extent.x - mStandardBounds.extent.x, mStandardBounds.point.x)),
  284. getMax(0, getMin(parent->mBounds.extent.y - mStandardBounds.extent.y, mStandardBounds.point.y))),
  285. mStandardBounds.extent);
  286. //set the flag
  287. mMaximized = false;
  288. if (isMethod("onRestore"))
  289. {
  290. Con::executef(this, 1, "onRestore");
  291. }
  292. }
  293. else
  294. {
  295. //only save the position if we're not minimized
  296. if (! mMinimized)
  297. {
  298. mStandardBounds = mBounds;
  299. }
  300. else
  301. {
  302. mMinimized = false;
  303. }
  304. //resize to fit the parent
  305. resize(Point2I(0, 0), parent->mBounds.extent);
  306. //set the flag
  307. mMaximized = true;
  308. if (isMethod("onMaximize"))
  309. {
  310. Con::executef(this, 1, "onMaximize");
  311. }
  312. }
  313. }
  314. else if (minimizing && mMinimizeButton.pointInRect(localPoint))
  315. {
  316. if (mMinimized)
  317. {
  318. //resize to the previous position and extent, bounded by the parent
  319. resize(Point2I(getMax(0, getMin(parent->mBounds.extent.x - mStandardBounds.extent.x, mStandardBounds.point.x)),
  320. getMax(0, getMin(parent->mBounds.extent.y - mStandardBounds.extent.y, mStandardBounds.point.y))),
  321. mStandardBounds.extent);
  322. //set the flag
  323. mMinimized = false;
  324. if (isMethod("onRestore"))
  325. {
  326. Con::executef(this, 1, "onRestore");
  327. }
  328. }
  329. else
  330. {
  331. if (parent->mBounds.extent.x < 100 || parent->mBounds.extent.y < mTitleHeight + 3)
  332. return;
  333. //only save the position if we're not maximized
  334. if (! mMaximized)
  335. {
  336. mStandardBounds = mBounds;
  337. }
  338. else
  339. {
  340. mMaximized = false;
  341. }
  342. //first find the lowest unused minimized index up to 32 minimized windows
  343. U32 indexMask = 0;
  344. iterator i;
  345. S32 count = 0;
  346. for (i = parent->begin(); i != parent->end() && count < 32; i++)
  347. {
  348. count++;
  349. S32 index;
  350. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  351. if (ctrl && ctrl->isMinimized(index))
  352. {
  353. indexMask |= (1 << index);
  354. }
  355. }
  356. //now find the first unused bit
  357. for (count = 0; count < 32; count++)
  358. {
  359. if (! (indexMask & (1 << count))) break;
  360. }
  361. //if we have more than 32 minimized windows, use the first position
  362. count = getMax(0, count);
  363. //this algorithm assumes all window have the same title height, and will minimize to 148 pix
  364. Point2I newExtent(148, mTitleHeight);
  365. //first, how many can fit across
  366. S32 numAcross = getMax(1, (parent->mBounds.extent.x / newExtent.x + 2));
  367. //find the new "mini position"
  368. Point2I newPosition;
  369. newPosition.x = (count % numAcross) * (newExtent.x + 2) + 2;
  370. newPosition.y = parent->mBounds.extent.y - (((count / numAcross) + 1) * (newExtent.y + 2)) - 2;
  371. //find the minimized position and extent
  372. resize(newPosition, newExtent);
  373. //set the index so other windows will not try to minimize to the same location
  374. mMinimizeIndex = count;
  375. //set the flag
  376. mMinimized = true;
  377. if (isMethod("onMinimize"))
  378. {
  379. Con::executef(this, 1, "onMinimize");
  380. }
  381. }
  382. }
  383. }
  384. GuiControl *GuiWindowCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  385. {
  386. //set the global if this is the first call (directly from the canvas)
  387. if (firstCall)
  388. {
  389. GuiControl::smCurResponder = NULL;
  390. }
  391. //if the window does not already contain the first responder, return false
  392. //ie. Can't tab into or out of a window
  393. if (! ControlIsChild(curResponder))
  394. {
  395. return NULL;
  396. }
  397. //loop through, checking each child to see if it is the one that follows the firstResponder
  398. GuiControl *tabCtrl = NULL;
  399. iterator i;
  400. for (i = begin(); i != end(); i++)
  401. {
  402. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  403. tabCtrl = ctrl->findNextTabable(curResponder, false);
  404. if (tabCtrl) break;
  405. }
  406. //to ensure the tab cycles within the current window...
  407. if (! tabCtrl)
  408. {
  409. tabCtrl = findFirstTabable();
  410. }
  411. mFirstResponder = tabCtrl;
  412. return tabCtrl;
  413. }
  414. GuiControl *GuiWindowCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  415. {
  416. if (firstCall)
  417. {
  418. GuiControl::smPrevResponder = NULL;
  419. }
  420. //if the window does not already contain the first responder, return false
  421. //ie. Can't tab into or out of a window
  422. if (! ControlIsChild(curResponder))
  423. {
  424. return NULL;
  425. }
  426. //loop through, checking each child to see if it is the one that follows the firstResponder
  427. GuiControl *tabCtrl = NULL;
  428. iterator i;
  429. for (i = begin(); i != end(); i++)
  430. {
  431. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  432. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  433. if (tabCtrl) break;
  434. }
  435. //to ensure the tab cycles within the current window...
  436. if (! tabCtrl)
  437. {
  438. tabCtrl = findLastTabable();
  439. }
  440. mFirstResponder = tabCtrl;
  441. return tabCtrl;
  442. }
  443. GuiWindowCtrl::Region GuiWindowCtrl::findHitRegion(const Point2I& pt)
  444. {
  445. if (mCanClose && mCloseButton.pointInRect(pt))
  446. {
  447. return CloseButton;
  448. }
  449. else if (mCanMaximize && mMaximizeButton.pointInRect(pt))
  450. {
  451. return MaxButton;
  452. }
  453. else if (mCanMinimize && mMinimizeButton.pointInRect(pt))
  454. {
  455. return MinButton;
  456. }
  457. else if (mTitleBar.pointInRect(pt))
  458. {
  459. return TitleBar;
  460. }
  461. return None;
  462. }
  463. bool GuiWindowCtrl::onKeyDown(const GuiEvent &event)
  464. {
  465. //if this control is a dead end, kill the event
  466. if ((! mVisible) || (! mActive) || (! mAwake)) return true;
  467. if ((event.keyCode == KEY_TAB) && (event.modifier & SI_CTRL))
  468. {
  469. //find the next sibling window, and select it
  470. GuiControl *parent = getParent();
  471. if (parent)
  472. {
  473. GuiWindowCtrl *firstWindow = NULL;
  474. iterator i;
  475. for (i = parent->begin(); i != parent->end(); i++)
  476. {
  477. GuiWindowCtrl *ctrl = dynamic_cast<GuiWindowCtrl *>(*i);
  478. if (ctrl && ctrl->getTabIndex() == mTabIndex + 1)
  479. {
  480. ctrl->selectWindow();
  481. return true;
  482. }
  483. else if (ctrl && ctrl->getTabIndex() == 0)
  484. {
  485. firstWindow = ctrl;
  486. }
  487. }
  488. //recycle from the beginning
  489. if (firstWindow != this)
  490. {
  491. firstWindow->selectWindow();
  492. return true;
  493. }
  494. }
  495. }
  496. return Parent::onKeyDown(event);
  497. }
  498. void GuiWindowCtrl::onFocus()
  499. {
  500. //bubble the focus up
  501. GuiControl *parent = getParent();
  502. if (parent)
  503. {
  504. parent->pushObjectToBack(this);
  505. parent->onFocus();
  506. }
  507. setFirstResponder(mFirstResponder);
  508. }
  509. void GuiWindowCtrl::selectWindow(void)
  510. {
  511. //first make sure this window is the front most of its siblings
  512. GuiControl *parent = getParent();
  513. if (parent)
  514. {
  515. parent->pushObjectToBack(this);
  516. }
  517. //also set the first responder to be the one within this window
  518. setFirstResponder(mFirstResponder);
  519. }
  520. void GuiWindowCtrl::ResizeComplete()
  521. {
  522. if ((mMouseResizeWidth || mMouseResizeHeight) && isMethod("onResize"))
  523. {
  524. Con::executef(this, 1, "onResize");
  525. }
  526. mMouseResizeWidth = false;
  527. mMouseResizeHeight = false;
  528. }
  529. void GuiWindowCtrl::MoveComplete()
  530. {
  531. if (mMouseMovingWin && isMethod("onMove"))
  532. {
  533. Con::executef(this, 1, "onMove");
  534. }
  535. mMouseMovingWin = false;
  536. }
  537. void GuiWindowCtrl::onRender(Point2I offset, const RectI &updateRect)
  538. {
  539. //Does this window have focus (does it or a child receive key events)?
  540. GuiCanvas *root = getRoot();
  541. GuiControl *firstResponder = root ? root->getFirstResponder() : NULL;
  542. bool hasFocus = (firstResponder && ControlIsChild(firstResponder));
  543. GuiControlState currentState = NormalState;
  544. if (mMinimized)
  545. {
  546. currentState = DisabledState;
  547. }
  548. else if (hasFocus)
  549. {
  550. currentState = SelectedState;
  551. }
  552. else if (curHitRegion == TitleBar)
  553. {
  554. currentState = HighlightState;
  555. }
  556. //Render the title bar
  557. Point2I extent = Point2I(mBounds.extent.x, mTitleHeight);
  558. RectI ctrlRectTitle = applyMargins(offset, extent, currentState, mProfile);
  559. if (!ctrlRectTitle.isValidRect())
  560. {
  561. return;
  562. }
  563. mTitleBar.set(Point2I(ctrlRectTitle.point.x - offset.x, ctrlRectTitle.point.y - offset.y), ctrlRectTitle.extent);
  564. renderUniversalRect(ctrlRectTitle, mProfile, currentState);
  565. //Render Text and buttons
  566. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  567. RectI fillRectTitle = applyBorders(ctrlRectTitle.point, ctrlRectTitle.extent, currentState, mProfile);
  568. RectI contentRectTitle = applyPadding(fillRectTitle.point, fillRectTitle.extent, currentState, mProfile);
  569. if (contentRectTitle.isValidRect())
  570. {
  571. RectI textRect = renderButtons(offset, contentRectTitle);
  572. renderText(textRect.point, textRect.extent, mText, mProfile);
  573. }
  574. //Render window contents
  575. if (!mMinimized)
  576. {
  577. currentState = currentState != SelectedState ? NormalState : SelectedState;
  578. Point2I offsetWithTileHeight = Point2I(offset.x, offset.y + mTitleHeight);
  579. Point2I boundsExtentMinusTileHeight = Point2I(mBounds.extent.x, mBounds.extent.y - mTitleHeight);
  580. RectI ctrlRectWindow = applyMargins(offsetWithTileHeight, boundsExtentMinusTileHeight, currentState, mContentProfile);
  581. if (!ctrlRectWindow.isValidRect())
  582. {
  583. return;
  584. }
  585. renderUniversalRect(ctrlRectWindow, mContentProfile, currentState);
  586. RectI fillRectWindow = applyBorders(ctrlRectWindow.point, ctrlRectWindow.extent, currentState, mContentProfile);
  587. RectI contentRectWindow = applyPadding(fillRectWindow.point, fillRectWindow.extent, currentState, mContentProfile);
  588. if (contentRectWindow.isValidRect())
  589. {
  590. //render the children
  591. renderChildControls(offset, contentRectWindow, updateRect);
  592. }
  593. }
  594. }
  595. RectI GuiWindowCtrl::renderButtons(const Point2I &offset, const RectI &contentRect)
  596. {
  597. S32 distanceFromEdge = 0;
  598. if (mCanClose)
  599. {
  600. GuiControlState state = getRegionCurrentState(Region::CloseButton);
  601. RectI content = renderButton(contentRect, distanceFromEdge, state, mCloseButtonProfile, Icon::Close);
  602. mCloseButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  603. distanceFromEdge += content.extent.x;
  604. GuiBorderProfile *leftProfile = mCloseButtonProfile->getLeftBorder();
  605. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  606. distanceFromEdge += leftSize;
  607. GuiBorderProfile *rightProfile = mCloseButtonProfile->getRightBorder();
  608. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  609. distanceFromEdge += rightSize;
  610. }
  611. if (mCanMaximize)
  612. {
  613. GuiControlState state = getRegionCurrentState(Region::MaxButton);
  614. RectI content = renderButton(contentRect, distanceFromEdge, state, mMaxButtonProfile, Icon::Max);
  615. mMaximizeButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  616. distanceFromEdge += content.extent.x;
  617. GuiBorderProfile *leftProfile = mMaxButtonProfile->getLeftBorder();
  618. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  619. distanceFromEdge += leftSize;
  620. GuiBorderProfile *rightProfile = mMaxButtonProfile->getRightBorder();
  621. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  622. distanceFromEdge += rightSize;
  623. }
  624. if (mCanMinimize)
  625. {
  626. GuiControlState state = getRegionCurrentState(Region::MinButton);
  627. RectI content = renderButton(contentRect, distanceFromEdge, state, mMinButtonProfile, Icon::Min);
  628. mMinimizeButton.set(Point2I(content.point.x - offset.x, content.point.y - offset.y), content.extent);
  629. distanceFromEdge += content.extent.x;
  630. GuiBorderProfile *leftProfile = mMinButtonProfile->getLeftBorder();
  631. S32 leftSize = (leftProfile) ? leftProfile->getMargin(state) : 0;
  632. distanceFromEdge += leftSize;
  633. GuiBorderProfile *rightProfile = mMinButtonProfile->getRightBorder();
  634. S32 rightSize = (rightProfile) ? rightProfile->getMargin(state) : 0;
  635. distanceFromEdge += rightSize;
  636. }
  637. if (getAlignmentType() != AlignmentType::RightAlign)
  638. {
  639. return RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.x - distanceFromEdge, contentRect.extent.y);
  640. }
  641. else
  642. {
  643. return RectI(contentRect.point.x + distanceFromEdge, contentRect.point.y, contentRect.extent.x - distanceFromEdge, contentRect.extent.y);
  644. }
  645. }
  646. RectI GuiWindowCtrl::renderButton(const RectI &contentRect, S32 distanceFromEdge, GuiControlState buttonState, GuiControlProfile *profile, Icon defaultIcon)
  647. {
  648. Point2I offset = Point2I(contentRect.point);
  649. Point2I extent = Point2I(contentRect.extent);
  650. RectI buttonContent = applyMargins(offset, 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 (getAlignmentType() != 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. GuiControl* parent = getParent();
  753. if (!parent)
  754. {
  755. return;
  756. }
  757. Point2I offset = getParent()->localToGlobalCoord(Point2I(0,0));
  758. RectI rightRect = RectI( ( ( winRect.extent.x + winRect.point.x ) - mResizeRightWidth + offset.x), winRect.point.y + mTitleHeight + offset.y, mResizeRightWidth, winRect.extent.y );
  759. RectI bottomRect = RectI( winRect.point.x + offset.x, ( ( winRect.point.y + winRect.extent.y ) - mResizeBottomHeight) + offset.y, winRect.extent.x , mResizeBottomHeight );
  760. bool resizeRight = rightRect.pointInRect( mousePos );
  761. bool resizeBottom = bottomRect.pointInRect( mousePos );
  762. if ( resizeRight && resizeBottom && mResizeHeight && mResizeWidth )
  763. {
  764. if (mNWSECursor == NULL)
  765. {
  766. SimObject *obj;
  767. obj = Sim::findObject("NWSECursor");
  768. mNWSECursor = dynamic_cast<GuiCursor*>(obj);
  769. }
  770. if(mNWSECursor != NULL)
  771. {
  772. cursor = mNWSECursor;
  773. }
  774. }
  775. else if ( resizeBottom && mResizeHeight )
  776. {
  777. if(mUpDownCursor == NULL)
  778. {
  779. SimObject *obj;
  780. obj = Sim::findObject("UpDownCursor");
  781. mUpDownCursor = dynamic_cast<GuiCursor*>(obj);
  782. }
  783. if (mUpDownCursor != NULL)
  784. {
  785. cursor = mUpDownCursor;
  786. }
  787. }
  788. else if ( resizeRight && mResizeWidth )
  789. {
  790. if (mLeftRightCursor == NULL)
  791. {
  792. SimObject *obj;
  793. obj = Sim::findObject("LeftRightCursor");
  794. mLeftRightCursor = dynamic_cast<GuiCursor*>(obj);
  795. }
  796. if (mLeftRightCursor != NULL)
  797. {
  798. cursor = mLeftRightCursor;
  799. }
  800. }
  801. }
  802. void GuiWindowCtrl::setControlContentProfile(GuiControlProfile* prof)
  803. {
  804. AssertFatal(prof, "GuiWindowCtrl::setControlContentProfile: invalid content profile");
  805. if (prof == mContentProfile)
  806. return;
  807. if (mAwake)
  808. mContentProfile->decRefCount();
  809. mContentProfile = prof;
  810. if (mAwake)
  811. mContentProfile->incRefCount();
  812. }
  813. void GuiWindowCtrl::setControlCloseButtonProfile(GuiControlProfile* prof)
  814. {
  815. AssertFatal(prof, "GuiWindowCtrl::setControlCloseButtonProfile: invalid close button profile");
  816. if (prof == mCloseButtonProfile)
  817. return;
  818. if (mAwake)
  819. mCloseButtonProfile->decRefCount();
  820. mCloseButtonProfile = prof;
  821. if (mAwake)
  822. mCloseButtonProfile->incRefCount();
  823. }
  824. void GuiWindowCtrl::setControlMinButtonProfile(GuiControlProfile* prof)
  825. {
  826. AssertFatal(prof, "GuiWindowCtrl::setControlMinButtonProfile: invalid minimize button profile");
  827. if (prof == mMinButtonProfile)
  828. return;
  829. if (mAwake)
  830. mMinButtonProfile->decRefCount();
  831. mMinButtonProfile = prof;
  832. if (mAwake)
  833. mMinButtonProfile->incRefCount();
  834. }
  835. void GuiWindowCtrl::setControlMaxButtonProfile(GuiControlProfile* prof)
  836. {
  837. AssertFatal(prof, "GuiWindowCtrl::setControlMaxButtonProfile: invalid maximize button profile");
  838. if (prof == mMaxButtonProfile)
  839. return;
  840. if (mAwake)
  841. mMaxButtonProfile->decRefCount();
  842. mMaxButtonProfile = prof;
  843. if (mAwake)
  844. mMaxButtonProfile->incRefCount();
  845. }
  846. void GuiWindowCtrl::setControlLeftRightCursor(GuiCursor* cursor)
  847. {
  848. AssertFatal(cursor, "GuiWindowCtrl::setControlLeftRightCursor: invalid cursor");
  849. if (cursor == mLeftRightCursor)
  850. return;
  851. mLeftRightCursor = cursor;
  852. }
  853. void GuiWindowCtrl::setControlUpDownCursor(GuiCursor* cursor)
  854. {
  855. AssertFatal(cursor, "GuiWindowCtrl::setControlUpDownCursor: invalid cursor");
  856. if (cursor == mUpDownCursor)
  857. return;
  858. mUpDownCursor = cursor;
  859. }
  860. void GuiWindowCtrl::setControlNWSECursor(GuiCursor* cursor)
  861. {
  862. AssertFatal(cursor, "GuiWindowCtrl::setControlNWSECursor: invalid cursor");
  863. if (cursor == mNWSECursor)
  864. return;
  865. mNWSECursor = cursor;
  866. }