guiGridCtrl.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. }
  63. //------------------------------------------------------------------------------
  64. void GuiGridCtrl::initPersistFields()
  65. {
  66. Parent::initPersistFields();
  67. addField("CellModeX", TypeEnum, Offset(mCellModeX, GuiGridCtrl), 1, &gCellModeTable);
  68. addField("CellModeY", TypeEnum, Offset(mCellModeY, GuiGridCtrl), 1, &gCellModeTable);
  69. addField("CellSizeX", TypeF32, Offset(mCellSizeX, GuiGridCtrl));
  70. addField("CellSizeY", TypeF32, Offset(mCellSizeY, GuiGridCtrl));
  71. addField("CellSpacingX", TypeF32, Offset(mCellSpacingX, GuiGridCtrl));
  72. addField("CellSpacingY", TypeF32, Offset(mCellSpacingY, GuiGridCtrl));
  73. addField("MaxColCount", TypeS32, Offset(mMaxColCount, GuiGridCtrl));
  74. addField("MaxRowCount", TypeS32, Offset(mMaxRowCount, GuiGridCtrl));
  75. addField("OrderMode", TypeEnum, Offset(mOrderMode, GuiGridCtrl), 1, &gOrderModeTable);
  76. addField("IsExtentDynamic", TypeBool, Offset(mIsExtentDynamic, GuiGridCtrl));
  77. }
  78. //------------------------------------------------------------------------------
  79. bool GuiGridCtrl::onWake()
  80. {
  81. if (!Parent::onWake())
  82. return false;
  83. return true;
  84. }
  85. //------------------------------------------------------------------------------
  86. void GuiGridCtrl::onSleep()
  87. {
  88. Parent::onSleep();
  89. }
  90. //------------------------------------------------------------------------------
  91. void GuiGridCtrl::inspectPostApply()
  92. {
  93. resize(getPosition(), getExtent());
  94. Parent::inspectPostApply();
  95. }
  96. //------------------------------------------------------------------------------
  97. void GuiGridCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  98. {
  99. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  100. getMax(mMinExtent.y, newExtent.y));
  101. //call set update both before and after
  102. setUpdate();
  103. RectI innerRect = getInnerRect(mBounds.point.Zero, actualNewExtent, NormalState, mProfile);
  104. if (!innerRect.isValidRect() && !mIsExtentDynamic)
  105. {
  106. return;
  107. }
  108. AdjustGrid(innerRect.extent);
  109. iterator i;
  110. U16 num = 0;
  111. for (i = begin(); i != end(); i++, num++)
  112. {
  113. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  114. ctrl->resize(getCellPosition(num, innerRect.extent), mCalcCellExt);
  115. }
  116. Point2I actualNewPosition = Point2I(newPosition);
  117. if(mIsExtentDynamic)
  118. {
  119. F32 h = mCeil((F32)size() / (F32)mCalcChainLength);
  120. if (mOrderMode == LRTB || mOrderMode == RLTB)
  121. {
  122. h = (h * mCalcCellExt.y) + ((h-1) * mCalcCellSpace.y);
  123. innerRect.extent.y = h;
  124. }
  125. else if (mOrderMode == TBLR || mOrderMode == BTLR)
  126. {
  127. h = (h * mCalcCellExt.x) + ((h - 1) * mCalcCellSpace.x);
  128. innerRect.extent.x = h;
  129. }
  130. else if (mOrderMode == LRBT || mOrderMode == RLBT)
  131. {
  132. h = (h * mCalcCellExt.y) + ((h - 1) * mCalcCellSpace.y);
  133. actualNewPosition.y += innerRect.extent.y - h;
  134. innerRect.extent.y = h;
  135. }
  136. else if (mOrderMode == TBRL || mOrderMode == BTRL)
  137. {
  138. h = (h * mCalcCellExt.x) + ((h - 1) * mCalcCellSpace.x);
  139. actualNewPosition.x += innerRect.extent.x - h;
  140. innerRect.extent.x = h;
  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. }
  150. //------------------------------------------------------------------------------
  151. Point2I GuiGridCtrl::getCellPosition(const U16 num, const Point2I &innerExtent)
  152. {
  153. Point2I result(0,0);
  154. U16 y = (U16)mFloor(num / mCalcChainLength);
  155. U16 x = (U16)(num % mCalcChainLength);
  156. F32 ChainCount = mCeil((F32)size() / (F32)mCalcChainLength);
  157. if (mOrderMode == LRTB)
  158. {
  159. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), y * (mCalcCellExt.y + mCalcCellSpace.y));
  160. }
  161. else if (mOrderMode == RLTB)
  162. {
  163. x = (mCalcChainLength - 1) - x;
  164. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  165. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, y * (mCalcCellExt.y + mCalcCellSpace.y));
  166. }
  167. else if (mOrderMode == TBLR)
  168. {
  169. result.set(y * (mCalcCellExt.x + mCalcCellSpace.x), x * (mCalcCellExt.y + mCalcCellSpace.y));
  170. }
  171. else if (mOrderMode == BTLR)
  172. {
  173. x = (mCalcChainLength - 1) - x;
  174. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  175. result.set(y * (mCalcCellExt.x + mCalcCellSpace.x), (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  176. }
  177. else if (mOrderMode == LRBT)
  178. {
  179. y = (ChainCount - 1) - y;
  180. result.set(x * (mCalcCellExt.x + mCalcCellSpace.x), y * (mCalcCellExt.y + mCalcCellSpace.y));
  181. }
  182. else if (mOrderMode == RLBT)
  183. {
  184. y = (ChainCount - 1) - y;
  185. x = (mCalcChainLength - 1) - x;
  186. S32 delta = innerExtent.x - ((mCalcChainLength * mCalcCellExt.x) + ((mCalcChainLength - 1) * mCalcCellSpace.x));
  187. result.set((x * (mCalcCellExt.x + mCalcCellSpace.x)) + delta, y * (mCalcCellExt.y + mCalcCellSpace.y));
  188. }
  189. else if (mOrderMode == TBRL)
  190. {
  191. y = (ChainCount - 1) - y;
  192. result.set(y * (mCalcCellExt.x + mCalcCellSpace.x), x * (mCalcCellExt.y + mCalcCellSpace.y));
  193. }
  194. else if (mOrderMode == BTRL)
  195. {
  196. y = (ChainCount - 1) - y;
  197. x = (mCalcChainLength - 1) - x;
  198. S32 delta = innerExtent.y - ((mCalcChainLength * mCalcCellExt.y) + ((mCalcChainLength - 1) * mCalcCellSpace.y));
  199. result.set(y * (mCalcCellExt.x + mCalcCellSpace.x), (x * (mCalcCellExt.y + mCalcCellSpace.y)) + delta);
  200. }
  201. return result;
  202. }
  203. //------------------------------------------------------------------------------
  204. void GuiGridCtrl::AdjustGrid(const Point2I& innerExtent)
  205. {
  206. Point2F cellExtX, cellExtY;
  207. if (mOrderMode == LRTB || mOrderMode == LRBT || mOrderMode == RLTB || mOrderMode == RLBT)
  208. {
  209. cellExtX = GetGridItemWidth(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  210. cellExtY = GetGridItemHeight(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  211. }
  212. else
  213. {
  214. cellExtY = GetGridItemWidth(innerExtent.y, mMaxRowCount, mCellSizeY, mCellSpacingY, mCellModeY);
  215. cellExtX = GetGridItemHeight(innerExtent.x, mMaxColCount, mCellSizeX, mCellSpacingX, mCellModeX);
  216. }
  217. mCalcCellExt.set(cellExtX.x, cellExtY.x);
  218. mCalcCellSpace.set(cellExtX.y, cellExtY.y);
  219. }
  220. Point2F GuiGridCtrl::GetGridItemWidth(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  221. {
  222. //The two values returned are the extent and spacing held in the x and y respectively
  223. if (cellMode == GuiGridCtrl::Percent)
  224. {
  225. mCalcChainLength = (U16)getMax((100 + spaceSize) / (itemSize + spaceSize), 1.f);
  226. }
  227. else
  228. {
  229. mCalcChainLength = (U16)getMax((totalArea + spaceSize) / (itemSize + spaceSize), 1.f);
  230. }
  231. if (maxChainLength > 0)
  232. {
  233. mCalcChainLength = (U16)getMin((S32)mCalcChainLength, maxChainLength);
  234. }
  235. if (cellMode == GuiGridCtrl::Absolute)
  236. {
  237. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  238. }
  239. else if (cellMode == GuiGridCtrl::Variable)
  240. {
  241. F32 remainder = totalArea - ((mCalcChainLength * itemSize) + ((mCalcChainLength - 1) * spaceSize));
  242. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  243. }
  244. else if (cellMode == GuiGridCtrl::Percent)
  245. {
  246. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  247. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  248. return Point2F(calcCellSize, calcSpaceSize);
  249. }
  250. return Point2F(1,1);
  251. }
  252. Point2F GuiGridCtrl::GetGridItemHeight(const S32 totalArea, const S32 maxChainLength, const F32 itemSize, const F32 spaceSize, const CellMode cellMode)
  253. {
  254. if (mIsExtentDynamic || cellMode == CellMode::Absolute)
  255. {
  256. return Point2F(mFloor(itemSize), mFloor(spaceSize));
  257. }
  258. else if (cellMode == CellMode::Variable)
  259. {
  260. U16 length = (U16)mClamp((totalArea + spaceSize) / (itemSize + spaceSize), 1, maxChainLength);
  261. F32 remainder = totalArea - ((length * itemSize) + ((length - 1) * spaceSize));
  262. return Point2F(mFloor(itemSize + (remainder / mCalcChainLength)), mFloor(spaceSize));
  263. }
  264. else if (cellMode == CellMode::Percent)
  265. {
  266. F32 calcCellSize = mFloor(getMin((F32)(totalArea * (itemSize / 100)), (F32)(totalArea)));
  267. F32 calcSpaceSize = mFloor(getMin((F32)(totalArea * (spaceSize / 100)), (F32)(totalArea)));
  268. return Point2F(calcCellSize, calcSpaceSize);
  269. }
  270. return Point2F(1, 1);
  271. }
  272. void GuiGridCtrl::onChildAdded(GuiControl *child)
  273. {
  274. resize(getPosition(), getExtent());
  275. }
  276. void GuiGridCtrl::onChildRemoved(SimObject *child)
  277. {
  278. resize(getPosition(), getExtent());
  279. }
  280. void GuiGridCtrl::setCellSize(F32 width, F32 height)
  281. {
  282. mCellSizeX = mAbs(width);
  283. mCellSizeY = mAbs(height);
  284. resize(getPosition(), getExtent());
  285. }
  286. void GuiGridCtrl::setCellSpacing(F32 x, F32 y)
  287. {
  288. mCellSpacingX = mAbs(x);
  289. mCellSpacingY = mAbs(y);
  290. resize(getPosition(), getExtent());
  291. }
  292. void GuiGridCtrl::setCellModeX(const CellMode mode)
  293. {
  294. // Sanity!
  295. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  296. mCellModeX = mode;
  297. resize(getPosition(), getExtent());
  298. }
  299. void GuiGridCtrl::setCellModeY(const CellMode mode)
  300. {
  301. // Sanity!
  302. AssertFatal(mode == Percent || mode == Variable || mode == Absolute, "Invalid cell mode.");
  303. mCellModeY = mode;
  304. resize(getPosition(), getExtent());
  305. }
  306. GuiGridCtrl::CellMode GuiGridCtrl::getCellModeEnum(const char* label)
  307. {
  308. // Search for Mnemonic.
  309. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  310. {
  311. if (dStricmp(cellModeEnums[i].label, label) == 0)
  312. return (CellMode)cellModeEnums[i].index;
  313. }
  314. // Warn.
  315. Con::warnf("GuiGridCtrl::getCellModeEnum() - Invalid cell mode of '%s'", label);
  316. return (CellMode)-1;
  317. }
  318. const char* GuiGridCtrl::getCellModeDescription(const GuiGridCtrl::CellMode mode)
  319. {
  320. // Search for Mnemonic.
  321. for (U32 i = 0; i < (sizeof(cellModeEnums) / sizeof(EnumTable::Enums)); i++)
  322. {
  323. if (cellModeEnums[i].index == mode)
  324. return cellModeEnums[i].label;
  325. }
  326. // Warn.
  327. Con::warnf("GuiGridCtrl::getCellModeDescription() - Invalid cell mode.");
  328. return StringTable->EmptyString;
  329. }
  330. const char* GuiGridCtrl::getOrderModeDescription(const GuiGridCtrl::OrderMode mode)
  331. {
  332. // Search for Mnemonic.
  333. for (U32 i = 0; i < (sizeof(orderModeEnums) / sizeof(EnumTable::Enums)); i++)
  334. {
  335. if (orderModeEnums[i].index == mode)
  336. return orderModeEnums[i].label;
  337. }
  338. // Warn.
  339. Con::warnf("GuiGridCtrl::getOrderModeDescription() - Invalid order mode.");
  340. return StringTable->EmptyString;
  341. }