TGUI  0.9-beta
GuiBase.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_GUI_BASE_HPP
27 #define TGUI_GUI_BASE_HPP
28 
29 
30 #include <TGUI/Container.hpp>
31 #include <TGUI/RelFloatRect.hpp>
32 #include <TGUI/Event.hpp>
33 #include <TGUI/Cursor.hpp>
34 #include <chrono>
35 #include <stack>
36 
38 
39 namespace tgui
40 {
44  class TGUI_API GuiBase
45  {
46  public:
47 
49  // Default constructor
51  GuiBase() = default;
52 
54  // Deleted copy constructor
56  GuiBase(const GuiBase& copy) = delete;
57 
59  // Deleted assignment operator overload
61  GuiBase& operator=(const GuiBase& right) = delete;
62 
64  // Virtual destructor
66  virtual ~GuiBase();
67 
68 
79  void setAbsoluteViewport(const FloatRect& viewport);
80 
81 
94  void setRelativeViewport(const FloatRect& viewport);
95 
96 
104 
105 
119  void setAbsoluteView(const FloatRect& view);
120 
121 
134  void setRelativeView(const FloatRect& view);
135 
136 
144 
145 
154  bool handleEvent(Event event);
155 
156 
160  void setTabKeyUsageEnabled(bool enabled);
161 
162 
166  bool isTabKeyUsageEnabled() const;
167 
168 
172  virtual void draw() = 0;
173 
174 
187 
188 
195  void setFont(const Font& font);
196 
197 
203  Font getFont() const;
204 
205 
212  const std::vector<Widget::Ptr>& getWidgets() const;
213 
214 
224  void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
225 
226 
240  Widget::Ptr get(const String& widgetName) const;
241 
242 
257  template <class T>
258  typename T::Ptr get(const String& widgetName) const
259  {
260  return m_container->get<T>(widgetName);
261  }
262 
263 
270  bool remove(const Widget::Ptr& widget);
271 
272 
278 
279 
289 
290 
300 
301 
312 
313 
324 
325 
334  bool focusNextWidget(bool recursive = true);
335 
336 
345  bool focusPreviousWidget(bool recursive = true);
346 
347 
352 
353 
359  void moveWidgetToFront(const Widget::Ptr& widget);
360 
361 
367  void moveWidgetToBack(const Widget::Ptr& widget);
368 
369 
380  std::size_t moveWidgetForward(const Widget::Ptr& widget);
381 
382 
393  std::size_t moveWidgetBackward(const Widget::Ptr& widget);
394 
395 
402  void setOpacity(float opacity);
403 
404 
411  float getOpacity() const;
412 
413 
422  void setTextSize(unsigned int size);
423 
424 
430  unsigned int getTextSize() const;
431 
432 
439  void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
440 
441 
448  void saveWidgetsToFile(const String& filename);
449 
450 
457  void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
458 
459 
466  void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
467 
468 
475  void saveWidgetsToStream(std::stringstream& stream) const;
476 
477 
490 
491 
499 
500 
512 
513 
519  void setDrawingUpdatesTime(bool drawUpdatesTime);
520 
521 
529  bool updateTime();
530 
531 
539  bool updateTime(Duration elapsedTime);
540 
541 
549  Vector2f mapPixelToView(int x, int y) const;
550 
551 
553  protected:
554 
556  // Function that has to be called when constructing the Gui, but after a backend already exists
558  virtual void init();
559 
560 
562  // Updates the view and changes the size of the root container when needed
564  virtual void updateContainerSize();
565 
566 
568  public:
569 
570  SignalFloatRect onViewChange = {"ViewChanged"};
571 
572 
574  protected:
575 
576  std::chrono::steady_clock::time_point m_lastUpdateTime;
577  bool m_windowFocused = true;
578 
579  RootContainer::Ptr m_container = nullptr;
580 
581  Widget::Ptr m_visibleToolTip = nullptr;
582  Duration m_tooltipTime;
583  bool m_tooltipPossible = false;
584  Vector2f m_lastMousePos;
585 
586  RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
587  RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
588  FloatRect m_lastView;
589 
590  bool m_drawUpdatesTime = true;
591  bool m_tabKeyUsageEnabled = true;
592 
593  Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
594  std::stack<Cursor::Type> m_overrideMouseCursors;
595  };
596 
598 }
599 
601 
602 #endif // TGUI_GUI_BASE_HPP
void setOpacity(float opacity)
Changes the opacity of all widgets.
bool isTabKeyUsageEnabled() const
Returns whether the tab key usage is enabled (if so, pressing tab will focus another widget)
Definition: Font.hpp:56
FloatRect that can contain absolute values or values relative to the parent size.
Definition: RelFloatRect.hpp:41
void setTextSize(unsigned int size)
Changes the character size of all existing and future child widgets.
void setDrawingUpdatesTime(bool drawUpdatesTime)
Sets whether drawing the gui will automatically update the internal clock or whether the user does it...
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
RelFloatRect getViewport() const
Returns to which part of the screen the gui will render.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in the gui.
bool handleEvent(Event event)
Passes the event to the widgets.
void loadWidgetsFromFile(const String &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
void setFont(const Font &font)
Changes the global font.
void unfocusAllWidgets()
Unfocus all the widgets.
void setRelativeView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport.
void setTabKeyUsageEnabled(bool enabled)
When the tab key usage is enabled, pressing tab will focus another widget.
Widget::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition: Event.hpp:37
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
float getOpacity() const
Returns the opacity of all the widgets.
bool updateTime()
Updates the internal clock (for timers, animations and blinking edit cursors)
void restoreOverrideMouseCursor()
Undoes the effect of the last call to setOverrideCursor.
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:177
void setRelativeViewport(const FloatRect &viewport)
Sets the part of the screen to which the gui will render as a ratio relative to the window size.
void setAbsoluteViewport(const FloatRect &viewport)
Sets the part of the screen to which the gui will render in pixels.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
void saveWidgetsToFile(const String &filename)
Saves the child widgets to a text file.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition: Container.hpp:601
Wrapper for durations.
Definition: Duration.hpp:52
Type
List of available cursors.
Definition: Cursor.hpp:48
@ Arrow
Arrow cursor (default)
Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const
Returns the leaf child widget below the mouse.
T::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition: GuiBase.hpp:258
unsigned int getTextSize() const
Returns the character size for future child widgets (and for existing widgets where the size wasn't c...
Wrapper class to store strings.
Definition: String.hpp:74
RootContainer::Ptr getContainer() const
Returns the internal container of the Gui.
void setAbsoluteView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport in pixels.
Font getFont() const
Returns the global font for this gui.
Base class for the Gui.
Definition: GuiBase.hpp:45
RelFloatRect getView() const
Returns the part of the gui that will be used to fill the viewport.
Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
void removeAllWidgets()
Removes all widgets that were added to the container.
void setOverrideMouseCursor(Cursor::Type type)
Overrides which cursor gets shown.
void requestMouseCursor(Cursor::Type type)
Function that is used by widgets to change the mouse cursor.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
bool focusNextWidget(bool recursive=true)
Focuses the next widget in the gui.
void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
void saveWidgetsToStream(std::stringstream &stream) const
Saves this the child widgets to a text file.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
virtual void draw()=0
Draws all the widgets that were added to the gui.
bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.