TGUI  0.9-dev
Grid.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
85  Grid(const Grid& copy);
86 
87 
91  Grid(Grid&& copy);
92 
93 
97  Grid& operator= (const Grid& right);
98 
99 
103  Grid& operator= (Grid&& right);
104 
105 
114  static Grid::Ptr copy(Grid::ConstPtr grid);
115 
116 
127  void setSize(const Layout2d& size) override;
128  using Widget::setSize;
129 
130 
141  void setAutoSize(bool autoSize);
142 
143 
151  bool getAutoSize() const;
152 
153 
159  bool remove(const Widget::Ptr& widget) override;
160 
161 
166  void removeAllWidgets() override;
167 
168 
179  void addWidget(const Widget::Ptr& widget,
180  std::size_t row,
181  std::size_t column,
182  Alignment alignment = Alignment::Center,
183  const Padding& padding = Padding{0});
184 
185 
195  Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
196 
197 
205  std::map<Widget::Ptr, std::pair<std::size_t, std::size_t>> getWidgetLocations() const;
206 
207 
215  void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = Padding(0, 0, 0, 0));
216 
217 
226  void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = Padding(0, 0, 0, 0));
227 
228 
237  Padding getWidgetPadding(const Widget::Ptr& widget) const;
238 
239 
249  Padding getWidgetPadding(std::size_t row, std::size_t column) const;
250 
251 
259  void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
260 
261 
270  void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
271 
272 
281  Alignment getWidgetAlignment(const Widget::Ptr& widget) const;
282 
283 
293  Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
294 
295 
302  const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
303 
304 
311  bool mouseOnWidget(Vector2f pos) const override;
312 
313 
321  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
322 
323 
325  protected:
326 
330  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
331 
332 
336  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
337 
338 
340  // Returns the minimum size required by the grid to display correctly all widgets.
342  Vector2f getMinimumSize() const;
343 
344 
346  // Reposition all the widgets.
348  void updatePositionsOfAllWidgets();
349 
350 
352  // Updates the position and size of the widget
354  void updateWidgets();
355 
356 
358  // Makes a copy of the widget
360  Widget::Ptr clone() const override
361  {
362  return std::make_shared<Grid>(*this);
363  }
364 
365 
367  protected:
368 
369  bool m_autoSize = true;
370 
371  std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
372  std::vector<std::vector<Padding>> m_objPadding;
373  std::vector<std::vector<Alignment>> m_objAlignment;
374 
375  std::vector<float> m_rowHeight;
376  std::vector<float> m_columnWidth;
377 
378  std::map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
379 
381  };
382 
384 }
385 
387 
388 #endif // TGUI_GRID_HPP
Container widget.
Definition: Container.hpp:47
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Grid.hpp:360
Alignment
The alignment of the widget in its cell.
Definition: Grid.hpp:53
Definition: Outline.hpp:38
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Grid.hpp:38
std::shared_ptr< const Grid > ConstPtr
Shared constant widget pointer.
Definition: Grid.hpp:43
std::shared_ptr< Grid > Ptr
Shared widget pointer.
Definition: Grid.hpp:42