guiTreeViewCtrl.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "gui/guiTreeViewCtrl.h"
  23. #include "graphics/dgl.h"
  24. #include "gui/guiDefaultControlRender.h"
  25. #include "gui/guiCanvas.h"
  26. #include "guiTreeViewCtrl_ScriptBinding.h"
  27. IMPLEMENT_CONOBJECT(GuiTreeViewCtrl);
  28. GuiTreeViewCtrl::GuiTreeViewCtrl()
  29. {
  30. mActive = true;
  31. mIsContainer = false;
  32. mIndentSize = 10;
  33. }
  34. GuiTreeViewCtrl::~GuiTreeViewCtrl()
  35. {
  36. }
  37. GuiTreeViewCtrl::TreeItem* GuiTreeViewCtrl::grabItemPtr(S32 index)
  38. {
  39. // Range Check
  40. if (index >= mItems.size() || index < 0)
  41. {
  42. Con::warnf("GuiListBoxCtrl::grabItemPtr - index out of range!");
  43. return nullptr;
  44. }
  45. // Grab our item
  46. return dynamic_cast<GuiTreeViewCtrl::TreeItem*>(mItems[index]);
  47. }
  48. void GuiTreeViewCtrl::initPersistFields()
  49. {
  50. Parent::initPersistFields();
  51. }
  52. void GuiTreeViewCtrl::onPreRender()
  53. {
  54. }
  55. void GuiTreeViewCtrl::onRender(Point2I offset, const RectI& updateRect)
  56. {
  57. RectI clip = dglGetClipRect();
  58. if (mFitParentWidth && (mBounds.extent.x != clip.extent.x || mItemSize.x != clip.extent.x))
  59. {
  60. mBounds.extent.x = clip.extent.x;
  61. mItemSize.x = clip.extent.x;
  62. }
  63. for (S32 i = 0, j = 0; i < mItems.size(); i++)
  64. {
  65. // Only render visible items
  66. if ((j + 1) * mItemSize.y + offset.y < updateRect.point.y)
  67. continue;
  68. // Break out once we're no longer in visible item range
  69. if (j * mItemSize.y + offset.y >= updateRect.point.y + updateRect.extent.y)
  70. break;
  71. RectI itemRect = RectI(offset.x, offset.y + (j * mItemSize.y), mItemSize.x, mItemSize.y);
  72. TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
  73. if(!treeItem || treeItem->isVisible)
  74. {
  75. // Render our item
  76. onRenderItem(itemRect, mItems[i]);
  77. j++;
  78. }
  79. }
  80. }
  81. void GuiTreeViewCtrl::onRenderItem(RectI& itemRect, LBItem* item)
  82. {
  83. TreeItem* treeItem = dynamic_cast<TreeItem*>(item);
  84. SimObject* obj = static_cast<SimObject*>(item->itemData);
  85. if (!treeItem)
  86. {
  87. Parent::onRenderItem(itemRect, item);
  88. return;
  89. }
  90. Point2I cursorPt = Point2I(0, 0);
  91. GuiCanvas* root = getRoot();
  92. if (root)
  93. {
  94. cursorPt = root->getCursorPos();
  95. }
  96. GuiControlState currentState = GuiControlState::NormalState;
  97. if (!mActive || !item->isActive)
  98. currentState = GuiControlState::DisabledState;
  99. else if (item->isSelected)
  100. currentState = GuiControlState::SelectedState;
  101. else if (itemRect.pointInRect(cursorPt))
  102. currentState = GuiControlState::HighlightState;
  103. RectI ctrlRect = applyMargins(itemRect.point, itemRect.extent, currentState, mProfile);
  104. if (!ctrlRect.isValidRect())
  105. {
  106. return;
  107. }
  108. renderUniversalRect(ctrlRect, mProfile, currentState);
  109. //Render Text
  110. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  111. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  112. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  113. // Indent by level
  114. contentRect.point.x += (treeItem->level * contentRect.extent.y);
  115. contentRect.extent.x -= (treeItem->level * contentRect.extent.y);
  116. // Render open/close triangle
  117. if(obj)
  118. {
  119. SimSet* setObj = dynamic_cast<SimSet*>(obj);
  120. GuiControl* guiCtrl = dynamic_cast<GuiControl*>(obj);
  121. bool showTriangle = (guiCtrl && guiCtrl->mIsContainer) || (!guiCtrl && setObj && setObj->size() > 0);
  122. if (showTriangle)
  123. {
  124. RectI drawArea = RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.y, contentRect.extent.y);
  125. treeItem->triangleArea.set(drawArea.point, drawArea.extent);
  126. ColorI color = mProfile->getFontColor(currentState);
  127. renderTriangleIcon(drawArea, color, treeItem->isOpen ? GuiDirection::Down : GuiDirection::Right, 8);
  128. }
  129. }
  130. contentRect.point.x += contentRect.extent.y;
  131. contentRect.extent.x -= contentRect.extent.y;
  132. renderText(contentRect.point, contentRect.extent, item->itemText, mProfile);
  133. }
  134. S32 GuiTreeViewCtrl::getHitIndex(const GuiEvent& event)
  135. {
  136. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  137. if (localPoint.y < 0)
  138. {
  139. return -1;
  140. }
  141. S32 slot = (S32)mFloor((F32)localPoint.y / (F32)mItemSize.y);
  142. for (S32 i = 0, j = 0; i < mItems.size(); i++)
  143. {
  144. TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
  145. if (treeItem && treeItem->isVisible)
  146. {
  147. if (j == slot)
  148. {
  149. return i;
  150. }
  151. j++;
  152. }
  153. }
  154. }
  155. void GuiTreeViewCtrl::handleItemClick(LBItem* hitItem, S32 hitIndex, const GuiEvent& event)
  156. {
  157. TreeItem* treeItem = dynamic_cast<TreeItem*>(hitItem);
  158. if (treeItem)
  159. {
  160. if (treeItem->triangleArea.pointInRect(event.mousePoint))
  161. {
  162. treeItem->isOpen = !treeItem->isOpen;
  163. setBranchesVisible(treeItem, treeItem->isOpen);
  164. return;
  165. }
  166. }
  167. Parent::handleItemClick(hitItem, hitIndex, event);
  168. }
  169. void GuiTreeViewCtrl::handleItemClick_ClickCallbacks(LBItem* hitItem, S32 hitIndex, const GuiEvent& event)
  170. {
  171. TreeItem* treeItem = dynamic_cast<TreeItem*>(hitItem);
  172. if (!treeItem)
  173. {
  174. Parent::handleItemClick_ClickCallbacks(hitItem, hitIndex, event);
  175. return;
  176. }
  177. SimObject* obj = static_cast<SimObject*>(treeItem->itemData);
  178. if (hitItem == mLastClickItem && event.mouseClickCount == 2)
  179. {
  180. if (caller->isMethod("onDoubleClick"))
  181. Con::executef(caller, 2, "onDoubleClick", Con::getIntArg(obj->getId()));
  182. }
  183. else if (caller->isMethod("onClick"))
  184. {
  185. Con::executef(caller, 2, "onClick", Con::getIntArg(obj->getId()));
  186. }
  187. }
  188. void GuiTreeViewCtrl::inspectObject(SimObject* obj)
  189. {
  190. clearItems();
  191. mRootObject = obj;
  192. StringTableEntry text = getObjectText(obj);
  193. S32 id = addItemWithID(text, obj->getId(), obj);
  194. TreeItem* treeItem = grabItemPtr(id);
  195. treeItem->level = 0;
  196. addBranches(treeItem, obj, 1);
  197. }
  198. void GuiTreeViewCtrl::addBranches(TreeItem* treeItem, SimObject* obj, U16 level)
  199. {
  200. SimSet* setObj = dynamic_cast<SimSet*>(obj);
  201. if(setObj)
  202. {
  203. for(auto sub : *setObj)
  204. {
  205. StringTableEntry text = getObjectText(sub);
  206. S32 id = addItemWithID(text, sub->getId(), sub);
  207. TreeItem* branch = grabItemPtr(id);
  208. branch->level = level;
  209. branch->trunk = treeItem;
  210. treeItem->branchList.push_back(branch);
  211. addBranches(branch, sub, level + 1);
  212. }
  213. }
  214. }
  215. GuiListBoxCtrl::LBItem* GuiTreeViewCtrl::createItem()
  216. {
  217. LBItem* newItem = new TreeItem;
  218. if (!newItem)
  219. {
  220. return nullptr;
  221. }
  222. return newItem;
  223. }
  224. void GuiTreeViewCtrl::refreshTree()
  225. {
  226. if (mRootObject)
  227. {
  228. inspectObject(mRootObject);
  229. }
  230. }
  231. StringTableEntry GuiTreeViewCtrl::getObjectText(SimObject* obj)
  232. {
  233. char buffer[1024];
  234. if (obj)
  235. {
  236. const char* pObjName = obj->getName();
  237. const char* pInternalName = obj->getInternalName();
  238. if (pObjName != NULL)
  239. dSprintf(buffer, sizeof(buffer), "%d: %s - %s", obj->getId(), obj->getClassName(), pObjName);
  240. else if (pInternalName != NULL)
  241. dSprintf(buffer, sizeof(buffer), "%d: %s [%s]", obj->getId(), obj->getClassName(), pInternalName);
  242. else
  243. dSprintf(buffer, sizeof(buffer), "%d: %s", obj->getId(), obj->getClassName());
  244. }
  245. return StringTable->insert(buffer, true);
  246. }
  247. void GuiTreeViewCtrl::calculateHeaderExtent()
  248. {
  249. if(mProfile)
  250. {
  251. GuiBorderProfile* topProfile = mProfile->getTopBorder();
  252. GuiBorderProfile* bottomProfile = mProfile->getBottomBorder();
  253. S32 topSize = (topProfile) ? topProfile->getMargin(NormalState) + topProfile->getBorder(NormalState) + topProfile->getPadding(NormalState) : 0;
  254. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(NormalState) + bottomProfile->getBorder(NormalState) + bottomProfile->getPadding(NormalState) : 0;
  255. GFont* font = mProfile->getFont();
  256. S32 fontSize = (font) ? font->getHeight() : 0;
  257. S32 height = topSize + bottomSize + fontSize;
  258. S32 width = mBounds.extent.x;
  259. }
  260. }
  261. void GuiTreeViewCtrl::setBranchesVisible(TreeItem* treeItem, bool isVisible)
  262. {
  263. for (auto branch : treeItem->branchList)
  264. {
  265. if(!isVisible || branch->isOpen)
  266. {
  267. setBranchesVisible(branch, isVisible);
  268. }
  269. branch->isVisible = isVisible;
  270. }
  271. }