Skip to content

Commit

Permalink
Test experimental sf::Event API
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Nov 8, 2023
1 parent 426d9bb commit aa6b816
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
with:
repository: SFML/SFML
path: sfml
ref: master
ref: event_api

- name: Configure SFML
run: |
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main() {
while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event);

if (event.type == sf::Event::Closed) {
if (event.is<sf::Event::Closed>()) {
window.close();
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple_windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main() {
sf::Event event{};
while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event);
if (event.type == sf::Event::Closed) {
if (event.is<sf::Event::Closed>()) {
if (childWindow.isOpen()) {
childWindow.close();
}
Expand All @@ -33,7 +33,7 @@ int main() {
if (childWindow.isOpen()) {
while (childWindow.pollEvent(event)) {
ImGui::SFML::ProcessEvent(childWindow, event);
if (event.type == sf::Event::Closed) {
if (event.is<sf::Event::Closed>()) {
childWindow.close();
ImGui::SFML::Shutdown(childWindow);
}
Expand Down
133 changes: 80 additions & 53 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,94 +283,121 @@ void ProcessEvent(const sf::Window& window, const sf::Event& event) {
ImGuiIO& io = ImGui::GetIO();

if (s_currWindowCtx->windowHasFocus) {
switch (event.type) {
case sf::Event::Resized:
switch (event.getType()) {
case sf::Event::Type::Resized:{
const auto& resized = event.get<sf::Event::Resized>();
io.DisplaySize =
ImVec2(static_cast<float>(event.size.width), static_cast<float>(event.size.height));
break;
case sf::Event::MouseMoved:
io.AddMousePosEvent(static_cast<float>(event.mouseMove.x),
static_cast<float>(event.mouseMove.y));
ImVec2(static_cast<float>(resized.width), static_cast<float>(resized.height));
} break;
case sf::Event::Type::MouseMoved:{
const auto& mouseMoved = event.get<sf::Event::MouseMoved>();
io.AddMousePosEvent(static_cast<float>(mouseMoved.x),
static_cast<float>(mouseMoved.y));
s_currWindowCtx->mouseMoved = true;
break;
case sf::Event::MouseButtonPressed: // fall-through
case sf::Event::MouseButtonReleased: {
const int button = event.mouseButton.button;
} break;
case sf::Event::Type::MouseButtonPressed: {
const auto& mouseButtonPressed = event.get<sf::Event::MouseButtonPressed>();
const int button = mouseButtonPressed.button;
if (button >= 0 && button < 3) {
if (event.type == sf::Event::MouseButtonPressed) {
s_currWindowCtx->mousePressed[event.mouseButton.button] = true;
io.AddMouseButtonEvent(button, true);
} else {
io.AddMouseButtonEvent(button, false);
}
s_currWindowCtx->mousePressed[button] = true;
io.AddMouseButtonEvent(button, true);
}
} break;
case sf::Event::TouchBegan: // fall-through
case sf::Event::TouchEnded: {
s_currWindowCtx->mouseMoved = false;
const unsigned int button = event.touch.finger;
if (event.type == sf::Event::TouchBegan && button < 3) {
s_currWindowCtx->touchDown[event.touch.finger] = true;
case sf::Event::Type::MouseButtonReleased: {
const int button = event.get<sf::Event::MouseButtonReleased>().button;
if (button >= 0 && button < 3) {
io.AddMouseButtonEvent(button, false);
}
} break;
case sf::Event::MouseWheelScrolled:
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel ||
(event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel && io.KeyShift)) {
io.AddMouseWheelEvent(0, event.mouseWheelScroll.delta);
} else if (event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel) {
io.AddMouseWheelEvent(event.mouseWheelScroll.delta, 0);
case sf::Event::Type::TouchBegan: {
s_currWindowCtx->mouseMoved = false;
const unsigned int button = event.get<sf::Event::TouchBegan>().finger;
if (button < 3) {
s_currWindowCtx->touchDown[button] = true;
}
} break;
case sf::Event::Type::TouchEnded:
s_currWindowCtx->mouseMoved = false;
break;
case sf::Event::KeyPressed: // fall-through
case sf::Event::KeyReleased: {
const bool down = (event.type == sf::Event::KeyPressed);
case sf::Event::Type::MouseWheelScrolled: {
const auto& mouseWheelScrolled = event.get<sf::Event::MouseWheelScrolled>();
if (mouseWheelScrolled.wheel == sf::Mouse::VerticalWheel ||
(mouseWheelScrolled.wheel == sf::Mouse::HorizontalWheel && io.KeyShift)) {
io.AddMouseWheelEvent(0, mouseWheelScrolled.delta);
} else if (mouseWheelScrolled.wheel == sf::Mouse::HorizontalWheel) {
io.AddMouseWheelEvent(mouseWheelScrolled.delta, 0);
}
} break;
case sf::Event::Type::KeyPressed: {
const auto& keyPressed = event.get<sf::Event::KeyPressed>();
const ImGuiKey mod = keycodeToImGuiMod(keyPressed.code);
// The modifier booleans are not reliable when it's the modifier
// itself that's being pressed. Detect these presses directly.
if (mod != ImGuiKey_None) {
io.AddKeyEvent(mod, true);
} else {
io.AddKeyEvent(ImGuiKey_ModCtrl, keyPressed.control);
io.AddKeyEvent(ImGuiKey_ModShift, keyPressed.shift);
io.AddKeyEvent(ImGuiKey_ModAlt, keyPressed.alt);
io.AddKeyEvent(ImGuiKey_ModSuper, keyPressed.system);
}

const ImGuiKey mod = keycodeToImGuiMod(event.key.code);
const ImGuiKey key = keycodeToImGuiKey(keyPressed.code);
io.AddKeyEvent(key, true);
io.SetKeyEventNativeData(key, keyPressed.code, -1);
} break;
case sf::Event::Type::KeyReleased: {
const auto& keyReleased = event.get<sf::Event::KeyReleased>();
const ImGuiKey mod = keycodeToImGuiMod(keyReleased.code);
// The modifier booleans are not reliable when it's the modifier
// itself that's being pressed. Detect these presses directly.
if (mod != ImGuiKey_None) {
io.AddKeyEvent(mod, down);
io.AddKeyEvent(mod, false);
} else {
io.AddKeyEvent(ImGuiKey_ModCtrl, event.key.control);
io.AddKeyEvent(ImGuiKey_ModShift, event.key.shift);
io.AddKeyEvent(ImGuiKey_ModAlt, event.key.alt);
io.AddKeyEvent(ImGuiKey_ModSuper, event.key.system);
io.AddKeyEvent(ImGuiKey_ModCtrl, keyReleased.control);
io.AddKeyEvent(ImGuiKey_ModShift, keyReleased.shift);
io.AddKeyEvent(ImGuiKey_ModAlt, keyReleased.alt);
io.AddKeyEvent(ImGuiKey_ModSuper, keyReleased.system);
}

const ImGuiKey key = keycodeToImGuiKey(event.key.code);
io.AddKeyEvent(key, down);
io.SetKeyEventNativeData(key, event.key.code, -1);
const ImGuiKey key = keycodeToImGuiKey(keyReleased.code);
io.AddKeyEvent(key, false);
io.SetKeyEventNativeData(key, keyReleased.code, -1);
} break;
case sf::Event::TextEntered:
case sf::Event::Type::TextEntered: {
const auto unicode = event.get<sf::Event::TextEntered>().unicode;
// Don't handle the event for unprintable characters
if (event.text.unicode < ' ' || event.text.unicode == 127) {
if (unicode < ' ' || unicode == 127) {
break;
}
io.AddInputCharacter(event.text.unicode);
break;
case sf::Event::JoystickConnected:
io.AddInputCharacter(unicode);
} break;
case sf::Event::Type::JoystickConnected: {
const auto joystickId = event.get<sf::Event::JoystickConnected>().joystickId;
if (s_currWindowCtx->joystickId == NULL_JOYSTICK_ID) {
s_currWindowCtx->joystickId = event.joystickConnect.joystickId;
s_currWindowCtx->joystickId = joystickId;
}
break;
case sf::Event::JoystickDisconnected:
if (s_currWindowCtx->joystickId == event.joystickConnect.joystickId) { // used gamepad
} break;
case sf::Event::Type::JoystickDisconnected: {
const auto joystickId = event.get<sf::Event::JoystickDisconnected>().joystickId;
if (s_currWindowCtx->joystickId == joystickId) { // used gamepad
// was
// disconnected
s_currWindowCtx->joystickId = getConnectedJoystickId();
}
break;
}
default:
break;
}
}

switch (event.type) {
case sf::Event::LostFocus: {
switch (event.getType()) {
case sf::Event::Type::LostFocus: {
io.AddFocusEvent(false);
s_currWindowCtx->windowHasFocus = false;
} break;
case sf::Event::GainedFocus:
case sf::Event::Type::GainedFocus:
io.AddFocusEvent(true);
s_currWindowCtx->windowHasFocus = true;
break;
Expand Down

0 comments on commit aa6b816

Please sign in to comment.