TGUI  1.1
Loading...
Searching...
No Matches
BackendGui.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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#include <TGUI/Container.hpp>
30#include <TGUI/RelFloatRect.hpp>
31#include <TGUI/Event.hpp>
32#include <TGUI/Cursor.hpp>
33#include <TGUI/TwoFingerScrollDetect.hpp>
34
35#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
36 #include <chrono>
37 #include <stack>
38#endif
39
41
42TGUI_MODULE_EXPORT namespace tgui
43{
47 class TGUI_API BackendGui
48 {
49 public:
50
55
57 // Deleted copy constructor
59 BackendGui(const BackendGui& copy) = delete;
60
62 // Deleted assignment operator overload
64 BackendGui& operator=(const BackendGui& right) = delete;
65
67 // Virtual destructor
69 virtual ~BackendGui();
70
71
82 void setAbsoluteViewport(const FloatRect& viewport);
83
84
97 void setRelativeViewport(const FloatRect& viewport);
98
99
106 TGUI_NODISCARD RelFloatRect getViewport() const;
107
108
122 void setAbsoluteView(const FloatRect& view);
123
124
137 void setRelativeView(const FloatRect& view);
138
139
146 TGUI_NODISCARD RelFloatRect getView() const;
147
148
157 bool handleEvent(Event event);
158
159
163 void setTabKeyUsageEnabled(bool enabled);
164
165
169 TGUI_NODISCARD bool isTabKeyUsageEnabled() const;
170
171
175 virtual void draw();
176
177
189 TGUI_NODISCARD RootContainer::Ptr getContainer() const;
190
191
197 TGUI_NODISCARD std::shared_ptr<BackendRenderTarget> getBackendRenderTarget() const;
198
199
206 void setFont(const Font& font);
207
208
214 TGUI_NODISCARD Font getFont() const;
215
216
223 TGUI_NODISCARD const std::vector<Widget::Ptr>& getWidgets() const;
224
225
238 void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
239
240
254 TGUI_NODISCARD Widget::Ptr get(const String& widgetName) const;
255
256
271 template <class WidgetType>
272 TGUI_NODISCARD typename WidgetType::Ptr get(const String& widgetName) const
273 {
274 return m_container->get<WidgetType>(widgetName);
275 }
276
277
284 bool remove(const Widget::Ptr& widget);
285
286
292
293
302 TGUI_NODISCARD Widget::Ptr getFocusedChild() const;
303
304
313 TGUI_NODISCARD Widget::Ptr getFocusedLeaf() const;
314
315
325 TGUI_NODISCARD Widget::Ptr getWidgetAtPosition(Vector2f pos) const;
326
327
337 TGUI_NODISCARD Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const;
338
339
348 bool focusNextWidget(bool recursive = true);
349
350
359 bool focusPreviousWidget(bool recursive = true);
360
361
366
367
373 void moveWidgetToFront(const Widget::Ptr& widget);
374
375
381 void moveWidgetToBack(const Widget::Ptr& widget);
382
383
394 std::size_t moveWidgetForward(const Widget::Ptr& widget);
395
396
407 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
408
409
420 bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
421
422
428 TGUI_NODISCARD int getWidgetIndex(const Widget::Ptr& widget) const;
429
430
437 void setOpacity(float opacity);
438
439
446 TGUI_NODISCARD float getOpacity() const;
447
448
457 void setTextSize(unsigned int size);
458
459
465 TGUI_NODISCARD unsigned int getTextSize() const;
466
467
474 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
475
476
484 void saveWidgetsToFile(const String& filename);
485
486
493 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
494
495
502 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
503
504
511 void saveWidgetsToStream(std::stringstream& stream) const;
512
513
526
527
535
536
548
549
555 void setDrawingUpdatesTime(bool drawUpdatesTime);
556
557
566
567
575 bool updateTime(Duration elapsedTime);
576
577
583 TGUI_NODISCARD Vector2f mapPixelToCoords(Vector2i pixel) const;
584
585
591 TGUI_NODISCARD Vector2f mapCoordsToPixel(Vector2f coord) const;
592
593
603 virtual void mainLoop(Color clearColor = {240, 240, 240}) = 0;
604
605
612 virtual void startTextInput(FloatRect inputRect);
613
614
619 virtual void stopTextInput();
620
621
629 virtual void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos);
630
631
646
647
657 TGUI_NODISCARD bool isKeyboardNavigationEnabled() const;
658
659
661 protected:
662
664 // Handles the backend-independent part of the two finger scrolling.
665 // Returns whether the touch event should be absorbed by the gui.
667 bool handleTwoFingerScroll(bool wasAlreadyScrolling);
668
670 // Updates the view and changes the size of the root container when needed.
671 // Derived classes should update m_framebufferSize in this function and then call this function from the base class.
673 virtual void updateContainerSize();
674
675
677 public:
678
679 SignalFloatRect onViewChange = {"ViewChanged"};
680
681
683 protected:
684
685 std::chrono::steady_clock::time_point m_lastUpdateTime;
686 bool m_windowFocused = true;
687
688 RootContainer::Ptr m_container = std::make_shared<RootContainer>();
689
690 Widget::Ptr m_visibleToolTip = nullptr;
691 Duration m_tooltipTime;
692 bool m_tooltipPossible = false;
693 Vector2f m_toolTipRelativePos;
694 Vector2i m_lastMousePos;
695 TwoFingerScrollDetect m_twoFingerScroll;
696
697 Vector2i m_framebufferSize;
698 RelFloatRect m_viewport{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
699 RelFloatRect m_view{RelativeValue{0}, RelativeValue{0}, RelativeValue{1}, RelativeValue{1}};
700 FloatRect m_lastView;
701
702 bool m_drawUpdatesTime = true;
703 bool m_tabKeyUsageEnabled = true;
704 bool m_keyboardNavigationEnabled = false; // TGUI_NEXT: Enable by default?
705
706 Cursor::Type m_requestedMouseCursor = Cursor::Type::Arrow;
707 std::stack<Cursor::Type> m_overrideMouseCursors;
708
709 std::shared_ptr<BackendRenderTarget> m_backendRenderTarget = nullptr;
710 };
711
713}
714
716
717#endif // TGUI_BACKEND_GUI_HPP
Base class for the Gui.
Definition BackendGui.hpp:48
TGUI_NODISCARD const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
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.
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.
TGUI_NODISCARD WidgetType::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
Definition BackendGui.hpp:272
TGUI_NODISCARD Font getFont() const
Returns the global font for this gui.
TGUI_NODISCARD RelFloatRect getViewport() const
Returns to which part of the screen the gui will render.
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.
void setAbsoluteView(const FloatRect &view)
Sets the part of the gui that will be used to fill the viewport in pixels.
virtual void startTextInput(FloatRect inputRect)
This function is called by TGUI when focusing a text field (EditBox or TextArea). It may result in th...
TGUI_NODISCARD RootContainer::Ptr getContainer() const
Returns the internal container of the Gui.
TGUI_NODISCARD Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
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.
TGUI_NODISCARD int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
TGUI_NODISCARD unsigned int getTextSize() const
Returns the character size for future child widgets (and for existing widgets where the size wasn't c...
void setAbsoluteViewport(const FloatRect &viewport)
Sets the part of the screen to which the gui will render in pixels.
TGUI_NODISCARD bool isKeyboardNavigationEnabled() const
Returns whether using the arrow keys can be used to navigate between widgets.
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.
TGUI_NODISCARD RelFloatRect getView() const
Returns the part of the gui that will be used to fill the viewport.
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.
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.
TGUI_NODISCARD Widget::Ptr get(const String &widgetName) const
Returns a pointer to an earlier created widget.
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...
TGUI_NODISCARD Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
TGUI_NODISCARD Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
TGUI_NODISCARD bool isTabKeyUsageEnabled() const
Returns whether the tab key usage is enabled (if so, pressing tab will focus another widget)
void setTabKeyUsageEnabled(bool enabled)
When the tab key usage is enabled, pressing tab will focus another widget.
bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
TGUI_NODISCARD std::shared_ptr< BackendRenderTarget > getBackendRenderTarget() const
Returns the backend render target that is assigned to the gui.
TGUI_NODISCARD Vector2f mapPixelToCoords(Vector2i pixel) const
Converts the pixel coordinate to a position within the view.
TGUI_NODISCARD Vector2f mapCoordsToPixel(Vector2f coord) const
Converts a position within the view to the corresponding pixel coordinate.
TGUI_NODISCARD float getOpacity() const
Returns the opacity of all the widgets.
void setTextSize(unsigned int size)
Changes the character size of all existing and future child widgets.
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.
void setKeyboardNavigationEnabled(bool enabled)
Changes whether using the arrow keys can be used to navigate between widgets.
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.
TGUI_NODISCARD Widget::Ptr getWidgetBelowMouseCursor(Vector2i mousePos) const
Returns the leaf child widget below the mouse.
Wrapper for colors.
Definition Color.hpp:72
Type
List of available cursors.
Definition Cursor.hpp:51
Wrapper for durations.
Definition Duration.hpp:56
Wrapper around the backend-specific font. All copies of the font will share the same internal font re...
Definition Font.hpp:61
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition Container.hpp:648
Wrapper class to store strings.
Definition String.hpp:101
Definition TwoFingerScrollDetect.hpp:45
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Definition Event.hpp:39
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