guiWindowCtrl.cc 35 KB

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