Skip to content

Commit c330984

Browse files
committed
Update to new SFML event API
1 parent 45050eb commit c330984

File tree

1 file changed

+51
-71
lines changed

1 file changed

+51
-71
lines changed

imgui-SFML.cpp

+51-71
Original file line numberDiff line numberDiff line change
@@ -283,100 +283,80 @@ void ProcessEvent(const sf::Window& window, const sf::Event& event) {
283283
ImGuiIO& io = ImGui::GetIO();
284284

285285
if (s_currWindowCtx->windowHasFocus) {
286-
switch (event.type) {
287-
case sf::Event::Resized:
286+
if (const auto* resized = event.getIf<sf::Event::Resized>()) {
288287
io.DisplaySize =
289-
ImVec2(static_cast<float>(event.size.width), static_cast<float>(event.size.height));
290-
break;
291-
case sf::Event::MouseMoved:
292-
io.AddMousePosEvent(static_cast<float>(event.mouseMove.x),
293-
static_cast<float>(event.mouseMove.y));
288+
ImVec2(static_cast<float>(resized->size.x), static_cast<float>(resized->size.y));
289+
} else if (const auto* mouseMoved = event.getIf<sf::Event::MouseMoved>()) {
290+
io.AddMousePosEvent(static_cast<float>(mouseMoved->position.x),
291+
static_cast<float>(mouseMoved->position.y));
294292
s_currWindowCtx->mouseMoved = true;
295-
break;
296-
case sf::Event::MouseButtonPressed: // fall-through
297-
case sf::Event::MouseButtonReleased: {
298-
const int button = static_cast<int>(event.mouseButton.button);
293+
} else if (const auto* mouseButtonPressed = event.getIf<sf::Event::MouseButtonPressed>()) {
294+
const int button = static_cast<int>(mouseButtonPressed->button);
299295
if (button >= 0 && button < 3) {
300-
if (event.type == sf::Event::MouseButtonPressed) {
301-
s_currWindowCtx->mousePressed[static_cast<int>(event.mouseButton.button)] =
302-
true;
303-
io.AddMouseButtonEvent(button, true);
304-
} else {
305-
io.AddMouseButtonEvent(button, false);
306-
}
296+
s_currWindowCtx->mousePressed[static_cast<int>(mouseButtonPressed->button)] = true;
297+
io.AddMouseButtonEvent(button, true);
307298
}
308-
} break;
309-
case sf::Event::TouchBegan: // fall-through
310-
case sf::Event::TouchEnded: {
299+
} else if (const auto* mouseButtonReleased =
300+
event.getIf<sf::Event::MouseButtonReleased>()) {
301+
const int button = static_cast<int>(mouseButtonReleased->button);
302+
if (button >= 0 && button < 3) io.AddMouseButtonEvent(button, false);
303+
} else if (const auto* touchBegan = event.getIf<sf::Event::TouchBegan>()) {
311304
s_currWindowCtx->mouseMoved = false;
312-
const unsigned int button = event.touch.finger;
313-
if (event.type == sf::Event::TouchBegan && button < 3) {
314-
s_currWindowCtx->touchDown[event.touch.finger] = true;
315-
}
316-
} break;
317-
case sf::Event::MouseWheelScrolled:
318-
if (event.mouseWheelScroll.wheel == sf::Mouse::Wheel::Vertical ||
319-
(event.mouseWheelScroll.wheel == sf::Mouse::Wheel::Horizontal && io.KeyShift)) {
320-
io.AddMouseWheelEvent(0, event.mouseWheelScroll.delta);
321-
} else if (event.mouseWheelScroll.wheel == sf::Mouse::Wheel::Horizontal) {
322-
io.AddMouseWheelEvent(event.mouseWheelScroll.delta, 0);
305+
const unsigned int button = touchBegan->finger;
306+
if (button < 3) s_currWindowCtx->touchDown[touchBegan->finger] = true;
307+
} else if (event.is<sf::Event::TouchEnded>()) {
308+
s_currWindowCtx->mouseMoved = false;
309+
} else if (const auto* mouseWheelScrolled = event.getIf<sf::Event::MouseWheelScrolled>()) {
310+
if (mouseWheelScrolled->wheel == sf::Mouse::Wheel::Vertical ||
311+
(mouseWheelScrolled->wheel == sf::Mouse::Wheel::Horizontal && io.KeyShift)) {
312+
io.AddMouseWheelEvent(0, mouseWheelScrolled->delta);
313+
} else if (mouseWheelScrolled->wheel == sf::Mouse::Wheel::Horizontal) {
314+
io.AddMouseWheelEvent(mouseWheelScrolled->delta, 0);
323315
}
324-
break;
325-
case sf::Event::KeyPressed: // fall-through
326-
case sf::Event::KeyReleased: {
327-
const bool down = (event.type == sf::Event::KeyPressed);
328-
329-
const ImGuiKey mod = keycodeToImGuiMod(event.key.code);
316+
}
317+
const auto handleKeyChanged = [&io](const sf::Event::KeyChanged& keyChanged, bool down) {
318+
const ImGuiKey mod = keycodeToImGuiMod(keyChanged.code);
330319
// The modifier booleans are not reliable when it's the modifier
331320
// itself that's being pressed. Detect these presses directly.
332321
if (mod != ImGuiKey_None) {
333322
io.AddKeyEvent(mod, down);
334323
} else {
335-
io.AddKeyEvent(ImGuiKey_ModCtrl, event.key.control);
336-
io.AddKeyEvent(ImGuiKey_ModShift, event.key.shift);
337-
io.AddKeyEvent(ImGuiKey_ModAlt, event.key.alt);
338-
io.AddKeyEvent(ImGuiKey_ModSuper, event.key.system);
324+
io.AddKeyEvent(ImGuiKey_ModCtrl, keyChanged.control);
325+
io.AddKeyEvent(ImGuiKey_ModShift, keyChanged.shift);
326+
io.AddKeyEvent(ImGuiKey_ModAlt, keyChanged.alt);
327+
io.AddKeyEvent(ImGuiKey_ModSuper, keyChanged.system);
339328
}
340329

341-
const ImGuiKey key = keycodeToImGuiKey(event.key.code);
330+
const ImGuiKey key = keycodeToImGuiKey(keyChanged.code);
342331
io.AddKeyEvent(key, down);
343-
io.SetKeyEventNativeData(key, static_cast<int>(event.key.code), -1);
344-
} break;
345-
case sf::Event::TextEntered:
332+
io.SetKeyEventNativeData(key, static_cast<int>(keyChanged.code), -1);
333+
};
334+
if (const auto* keyPressed = event.getIf<sf::Event::KeyPressed>()) {
335+
handleKeyChanged(*keyPressed, true);
336+
} else if (const auto* keyReleased = event.getIf<sf::Event::KeyReleased>()) {
337+
handleKeyChanged(*keyReleased, false);
338+
} else if (const auto* textEntered = event.getIf<sf::Event::TextEntered>()) {
346339
// Don't handle the event for unprintable characters
347-
if (event.text.unicode < ' ' || event.text.unicode == 127) {
348-
break;
349-
}
350-
io.AddInputCharacter(event.text.unicode);
351-
break;
352-
case sf::Event::JoystickConnected:
353-
if (s_currWindowCtx->joystickId == NULL_JOYSTICK_ID) {
354-
s_currWindowCtx->joystickId = event.joystickConnect.joystickId;
355-
}
356-
break;
357-
case sf::Event::JoystickDisconnected:
358-
if (s_currWindowCtx->joystickId == event.joystickConnect.joystickId) { // used gamepad
359-
// was
360-
// disconnected
340+
if (!(textEntered->unicode < ' ' || textEntered->unicode == 127))
341+
io.AddInputCharacter(textEntered->unicode);
342+
} else if (const auto* joystickConnected = event.getIf<sf::Event::JoystickConnected>()) {
343+
if (s_currWindowCtx->joystickId == NULL_JOYSTICK_ID)
344+
s_currWindowCtx->joystickId = joystickConnected->joystickId;
345+
} else if (const auto* joystickDisconnected =
346+
event.getIf<sf::Event::JoystickDisconnected>()) {
347+
if (s_currWindowCtx->joystickId == joystickDisconnected->joystickId)
348+
// used gamepad was disconnected
361349
s_currWindowCtx->joystickId = getConnectedJoystickId();
362-
}
363-
break;
364-
default:
365-
break;
366350
}
367351
}
368352

369-
switch (event.type) {
370-
case sf::Event::LostFocus: {
353+
if (event.is<sf::Event::FocusLost>()) {
371354
io.AddFocusEvent(false);
372355
s_currWindowCtx->windowHasFocus = false;
373-
} break;
374-
case sf::Event::GainedFocus:
356+
}
357+
if (event.is<sf::Event::FocusGained>()) {
375358
io.AddFocusEvent(true);
376359
s_currWindowCtx->windowHasFocus = true;
377-
break;
378-
default:
379-
break;
380360
}
381361
}
382362

0 commit comments

Comments
 (0)