guiChainCtrl.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/guiChainCtrl.h"
  25. #include "gui/editor/guiEditCtrl.h"
  26. #include "guiChainCtrl_ScriptBinding.h"
  27. IMPLEMENT_CONOBJECT(GuiChainCtrl);
  28. //------------------------------------------------------------------------------
  29. GuiChainCtrl::GuiChainCtrl()
  30. {
  31. mChildSpacing = 0;
  32. mIsVertical = true;
  33. mPrevIsVertical = true;
  34. mBounds.extent.set(140, mEditOpenSpace);
  35. mResizeGuard = false;
  36. }
  37. //------------------------------------------------------------------------------
  38. void GuiChainCtrl::initPersistFields()
  39. {
  40. Parent::initPersistFields();
  41. addGroup("Gui Chain Settings");
  42. addField("ChildSpacing", TypeS32, Offset(mChildSpacing, GuiChainCtrl));
  43. addField("IsVertical", TypeBool, Offset(mIsVertical, GuiChainCtrl));
  44. endGroup("Gui Chain Settings");
  45. }
  46. //------------------------------------------------------------------------------
  47. void GuiChainCtrl::inspectPreApply()
  48. {
  49. mPrevIsVertical = mIsVertical;
  50. Parent::inspectPreApply();
  51. }
  52. void GuiChainCtrl::inspectPostApply()
  53. {
  54. if (mPrevIsVertical != mIsVertical && isEditMode())
  55. {
  56. //Since we're in the editor, we'll swap x and y, then calculateExtent will fix one of them.
  57. iterator i;
  58. for (i = begin(); i != end(); i++)
  59. {
  60. GuiControl* ctrl = static_cast<GuiControl*>(*i);
  61. if (ctrl->isVisible())
  62. {
  63. S32 temp = ctrl->mBounds.point.x;
  64. ctrl->mBounds.point.x = ctrl->mBounds.point.y;
  65. ctrl->mBounds.point.y = temp;
  66. }
  67. }
  68. }
  69. calculateExtent();
  70. Parent::inspectPostApply();
  71. }
  72. //------------------------------------------------------------------------------
  73. void GuiChainCtrl::childResized(GuiControl *child)
  74. {
  75. calculateExtent(isEditMode());
  76. Parent::childResized(child);
  77. }
  78. void GuiChainCtrl::childMoved(GuiControl* child)
  79. {
  80. calculateExtent(isEditMode());
  81. Parent::childMoved(child);
  82. }
  83. void GuiChainCtrl::childrenReordered()
  84. {
  85. calculateExtent(isEditMode());
  86. Parent::childrenReordered();
  87. }
  88. void GuiChainCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  89. {
  90. if (!mResizeGuard)
  91. {
  92. mResizeGuard = true;
  93. Point2I actualNewExtent = newExtent;
  94. if (mIsVertical)
  95. {
  96. actualNewExtent.y = getExtent().y;
  97. }
  98. else
  99. {
  100. actualNewExtent.x = getExtent().x;
  101. }
  102. if(newPosition != getPosition() || actualNewExtent != getExtent())
  103. {
  104. Parent::resize(newPosition, actualNewExtent);
  105. }
  106. mResizeGuard = false;
  107. }
  108. }
  109. void GuiChainCtrl::onChildAdded(GuiControl *child)
  110. {
  111. //Ensure the child isn't positioned to the center
  112. if (child->getHorizSizing() == horizResizeCenter && !mIsVertical)
  113. {
  114. child->setHorizSizing(horizResizeLeft);
  115. }
  116. if (child->getVertSizing() == vertResizeCenter && mIsVertical)
  117. {
  118. child->setVertSizing(vertResizeTop);
  119. }
  120. if (isEditMode())
  121. {
  122. child->mBounds.point = Point2I::Zero;
  123. }
  124. Parent::onChildAdded(child);
  125. calculateExtent();
  126. }
  127. void GuiChainCtrl::onChildRemoved(SimObject *child)
  128. {
  129. calculateExtent();
  130. }
  131. void GuiChainCtrl::calculateExtent(bool holdLength)
  132. {
  133. Point2I offset = Point2I::Zero;
  134. RectI innerRect = getInnerRect(offset);
  135. S32 length = positionChildren(innerRect);
  136. S32 oldLength = mIsVertical ? innerRect.extent.y : innerRect.extent.x;
  137. if (isEditMode())
  138. {
  139. length += mEditOpenSpace;//This gives some space to add new controls.
  140. }
  141. if (holdLength && oldLength > length)
  142. {
  143. length = oldLength;
  144. }
  145. if (!mIsVertical)
  146. {
  147. innerRect.extent.x = length;
  148. }
  149. else
  150. {
  151. innerRect.extent.y = length;
  152. }
  153. //call set update both before and after
  154. setUpdate();
  155. Point2I oldExtent = getExtent();
  156. mBounds.extent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  157. GuiControl *parent = getParent();
  158. if(oldExtent != getExtent() && parent)
  159. {
  160. parent->childResized(this);
  161. }
  162. setUpdate();
  163. }
  164. S32 GuiChainCtrl::positionChildren(RectI &innerRect)
  165. {
  166. iterator i;
  167. S32 length = 0;
  168. for (i = begin(); i != end(); i++)
  169. {
  170. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  171. if (ctrl->isVisible() || smDesignTime)
  172. {
  173. if (length != 0)
  174. {
  175. length += mChildSpacing;
  176. }
  177. Point2I childPos = ctrl->getPosition();
  178. if (!mIsVertical)
  179. {
  180. childPos.x = length;
  181. }
  182. else
  183. {
  184. childPos.y = length;
  185. }
  186. ctrl->mBounds.point = childPos;
  187. length += mIsVertical ? ctrl->getExtent().y : ctrl->getExtent().x;
  188. }
  189. }
  190. return length;
  191. }
  192. void GuiChainCtrl::onRender(Point2I offset, const RectI& updateRect)
  193. {
  194. Parent::onRender(offset, updateRect);
  195. if (isEditMode())
  196. {
  197. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  198. const GuiControl* addSet = edit->getAddSet();
  199. SimSet& selectSet = edit->getSelectedSet();
  200. if(this == addSet || selectSet.isMember(this))
  201. {
  202. ColorI fill = edit->getEditorColor();
  203. fill.alpha = 100;
  204. RectI rect;
  205. if (mIsVertical)
  206. {
  207. rect = RectI(0, mBounds.extent.y - mEditOpenSpace, mBounds.extent.x, mEditOpenSpace);
  208. }
  209. else
  210. {
  211. rect = RectI(mBounds.extent.x - mEditOpenSpace, 0, mEditOpenSpace, mBounds.extent.y);
  212. }
  213. rect.point = localToGlobalCoord(rect.point);
  214. dglDrawRectFill(rect, fill);
  215. dglSetBitmapModulation(getFontColor(edit->mProfile, NormalState));
  216. F32 tempAdjust = mFontSizeAdjust;
  217. mFontSizeAdjust = 1.5f;
  218. renderText(rect.point, rect.extent, "+", edit->mProfile);
  219. mFontSizeAdjust = tempAdjust;
  220. }
  221. }
  222. }