TGUI 1.14-dev
Loading...
Searching...
No Matches
BackendGui.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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#ifndef TGUI_BACKEND_GUI_HPP
26#define TGUI_BACKEND_GUI_HPP
27
28#include <TGUI/Container.hpp>
29#include <TGUI/Cursor.hpp>
30#include <TGUI/Event.hpp>
31#include <TGUI/RelFloatRect.hpp>
32#include <TGUI/TwoFingerScrollDetect.hpp>
33
34#include <chrono>
35#include <stack>
36
38
39namespace tgui
40{
44 class TGUI_API BackendGui
45 {
46 public:
51
53 // Deleted copy constructor
55 BackendGui(const BackendGui& copy) = delete;
56
58 // Deleted assignment operator overload
60 BackendGui& operator=(const BackendGui& right) = delete;
61
63 // Virtual destructor
65 virtual ~BackendGui();
66
77 void setAbsoluteViewport(const FloatRect& viewport);
78
91 void setRelativeViewport(const FloatRect& viewport);
92
99 [[nodiscard]] RelFloatRect getViewport() const;
100
114 void setAbsoluteView(const FloatRect& view);
115
128 void setRelativeView(const FloatRect& view);
129
136 [[nodiscard]] RelFloatRect getView() const;
137
154 bool handleEvent(Event event);
155
159 void setTabKeyUsageEnabled(bool enabled);
160
164 [[nodiscard]] bool isTabKeyUsageEnabled() const;
165
169 // TGUI_NEXT: Remove from base class, as some backends require parameters. Each Gui class should declare its own
170 // draw function with the parameters that its renderer needs, and mainLoop should render via functions
171 // in the render target to start and present a frame instead of relying on draw() and presentScreen().
172 virtual void draw();
173
184 [[nodiscard]] RootContainer::Ptr getContainer() const;
185
191 [[nodiscard]] std::shared_ptr<BackendRenderTarget> getBackendRenderTarget() const;
192
198 void setFont(const Font& font);
199
205 [[nodiscard]] Font getFont() const;
206
212 [[nodiscard]] const std::vector<Widget::Ptr>& getWidgets() const;
213
225 void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
226
239 [[nodiscard]] Widget::Ptr get(const String& widgetName) const;
240
254 template <class WidgetType>
255 [[nodiscard]] typename WidgetType::Ptr get(const String& widgetName) const
256 {
257 return m_container->get<WidgetType>(widgetName);
258 }
259
265 bool remove(const Widget::Ptr& widget);
266
271
280 [[nodiscard]] Widget::Ptr getFocusedChild() const;
281
290 [[nodiscard]] Widget::Ptr getFocusedLeaf() const;
291
292#ifndef TGUI_REMOVE_DEPRECATED_CODE
302 TGUI_DEPRECATED("Use getWidgetAtPos(pos, true) instead")
303 [[nodiscard]] Widget::Ptr getWidgetAtPosition(Vector2f pos) const;
304#endif
305
320 [[nodiscard]] Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const;
321
322#ifndef TGUI_REMOVE_DEPRECATED_CODE
332 TGUI_DEPRECATED("Use getWidgetBelowMouseCursor(mousePos, true) instead")
333 [[nodiscard]] Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const;
334#endif
335
350 [[nodiscard]] Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos, bool recursive) const;
351
362 [[nodiscard]] Vector2i getLastMousePosition() const;
363
372 bool focusNextWidget(bool recursive = true);
373
382 bool focusPreviousWidget(bool recursive = true);
383
388
394 void moveWidgetToFront(const Widget::Ptr& widget);
395
401 void moveWidgetToBack(const Widget::Ptr& widget);
402
413 std::size_t moveWidgetForward(const Widget::Ptr& widget);
414
425 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
426
437 bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
438
444 [[nodiscard]] int getWidgetIndex(const Widget::Ptr& widget) const;
445
451 void setOpacity(float opacity);
452
458 [[nodiscard]] float getOpacity() const;
459
468 void setTextSize(unsigned int size);
469
475 [[nodiscard]] unsigned int getTextSize() const;
476
487 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
488
497 void loadWidgetsFromFile(const String& filename, const FormLoadOptions& options);
498
506 void saveWidgetsToFile(const String& filename) const;
507
516 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
517
526 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
527
534 void loadWidgetsFromStream(std::stringstream& stream, const FormLoadOptions& options);
535
542 void loadWidgetsFromStream(std::stringstream&& stream, const FormLoadOptions& options);
543
549 void saveWidgetsToStream(std::stringstream& stream) const;
550
563
571
583
589 void setDrawingUpdatesTime(bool drawUpdatesTime);
590
599
607 bool updateTime(Duration elapsedTime);
608
614 [[nodiscard]] Vector2f mapPixelToCoords(Vector2i pixel) const;
615
621 [[nodiscard]] Vector2f mapCoordsToPixel(Vector2f coord) const;
622
632 virtual void mainLoop(Color clearColor = {240, 240, 240}) = 0;
633
640 virtual void startTextInput(FloatRect inputRect);
641
646 virtual void stopTextInput();
647
655 virtual void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos);
656
664 [[nodiscard]] virtual bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const; // TGUI_NEXT: Pure virtual
665
680
690 [[nodiscard]] bool isKeyboardNavigationEnabled() const;
691
693
694 protected:
696 // Handles the backend-independent part of the two finger scrolling.
697 // Returns whether the touch event should be absorbed by the gui.
699 bool handleTwoFingerScroll(bool wasAlreadyScrolling);
700
702 // Updates the view and changes the size of the root container when needed.
703 // Derived classes should update m_framebufferSize in this function and then call this function from the base class.
705 virtual void updateContainerSize();
706
708 // Used when the NumLock is off to translate keypad key events to key events for text cursor navigation.
709 // The key parameter should be a value from Numpad0 to Numpad9 and the output is the key code that corresponds to the
710 // wanted functionality (e.g. for Numpad0 we return the Insert key). Unknown is returned for Numpad5.
712 static Event::KeyboardKey translateKeypadKey(Event::KeyboardKey key);
713
715
716 public:
717 SignalFloatRect onViewChange = {"ViewChanged"};
718 Signal onWindowFocus = {"WindowFocused"};
719 Signal onWindowUnfocus = {"WindowUnfocused"};
720
722
723 protected:
724 std::chrono::steady_clock::time_point m_lastUpdateTime;
725 bool m_windowFocused = true;
726
727 RootContainer::Ptr m_container = std::make_shared<RootContainer>();
728
729 Widget::Ptr m_visibleToolTip = nullptr;
730 Duration m_tooltipTime;
731 bool m_tooltipPossible = false;
732 Vector2f m_toolTipRelativePos;
733 Vector2i m_lastMousePos;
734 std::uintptr_t m_trackedFingerId = 0;
735 TwoFingerScrollDetect m_twoFingerScroll;
736
737 Vector2i m_framebufferSize;
738 RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
739 RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
740 FloatRect m_lastView;
741
742 bool m_drawUpdatesTime = true;
743 bool m_tabKeyUsageEnabled = true;
744 bool m_keyboardNavigationEnabled = false; // TGUI_NEXT: Enable by default?
745
746 Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
747 std::stack<Cursor::Type> m_overrideMouseCursors;
748
749 std::shared_ptr<BackendRenderTarget> m_backendRenderTarget = nullptr;
750 };
751
753} // namespace tgui
754
756
757#endif // TGUI_BACKEND_GUI_HPP
void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
virtual void stopTextInput()
This function is called by TGUI when unfocusing a text field (EditBox or TextArea)....
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.
bool isKeyboardNavigationEnabled() const
Returns whether using the arrow keys can be used to navigate between widgets.
Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const
Returns the widget that is located at the given position.
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.
virtual void startTextInput(FloatRect inputRect)
This function is called by TGUI when focusing a text field (EditBox or TextArea). It may result in th...
virtual void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos)
This function is called by TGUI when the position of the caret changes in a text field (EditBox or Te...
virtual void mainLoop(Color clearColor={240, 240, 240})=0
Give the gui control over the main loop.
virtual bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const
Checks the state for one of the modifier keys.
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.
Signal onWindowFocus
The window sent a GainedFocus event.
Definition BackendGui.hpp:718
void loadWidgetsFromStream(std::stringstream &stream, const FormLoadOptions &options)
Loads the child widgets from a string stream with load options.
void saveWidgetsToFile(const String &filename) const
Saves the child widgets to a text file.
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.
Signal onWindowUnfocus
The window sent a LostFocus event.
Definition BackendGui.hpp:719
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.
Vector2f mapCoordsToPixel(Vector2f coord) const
Converts a position within the view to the corresponding pixel coordinate.
void setRelativeView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport.
void loadWidgetsFromFile(const String &filename, const FormLoadOptions &options)
Loads the child widgets from a text file.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
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...
Vector2i getLastMousePosition() const
Returns the mouse position of the last mouse event that was handled by the gui.
void loadWidgetsFromStream(std::stringstream &&stream, const FormLoadOptions &options)
Loads the child widgets from a string stream with load options.
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).
Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos, bool recursive) const
Returns the widget below the mouse.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
Vector2f mapPixelToCoords(Vector2i pixel) const
Converts the pixel coordinate to a position within the view.
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.
SignalFloatRect onViewChange
The view was changed. Optional parameter: new view rectangle.
Definition BackendGui.hpp:717
WidgetType::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition BackendGui.hpp:255
BackendGui()
Default constructor.
void setKeyboardNavigationEnabled(bool enabled)
Changes whether using the arrow keys can be used to navigate between widgets.
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.
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.
Wrapper for colors.
Definition Color.hpp:63
Type
List of available cursors.
Definition Cursor.hpp:48
Wrapper for durations.
Definition Duration.hpp:52
Wrapper around the backend-specific font. All copies of the font will share the same internal font re...
Definition Font.hpp:54
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition Container.hpp:690
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
Definition TwoFingerScrollDetect.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
SignalTyped< FloatRect > SignalFloatRect
Signal with one "FloatRect" as optional unbound parameter.
Definition Signal.hpp:415
Definition Event.hpp:36
KeyModifier
Modifiers keys.
Definition Event.hpp:155
KeyboardKey
Keyboard key codes.
Definition Event.hpp:40
Options for loadWidgetsFromFile / loadWidgetsFromStream.
Definition FormLoadOptions.hpp:42
FloatRect that can contain absolute values or values relative to the parent size.
Definition RelFloatRect.hpp:39
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:163