guiTreeViewCtrl.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. mMultipleSelections = true;
  34. mTouchPoint = Point2I::Zero;
  35. mDragActive = false;
  36. mDragIndex = 0;
  37. mIsDragLegal = false;
  38. }
  39. GuiTreeViewCtrl::~GuiTreeViewCtrl()
  40. {
  41. }
  42. GuiTreeViewCtrl::TreeItem* GuiTreeViewCtrl::grabItemPtr(S32 index)
  43. {
  44. // Range Check
  45. if (index >= mItems.size() || index < 0)
  46. {
  47. Con::warnf("GuiTreeViewCtrl::grabItemPtr - index out of range!");
  48. return nullptr;
  49. }
  50. // Grab our item
  51. TreeItem* treeItem = dynamic_cast<GuiTreeViewCtrl::TreeItem*>(mItems[index]);
  52. return treeItem;
  53. }
  54. void GuiTreeViewCtrl::initPersistFields()
  55. {
  56. Parent::initPersistFields();
  57. }
  58. void GuiTreeViewCtrl::onTouchDown(const GuiEvent& event)
  59. {
  60. mTouchPoint == event.mousePoint;
  61. Parent::onTouchDown(event);
  62. }
  63. void GuiTreeViewCtrl::onTouchDragged(const GuiEvent& event)
  64. {
  65. mDragActive = false;
  66. if (!mActive || !mVisible)
  67. return;
  68. S32 hitIndex = getHitIndex(event);
  69. if (hitIndex >= mItems.size() || hitIndex == -1)
  70. return;
  71. LBItem* hitItem = mItems[hitIndex];
  72. if (hitItem == NULL || !hitItem->isActive)
  73. return;
  74. if (mAbs(mTouchPoint.x - event.mousePoint.x) > 2 || mAbs(mTouchPoint.y - event.mousePoint.y) > 2)
  75. {
  76. mDragActive = true;
  77. }
  78. }
  79. void GuiTreeViewCtrl::onTouchUp(const GuiEvent& event)
  80. {
  81. if (mDragActive && mIsDragLegal)
  82. {
  83. TreeItem* dragItem = grabItemPtr(mDragIndex);
  84. if (mReorderMethod == ReorderMethod::Below && dragItem->isOpen)
  85. {
  86. mReorderMethod = ReorderMethod::Insert;
  87. }
  88. SimGroup* target = static_cast<SimGroup*>(mReorderMethod == ReorderMethod::Insert ? dragItem->itemData : dragItem->trunk->itemData);
  89. if (!target)
  90. {
  91. Con::warnf("GuiTreeViewCtrl::onTouchUp - attempted to drag selection into an object that is not a SimGroup");
  92. return;
  93. }
  94. vector<SimObject*> objectAboveTargetList = vector<SimObject*>();
  95. if (mReorderMethod != ReorderMethod::Insert)
  96. {
  97. S32 index = mReorderMethod == ReorderMethod::Below ? mDragIndex : mDragIndex - 1;
  98. TreeItem* checkItem = grabItemPtr(index);
  99. while (checkItem->level == dragItem->level)
  100. {
  101. if(!checkItem->isSelected)
  102. {
  103. SimObject* obj = static_cast<SimObject*>(checkItem->itemData);
  104. objectAboveTargetList.push_back(obj);
  105. }
  106. index = index - 1;
  107. checkItem = grabItemPtr(index);
  108. }
  109. }
  110. else
  111. {
  112. dragItem->isOpen = true;
  113. }
  114. for (S32 i = mItems.size() - 1; i >= 0; i--)
  115. {
  116. TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
  117. if(treeItem && treeItem->isSelected)
  118. {
  119. SimObject* obj = static_cast<SimObject*>(treeItem->itemData);
  120. if(obj)
  121. {
  122. target->addObject(obj);
  123. target->bringObjectToFront(obj);
  124. }
  125. }
  126. }
  127. for (auto obj : objectAboveTargetList)
  128. {
  129. SimGroup* group = obj->getGroup();
  130. group->bringObjectToFront(obj);
  131. }
  132. GuiControl* control = static_cast<GuiControl*>(mReorderMethod != ReorderMethod::Insert ? dragItem->trunk->itemData : dragItem->itemData);
  133. if (control)
  134. {
  135. control->childrenReordered();
  136. }
  137. refreshTree();
  138. }
  139. mDragActive = false;
  140. Parent::onTouchUp(event);
  141. }
  142. void GuiTreeViewCtrl::onPreRender()
  143. {
  144. }
  145. void GuiTreeViewCtrl::onRender(Point2I offset, const RectI& updateRect)
  146. {
  147. RectI clip = dglGetClipRect();
  148. if (mFitParentWidth && (mBounds.extent.x != clip.extent.x || mItemSize.x != clip.extent.x))
  149. {
  150. mBounds.extent.x = clip.extent.x;
  151. mItemSize.x = clip.extent.x;
  152. }
  153. RectI dragRect;
  154. for (S32 i = 0, j = 0; i < mItems.size(); i++)
  155. {
  156. // Only render visible items
  157. if ((j + 1) * mItemSize.y + offset.y < updateRect.point.y)
  158. continue;
  159. // Break out once we're no longer in visible item range
  160. if (j * mItemSize.y + offset.y >= updateRect.point.y + updateRect.extent.y)
  161. break;
  162. RectI itemRect = RectI(offset.x, offset.y + (j * mItemSize.y), mItemSize.x, mItemSize.y);
  163. TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
  164. if(!treeItem || treeItem->isVisible)
  165. {
  166. // Render our item
  167. onRenderItem(itemRect, mItems[i]);
  168. if (mDragActive && j == mDragIndex)
  169. {
  170. dragRect = RectI(itemRect);
  171. }
  172. j++;
  173. }
  174. }
  175. if(mDragActive && mIsDragLegal)
  176. {
  177. onRenderDragLine(dragRect);
  178. }
  179. }
  180. void GuiTreeViewCtrl::onRenderItem(RectI& itemRect, LBItem* item)
  181. {
  182. TreeItem* treeItem = dynamic_cast<TreeItem*>(item);
  183. SimObject* obj = static_cast<SimObject*>(item->itemData);
  184. if (!treeItem)
  185. {
  186. Parent::onRenderItem(itemRect, item);
  187. return;
  188. }
  189. Point2I cursorPt = Point2I(0, 0);
  190. GuiCanvas* root = getRoot();
  191. if (root)
  192. {
  193. cursorPt = root->getCursorPos();
  194. }
  195. GuiControlState currentState = GuiControlState::NormalState;
  196. if (!mActive || !item->isActive)
  197. currentState = GuiControlState::DisabledState;
  198. else if (item->isSelected)
  199. currentState = GuiControlState::SelectedState;
  200. else if (itemRect.pointInRect(cursorPt))
  201. currentState = GuiControlState::HighlightState;
  202. RectI ctrlRect = applyMargins(itemRect.point, itemRect.extent, currentState, mProfile);
  203. if (!ctrlRect.isValidRect())
  204. {
  205. return;
  206. }
  207. renderUniversalRect(ctrlRect, mProfile, currentState);
  208. //Render Text
  209. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  210. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  211. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  212. // Indent by level
  213. contentRect.point.x += (treeItem->level * contentRect.extent.y);
  214. contentRect.extent.x -= (treeItem->level * contentRect.extent.y);
  215. // Render open/close triangle
  216. if(obj)
  217. {
  218. SimGroup* setObj = dynamic_cast<SimGroup*>(obj);
  219. GuiControl* guiCtrl = dynamic_cast<GuiControl*>(obj);
  220. bool showTriangle = (guiCtrl && guiCtrl->mIsContainer) || (!guiCtrl && setObj && setObj->size() > 0);
  221. if (showTriangle)
  222. {
  223. RectI drawArea = RectI(contentRect.point.x, contentRect.point.y, contentRect.extent.y, contentRect.extent.y);
  224. treeItem->triangleArea.set(drawArea.point, drawArea.extent);
  225. ColorI color = mProfile->getFontColor(currentState);
  226. renderTriangleIcon(drawArea, color, treeItem->isOpen ? GuiDirection::Down : GuiDirection::Right, 8);
  227. }
  228. }
  229. contentRect.point.x += contentRect.extent.y;
  230. contentRect.extent.x -= contentRect.extent.y;
  231. renderText(contentRect.point, contentRect.extent, item->itemText, mProfile);
  232. }
  233. void GuiTreeViewCtrl::onRenderDragLine(RectI& itemRect)
  234. {
  235. ColorI colorW = ColorI(255,255,255,150);
  236. ColorI colorB = ColorI(0, 0, 0, 150);
  237. RectI rect = RectI(itemRect);
  238. rect.inset(4, 2);
  239. if (mReorderMethod == ReorderMethod::Insert)
  240. {
  241. dglDrawRect(itemRect, colorW);
  242. itemRect.inset(-1, -1);
  243. dglDrawRect(itemRect, colorB);
  244. }
  245. else if (mReorderMethod == ReorderMethod::Above)
  246. {
  247. itemRect.extent.y = 4;
  248. dglDrawRect(itemRect, colorW);
  249. itemRect.inset(-1, -1);
  250. dglDrawRect(itemRect, colorB);
  251. }
  252. else if(mReorderMethod == ReorderMethod::Below)
  253. {
  254. itemRect.point.y += mItemSize.y;
  255. itemRect.extent.y = 4;
  256. dglDrawRect(itemRect, colorW);
  257. itemRect.inset(-1, -1);
  258. dglDrawRect(itemRect, colorB);
  259. }
  260. }
  261. S32 GuiTreeViewCtrl::getHitIndex(const GuiEvent& event)
  262. {
  263. Point2I localPoint = globalToLocalCoord(event.mousePoint);
  264. if (localPoint.y < 0)
  265. {
  266. return -1;
  267. }
  268. S32 slot = (S32)mFloor((F32)localPoint.y / (F32)mItemSize.y);
  269. for (S32 i = 0, j = 0; i < mItems.size(); i++)
  270. {
  271. TreeItem* treeItem = dynamic_cast<TreeItem*>(mItems[i]);
  272. if (treeItem && treeItem->isVisible)
  273. {
  274. if (j == slot)
  275. {
  276. S32 roundSlot = (S32)mRound((F32)localPoint.y / (F32)mItemSize.y);
  277. mReorderMethod = roundSlot == slot ? ReorderMethod::Above : ReorderMethod::Below;
  278. SimObject* obj = static_cast<SimObject*>(treeItem->itemData);
  279. bool showTriangle = false;
  280. if(obj)
  281. {
  282. SimGroup* setObj = dynamic_cast<SimGroup*>(obj);
  283. GuiControl* guiCtrl = dynamic_cast<GuiControl*>(obj);
  284. showTriangle = (guiCtrl && guiCtrl->mIsContainer) || (!guiCtrl && setObj && setObj->size() > 0);
  285. if (((slot * mItemSize.y) + 5) < localPoint.y && mReorderMethod == ReorderMethod::Above && showTriangle)
  286. {
  287. mReorderMethod = ReorderMethod::Insert;
  288. }
  289. }
  290. if (j == 0)
  291. {
  292. mReorderMethod = ReorderMethod::Insert;
  293. }
  294. mIsDragLegal = true;
  295. for(TreeItem* trunk = treeItem; trunk != nullptr; trunk = trunk->trunk)
  296. {
  297. if (trunk->isSelected)
  298. {
  299. mIsDragLegal = false;
  300. break;
  301. }
  302. }
  303. mDragIndex = j;
  304. return i;
  305. }
  306. j++;
  307. }
  308. }
  309. return -1;
  310. }
  311. void GuiTreeViewCtrl::handleItemClick(LBItem* hitItem, S32 hitIndex, const GuiEvent& event)
  312. {
  313. TreeItem* treeItem = dynamic_cast<TreeItem*>(hitItem);
  314. if (treeItem)
  315. {
  316. if (treeItem->triangleArea.pointInRect(event.mousePoint))
  317. {
  318. treeItem->isOpen = !treeItem->isOpen;
  319. setBranchesVisible(treeItem, treeItem->isOpen);
  320. return;
  321. }
  322. }
  323. Parent::handleItemClick(hitItem, hitIndex, event);
  324. }
  325. void GuiTreeViewCtrl::handleItemClick_ClickCallbacks(LBItem* hitItem, S32 hitIndex, const GuiEvent& event)
  326. {
  327. TreeItem* treeItem = dynamic_cast<TreeItem*>(hitItem);
  328. if (!treeItem)
  329. {
  330. Parent::handleItemClick_ClickCallbacks(hitItem, hitIndex, event);
  331. return;
  332. }
  333. SimObject* obj = static_cast<SimObject*>(treeItem->itemData);
  334. if (hitItem == mLastClickItem && event.mouseClickCount == 2)
  335. {
  336. if (caller->isMethod("onDoubleClick"))
  337. Con::executef(caller, 2, "onDoubleClick", Con::getIntArg(obj->getId()));
  338. }
  339. else if (caller->isMethod("onClick"))
  340. {
  341. Con::executef(caller, 2, "onClick", Con::getIntArg(obj->getId()));
  342. }
  343. }
  344. void GuiTreeViewCtrl::inspectObject(SimObject* obj)
  345. {
  346. clearItems();
  347. mRootObject = obj;
  348. StringTableEntry text = getObjectText(obj);
  349. S32 id = addItemWithID(text, obj->getId(), obj);
  350. TreeItem* treeItem = grabItemPtr(id);
  351. treeItem->level = 0;
  352. addBranches(treeItem, obj, 1);
  353. }
  354. void GuiTreeViewCtrl::uninspectObject()
  355. {
  356. clearItems();
  357. mRootObject = NULL;
  358. }
  359. void GuiTreeViewCtrl::addBranches(TreeItem* treeItem, SimObject* obj, U16 level)
  360. {
  361. SimGroup* setObj = dynamic_cast<SimGroup*>(obj);
  362. if(setObj)
  363. {
  364. for(auto sub : *setObj)
  365. {
  366. StringTableEntry text = getObjectText(sub);
  367. S32 index = addItemWithID(text, sub->getId(), sub);
  368. TreeItem* branch = grabItemPtr(index);
  369. branch->level = level;
  370. branch->trunk = treeItem;
  371. treeItem->branchList.push_back(branch);
  372. addBranches(branch, sub, level + 1);
  373. }
  374. }
  375. }
  376. GuiListBoxCtrl::LBItem* GuiTreeViewCtrl::createItem()
  377. {
  378. LBItem* newItem = new TreeItem;
  379. if (!newItem)
  380. {
  381. return nullptr;
  382. }
  383. return newItem;
  384. }
  385. void GuiTreeViewCtrl::refreshTree()
  386. {
  387. if (mRootObject)
  388. {
  389. Vector<U32> selectedList;
  390. Vector<U32> openList;
  391. for (auto item : mItems)
  392. {
  393. TreeItem* treeItem = dynamic_cast<TreeItem*>(item);
  394. if (item->isSelected)
  395. {
  396. selectedList.push_back(treeItem->ID);
  397. }
  398. if (treeItem && treeItem->isOpen)
  399. {
  400. openList.push_back(treeItem->ID);
  401. }
  402. }
  403. Point2I pos = Point2I::Zero;
  404. auto scroller = dynamic_cast<GuiScrollCtrl*>(getParent());
  405. if (scroller)
  406. {
  407. pos = scroller->mScrollOffset;
  408. }
  409. inspectObject(mRootObject);
  410. for (auto item : mItems)
  411. {
  412. TreeItem* treeItem = dynamic_cast<TreeItem*>(item);
  413. if (selectedList.contains(treeItem->ID))
  414. {
  415. item->isSelected = true;
  416. mSelectedItems.push_front(item);
  417. }
  418. if (treeItem && openList.contains(treeItem->ID))
  419. {
  420. treeItem->isOpen = true;
  421. }
  422. else if(treeItem)
  423. {
  424. treeItem->isOpen = false;
  425. setBranchesVisible(treeItem, treeItem->isOpen);
  426. }
  427. }
  428. if (scroller)
  429. {
  430. scroller->scrollTo(pos.x, pos.y);
  431. }
  432. }
  433. }
  434. StringTableEntry GuiTreeViewCtrl::getObjectText(SimObject* obj)
  435. {
  436. char buffer[1024];
  437. if (obj)
  438. {
  439. const char* pObjName = obj->getName();
  440. const char* pInternalName = obj->getInternalName();
  441. if (pObjName != NULL)
  442. dSprintf(buffer, sizeof(buffer), "%d: %s - %s", obj->getId(), obj->getClassName(), pObjName);
  443. else if (pInternalName != NULL)
  444. dSprintf(buffer, sizeof(buffer), "%d: %s [%s]", obj->getId(), obj->getClassName(), pInternalName);
  445. else
  446. dSprintf(buffer, sizeof(buffer), "%d: %s", obj->getId(), obj->getClassName());
  447. }
  448. return StringTable->insert(buffer, true);
  449. }
  450. void GuiTreeViewCtrl::calculateHeaderExtent()
  451. {
  452. if(mProfile)
  453. {
  454. GuiBorderProfile* topProfile = mProfile->getTopBorder();
  455. GuiBorderProfile* bottomProfile = mProfile->getBottomBorder();
  456. S32 topSize = (topProfile) ? topProfile->getMargin(NormalState) + topProfile->getBorder(NormalState) + topProfile->getPadding(NormalState) : 0;
  457. S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(NormalState) + bottomProfile->getBorder(NormalState) + bottomProfile->getPadding(NormalState) : 0;
  458. GFont* font = mProfile->getFont();
  459. S32 fontSize = (font) ? font->getHeight() : 0;
  460. S32 height = topSize + bottomSize + fontSize;
  461. S32 width = mBounds.extent.x;
  462. }
  463. }
  464. void GuiTreeViewCtrl::setBranchesVisible(TreeItem* treeItem, bool isVisible)
  465. {
  466. for (auto branch : treeItem->branchList)
  467. {
  468. if(!isVisible || branch->isOpen)
  469. {
  470. setBranchesVisible(branch, isVisible);
  471. }
  472. branch->isVisible = isVisible;
  473. }
  474. }
  475. void GuiTreeViewCtrl::setItemOpen(S32 index, bool isOpen)
  476. {
  477. if ((index >= mItems.size()) || index < 0)
  478. {
  479. Con::warnf("GuiTreeViewCtrl::setItemOpen - invalid index");
  480. return;
  481. }
  482. TreeItem* item = dynamic_cast<TreeItem*>(mItems[index]);
  483. item->isOpen = isOpen;
  484. }
  485. bool GuiTreeViewCtrl::getItemOpen(S32 index)
  486. {
  487. if ((index >= mItems.size()) || index < 0)
  488. {
  489. Con::warnf("GuiTreeViewCtrl::getItemOpen - invalid index");
  490. return true;
  491. }
  492. TreeItem* item = dynamic_cast<TreeItem*>(mItems[index]);
  493. return item->isOpen;
  494. }
  495. S32 GuiTreeViewCtrl::getItemTrunk(S32 index)
  496. {
  497. if ((index >= mItems.size()) || index < 0)
  498. {
  499. Con::warnf(" GuiTreeViewCtrl::getItemTrunk - invalid index");
  500. return -1;
  501. }
  502. TreeItem* item = dynamic_cast<TreeItem*>(mItems[index]);
  503. return item->level == 0 ? -1 : getItemIndex(item->trunk);
  504. }