2
0

guiContainer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/containers/guiContainer.h"
  24. #include "gui/containers/guiPanel.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. IMPLEMENT_CONOBJECT( GuiContainer );
  28. ConsoleDocClass( GuiContainer,
  29. "@brief Brief Desc.\n\n"
  30. "@tsexample\n"
  31. "// Comment:\n"
  32. "%okButton = new ClassObject()\n"
  33. "instantiation\n"
  34. "@endtsexample\n\n"
  35. "@ingroup GuiContainers"
  36. );
  37. ImplementEnumType( GuiDockingType,
  38. "\n\n"
  39. "@ingroup GuiContainers" )
  40. { Docking::dockNone, "None" },
  41. { Docking::dockClient, "Client" },
  42. { Docking::dockTop, "Top" },
  43. { Docking::dockBottom, "Bottom" },
  44. { Docking::dockLeft, "Left" },
  45. { Docking::dockRight, "Right" }
  46. EndImplementEnumType;
  47. //-----------------------------------------------------------------------------
  48. GuiContainer::GuiContainer()
  49. {
  50. mUpdateLayout = false;
  51. mValidDockingMask = Docking::dockNone | Docking::dockBottom |
  52. Docking::dockTop | Docking::dockClient |
  53. Docking::dockLeft | Docking::dockRight;
  54. mIsContainer = true;
  55. }
  56. //-----------------------------------------------------------------------------
  57. GuiContainer::~GuiContainer()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. void GuiContainer::initPersistFields()
  62. {
  63. Con::setIntVariable("$DOCKING_NONE", Docking::dockNone);
  64. Con::setIntVariable("$DOCKING_CLIENT", Docking::dockClient);
  65. Con::setIntVariable("$DOCKING_TOP", Docking::dockTop);
  66. Con::setIntVariable("$DOCKING_BOTTOM", Docking::dockBottom);
  67. Con::setIntVariable("$DOCKING_LEFT", Docking::dockLeft);
  68. Con::setIntVariable("$DOCKING_RIGHT", Docking::dockRight);
  69. addGroup( "Layout" );
  70. addProtectedField("docking", TYPEID< Docking::DockingType >(), Offset(mSizingOptions.mDocking, GuiContainer), &setDockingField, &defaultProtectedGetFn, "" );
  71. addField("margin", TypeRectSpacingI, Offset(mSizingOptions.mPadding, GuiContainer));
  72. addField("padding", TypeRectSpacingI, Offset(mSizingOptions.mInternalPadding, GuiContainer));
  73. addField("anchorTop", TypeBool, Offset(mSizingOptions.mAnchorTop, GuiContainer));
  74. addField("anchorBottom", TypeBool, Offset(mSizingOptions.mAnchorBottom, GuiContainer));
  75. addField("anchorLeft", TypeBool, Offset(mSizingOptions.mAnchorLeft, GuiContainer));
  76. addField("anchorRight", TypeBool, Offset(mSizingOptions.mAnchorRight, GuiContainer));
  77. endGroup( "Layout" );
  78. Parent::initPersistFields();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void GuiContainer::onChildAdded(GuiControl* control)
  82. {
  83. Parent::onChildAdded( control );
  84. setUpdateLayout();
  85. }
  86. //-----------------------------------------------------------------------------
  87. void GuiContainer::onChildRemoved(GuiControl* control)
  88. {
  89. Parent::onChildRemoved( control );
  90. setUpdateLayout();
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool GuiContainer::reOrder(SimObject* obj, SimObject* target)
  94. {
  95. if ( !Parent::reOrder(obj, target) )
  96. return false;
  97. setUpdateLayout();
  98. return true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool GuiContainer::resize( const Point2I &newPosition, const Point2I &newExtent )
  102. {
  103. if( !Parent::resize( newPosition, newExtent ) )
  104. return false;
  105. RectI clientRect = getClientRect();
  106. layoutControls( clientRect );
  107. GuiControl *parent = getParent();
  108. S32 docking = getDocking();
  109. if( parent && docking != Docking::dockNone && docking != Docking::dockInvalid )
  110. setUpdateLayout( updateParent );
  111. return true;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void GuiContainer::addObject(SimObject *obj)
  115. {
  116. Parent::addObject(obj);
  117. setUpdateLayout();
  118. }
  119. //-----------------------------------------------------------------------------
  120. void GuiContainer::removeObject(SimObject *obj)
  121. {
  122. Parent::removeObject(obj);
  123. setUpdateLayout();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void GuiContainer::parentResized(const RectI &oldParentRect, const RectI &newParentRect)
  127. {
  128. //if(!mCanResize)
  129. // return;
  130. // If it's a control that specifies invalid docking, we'll just treat it as an old GuiControl
  131. if( getDocking() & Docking::dockInvalid || getDocking() & Docking::dockNone)
  132. return Parent::parentResized( oldParentRect, newParentRect );
  133. S32 deltaX = newParentRect.extent.x - oldParentRect.extent.x;
  134. S32 deltaY = newParentRect.extent.y - oldParentRect.extent.y;
  135. // Update Self
  136. RectI oldThisRect = getBounds();
  137. anchorControl( this, Point2I( deltaX, deltaY ) );
  138. RectI newThisRect = getBounds();
  139. // Update Deltas to pass on to children
  140. deltaX = newThisRect.extent.x - oldThisRect.extent.x;
  141. deltaY = newThisRect.extent.y - oldThisRect.extent.y;
  142. // Iterate over all children and update their anchors
  143. iterator nI = begin();
  144. for( ; nI != end(); nI++ )
  145. {
  146. // Sanity
  147. GuiControl *control = dynamic_cast<GuiControl*>( (*nI) );
  148. if( control )
  149. control->parentResized( oldThisRect, newThisRect );
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. void GuiContainer::childResized(GuiControl *child)
  154. {
  155. Parent::childResized( child );
  156. setUpdateLayout();
  157. }
  158. //-----------------------------------------------------------------------------
  159. bool GuiContainer::layoutControls( RectI &clientRect )
  160. {
  161. // This variable is set to the first 'Client' docking
  162. // control that is found. We defer client docking until
  163. // after all other docks have been made since it will consume
  164. // the remaining client area available.
  165. GuiContainer *clientDocking = NULL;
  166. // Iterate over all children and perform docking
  167. iterator nI = begin();
  168. for( ; nI != end(); nI++ )
  169. {
  170. // Layout Content with proper docking (Client Default)
  171. GuiControl *control = static_cast<GuiControl*>(*nI);
  172. // If we're invisible we don't get counted in docking
  173. if( control == NULL || !control->isVisible() )
  174. continue;
  175. S32 dockingMode = Docking::dockNone;
  176. GuiContainer *container = dynamic_cast<GuiContainer*>(control);
  177. if( container != NULL )
  178. dockingMode = container->getDocking();
  179. else
  180. continue;
  181. // See above note about clientDocking pointer
  182. if( dockingMode & Docking::dockClient && clientDocking == NULL )
  183. clientDocking = container;
  184. // Dock Appropriately
  185. if( !(dockingMode & Docking::dockClient) )
  186. dockControl( container, dockingMode, clientRect );
  187. }
  188. // Do client dock
  189. if( clientDocking != NULL )
  190. dockControl( clientDocking, Docking::dockClient, clientRect );
  191. return true;
  192. }
  193. //-----------------------------------------------------------------------------
  194. bool GuiContainer::dockControl( GuiContainer *control, S32 dockingMode, RectI &clientRect )
  195. {
  196. if( !control )
  197. return false;
  198. // Make sure this class support docking of this type
  199. if( !(dockingMode & getValidDockingMask()))
  200. return false;
  201. // If our client rect has run out of room, we can't dock any more
  202. if( !clientRect.isValidRect() )
  203. return false;
  204. // Dock Appropriately
  205. RectI dockRect;
  206. RectSpacingI rectShrinker;
  207. ControlSizing sizingOptions = control->getSizingOptions();
  208. switch( dockingMode )
  209. {
  210. case Docking::dockClient:
  211. // Inset by padding
  212. sizingOptions.mPadding.insetRect(clientRect);
  213. // Dock to entirety of client rectangle
  214. control->resize( clientRect.point, clientRect.extent );
  215. // Remove Client Rect, can only have one client dock
  216. clientRect.set(0,0,0,0);
  217. break;
  218. case Docking::dockTop:
  219. dockRect = clientRect;
  220. dockRect.extent.y = getMin( control->getHeight() + sizingOptions.mPadding.top + sizingOptions.mPadding.bottom , clientRect.extent.y );
  221. // Subtract our rect
  222. clientRect.point.y += dockRect.extent.y;
  223. clientRect.extent.y -= dockRect.extent.y;
  224. // Inset by padding
  225. sizingOptions.mPadding.insetRect(dockRect);
  226. // Resize
  227. control->resize( dockRect.point, dockRect.extent );
  228. break;
  229. case Docking::dockBottom:
  230. dockRect = clientRect;
  231. dockRect.extent.y = getMin( control->getHeight() + sizingOptions.mPadding.top + sizingOptions.mPadding.bottom, clientRect.extent.y );
  232. dockRect.point.y += clientRect.extent.y - dockRect.extent.y;
  233. // Subtract our rect
  234. clientRect.extent.y -= dockRect.extent.y;
  235. // Inset by padding
  236. sizingOptions.mPadding.insetRect(dockRect);
  237. // Resize
  238. control->resize( dockRect.point, dockRect.extent );
  239. break;
  240. case Docking::dockLeft:
  241. dockRect = clientRect;
  242. dockRect.extent.x = getMin( control->getWidth() + sizingOptions.mPadding.left + sizingOptions.mPadding.right, clientRect.extent.x );
  243. // Subtract our rect
  244. clientRect.point.x += dockRect.extent.x;
  245. clientRect.extent.x -= dockRect.extent.x;
  246. // Inset by padding
  247. sizingOptions.mPadding.insetRect(dockRect);
  248. // Resize
  249. control->resize( dockRect.point, dockRect.extent );
  250. break;
  251. case Docking::dockRight:
  252. dockRect = clientRect;
  253. dockRect.extent.x = getMin( control->getWidth() + sizingOptions.mPadding.left + sizingOptions.mPadding.right, clientRect.extent.x );
  254. dockRect.point.x += clientRect.extent.x - dockRect.extent.x;
  255. // Subtract our rect
  256. clientRect.extent.x -= dockRect.extent.x;
  257. // Inset by padding
  258. sizingOptions.mPadding.insetRect(dockRect);
  259. // Resize
  260. control->resize( dockRect.point, dockRect.extent );
  261. break;
  262. case Docking::dockNone:
  263. control->setUpdateLayout();
  264. break;
  265. }
  266. return true;
  267. }
  268. //-----------------------------------------------------------------------------
  269. bool GuiContainer::anchorControl( GuiControl *control, const Point2I &deltaParentExtent )
  270. {
  271. GuiContainer *container = dynamic_cast<GuiContainer*>( control );
  272. if( !control || !container )
  273. return false;
  274. // If we're docked, we don't anchor to anything
  275. if( (container->getDocking() & Docking::dockAny) || !(container->getDocking() & Docking::dockInvalid) )
  276. return false;
  277. if( deltaParentExtent.isZero() )
  278. return false;
  279. RectI oldRect = control->getBounds();
  280. RectI newRect = control->getBounds();
  281. F32 deltaBottom = mSizingOptions.mAnchorBottom ? (F32)deltaParentExtent.y : 0.0f;
  282. F32 deltaRight = mSizingOptions.mAnchorRight ? (F32)deltaParentExtent.x : 0.0f;
  283. F32 deltaLeft = mSizingOptions.mAnchorLeft ? 0.0f : (F32)deltaParentExtent.x;
  284. F32 deltaTop = mSizingOptions.mAnchorTop ? 0.0f : (F32)deltaParentExtent.y;
  285. // Apply Delta's to newRect
  286. newRect.point.x += (S32)deltaLeft;
  287. newRect.extent.x += (S32)(deltaRight - deltaLeft);
  288. newRect.point.y += (S32)deltaTop;
  289. newRect.extent.y += (S32)(deltaBottom - deltaTop);
  290. Point2I minExtent = control->getMinExtent();
  291. // Only resize if our minExtent is satisfied with it.
  292. if( !( newRect.extent.x >= minExtent.x && newRect.extent.y >= minExtent.y ) )
  293. return false;
  294. if( newRect.point == oldRect.point && newRect.extent == oldRect.extent )
  295. return false;
  296. // Finally Size the control
  297. control->resize( newRect.point, newRect.extent );
  298. // We made changes
  299. return true;
  300. }
  301. //-----------------------------------------------------------------------------
  302. void GuiContainer::onPreRender()
  303. {
  304. if( mUpdateLayout == updateNone )
  305. return;
  306. RectI clientRect = getClientRect();
  307. if( mUpdateLayout & updateSelf )
  308. layoutControls( clientRect );
  309. GuiContainer *parent = dynamic_cast<GuiContainer*>( getParent() );
  310. if( parent && ( mUpdateLayout & updateParent ) )
  311. parent->setUpdateLayout();
  312. // Always set AFTER layoutControls call to prevent recursive calling of layoutControls - JDD
  313. mUpdateLayout = updateNone;
  314. Parent::onPreRender();
  315. }
  316. //-----------------------------------------------------------------------------
  317. const RectI GuiContainer::getClientRect()
  318. {
  319. RectI resRect = RectI( Point2I(0,0), getExtent() );
  320. // Inset by padding
  321. mSizingOptions.mInternalPadding.insetRect( resRect );
  322. return resRect;
  323. }
  324. //-----------------------------------------------------------------------------
  325. void GuiContainer::setDocking( S32 docking )
  326. {
  327. mSizingOptions.mDocking = docking;
  328. setUpdateLayout( updateParent );
  329. }