TGUI  0.9.3
GuiBase.hpp
1
2//
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
39namespace 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
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
406 bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
407
408
414 int getWidgetIndex(const Widget::Ptr& widget) const;
415
416
423 void setOpacity(float opacity);
424
425
432 float getOpacity() const;
433
434
443 void setTextSize(unsigned int size);
444
445
451 unsigned int getTextSize() const;
452
453
460 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
461
462
470 void saveWidgetsToFile(const String& filename);
471
472
479 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
480
481
488 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
489
490
497 void saveWidgetsToStream(std::stringstream& stream) const;
498
499
512
513
521
522
534
535
541 void setDrawingUpdatesTime(bool drawUpdatesTime);
542
543
552
553
561 bool updateTime(Duration elapsedTime);
562
563
571 Vector2f mapPixelToView(int x, int y) const;
572
573
581 virtual void mainLoop() = 0;
582
583
585 protected:
586
588 // Function that has to be called when constructing the Gui, but after a backend already exists
590 virtual void init();
591
592
594 // Updates the view and changes the size of the root container when needed
596 virtual void updateContainerSize();
597
598
600 public:
601
602 SignalFloatRect onViewChange = {"ViewChanged"};
603
604
606 protected:
607
608 std::chrono::steady_clock::time_point m_lastUpdateTime;
609 bool m_windowFocused = true;
610
611 RootContainer::Ptr m_container = nullptr;
612
613 Widget::Ptr m_visibleToolTip = nullptr;
614 Duration m_tooltipTime;
615 bool m_tooltipPossible = false;
616 Vector2f m_toolTipRelativePos;
617 Vector2f m_lastMousePos;
618
619 RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
620 RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
621 FloatRect m_lastView;
622
623 bool m_drawUpdatesTime = true;
624 bool m_tabKeyUsageEnabled = true;
625
626 Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
627 std::stack<Cursor::Type> m_overrideMouseCursors;
628 };
629
631}
632
634
635#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.
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.
bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
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.
int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
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.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
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:680
Wrapper class to store strings.
Definition: String.hpp:79
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