guiGridCtrl.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 "gui/containers/guiGridCtrl.h"
  25. #include "guiGridCtrl_ScriptBinding.h"
  26. static EnumTable::Enums cellModeEnums[] =
  27. {
  28. { GuiGridCtrl::Absolute, "absolute" },
  29. { GuiGridCtrl::Variable, "variable" },
  30. { GuiGridCtrl::Percent, "percent" }
  31. };
  32. static EnumTable gCellModeTable(3, &cellModeEnums[0]);
  33. static EnumTable::Enums orderModeEnums[] =
  34. {
  35. { GuiGridCtrl::LRTB, "lrtb" },
  36. { GuiGridCtrl::RLTB, "rltb" },
  37. { GuiGridCtrl::TBLR, "tblr" },
  38. { GuiGridCtrl::TBRL, "tbrl" },
  39. { GuiGridCtrl::LRBT, "lrbt" },
  40. { GuiGridCtrl::RLBT, "rlbt" },
  41. { GuiGridCtrl::BTLR, "btlr" },
  42. { GuiGridCtrl::BTRL, "btrl" }
  43. };
  44. static EnumTable gOrderModeTable(8, &orderModeEnums[0]);
  45. //------------------------------------------------------------------------------
  46. IMPLEMENT_CONOBJECT(GuiGridCtrl);
  47. //------------------------------------------------------------------------------
  48. GuiGridCtrl::GuiGridCtrl()
  49. {
  50. mIsContainer = true;
  51. mCellModeX = CellMode::Absolute;
  52. mCellModeY = CellMode::Absolute;
  53. mCellSizeX = 20;
  54. mCellSizeY = 20;
  55. mCellSpacingX = 0;
  56. mCellSpacingY = 0;
  57. mMaxColCount = 0;
  58. mMaxRowCount = 0;
  59. mOrderMode = OrderMode::LRTB;
  60. mIsExtentDynamic = true;
  61. setField("profile", "GuiDefaultProfile");
  62. mResizeGuard = false;
  63. }
  64. //------------------------------------------------------------------------------
  65. void GuiGridCtrl::initPersistFields()
  66. {
  67. Parent::initPersistFields();
  68. addField("CellModeX", TypeEnum, Offset(mCellModeX, GuiGridCtrl), 1, &gCellModeTable);
  69. addField("CellModeY", TypeEnum, Offset(mCellModeY, GuiGridCtrl), 1, &gCellModeTable);
  70. addField("CellSizeX", TypeF32, Offset(mCellSizeX, GuiGridCtrl));
  71. addField("CellSizeY", TypeF32, Offset(mCellSizeY, GuiGridCtrl));
  72. addField("CellSpacingX", TypeF32, Offset(mCellSpacingX, GuiGridCtrl));
  73. addField("CellSpacingY", TypeF32, Offset(mCellSpacingY, GuiGridCtrl));
  74. addField("MaxColCount", TypeS32, Offset(mMaxColCount, GuiGridCtrl));
  75. addField("MaxRowCount", TypeS32, Offset(mMaxRowCount, GuiGridCtrl));
  76. addField("OrderMode", TypeEnum, Offset(mOrderMode, GuiGridCtrl), 1, &gOrderModeTable);
  77. addField("IsExtentDynamic", TypeBool, Offset(mIsExtentDynamic, GuiGridCtrl));
  78. }
  79. //------------------------------------------------------------------------------
  80. bool GuiGridCtrl::onWake()
  81. {
  82. if (!Parent::onWake())
  83. return false;
  84. return true;
  85. }
  86. //------------------------------------------------------------------------------
  87. void GuiGridCtrl::onSleep()
  88. {
  89. Parent::onSleep();
  90. }
  91. //------------------------------------------------------------------------------
  92. void GuiGridCtrl::inspectPostApply()
  93. {
  94. resize(getPosition(), getExtent());
  95. Parent::inspectPostApply();
  96. }
  97. //------------------------------------------------------------------------------
  98. void GuiGridCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  99. {
  100. if (!mResizeGuard)//Prevent circular resizing
  101. {
  102. mResizeGuard = true;
  103. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  104. getMax(mMinExtent.y, newExtent.y));
  105. //call set update both before and after
  106. setUpdate();
  107. Point2I zero = mBounds.point.Zero;
  108. RectI innerRect = getInnerRect(zero, actualNewExtent, NormalState, mProfile);
  109. if (!innerRect.isValidRect() && !mIsExtentDynamic)
  110. {
  111. return;
  112. }
  113. AdjustGrid(innerRect.extent);
  114. iterator i;
  115. U16 cellNumber = 0;
  116. mChainNumber = 0;
  117. mRunningChainHeight = 0;
  118. mCurrentChainHeight = 0;
  119. for (i = begin(); i != end(); i++, cellNumber++)
  120. {
  121. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  122. Point2I cellPos = getCellPosition(cellNumber, innerRect.extent, ctrl);
  123. Point2I cellExt = getCellExtent(ctrl);
  124. ctrl->resize(cellPos, cellExt);
  125. }
  126. mRunningChainHeight += mCurrentChainHeight;
  127. Point2I actualNewPosition = Point2I(newPosition);
  128. if(mIsExtentDynamic)
  129. {
  130. if(isEditMode())
  131. {
  132. mRunningChainHeight += 20;
  133. }
  134. if (IsVertical())
  135. {
  136. innerRect.extent.y = mRunningChainHeight;
  137. }
  138. else
  139. {
  140. innerRect.extent.x = mRunningChainHeight;
  141. }
  142. actualNewExtent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  143. }
  144. mBounds.set(actualNewPosition, actualNewExtent);
  145. GuiControl *parent = getParent();
  146. if (parent)
  147. parent->childResized(this);
  148. setUpdate();
  149. mResizeGuard = false;
  150. }
  151. }
  152. //------------------------------------------------------------------------------
  153. Point2I GuiGridCtrl::getCellPosition(const U16 cellNumber, const Point2I &innerExtent, GuiControl *ctrl)
  154. {
  155. if(mCalcChainLength == 0)
  156. return Point2I(0, 0);
  157. Point2I result(0,0);
  158. U16 y = (U16)mFloor(cellNumber / mCalcChainLength);
  159. U16 x = (U16)(cellNumber % mCalcChainLength);
  160. F32 ChainCount = mCeil((F32)size() / (F32)mCalcChainLength);
  161. if (y != mChainNumber)
  162. {
  163. mRunningChainHeight += (mCurrentChainHeight + (U32)(IsVertical() ? mCalcCellSpace.y : mCalcCellSpace.x));
  164. mCurrentChainHeight = 0;
  165. mChainNumber = y;
  166. }
  167. if (mOrderMode == LRTB)
  168. {
  169. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), mRunningChainHeight);
  170. }
  171. else if (mOrderMode == RLTB)
  172. {
  173. x = (mCalcChainLength - 1) - x;
  174. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  175. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, mRunningChainHeight);
  176. }
  177. else if (mOrderMode == TBLR)
  178. {
  179. result.set(mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  180. }
  181. else if (mOrderMode == BTLR)
  182. {
  183. x = (mCalcChainLength - 1) - x;
  184. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  185. result.set(mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  186. }
  187. else if (mOrderMode == LRBT)
  188. {
  189. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  190. }
  191. else if (mOrderMode == RLBT)
  192. {
  193. x = (mCalcChainLength - 1) - x;
  194. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  195. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, (innerExtent.y - (HasVariableChainHeight() ? ctrl->getExtent().y : mCalcCellExt.y)) - (S32)mRunningChainHeight);
  196. }
  197. else if (mOrderMode == TBRL)
  198. {
  199. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, x * (mCalcCellExt.y + mCalcCellSpace.y));
  200. }
  201. else if (mOrderMode == BTRL)
  202. {
  203. x = (mCalcChainLength - 1) - x;
  204. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  205. result.set((innerExtent.x - (HasVariableChainHeight() ? ctrl->getExtent().x : mCalcCellExt.x)) - (S32)mRunningChainHeight, (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  206. }
  207. return result;
  208. }
  209. Point2I GuiGridCtrl::getCellExtent(GuiControl *ctrl)
  210. {
  211. if (!HasVariableChainHeight())
  212. {
  213. mCurrentChainHeight = (U32)(IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  214. return mCalcCellExt;
  215. }
  216. S32 CellHeight = getMax(IsVertical() ? ctrl->getExtent().y : ctrl->getExtent().x, IsVertical() ? mCalcCellExt.y : mCalcCellExt.x);
  217. mCurrentChainHeight = getMax(mCurrentChainHeight, (U32)CellHeight);
  218. return IsVertical() ? Point2I(mCalcCellExt.x, CellHeight) : Point2I(CellHeight, mCalcCellExt.y);
  219. }
  220. //------------------------------------------------------------------------------
  221. void GuiGridCtrl::AdjustGrid(const Point2I& innerExtent)
  222. {
  223. Point2F cellExtX, cellExtY;
  224. if (IsVertical())
  225. {
  226. cellExtX = GetGridItemWidth(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  227. cellExtY = GetGridItemHeight(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  228. }
  229. else
  230. {
  231. cellExtY = GetGridItemWidth(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  232. cellExtX = GetGridItemHeight(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  233. }
  234. mCalcCellExt.set(cellExtX.x, cellExtY.x);
  235. mCalcCellSpace.set(cellExtX.y, cellExtY.y);
  236. }
  237. Point2F GuiGridCtrl::GetGridItemWidth(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  238. {
  239. //The two values returned are the extent and spacing held in the x and y respectively
  240. if (cellMode == GuiGridCtrl::Percent)
  241. {
  242. mCalcChainLength = (U16)getMax((100 + spaceSize) / (itemSize + spaceSize), 1.f);
  243. }
  244. else
  245. {
  246. mCalcChainLength = (U16)getMax((totalArea + spaceSize) / (itemSize + spaceSize), 1.f);
  247. }
  248. if (maxChainLength > 0)
  249. {
  250. mCalcChainLength = (U16)getMin((S32)mCalcChainLength, maxChainLength);
  251. }
  252. if (cellMode == GuiGridCtrl::Absolute)
  253. {
  254. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  255. }
  256. else if (cellMode == GuiGridCtrl::Variable)
  257. {
  258. F32 remainder = totalArea - ((mCalcChainLength * itemSize) + ((mCalcChainLength - 1) * spaceSize));
  259. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  260. }
  261. else if (cellMode == GuiGridCtrl::Percent)
  262. {
  263. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  264. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  265. return Point2F(calcCellSize, calcSpaceSize);
  266. }
  267. return Point2F(1,1);
  268. }
  269. Point2F GuiGridCtrl::GetGridItemHeight(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  270. {
  271. if (mIsExtentDynamic || cellMode == CellMode::Absolute)
  272. {
  273. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  274. }
  275. else if (cellMode == CellMode::Variable)
  276. {
  277. U16 length = (U16)mClamp((totalArea + spaceSize) / (itemSize + spaceSize), 1, maxChainLength);
  278. F32 remainder = totalArea - ((length * itemSize) + ((length - 1) * spaceSize));
  279. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  280. }
  281. else if (cellMode == CellMode::Percent)
  282. {
  283. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  284. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  285. return Point2F(calcCellSize, calcSpaceSize);
  286. }
  287. return Point2F(1, 1);
  288. }
  289. void GuiGridCtrl::onChildAdded(GuiControl* child)
  290. {
  291. //Ensure the child isn't positioned to the center
  292. if (child->getHorizSizing() == horizResizeCenter)
  293. {
  294. child->setHorizSizing(horizResizeLeft);
  295. }
  296. if (child->getVertSizing() == vertResizeCenter)
  297. {
  298. child->setVertSizing(vertResizeTop);
  299. }
  300. resize(getPosition(), getExtent());
  301. Parent::onChildAdded(child);
  302. }
  303. void GuiGridCtrl::onChildRemoved(GuiControl* child)
  304. {
  305. resize(getPosition(), getExtent());
  306. }
  307. void GuiGridCtrl::childResized(GuiControl* child)
  308. {
  309. resize(getPosition(), getExtent());
  310. Parent::childResized(child);
  311. }
  312. void GuiGridCtrl::childMoved(GuiControl* child)
  313. {
  314. resize(getPosition(), getExtent());
  315. Parent::childMoved(child);
  316. }
  317. void GuiGridCtrl::childrenReordered()
  318. {
  319. resize(getPosition(), getExtent());
  320. Parent::childrenReordered();
  321. }
  322. void GuiGridCtrl::setCellSize(F32 width, F32 height)
  323. {
  324. mCellSizeX = mAbs(width);
  325. mCellSizeY = mAbs(height);
  326. resize(getPosition(), getExtent());
  327. }
  328. void GuiGridCtrl::setCellSpacing(F32 x, F32 y)
  329. {
  330. mCellSpacingX = mAbs(x);
  331. mCellSpacingY = mAbs(y);
  332. resize(getPosition(), getExtent());
  333. }
  334. void GuiGridCtrl::setCellModeX(const CellMode mode)
  335. {
  336. // Sanity!
  337. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  338. mCellModeX = mode;
  339. resize(getPosition(), getExtent());
  340. }
  341. void GuiGridCtrl::setCellModeY(const CellMode mode)
  342. {
  343. // Sanity!
  344. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  345. mCellModeY = mode;
  346. resize(getPosition(), getExtent());
  347. }
  348. GuiGridCtrl::CellMode GuiGridCtrl::getCellModeEnum(const char* label)
  349. {
  350. // Search for Mnemonic.
  351. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  352. {
  353. if (dStricmp(cellModeEnums[i].label, label) == 0)
  354. return (CellMode)cellModeEnums[i].index;
  355. }
  356. // Warn.
  357. Con::warnf("GuiGridCtrl::getCellModeEnum() - Invalid cell mode of '%s'", label);
  358. return (CellMode)-1;
  359. }
  360. const char* GuiGridCtrl::getCellModeDescription(const GuiGridCtrl::CellMode mode)
  361. {
  362. // Search for Mnemonic.
  363. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  364. {
  365. if (cellModeEnums[i].index == mode)
  366. return cellModeEnums[i].label;
  367. }
  368. // Warn.
  369. Con::warnf("GuiGridCtrl::getCellModeDescription() - Invalid cell mode.");
  370. return StringTable->EmptyString;
  371. }
  372. const char* GuiGridCtrl::getOrderModeDescription(const GuiGridCtrl::OrderMode mode)
  373. {
  374. // Search for Mnemonic.
  375. for (U32 i = 0; i < (sizeof(orderModeEnums) / sizeof(EnumTable::Enums)); i++)
  376. {
  377. if (orderModeEnums[i].index == mode)
  378. return orderModeEnums[i].label;
  379. }
  380. // Warn.
  381. Con::warnf("GuiGridCtrl::getOrderModeDescription() - Invalid order mode.");
  382. return StringTable->EmptyString;
  383. }