TGUI  0.9-dev
BackendGui.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_BACKEND_GUI_HPP
27#define TGUI_BACKEND_GUI_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 BackendGui
45 {
46 public:
47
52
54 // Deleted copy constructor
56 BackendGui(const BackendGui& copy) = delete;
57
59 // Deleted assignment operator overload
61 BackendGui& operator=(const BackendGui& right) = delete;
62
64 // Virtual destructor
66 virtual ~BackendGui();
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();
173
174
187
188
194 std::shared_ptr<BackendRenderTarget> getBackendRenderTarget() const;
195
196
203 void setFont(const Font& font);
204
205
211 Font getFont() const;
212
213
220 const std::vector<Widget::Ptr>& getWidgets() const;
221
222
232 void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
233
234
248 Widget::Ptr get(const String& widgetName) const;
249
250
265 template <class T>
266 typename T::Ptr get(const String& widgetName) const
267 {
268 return m_container->get<T>(widgetName);
269 }
270
271
278 bool remove(const Widget::Ptr& widget);
279
280
286
287
297
298
308
309
320
321
332
333
342 bool focusNextWidget(bool recursive = true);
343
344
353 bool focusPreviousWidget(bool recursive = true);
354
355
360
361
367 void moveWidgetToFront(const Widget::Ptr& widget);
368
369
375 void moveWidgetToBack(const Widget::Ptr& widget);
376
377
388 std::size_t moveWidgetForward(const Widget::Ptr& widget);
389
390
401 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
402
403
414 bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
415
416
422 int getWidgetIndex(const Widget::Ptr& widget) const;
423
424
431 void setOpacity(float opacity);
432
433
440 float getOpacity() const;
441
442
451 void setTextSize(unsigned int size);
452
453
459 unsigned int getTextSize() const;
460
461
468 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
469
470
478 void saveWidgetsToFile(const String& filename);
479
480
487 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
488
489
496 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
497
498
505 void saveWidgetsToStream(std::stringstream& stream) const;
506
507
520
521
529
530
542
543
549 void setDrawingUpdatesTime(bool drawUpdatesTime);
550
551
560
561
569 bool updateTime(Duration elapsedTime);
570
571
579 Vector2f mapPixelToView(int x, int y) const;
580
581
589 virtual void mainLoop() = 0;
590
591
593 protected:
594
595#ifndef TGUI_REMOVE_DEPRECATED_CODE
597 // Function that has to be called when constructing the Gui, but after a backend already exists
599 TGUI_DEPRECATED("You no longer need to call the init function") virtual void init() {};
600#endif
601
603 // Updates the view and changes the size of the root container when needed.
604 // Derived classes should updat the m_windowSize member in this function and then call this function from the base class.
606 virtual void updateContainerSize();
607
608
610 public:
611
612 SignalFloatRect onViewChange = {"ViewChanged"};
613
614
616 protected:
617
618 std::chrono::steady_clock::time_point m_lastUpdateTime;
619 bool m_windowFocused = true;
620
621 RootContainer::Ptr m_container = nullptr;
622
623 Widget::Ptr m_visibleToolTip = nullptr;
624 Duration m_tooltipTime;
625 bool m_tooltipPossible = false;
626 Vector2f m_toolTipRelativePos;
627 Vector2f m_lastMousePos;
628
629 Vector2i m_windowSize;
630 RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
631 RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
632 FloatRect m_lastView;
633
634 bool m_drawUpdatesTime = true;
635 bool m_tabKeyUsageEnabled = true;
636
637 Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
638 std::stack<Cursor::Type> m_overrideMouseCursors;
639
640 std::shared_ptr<BackendRenderTarget> m_backendRenderTarget = nullptr;
641 };
642
644
645#ifndef TGUI_REMOVE_DEPRECATED_CODE
646 using GuiBase TGUI_DEPRECATED("GuiBase was renamed to BackendGui") = BackendGui;
647#endif
648
650}
651
653
654#endif // TGUI_BACKEND_GUI_HPP
Base class for the Gui.
Definition: BackendGui.hpp:45
void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
void setFont(const Font &font)
Changes the global font.
void loadWidgetsFromFile(const String &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const
Returns the leaf child widget below the mouse.
bool updateTime()
Updates the internal clock (for timers, animations and blinking edit cursors)
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.
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.
Font getFont() const
Returns the global font for this gui.
RootContainer::Ptr getContainer() const
Returns the internal container of the Gui.
void setAbsoluteViewport(const FloatRect &viewport)
Sets the part of the screen to which the gui will render in pixels.
virtual void mainLoop()=0
Give the gui control over the main loop.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
void saveWidgetsToStream(std::stringstream &stream) const
Saves this the child widgets to a text file.
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in the gui.
virtual void draw()
Draws all the widgets that were added to the gui.
void setOpacity(float opacity)
Changes the opacity of all widgets.
void restoreOverrideMouseCursor()
Undoes the effect of the last call to setOverrideCursor.
Widget::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
void removeAllWidgets()
Removes all widgets that were added to the container.
void setRelativeView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
void saveWidgetsToFile(const String &filename)
Saves the child widgets to a text file.
bool handleEvent(Event event)
Passes the event to the widgets.
bool focusNextWidget(bool recursive=true)
Focuses the next widget in the gui.
void unfocusAllWidgets()
Unfocus all the widgets.
void setDrawingUpdatesTime(bool drawUpdatesTime)
Sets whether drawing the gui will automatically update the internal clock or whether the user does it...
void setTabKeyUsageEnabled(bool enabled)
When the tab key usage is enabled, pressing tab will focus another widget.
unsigned int getTextSize() const
Returns the character size for future child widgets (and for existing widgets where the size wasn't c...
bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
bool isTabKeyUsageEnabled() const
Returns whether the tab key usage is enabled (if so, pressing tab will focus another widget)
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
void setTextSize(unsigned int size)
Changes the character size of all existing and future child widgets.
std::shared_ptr< BackendRenderTarget > getBackendRenderTarget() const
Returns the backend render target that is assigned to the gui.
bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
BackendGui()
Default constructor.
Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
float getOpacity() const
Returns the opacity of all the widgets.
void setOverrideMouseCursor(Cursor::Type type)
Overrides which cursor gets shown.
RelFloatRect getView() const
Returns the part of the gui that will be used to fill the viewport.
T::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition: BackendGui.hpp:266
void requestMouseCursor(Cursor::Type type)
Function that is used by widgets to change the mouse cursor.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
Type
List of available cursors.
Definition: Cursor.hpp:48
@ Arrow
Arrow cursor (default)
Wrapper for durations.
Definition: Duration.hpp:52
Definition: Font.hpp:57
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