TGUI  0.8.5
TreeView.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_TREE_VIEW_HPP
27 #define TGUI_TREE_VIEW_HPP
28 
29 #include <TGUI/CopiedSharedPtr.hpp>
30 #include <TGUI/Widgets/Scrollbar.hpp>
31 #include <TGUI/Renderers/TreeViewRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
42  class TGUI_API TreeView : public Widget
43  {
44  public:
45 
46  typedef std::shared_ptr<TreeView> Ptr;
47  typedef std::shared_ptr<const TreeView> ConstPtr;
48 
50  struct ConstNode
51  {
52  bool expanded;
53  sf::String text;
54  std::vector<ConstNode> nodes;
55  };
56 
58  struct Node
59  {
60  Text text;
61  unsigned depth = 0;
62  bool expanded = true;
63  Node* parent;
64  std::vector<std::shared_ptr<Node>> nodes;
65  };
66 
67 
69  // Default constructor
71  TreeView();
72 
74  // Copy constructor
76  TreeView(const TreeView&);
77 
79  // Move constructor
81  TreeView(TreeView&&) = default;
82 
84  // Overload of copy assignment operator
86  TreeView& operator=(const TreeView&);
87 
89  // Overload of move assignment operator
91  TreeView& operator=(TreeView&&) = default;
92 
93 
98  static TreeView::Ptr create();
99 
100 
106  static TreeView::Ptr copy(TreeView::ConstPtr treeView);
107 
108 
113  TreeViewRenderer* getSharedRenderer();
114  const TreeViewRenderer* getSharedRenderer() const;
115 
116 
122  TreeViewRenderer* getRenderer();
123  const TreeViewRenderer* getRenderer() const;
124 
125 
130  void setSize(const Layout2d& size) override;
131  using Widget::setSize;
132 
133 
142  bool addItem(const std::vector<sf::String>& hierarchy, bool createParents = true);
143 
144 
150  void expand(const std::vector<sf::String>& hierarchy);
151 
152 
156  void expandAll();
157 
158 
164  void collapse(const std::vector<sf::String>& hierarchy);
165 
166 
170  void collapseAll();
171 
172 
180  bool selectItem(const std::vector<sf::String>& hierarchy);
181 
182 
186  void deselectItem();
187 
188 
197  bool removeItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
198 
199 
203  void removeAllItems();
204 
205 
210  std::vector<sf::String> getSelectedItem() const;
211 
212 
217  std::vector<ConstNode> getNodes() const;
218 
219 
224  void setItemHeight(unsigned int itemHeight);
225 
226 
231  unsigned int getItemHeight() const;
232 
233 
244  void setTextSize(unsigned int textSize);
245 
246 
251  unsigned int getTextSize() const;
252 
253 
259  bool mouseOnWidget(Vector2f pos) const override;
260 
264  void leftMousePressed(Vector2f pos) override;
265 
269  void leftMouseReleased(Vector2f pos) override;
270 
274  void mouseMoved(Vector2f pos) override;
275 
279  bool mouseWheelScrolled(float delta, Vector2f pos) override;
280 
284  void mouseNoLongerOnWidget() override;
285 
289  void mouseNoLongerDown() override;
290 
294  void markNodesDirty();
295 
296 
304  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
305 
306 
307 
309  protected:
310 
320  Signal& getSignal(std::string signalName) override;
321 
322 
329  void rendererChanged(const std::string& property) override;
330 
331 
335  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
336 
337 
341  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
342 
343 
345  // Returns the size without the borders
347  Vector2f getInnerSize() const;
348 
349 
351  // Update the colors and text style of the selected and hovered items
353  void updateSelectedAndHoveringItemColorsAndStyle();
354 
355 
357  // Update the color and text style of all the items
359  void updateItemColorsAndStyle();
360 
361 
363  // This function is called every frame with the time passed since the last frame.
365  void update(sf::Time elapsedTime) override;
366 
367 
369  // Makes a copy of the widget
371  Widget::Ptr clone() const override
372  {
373  return std::make_shared<TreeView>(*this);
374  }
375 
376 
380  void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
381 
382 
386  void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const sf::String& text);
387 
388 
392  Node* findParentNode(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
393 
394 
396  // Expands or collapses one of the visible items
398  void toggleNodeInternal(std::size_t index);
399 
400 
402  // Expands or collapses a node
404  bool expandOrCollapse(const std::vector<sf::String>& hierarchy, bool expand);
405 
406 
408  // Helper function to load the items from a text file
410  void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
411 
412 
414  // Rebuilds the list of visible items and positions the texts
416  unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
417 
418 
420  // Updates the text colors of the selected and hovered items
422  void updateSelectedAndHoveringItemColors();
423 
424 
426  // Updates the text color of the hovered item
428  void updateHoveredItem(int item);
429 
430 
432  // Updates the text color of the selected item
434  void updateSelectedItem(int item);
435 
436 
438  public:
439 
440  SignalItemHierarchy onItemSelect = {"ItemSelected"};
441  SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
442  SignalItemHierarchy onExpand = {"Expanded"};
443  SignalItemHierarchy onCollapse = {"Collapsed"};
444 
445 
447  protected:
448 
449  // This contains the nodes of the tree
450  std::vector<std::shared_ptr<Node>> m_nodes;
451  std::vector<std::shared_ptr<Node>> m_visibleNodes;
452 
453  int m_selectedItem = -1;
454  int m_hoveredItem = -1;
455 
456  // The size must be stored
457  unsigned int m_itemHeight = 0;
458  unsigned int m_requestedTextSize = 0;
459  unsigned int m_textSize = 0;
460  float m_maxRight = 0;
461 
462  Vector2f m_iconBounds;
463 
464  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
465  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
466 
467 
468  bool m_possibleDoubleClick = false;
469  int m_doubleClickNodeIndex = -1;
470 
471  Sprite m_spriteBranchExpanded;
472  Sprite m_spriteBranchCollapsed;
473  Sprite m_spriteLeaf;
474 
475  // Cached renderer properties
476  Borders m_bordersCached;
477  Borders m_paddingCached;
478  Color m_borderColorCached;
479  Color m_backgroundColorCached;
480  Color m_textColorCached;
481  Color m_textColorHoverCached;
482  Color m_selectedTextColorCached;
483  Color m_selectedTextColorHoverCached;
484  Color m_selectedBackgroundColorCached;
485  Color m_selectedBackgroundColorHoverCached;
486  Color m_backgroundColorHoverCached;
487  TextStyle m_textStyleCached;
488 
490  };
491 
493 }
494 
496 
497 #endif // TGUI_TREE_VIEW_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:45
Wrapper for colors.
Definition: Color.hpp:48
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:258
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:77
Definition: Vector2f.hpp:37
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:609
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: TreeView.hpp:371
Definition: Text.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Internal representation of a node.
Definition: TreeView.hpp:58
Definition: TreeViewRenderer.hpp:36
Tree view widget.
Definition: TreeView.hpp:42
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38
Read-only node representation used by getNodes.
Definition: TreeView.hpp:50