guiTreeViewCtrl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #ifndef _GUI_TREEVIEWCTRL_H
  23. #define _GUI_TREEVIEWCTRL_H
  24. #include "collection/bitSet.h"
  25. #include "math/mRect.h"
  26. #include "gui/guiControl.h"
  27. #include "gui/guiArrayCtrl.h"
  28. //------------------------------------------------------------------------------
  29. class GuiTreeViewCtrl : public GuiArrayCtrl
  30. {
  31. private:
  32. typedef GuiArrayCtrl Parent;
  33. public:
  34. /// @section GuiTreeViewCtrl_Intro Introduction
  35. /// @nosubgrouping
  36. ///
  37. struct Item
  38. {
  39. enum ItemState
  40. {
  41. Selected = BIT(0),
  42. Expanded = BIT(1),
  43. Focus = BIT(2),
  44. MouseOverBmp = BIT(3),
  45. MouseOverText = BIT(4),
  46. InspectorData = BIT(5), ///< Set if we're representing some inspector
  47. /// info (ie, use mInspectorInfo, not mScriptInfo)
  48. VirtualParent = BIT(6), ///< This indicates that we should be rendered as
  49. /// a parent even though we don't have any children.
  50. /// This is useful for preventing scenarios where
  51. /// we might want to create thousands of
  52. /// Items that might never be shown (for instance
  53. /// if we're browsing the object hierarchy in
  54. /// Torque, which might have thousands of objects).
  55. };
  56. BitSet32 mState;
  57. SimObjectPtr<GuiControlProfile> mProfile;
  58. S16 mId;
  59. U16 mTabLevel;
  60. Item * mParent;
  61. Item * mChild;
  62. Item * mNext;
  63. Item * mPrevious;
  64. S32 mIcon; //stores the icon that will represent the item in the tree
  65. S32 mDataRenderWidth; /// this stores the pixel width needed
  66. /// to render the item's data in the
  67. /// onRenderCell function to optimize
  68. /// for speed.
  69. Item( GuiControlProfile *pProfile );
  70. ~Item();
  71. struct ScriptTag
  72. {
  73. S8 mNormalImage;
  74. S8 mExpandedImage;
  75. char* mText;
  76. char* mValue;
  77. } mScriptInfo;
  78. struct InspectorTag
  79. {
  80. SimObjectPtr<SimObject> mObject;
  81. } mInspectorInfo;
  82. /// @name Get Methods
  83. /// @{
  84. ///
  85. const S8 getNormalImage() const;
  86. const S8 getExpandedImage() const;
  87. char *getText();
  88. char *getValue();
  89. inline const S16 getID() const { return mId; };
  90. SimObject *getObject();
  91. const U32 getDisplayTextLength();
  92. const S32 getDisplayTextWidth(GFont *font);
  93. void getDisplayText(U32 bufLen, char *buf);
  94. /// @}
  95. /// @name Set Methods
  96. /// @{
  97. /// Set whether an item is expanded or not (showing children or having them hidden)
  98. void setExpanded(const bool f);
  99. /// Set the image to display when an item IS expanded
  100. void setExpandedImage(const S8 id);
  101. /// Set the image to display when an item is NOT expanded
  102. void setNormalImage(const S8 id);
  103. /// Assign a SimObject pointer to an inspector data item
  104. void setObject(SimObject *obj);
  105. /// Set the items displayable text (caption)
  106. void setText(char *txt);
  107. /// Set the items script value (data)
  108. void setValue(const char *val);
  109. /// Set the items virtual parent flag
  110. void setVirtualParent( bool value );
  111. /// @}
  112. /// @name State Retrieval
  113. /// @{
  114. /// Returns true if this item is expanded. For
  115. /// inspector objects, the expansion is stored
  116. /// on the SimObject, for other things we use our
  117. /// bit vector.
  118. const bool isExpanded() const;
  119. /// Returns true if an item is inspector data
  120. /// or false if it's just an item.
  121. inline const bool isInspectorData() const { return mState.test(InspectorData); };
  122. /// Returns true if we should show the expand art
  123. /// and make the item interact with the mouse as if
  124. /// it were a parent.
  125. const bool isParent() const;
  126. /// @}
  127. /// @name Searching Methods
  128. /// @{
  129. /// Find an inspector data item by it's SimObject pointer
  130. Item *findChildByValue(const SimObject *obj);
  131. /// Find a regular data item by it's script value
  132. Item *findChildByValue(StringTableEntry Value);
  133. /// @}
  134. };
  135. /// @name Enums
  136. /// @{
  137. ///
  138. enum TreeState
  139. {
  140. RebuildVisible = BIT(0), ///< Temporary flag, we have to rebuild the tree.
  141. IsInspector = BIT(1), ///< We are mapping a SimObject hierarchy.
  142. IsEditable = BIT(2), ///< We allow items to be moved around.
  143. ShowTreeLines = BIT(3), ///< Should we render tree lines or just icons?
  144. BuildingVisTree = BIT(4), ///< We are currently building the visible tree (prevent recursion)
  145. };
  146. protected:
  147. enum
  148. {
  149. MaxIcons = 32,
  150. };
  151. enum Icons
  152. {
  153. Default1 = 0,
  154. SimGroup1,
  155. SimGroup2,
  156. SimGroup3,
  157. SimGroup4,
  158. Camera,
  159. Hidden,
  160. Lock1,
  161. Lock2,
  162. Default,
  163. Icon31,
  164. Icon32
  165. };
  166. enum mDragMidPointFlags
  167. {
  168. NomDragMidPoint,
  169. AbovemDragMidPoint,
  170. BelowmDragMidPoint
  171. };
  172. ///
  173. enum HitFlags
  174. {
  175. OnIndent = BIT(0),
  176. OnImage = BIT(1),
  177. OnText = BIT(2),
  178. OnRow = BIT(3),
  179. };
  180. ///
  181. enum BmpIndices
  182. {
  183. BmpDunno,
  184. BmpLastChild,
  185. BmpChild,
  186. BmpExp,
  187. BmpExpN,
  188. BmpExpP,
  189. BmpExpPN,
  190. BmpCon,
  191. BmpConN,
  192. BmpConP,
  193. BmpConPN,
  194. BmpLine,
  195. BmpGlow,
  196. };
  197. /// @}
  198. public:
  199. ///
  200. Vector<Item*> mItems;
  201. Vector<Item*> mVisibleItems;
  202. Vector<Item*> mSelectedItems;
  203. Vector<S32> mSelected; ///< Used for tracking stuff that was
  204. /// selected, but may not have been
  205. /// created at time of selection
  206. S32 mItemCount;
  207. Item * mItemFreeList; ///< We do our own free list, as we
  208. /// we want to be able to recycle
  209. /// item ids and do some other clever
  210. /// things.
  211. Item * mRoot;
  212. S32 mInstantGroup;
  213. S32 mMaxWidth;
  214. S32 mSelectedItem;
  215. S32 mDraggedToItem;
  216. S32 mTempItem;
  217. S32 mStart;
  218. BitSet32 mFlags;
  219. protected:
  220. TextureHandle mIconTable[MaxIcons];
  221. // for debugging
  222. bool mDebug;
  223. S32 mTabSize;
  224. S32 mTextOffset;
  225. bool mFullRowSelect;
  226. S32 mItemHeight;
  227. bool mDestroyOnSleep;
  228. bool mSupportMouseDragging;
  229. bool mMultipleSelections;
  230. bool mDeleteObjectAllowed;
  231. bool mDragToItemAllowed;
  232. S32 mOldDragY;
  233. S32 mCurrentDragCell;
  234. S32 mPreviousDragCell;
  235. S32 mDragMidPoint;
  236. bool mMouseDragged;
  237. StringTableEntry mBitmapBase;
  238. TextureHandle mTexRollover;
  239. TextureHandle mTexSelected;
  240. // Hack to get passed always recursively building tree EVERY TICK!
  241. S32 mTicksPassed;
  242. S32 mTreeRefreshInterval;
  243. ColorI mAltFontColor;
  244. ColorI mAltFontColorHL;
  245. ColorI mAltFontColorSE;
  246. SimObjectPtr<SimObject> mRootObject;
  247. void destroyChildren(Item * item, Item * parent);
  248. void destroyItem(Item * item);
  249. void destroyTree();
  250. void deleteItem(Item *item);
  251. void buildItem(Item * item, U32 tabLevel, bool bForceFullUpdate = false);
  252. bool hitTest(const Point2I & pnt, Item* & item, BitSet32 & flags);
  253. virtual bool onVirtualParentBuild(Item *item, bool bForceFullUpdate = false);
  254. virtual bool onVirtualParentExpand(Item *item);
  255. virtual bool onVirtualParentCollapse(Item *item);
  256. virtual void onItemSelected( Item *item );
  257. void addInspectorDataItem(Item *parent, SimObject *obj);
  258. public:
  259. GuiTreeViewCtrl();
  260. virtual ~GuiTreeViewCtrl();
  261. /// Used for syncing the mSelected and mSelectedItems lists.
  262. void syncSelection();
  263. void lockSelection(bool lock);
  264. void hideSelection(bool hide);
  265. void addSelection(S32 itemId);
  266. /// Should use addSelection and removeSelection when calling from script
  267. /// instead of setItemSelected. Use setItemSelected when you want to select
  268. /// something in the treeview as it has script call backs.
  269. void removeSelection(S32 itemId);
  270. /// Sets the flag of the item with the matching itemId.
  271. bool setItemSelected(S32 itemId, bool select);
  272. bool setItemExpanded(S32 itemId, bool expand);
  273. bool setItemValue(S32 itemId, StringTableEntry Value);
  274. const char * getItemText(S32 itemId);
  275. const char * getItemValue(S32 itemId);
  276. StringTableEntry getTextToRoot(S32 itemId, const char *delimiter = "");
  277. Item * getItem(S32 itemId);
  278. Item * createItem(S32 icon);
  279. bool editItem( S32 itemId, const char* newText, const char* newValue );
  280. // insertion/removal
  281. void unlinkItem(Item * item);
  282. S32 insertItem(S32 parentId, const char * text, const char * value = "", const char * iconString = "", S16 normalImage = 0, S16 expandedImage = 1);
  283. bool removeItem(S32 itemId);
  284. void removeAllChildren(S32 itemId); // Remove all children of the given item
  285. bool buildIconTable(const char * icons);
  286. void setInstantGroup(SimObject * obj);
  287. S32 getIcon(const char * iconString);
  288. // tree items
  289. const S32 getFirstRootItem() const;
  290. S32 getChildItem(S32 itemId);
  291. S32 getParentItem(S32 itemId);
  292. S32 getNextSiblingItem(S32 itemId);
  293. S32 getPrevSiblingItem(S32 itemId);
  294. S32 getItemCount();
  295. S32 getSelectedItem();
  296. S32 getSelectedItem(S32 index); // Given an item's index in the selection list, return its itemId
  297. S32 getSelectedItemsCount() {return mSelectedItems.size();} // Returns the number of selected items
  298. void moveItemUp( S32 itemId );
  299. void moveItemDown( S32 itemId );
  300. // misc.
  301. bool scrollVisible( Item *item );
  302. bool scrollVisible( S32 itemId );
  303. bool scrollVisibleByObjectId( S32 objID );
  304. void deleteSelection();
  305. void clearSelection();
  306. S32 findItemByName(const char *name);
  307. S32 findItemByObjectId(S32 iObjId);
  308. // GuiControl
  309. bool onWake();
  310. void onSleep();
  311. void onPreRender();
  312. bool onKeyDown( const GuiEvent &event );
  313. void onTouchDown(const GuiEvent &event);
  314. void onMiddleMouseDown(const GuiEvent &event);
  315. void onTouchMove(const GuiEvent &event);
  316. void onTouchEnter(const GuiEvent &event);
  317. void onTouchLeave(const GuiEvent &event);
  318. void onRightMouseDown(const GuiEvent &event);
  319. void onTouchDragged(const GuiEvent &event);
  320. void onTouchUp(const GuiEvent &event);
  321. /// Returns false if the object is a child of one of the inner items.
  322. bool childSearch(Item * item, SimObject *obj, bool yourBaby);
  323. /// Find immediately available inspector items (eg ones that aren't children of other inspector items)
  324. /// and then update their sets
  325. void inspectorSearch(Item * item, Item * parent, SimSet * parentSet, SimSet * newParentSet);
  326. // GuiArrayCtrl
  327. void onRenderCell(Point2I offset, Point2I cell, bool, bool);
  328. void onRender(Point2I offset, const RectI &updateRect);
  329. static void initPersistFields();
  330. void inspectObject(SimObject * obj, bool okToEdit);
  331. void buildVisibleTree(bool bForceFullUpdate = false);
  332. DECLARE_CONOBJECT(GuiTreeViewCtrl);
  333. };
  334. #endif