Skip to content

Commit

Permalink
SFML backend no longer uses sf::Keyboard::isKeyPressed to check modif…
Browse files Browse the repository at this point in the history
…ier keys
  • Loading branch information
texus committed Aug 17, 2024
1 parent efa878d commit 6e21723
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TGUI 1.5 (TBD)
- Scrollbar::setSize no longer affects orientation once setOrientation is called
- Opacity of ScrollablePanel wasn't applied to its scrollbars
- Setting opacity of SeparatorLine had no effect
- SFML backend no longer uses sf::Keyboard::isKeyPressed to check modifier keys


TGUI 1.4.1 (20 July 2024)
Expand Down
7 changes: 4 additions & 3 deletions gui-builder/src/Form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1350,12 +1350,13 @@ std::vector<std::pair<tgui::Vector2f, tgui::Vector2f>> Form::getAlignmentLines()
const auto controlModifier = tgui::Event::KeyModifier::Control;
#endif

const auto selectedWidget = m_selectedWidget->ptr;
const auto* gui = selectedWidget->getParentGui();
if (!m_draggingWidget && !m_draggingSelectionSquare
&& !tgui::getBackend()->isKeyboardModifierPressed(controlModifier)
&& !tgui::getBackend()->isKeyboardModifierPressed(tgui::Event::KeyModifier::Shift))
&& !gui->isKeyboardModifierPressed(controlModifier)
&& !gui->isKeyboardModifierPressed(tgui::Event::KeyModifier::Shift))
return lines;

const auto selectedWidget = m_selectedWidget->ptr;
const tgui::Vector2f selectedTopLeft = selectedWidget->getAbsolutePosition();
const tgui::Vector2f selectedBottomRight = selectedWidget->getAbsolutePosition() + selectedWidget->getSize();
const auto widgets = selectedWidget->getParent()->getWidgets();
Expand Down
2 changes: 1 addition & 1 deletion include/TGUI/Backend/Window/Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD virtual bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) = 0;
TGUI_DEPRECATED("Use gui.isKeyboardModifierPressed(modifierKey) instead") TGUI_NODISCARD virtual bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) = 0;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the contents of the clipboard
Expand Down
9 changes: 9 additions & 0 deletions include/TGUI/Backend/Window/BackendGui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,15 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the state for one of the modifier keys
///
/// @param modifierKey The modifier key of which the state is being queried
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD virtual bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const; // TGUI_NEXT: Pure virtual

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes whether using the arrow keys can be used to navigate between widgets
///
Expand Down
4 changes: 2 additions & 2 deletions include/TGUI/Backend/Window/GLFW/BackendGLFW.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;
TGUI_DEPRECATED("Use gui.isKeyboardModifierPressed(modifierKey) instead") TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the contents of the clipboard
Expand Down Expand Up @@ -152,7 +152,7 @@ TGUI_MODULE_EXPORT namespace tgui
Cursor::Type mouseCursor = Cursor::Type::Arrow;
};

std::unordered_map<BackendGui*, GuiResources> m_guiResources;
std::unordered_map<const BackendGui*, GuiResources> m_guiResources;
std::unordered_map<Cursor::Type, GLFWcursor*> m_mouseCursors;
};

Expand Down
9 changes: 9 additions & 0 deletions include/TGUI/Backend/Window/GLFW/BackendGuiGLFW.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the state for one of the modifier keys
///
/// @param modifierKey The modifier key of which the state is being queried
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

Expand Down
9 changes: 9 additions & 0 deletions include/TGUI/Backend/Window/Raylib/BackendGuiRaylib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the state for one of the modifier keys
///
/// @param modifierKey The modifier key of which the state is being queried
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Breaks out of the main loop that is implemented inside the mainLoop function
///
Expand Down
2 changes: 1 addition & 1 deletion include/TGUI/Backend/Window/Raylib/BackendRaylib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;
TGUI_DEPRECATED("Use gui.isKeyboardModifierPressed(modifierKey) instead") TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the contents of the clipboard
Expand Down
9 changes: 9 additions & 0 deletions include/TGUI/Backend/Window/SDL/BackendGuiSDL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the state for one of the modifier keys
///
/// @param modifierKey The modifier key of which the state is being queried
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

