TGUI 1.11
Loading...
Searching...
No Matches
BackendGuiSFML.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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_SFML_HPP
26#define TGUI_BACKEND_GUI_SFML_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/Backend/Window/BackendGui.hpp>
30
31#include <SFML/Window.hpp>
32
34
35namespace tgui
36{
38
39 class TGUI_API BackendGuiSFML : public BackendGui
40 {
41 public:
42
49
54
85 bool handleEvent(sf::Event event);
87
88#if SFML_VERSION_MAJOR >= 3
141 template <typename... Ts>
142 void handleWindowEvents(Ts&&... handlers)
143 {
144 if (!m_window)
145 return;
146
147 m_window->handleEvents([this,&handlers...](auto&& event) {
148 using EventType = decltype(event);
149
150 // We can't properly detect whether the handlers are valid (e.g. when using an invalid event type),
151 // but we can do some tests that will give errors on handlers that are wrong but almost correct.
152 static_assert(!std::disjunction_v<std::is_invocable_r<bool, Ts, EventType, bool>...>, "Handler for handleWindowEvents can't have both an extra bool argument and a bool return type");
153
154 // If any of the handlers return a bool then they will be executed before the event is passed to the gui.
155 // The returned boolean then decides whether the gui should still process the event or not.
156 // Only one of the handlers should match at most, but letting the compiler figure out which handler to
157 // call by creating an overload set is tricky because only handlers with a bool returns should be called here.
158 auto callIfMatchesAndReturnsBool = [&event](auto&& handler)
159 {
160 // std::is_invocable_r_v would still return true if the type is convertable to bool, so we use std::invoke_result_t to test the return type
161 using FuncType = decltype(handler);
162 if constexpr (std::is_invocable_v<FuncType, EventType>)
163 {
164 if constexpr (std::is_same_v<std::invoke_result_t<FuncType, EventType>, bool>)
165 return std::invoke(std::forward<FuncType>(handler), std::forward<EventType>(event));
166 else
167 {
168 static_assert(std::is_same_v<std::invoke_result_t<FuncType, EventType>, void>, "Handler for handleWindowEvents must have either 'void' or 'bool' return type");
169 return true;
170 }
171 }
172 else
173 return true;
174 };
175 const bool passEventToGui = (callIfMatchesAndReturnsBool(std::forward<Ts>(handlers)) && ...);
176
177 // Let the gui handle the event
178 bool eventHandledByGui = false;
179 if (passEventToGui)
180 eventHandledByGui = this->handleEvent(std::forward<EventType>(event));
181
182 // After the gui has handled the events, we call the handlers that return nothing.
183 // These handlers can have an optional bool parameter that indicates whether the event was processed by the gui.
184 auto callIfMatchesAndReturnsVoid = [&event](auto&& handler, auto&&... extraArgs)
185 {
186 using FuncType = decltype(handler);
187 if constexpr (std::is_invocable_v<FuncType, EventType, decltype(extraArgs)...>)
188 {
189 if constexpr (std::is_same_v<std::invoke_result_t<FuncType, EventType, decltype(extraArgs)...>, void>)
190 std::invoke(std::forward<FuncType>(handler), std::forward<EventType>(event), std::forward<decltype(extraArgs)>(extraArgs)...);
191 }
192 };
193 (callIfMatchesAndReturnsVoid(std::forward<Ts>(handlers)), ...);
194 (callIfMatchesAndReturnsVoid(std::forward<Ts>(handlers), eventHandledByGui), ...);
195 });
196 }
197#endif
198
244 void mainLoop(Color clearColor = {240, 240, 240}) override;
245
254 TGUI_NODISCARD bool convertEvent(const sf::Event& eventSFML, Event& eventTGUI);
255
261 sf::Window* getWindow() const;
262
269 void startTextInput(FloatRect inputRect) override;
270
275 void stopTextInput() override;
276
286 void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;
287
295 TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;
296
298 protected:
299
305 void setGuiWindow(sf::Window& window);
306
308 // Updates the view and changes the size of the root container when needed
310 void updateContainerSize() override;
311
313 protected:
314
315 sf::Window* m_window = nullptr;
316
317 bool m_modifierKeySystemPressed = false;
318 bool m_modifierKeyControlPressed = false;
319 bool m_modifierKeyShiftPressed = false;
320 bool m_modifierKeyAltPressed = false;
321
323 };
324}
325
327
328#endif // TGUI_BACKEND_GUI_SFML_HPP
~BackendGuiSFML()
Destructor.
bool convertEvent(const sf::Event &eventSFML, Event &eventTGUI)
Helper function that converts an SFML event to a TGUI event.
void setGuiWindow(sf::Window &window)
Sets the window which the gui should use.
sf::Window * getWindow() const
Returns the window that was provided to the gui.
bool handleEvent(sf::Event event)
Passes the event to the widgets.
void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override
This function is called by TGUI when the position of the caret changes in a text field (EditBox or Te...
void handleWindowEvents(Ts &&... handlers)
Handle all pending window events at once using callbacks, as alternative to polling events.
Definition BackendGuiSFML.hpp:142
BackendGuiSFML()
Default constructor.
void mainLoop(Color clearColor={240, 240, 240}) override
Give the gui control over the main loop.
void startTextInput(FloatRect inputRect) override
This function is called by TGUI when focusing a text field (EditBox or TextArea). It will result in t...
void stopTextInput() override
This function is called by TGUI when unfocusing a text field (EditBox or TextArea)....
bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override
Checks the state for one of the modifier keys.
bool handleEvent(Event event)
Passes the event to the widgets.
BackendGui()
Default constructor.
Wrapper for colors.
Definition Color.hpp:71
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Definition Event.hpp:38
KeyModifier
Modifiers keys.
Definition Event.hpp:157