TGUI 1.13
Loading...
Searching...
No Matches
BackendGuiSFML.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_SFML_HPP
26#define TGUI_BACKEND_GUI_SFML_HPP
27
28#include <TGUI/Config.hpp>
29
30#include <TGUI/Backend/Window/BackendGui.hpp>
31
32#include <SFML/Window.hpp>
33
35
36namespace tgui
37{
39
40 class TGUI_API BackendGuiSFML : public BackendGui
41 {
42 public:
49
53 ~BackendGuiSFML() override;
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(
148 [this, &handlers...](auto&& event)
149 {
150 using EventType = decltype(event);
151
152 // We can't properly detect whether the handlers are valid (e.g. when using an invalid event type),
153 // but we can do some tests that will give errors on handlers that are wrong but almost correct.
154 static_assert(!std::disjunction_v<std::is_invocable_r<bool, Ts, EventType, bool>...>,
155 "Handler for handleWindowEvents can't have both an extra bool argument and a bool return type");
156
157 // If any of the handlers return a bool then they will be executed before the event is passed to the gui.
158 // The returned boolean then decides whether the gui should still process the event or not.
159 // Only one of the handlers should match at most, but letting the compiler figure out which handler to
160 // call by creating an overload set is tricky because only handlers with a bool returns should be called here.
161 auto callIfMatchesAndReturnsBool = [&event](auto&& handler)
162 {
163 // 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
164 using FuncType = decltype(handler);
165 if constexpr (std::is_invocable_v<FuncType, EventType>)
166 {
167 if constexpr (std::is_same_v<std::invoke_result_t<FuncType, EventType>, bool>)
168 return std::invoke(std::forward<FuncType>(handler), std::forward<EventType>(event));
169 else
170 {
171 static_assert(std::is_same_v<std::invoke_result_t<FuncType, EventType>, void>,
172 "Handler for handleWindowEvents must have either 'void' or 'bool' return type");
173 return true;
174 }
175 }
176 else
177 return true;
178 };
179 const bool passEventToGui = (callIfMatchesAndReturnsBool(std::forward<Ts>(handlers)) && ...);
180
181 // Let the gui handle the event
182 bool eventHandledByGui = false;
183 if (passEventToGui)
184 eventHandledByGui = handleEvent(std::forward<EventType>(event));
185
186 // After the gui has handled the events, we call the handlers that return nothing.
187 // These handlers can have an optional bool parameter that indicates whether the event was processed by the gui.
188 auto callIfMatchesAndReturnsVoid = [&event](auto&& handler, auto&&... extraArgs)
189 {
190 using FuncType = decltype(handler);
191 if constexpr (std::is_invocable_v<FuncType, EventType, decltype(extraArgs)...>)
192 {
193 if constexpr (std::is_same_v<std::invoke_result_t<FuncType, EventType, decltype(extraArgs)...>, void>)
194 std::invoke(std::forward<FuncType>(handler),
195 std::forward<EventType>(event),
196 std::forward<decltype(extraArgs)>(extraArgs)...);
197 }
198 };
199 (callIfMatchesAndReturnsVoid(std::forward<Ts>(handlers)), ...);
200 (callIfMatchesAndReturnsVoid(std::forward<Ts>(handlers), eventHandledByGui), ...);
201 });
202 }
203#endif
204
250 void mainLoop(Color clearColor = {240, 240, 240}) override;
251
260 [[nodiscard]] bool convertEvent(const sf::Event& eventSFML, Event& eventTGUI);
261
267 sf::Window* getWindow() const;
268
275 void startTextInput(FloatRect inputRect) override;
276
281 void stopTextInput() override;
282
292 void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;
293
301 [[nodiscard]] bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;
302
304
305 protected:
311 void setGuiWindow(sf::Window& window);
312
314 // Updates the view and changes the size of the root container when needed
316 void updateContainerSize() override;
317
319
320 protected:
321 sf::Window* m_window = nullptr;
322
323 bool m_modifierKeySystemPressed = false;
324 bool m_modifierKeyControlPressed = false;
325 bool m_modifierKeyShiftPressed = false;
326 bool m_modifierKeyAltPressed = false;
327
329 };
330} // namespace tgui
331
333
334#endif // TGUI_BACKEND_GUI_SFML_HPP
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...
~BackendGuiSFML() override
Destructor.
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:63
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Definition Event.hpp:36
KeyModifier
Modifiers keys.
Definition Event.hpp:155