Expand Down
4 changes: 2 additions & 2 deletions include/TGUI/Backend/Window/SDL/BackendSDL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;
TGUI_DEPRECATED("Use gui.isKeyboardModifierPressed(modifierKey) instead") TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the contents of the clipboard
Expand Down Expand Up @@ -172,7 +172,7 @@ TGUI_MODULE_EXPORT namespace tgui
Cursor::Type mouseCursor = Cursor::Type::Arrow;
};

std::unordered_map<BackendGui*, GuiResources> m_guiResources;
std::unordered_map<const BackendGui*, GuiResources> m_guiResources;
std::unordered_map<Cursor::Type, SDL_Cursor*> m_mouseCursors;

#if defined(TGUI_SYSTEM_LINUX) && defined(TGUI_USE_X11) && (SDL_MAJOR_VERSION < 3) && defined(SDL_VIDEO_DRIVER_X11)
Expand Down
14 changes: 14 additions & 0 deletions include/TGUI/Backend/Window/SFML/BackendGuiSFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateTextCursorPosition(FloatRect inputRect, Vector2f caretPos) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Checks the state for one of the modifier keys
///
/// @param modifierKey The modifier key of which the state is being queried
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

Expand All @@ -191,6 +200,11 @@ TGUI_MODULE_EXPORT namespace tgui

sf::Window* m_window = nullptr;

bool m_modifierKeySystemPressed = false;
bool m_modifierKeyControlPressed = false;
bool m_modifierKeyShiftPressed = false;
bool m_modifierKeyAltPressed = false;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};
}
Expand Down
4 changes: 2 additions & 2 deletions include/TGUI/Backend/Window/SFML/BackendSFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return Whether queries modifier key is being pressed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;
TGUI_DEPRECATED("Use gui.isKeyboardModifierPressed(modifierKey) instead") TGUI_NODISCARD bool isKeyboardModifierPressed(Event::KeyModifier modifierKey) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the contents of the clipboard
Expand Down Expand Up @@ -182,7 +182,7 @@ TGUI_MODULE_EXPORT namespace tgui
Cursor::Type mouseCursor = Cursor::Type::Arrow;
};

std::unordered_map<BackendGui*, GuiResources> m_guiResources;
std::unordered_map<const BackendGui*, GuiResources> m_guiResources;
std::unordered_map<Cursor::Type, std::unique_ptr<sf::Cursor>> m_mouseCursors;

