TGUI  0.9.1
GuiBase.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
559  virtual void mainLoop() = 0;
560 
561 
563  protected:
564 
566  // Function that has to be called when constructing the Gui, but after a backend already exists
568  virtual void init();
569 
570 
572  // Updates the view and changes the size of the root container when needed
574  virtual void updateContainerSize();
575 
576 
578  public:
579 
580  SignalFloatRect onViewChange = {"ViewChanged"};
581 
582 
584  protected:
585 
586  std::chrono::steady_clock::time_point m_lastUpdateTime;
587  bool m_windowFocused = true;
588 
589  RootContainer::Ptr m_container = nullptr;
590 
591  Widget::Ptr m_visibleToolTip = nullptr;
592  Duration m_tooltipTime;
593  bool m_tooltipPossible = false;
594  Vector2f m_lastMousePos;
595 
596  RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
597  RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
598  FloatRect m_lastView;
599 
600  bool m_drawUpdatesTime = true;
601  bool m_tabKeyUsageEnabled = true;
602 
603  Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
604  std::stack<Cursor::Type> m_overrideMouseCursors;
605  };
606 
608 }
609 
611 
612 #endif // TGUI_GUI_BASE_HPP
Type
List of available cursors.
Definition: Cursor.hpp:48
@ Arrow
Arrow cursor (default)
Wrapper for durations.
Definition: Duration.hpp:52
Definition: Font.hpp:56
Base class for the Gui.
Definition: GuiBase.hpp:45
bool handleEvent(Event event)
Passes the event to the widgets.
RelFloatRect getViewport() const
Returns to which part of the screen the gui will render.
void setAbsoluteView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport in pixels.
void setRelativeView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport.
bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
RelFloatRect getView() const
Returns the part of the gui that will be used to fill the viewport.
Widget::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
void setDrawingUpdatesTime(bool drawUpdatesTime)
Sets whether drawing the gui will automatically update the internal clock or whether the user does it...
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
void setFont(const Font &font)
Changes the global font.
virtual void draw()=0
Draws all the widgets that were added to the gui.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
void setAbsoluteViewport(const FloatRect &viewport)
Sets the part of the screen to which the gui will render in pixels.
bool isTabKeyUsageEnabled() const
Returns whether the tab key usage is enabled (if so, pressing tab will focus another widget)
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const
Returns the leaf child widget below the mouse.
void setOverrideMouseCursor(Cursor::Type type)
Overrides which cursor gets shown.
virtual void mainLoop()=0
Give the gui control over the main loop.
T::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition: GuiBase.hpp:258
RootContainer::Ptr getContainer() const
Returns the internal container of the Gui.
unsigned int getTextSize() const
Returns the character size for future child widgets (and for existing widgets where the size wasn't c...
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 setOpacity(float opacity)
Changes the opacity of all widgets.
void loadWidgetsFromFile(const String &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
void setTabKeyUsageEnabled(bool enabled)
When the tab key usage is enabled, pressing tab will focus another widget.
void restoreOverrideMouseCursor()
Undoes the effect of the last call to setOverrideCursor.
void requestMouseCursor(Cursor::Type type)
Function that is used by widgets to change the mouse cursor.
bool updateTime()
Updates the internal clock (for timers, animations and blinking edit cursors)
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in the gui.
void saveWidgetsToFile(const String &filename)
Saves the child widgets to a text file.
void saveWidgetsToStream(std::stringstream &stream) const
Saves this the child widgets to a text file.
Font getFont() const
Returns the global font for this gui.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
void removeAllWidgets()
Removes all widgets that were added to the container.
float getOpacity() const
Returns the opacity of all the widgets.
Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
bool focusNextWidget(bool recursive=true)
Focuses the next widget in the gui.
void setTextSize(unsigned int size)
Changes the character size of all existing and future child widgets.
void unfocusAllWidgets()
Unfocus all the widgets.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition: Container.hpp:629
Wrapper class to store strings.
Definition: String.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Event.hpp:37
FloatRect that can contain absolute values or values relative to the parent size.
Definition: RelFloatRect.hpp:41
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:177