TGUI  0.8-dev
Grid.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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_GRID_HPP
27 #define TGUI_GRID_HPP
28 
29 
30 #include <TGUI/Container.hpp>
31 
33 
34 namespace tgui
35 {
37 
38  class TGUI_API Grid : public Container
39  {
40  public:
41 
42  typedef std::shared_ptr<Grid> Ptr;
43  typedef std::shared_ptr<const Grid> ConstPtr;
44 
45 
53  enum class Alignment
54  {
55  Center,
56  UpperLeft,
57  Up,
58  UpperRight,
59  Right,
60  BottomRight,
61  Bottom,
62  BottomLeft,
63  Left
64  };
65 
66 
68  // Default constructor
70  Grid();
71 
72 
79  static Grid::Ptr create();
80 
81 
88  Grid(const Grid& copy);
89 
90 
99  Grid& operator= (const Grid& right);
100 
101 
110  static Grid::Ptr copy(Grid::ConstPtr grid);
111 
112 
123  virtual void setSize(const Layout2d& size) override;
125 
126 
133  virtual sf::Vector2f getSize() const override;
134 
135 
144  virtual bool remove(const Widget::Ptr& widget) override;
145 
146 
151  virtual void removeAllWidgets() override;
152 
153 
164  void addWidget(const Widget::Ptr& widget,
165  std::size_t row,
166  std::size_t column,
167  const Borders& borders = Borders{0},
168  Alignment alignment = Alignment::Center);
169 
170 
180  Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
181 
182 
190  void setWidgetBorders(const Widget::Ptr& widget, const Borders& borders = Borders(0, 0, 0, 0));
191 
192 
201  void setWidgetBorders(std::size_t row, std::size_t column, const Borders& borders = Borders(0, 0, 0, 0));
202 
203 
212  Borders getWidgetBorders(const Widget::Ptr& widget) const;
213 
214 
224  Borders getWidgetBorders(std::size_t row, std::size_t column) const;
225 
226 
234  void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
235 
236 
245  void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
246 
247 
256  Alignment getWidgetAlignment(const Widget::Ptr& widget) const;
257 
258 
268  Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
269 
270 
277  const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
278 
279 
283  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
284 
285 
293  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
294 
295 
297  protected:
298 
299 
301  // Returns the minimum size required by the grid to display correctly all widgets.
303  sf::Vector2f getMinSize();
304 
305 
307  // Reposition all the widgets.
309  void updatePositionsOfAllWidgets();
310 
311 
313  // Updates the position and size of the widget
315  void updateWidgets();
316 
317 
319  // Makes a copy of the widget
321  virtual Widget::Ptr clone() const override
322  {
323  return std::make_shared<Grid>(*this);
324  }
325 
326 
328  protected:
329 
330  std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
331  std::vector<std::vector<Borders>> m_objBorders;
332  std::vector<std::vector<Alignment>> m_objAlignment;
333 
334  std::vector<float> m_rowHeight;
335  std::vector<float> m_columnWidth;
336 
337  std::map<Widget::Ptr, unsigned int> m_connectedCallbacks;
338 
339  sf::Vector2f m_realSize; // Actual size of the grid, while m_size contains the intended size
340 
342  };
343 
345 }
346 
348 
349 #endif // TGUI_GRID_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Alignment
The alignment of the widget in its cell.
Definition: Grid.hpp:53
std::shared_ptr< Grid > Ptr
Shared widget pointer.
Definition: Grid.hpp:42
std::shared_ptr< const Grid > ConstPtr
Shared constant widget pointer.
Definition: Grid.hpp:43
Class to store the position or size of a widget.
Definition: Layout.hpp:263
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Grid.hpp:321
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Definition: Outline.hpp:37
Definition: Grid.hpp:38