static std::vector<std::unique_ptr<sf::Cursor>> m_leakedCursors;
Expand Down
30 changes: 26 additions & 4 deletions include/TGUI/Keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,19 @@ namespace tgui
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_NODISCARD inline bool isShiftPressed()
#ifndef TGUI_REMOVE_DEPRECATED_CODE
TGUI_DEPRECATED("Use isShiftPressed(gui) instead") TGUI_NODISCARD inline bool isShiftPressed()
{
TGUI_IGNORE_DEPRECATED_WARNINGS_START
return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Shift);
TGUI_IGNORE_DEPRECATED_WARNINGS_END
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_NODISCARD inline bool isShiftPressed(const BackendGui* gui)
{
return gui->isKeyboardModifierPressed(Event::KeyModifier::Shift);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -130,13 +139,26 @@ namespace tgui
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_NODISCARD inline bool isMultiselectModifierPressed()
#ifndef TGUI_REMOVE_DEPRECATED_CODE
TGUI_DEPRECATED("Use isMultiselectModifierPressed(gui) instead") TGUI_NODISCARD inline bool isMultiselectModifierPressed()
{
TGUI_IGNORE_DEPRECATED_WARNINGS_START
#ifdef TGUI_SYSTEM_MACOS
return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::System);
#else
return getBackend()->isKeyboardModifierPressed(Event::KeyModifier::Control);
#endif
TGUI_IGNORE_DEPRECATED_WARNINGS_END
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_NODISCARD inline bool isMultiselectModifierPressed(const BackendGui* gui)
{
#ifdef TGUI_SYSTEM_MACOS
return gui->isKeyboardModifierPressed(Event::KeyModifier::System);
#else
return gui->isKeyboardModifierPressed(Event::KeyModifier::Control);
#endif
}

Expand Down
9 changes: 9 additions & 0 deletions src/Backend/Window/BackendGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,15 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BackendGui::isKeyboardModifierPressed(Event::KeyModifier modifierKey) const
{
TGUI_IGNORE_DEPRECATED_WARNINGS_START
return getBackend()->isKeyboardModifierPressed(modifierKey);
TGUI_IGNORE_DEPRECATED_WARNINGS_END
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BackendGui::setKeyboardNavigationEnabled(bool enabled)
{
m_keyboardNavigationEnabled = enabled;
Expand Down
3 changes: 2 additions & 1 deletion src/Backend/Window/GLFW/BackendGLFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ namespace tgui

bool BackendGLFW::isKeyboardModifierPressed(Event::KeyModifier modifierKey)
{
/// TODO: If there are multiple windows then we need to do this on the focused window
// This code may be wrong when there are multiple windows. This function has been deprecated in favor of the
// function in the gui object, which calls glfwGetKey on the attached window instead of an arbitrarily one.
GLFWwindow* window = getAnyWindow();
if (!window)
return false;
Expand Down
20 changes: 20 additions & 0 deletions src/Backend/Window/GLFW/BackendGuiGLFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,26 @@ namespace tgui
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BackendGuiGLFW::isKeyboardModifierPressed(Event::KeyModifier modifierKey) const
{
switch (modifierKey)
{
case Event::KeyModifier::System:
return (glfwGetKey(m_window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(m_window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS);
case Event::KeyModifier::Control:
return (glfwGetKey(m_window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(m_window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS);
case Event::KeyModifier::Shift:
return (glfwGetKey(m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(m_window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
case Event::KeyModifier::Alt:
return (glfwGetKey(m_window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(m_window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS);
}

TGUI_ASSERT(false, "BackendGuiGLFW::isKeyboardModifierPressed called with an invalid value");
return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BackendGuiGLFW::setGuiWindow(GLFWwindow* window)
{
TGUI_ASSERT(std::dynamic_pointer_cast<BackendGLFW>(getBackend()), "BackendGuiGLFW requires system backend of type BackendGLFW");
Expand Down
20 changes: 20 additions & 0 deletions src/Backend/Window/Raylib/BackendGuiRaylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BackendGuiRaylib::isKeyboardModifierPressed(Event::KeyModifier modifierKey) const
{
switch (modifierKey)
{
case Event::KeyModifier::System:
return IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_RIGHT_SUPER);
case Event::KeyModifier::Control:
return IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
case Event::KeyModifier::Shift:
return IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
case Event::KeyModifier::Alt:
return IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT);
}

TGUI_ASSERT(false, "BackendGuiRaylib::isKeyboardModifierPressed called with an invalid value");
return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BackendGuiRaylib::endMainLoop()
{
m_endMainLoop = true;
Expand Down
21 changes: 21 additions & 0 deletions src/Backend/Window/SDL/BackendGuiSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,27 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BackendGuiSDL::isKeyboardModifierPressed(Event::KeyModifier modifierKey) const
{
const SDL_Keymod pressedModifiers = SDL_GetModState();
switch (modifierKey)
{
case Event::KeyModifier::System:
return (pressedModifiers & SDL_KMOD_GUI) != 0;
case Event::KeyModifier::Control:
return (pressedModifiers & SDL_KMOD_CTRL) != 0;
case Event::KeyModifier::Shift:
return (pressedModifiers & SDL_KMOD_SHIFT) != 0;
case Event::KeyModifier::Alt:
return (pressedModifiers & SDL_KMOD_ALT) != 0;
}

TGUI_ASSERT(false, "BackendGuiSDL::isKeyboardModifierPressed called with an invalid value");
return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BackendGuiSDL::setGuiWindow(SDL_Window* window)
{
TGUI_ASSERT(std::dynamic_pointer_cast<BackendSDL>(getBackend()), "BackendGuiSDL requires system backend of type BackendSDL");
Expand Down
Loading

0 comments on commit 6e21723

Please sign in to comment.