TGUI  0.8.2
TreeView.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
176  void deselectItem();
177 
178 
187  bool removeItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
188 
189 
193  void removeAllItems();
194 
195 
200  std::vector<sf::String> getSelectedItem() const;
201 
202 
207  std::vector<ConstNode> getNodes() const;
208 
209 
214  void setItemHeight(unsigned int itemHeight);
215 
216 
221  unsigned int getItemHeight() const;
222 
223 
234  void setTextSize(unsigned int textSize);
235 
236 
241  unsigned int getTextSize() const;
242 
243 
249  bool mouseOnWidget(Vector2f pos) const override;
250 
254  void leftMousePressed(Vector2f pos) override;
255 
259  void leftMouseReleased(Vector2f pos) override;
260 
264  void mouseMoved(Vector2f pos) override;
265 
269  bool mouseWheelScrolled(float delta, Vector2f pos) override;
270 
274  void mouseNoLongerOnWidget() override;
275 
279  void mouseNoLongerDown() override;
280 
284  void markNodesDirty();
285 
286 
294  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
295 
296 
297 
299  protected:
300 
310  Signal& getSignal(std::string signalName) override;
311 
312 
319  void rendererChanged(const std::string& property) override;
320 
321 
325  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
326 
327 
331  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
332 
333 
335  // Returns the size without the borders
337  Vector2f getInnerSize() const;
338 
339 
341  // Update the colors and text style of the selected and hovered items
343  void updateSelectedAndHoveringItemColorsAndStyle();
344 
345 
347  // Update the color and text style of all the items
349  void updateItemColorsAndStyle();
350 
351 
353  // This function is called every frame with the time passed since the last frame.
355  void update(sf::Time elapsedTime) override;
356 
357 
359  // Makes a copy of the widget
361  Widget::Ptr clone() const override
362  {
363  return std::make_shared<TreeView>(*this);
364  }
365 
366 
370  void updateTextColors(std::vector<std::shared_ptr<Node>>& nodes);
371 
372 
376  void createNode(std::vector<std::shared_ptr<Node>>& menus, Node* parent, const sf::String& text);
377 
378 
382  Node* findParentNode(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<std::shared_ptr<Node>>& nodes, Node* parent, bool createParents);
383 
384 
386  // Expands or collapses one of the visible items
388  void toggleNodeInternal(std::size_t index);
389 
390 
392  // Expands or collapses a node
394  bool expandOrCollapse(const std::vector<sf::String>& hierarchy, bool expand);
395 
396 
398  // Helper function to load the items from a text file
400  void loadItems(const std::unique_ptr<DataIO::Node>& node, std::vector<std::shared_ptr<Node>>& items, Node* parent);
401 
402 
404  // Rebuilds the list of visible items and positions the texts
406  unsigned int updateVisibleNodes(std::vector<std::shared_ptr<Node>>& nodes, Node* selectedNode, float textPadding, unsigned int pos);
407 
408 
410  // Updates the text colors of the selected and hovered items
412  void updateSelectedAndHoveringItemColors();
413 
414 
416  // Updates the text color of the hovered item
418  void updateHoveredItem(int item);
419 
420 
422  // Updates the text color of the selected item
424  void updateSelectedItem(int item);
425 
426 
428  public:
429 
430  SignalItemHierarchy onItemSelect = {"ItemSelected"};
431  SignalItemHierarchy onDoubleClick = {"DoubleClicked"};
432  SignalItemHierarchy onExpand = {"Expanded"};
433  SignalItemHierarchy onCollapse = {"Collapsed"};
434 
435 
437  protected:
438 
439  // This contains the nodes of the tree
440  std::vector<std::shared_ptr<Node>> m_nodes;
441  std::vector<std::shared_ptr<Node>> m_visibleNodes;
442 
443  int m_selectedItem = -1;
444  int m_hoveredItem = -1;
445 
446  // The size must be stored
447  unsigned int m_itemHeight = 0;
448  unsigned int m_requestedTextSize = 0;
449  unsigned int m_textSize = 0;
450  float m_maxRight = 0;
451 
452  Vector2f m_iconBounds;
453 
454  CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
455  CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
456 
457 
458  bool m_possibleDoubleClick = false;
459  int m_doubleClickNodeIndex = -1;
460 
461  Sprite m_spriteBranchExpanded;
462  Sprite m_spriteBranchCollapsed;
463  Sprite m_spriteLeaf;
464 
465  // Cached renderer properties
466  Borders m_bordersCached;
467  Borders m_paddingCached;
468  Color m_borderColorCached;
469  Color m_backgroundColorCached;
470  Color m_textColorCached;
471  Color m_textColorHoverCached;
472  Color m_selectedTextColorCached;
473  Color m_selectedTextColorHoverCached;
474  Color m_selectedBackgroundColorCached;
475  Color m_selectedBackgroundColorHoverCached;
476  Color m_backgroundColorHoverCached;
477  TextStyle m_textStyleCached;
478 
480  };
481 
483 }
484 
486 
487 #endif // TGUI_TREE_VIEW_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:245
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:580
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: TreeView.hpp:361
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