guiWindowCtrl.cc 35 KB

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