Skip to content

Commit 6da4043

Browse files
committed
handle events
1 parent 717524b commit 6da4043

File tree

10 files changed

+67
-28
lines changed

10 files changed

+67
-28
lines changed

include/gf2/core/Event.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include <cstdint>
77

8-
#include <array>
98
#include <string_view>
109
#include <variant>
1110

@@ -174,24 +173,24 @@ namespace gf {
174173
struct GF_CORE_API MouseMovedEvent {
175174
WindowId window_id;
176175
MouseId mouse_id;
177-
Vec2I position;
178-
Vec2I motion;
176+
Vec2F position;
177+
Vec2F motion;
179178
};
180179

181180
struct GF_CORE_API MouseButtonPressedEvent {
182181
WindowId window_id;
183182
MouseId mouse_id;
184183
MouseButton button;
185-
Vec2F position;
186184
uint8_t clicks;
185+
Vec2F position;
187186
};
188187

189188
struct GF_CORE_API MouseButtonReleasedEvent {
190189
WindowId window_id;
191190
MouseId mouse_id;
192191
MouseButton button;
193-
Vec2F position;
194192
uint8_t clicks;
193+
Vec2F position;
195194
};
196195

197196
struct GF_CORE_API MouseWheelScrolledEvent {

include/gf2/core/MouseTypes.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
#include <cstdint>
77

8+
#include <limits>
9+
#include <type_traits>
10+
811
namespace gf {
912

10-
enum class MouseButton : uint32_t { // NOLINT(performance-enum-size)
13+
enum class MouseButton : uint8_t {
1114
None,
1215
Left,
1316
Middle,
@@ -18,13 +21,13 @@ namespace gf {
1821
Touch2,
1922
};
2023

21-
constexpr MouseButton AnyMouseButton = static_cast<MouseButton>(0xFFFFFFFF);
24+
constexpr MouseButton AnyMouseButton = MouseButton{ std::numeric_limits<std::underlying_type_t<MouseButton>>::max() };
2225

2326
enum class MouseId : uint32_t;
2427

25-
constexpr MouseId TouchMouseId = static_cast<MouseId>(0xFFFFFFFF);
28+
constexpr MouseId TouchMouseId = MouseId{ std::numeric_limits<std::underlying_type_t<MouseId>>::max() };
2629

27-
enum class MouseWheelDirection : uint32_t { // NOLINT(performance-enum-size)
30+
enum class MouseWheelDirection { // NOLINT(performance-enum-size)
2831
Normal,
2932
Flipped,
3033
};

include/gf2/core/TouchTypes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ namespace gf {
1414

1515
constexpr TouchId MouseTouchId = TouchId{ std::numeric_limits<std::underlying_type_t<TouchId>>::max() };
1616

17+
enum class TouchDeviceType { // NOLINT(performance-enum-size)
18+
Invalid = -1,
19+
Direct,
20+
IndirectAbsolute,
21+
IndirectRelative,
22+
};
23+
1724
enum class FingerId : uint64_t;
1825

1926
} // namespace gf

include/gf2/graphics/Cursor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace gf {
4444
SDL_Cursor* m_cursor = nullptr;
4545
};
4646

47+
void capture_mouse(bool captured = true);
48+
4749
}
4850

4951
#endif // GF_CURSOR_H

include/gf2/graphics/Gamepad.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ namespace gf {
4747
void close();
4848

4949
private:
50-
friend class Gamepad;
50+
friend struct Gamepad;
5151
GamepadDevice(SDL_Gamepad* gamepad);
5252
SDL_Gamepad* m_gamepad = nullptr;
5353
};
5454

55-
class GF_GRAPHICS_API Gamepad {
56-
public:
55+
struct GF_GRAPHICS_API Gamepad {
5756
static GamepadDevice open(GamepadId id);
5857
static GamepadDevice from_id(GamepadId id);
5958

include/gf2/graphics/Keyboard.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#ifndef GF_KEYBOARD_H
44
#define GF_KEYBOARD_H
55

6+
#include <tuple>
7+
8+
#include <gf2/core/Flags.h>
69
#include <gf2/core/Keycode.h>
710
#include <gf2/core/Modifier.h>
811
#include <gf2/core/Scancode.h>
@@ -18,8 +21,8 @@ namespace gf {
1821
static const char* keycode_name(Keycode keycode);
1922
static Keycode keycode_from_name(const char* name);
2023

21-
static Keycode localize(Scancode scancode);
22-
static Scancode unlocalize(Keycode keycode);
24+
static Keycode localize(Scancode scancode, Flags<Modifier> modifiers);
25+
static std::tuple<Scancode, Flags<Modifier>> unlocalize(Keycode keycode);
2326
};
2427

2528
} // namespace gf

library/graphics/Cursor.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ namespace gf {
7979
return;
8080
}
8181

82+
// TODO: [SDL3] check return
8283
SDL_SetCursor(cursor->m_cursor);
8384
}
8485

86+
void capture_mouse(bool captured)
87+
{
88+
if (!SDL_CaptureMouse(captured)) {
89+
Log::error("Failed to capture the mouse: {}", SDL_GetError());
90+
}
91+
}
92+
8593
}

library/graphics/Keyboard.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
#include <gf2/graphics/Keyboard.h>
55

6+
#include <type_traits>
7+
68
#include <SDL3/SDL.h>
9+
#include "gf2/core/Modifier.h"
710

811
namespace gf {
912

1013
namespace {
1114

15+
static_assert(std::is_same_v<std::underlying_type_t<Modifier>, SDL_Keymod>);
16+
1217
template<Modifier Mod, SDL_Keymod Value>
1318
constexpr void modifier_check()
1419
{
@@ -493,14 +498,16 @@ namespace gf {
493498
return static_cast<Keycode>(SDL_GetKeyFromName(name));
494499
}
495500

496-
Keycode Keyboard::localize(Scancode scancode)
501+
Keycode Keyboard::localize(Scancode scancode, Flags<Modifier> modifiers)
497502
{
498-
return static_cast<Keycode>(SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(scancode), SDL_KMOD_NONE, true));
503+
return static_cast<Keycode>(SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(scancode), modifiers.value(), true));
499504
}
500505

501-
Scancode Keyboard::unlocalize(Keycode keycode)
506+
std::tuple<Scancode, Flags<Modifier>> Keyboard::unlocalize(Keycode keycode)
502507
{
503-
return static_cast<Scancode>(SDL_GetScancodeFromKey(static_cast<SDL_Keycode>(keycode), nullptr));
508+
SDL_Keymod modifiers = 0;
509+
SDL_Scancode scancode = SDL_GetScancodeFromKey(static_cast<SDL_Keycode>(keycode), &modifiers);
510+
return { static_cast<Scancode>(scancode), static_cast<Modifier>(modifiers) };
504511
}
505512

506513
} // namespace gf

library/graphics/Mouse.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ namespace gf {
1111

1212
namespace {
1313

14-
template<MouseButton Button, uint32_t Value>
14+
static_assert(std::is_same_v<std::underlying_type_t<MouseId>, SDL_MouseID>);
15+
static_assert(std::is_same_v<std::underlying_type_t<MouseWheelDirection>, std::make_signed_t<SDL_MouseWheelDirection>>);
16+
static_assert(TouchMouseId == static_cast<MouseId>(SDL_TOUCH_MOUSEID));
17+
18+
template<MouseButton Button, uint8_t Value>
1519
constexpr void mouse_button_check()
1620
{
17-
static_assert(static_cast<uint32_t>(Button) == Value, "Check gf::Mouse");
21+
static_assert(static_cast<uint8_t>(Button) == Value);
1822
}
1923

2024
[[maybe_unused]] constexpr void mouse_check()
@@ -24,9 +28,6 @@ namespace gf {
2428
mouse_button_check<MouseButton::Right, SDL_BUTTON_RIGHT>();
2529
mouse_button_check<MouseButton::XButton1, SDL_BUTTON_X1>();
2630
mouse_button_check<MouseButton::XButton2, SDL_BUTTON_X2>();
27-
28-
static_assert(std::is_same_v<std::underlying_type_t<MouseId>, decltype(SDL_TOUCH_MOUSEID)>, "Check gf::Mouse");
29-
static_assert(TouchMouseId == static_cast<MouseId>(SDL_TOUCH_MOUSEID), "Check gf::Mouse");
3031
}
3132

3233
} // namespace

library/graphics/Touch.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Copyright (c) 2023-2025 Julien Bernard
33

44
#include <type_traits>
5-
#include <utility>
65

76
#include <SDL3/SDL.h>
87

@@ -11,13 +10,24 @@
1110
namespace gf {
1211

1312
namespace {
13+
static_assert(std::is_same_v<std::underlying_type_t<TouchId>, SDL_TouchID>);
14+
static_assert(MouseTouchId == static_cast<TouchId>(SDL_MOUSE_TOUCHID));
15+
static_assert(std::is_same_v<std::underlying_type_t<TouchDeviceType>, std::underlying_type_t<SDL_TouchDeviceType>>);
1416

15-
[[maybe_unused]] constexpr void touch_check()
17+
static_assert(std::is_same_v<std::underlying_type_t<FingerId>, SDL_FingerID>);
18+
19+
template<TouchDeviceType Type, SDL_TouchDeviceType Value>
20+
constexpr void touch_device_type_check()
1621
{
17-
static_assert(std::is_same_v<std::underlying_type_t<TouchId>, decltype(SDL_MOUSE_TOUCHID)>);
18-
static_assert(MouseTouchId == static_cast<TouchId>(SDL_MOUSE_TOUCHID));
22+
static_assert(static_cast<SDL_TouchDeviceType>(Type) == Value);
23+
}
1924

20-
static_assert(std::is_same_v<std::underlying_type_t<FingerId>, decltype(std::declval<SDL_Finger>().id)>);
25+
[[maybe_unused]] constexpr void touch_check()
26+
{
27+
touch_device_type_check<TouchDeviceType::Invalid, SDL_TOUCH_DEVICE_INVALID>();
28+
touch_device_type_check<TouchDeviceType::Direct, SDL_TOUCH_DEVICE_DIRECT>();
29+
touch_device_type_check<TouchDeviceType::IndirectAbsolute, SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE>();
30+
touch_device_type_check<TouchDeviceType::IndirectRelative, SDL_TOUCH_DEVICE_INDIRECT_RELATIVE>();
2131
}
2232

2333
} // namespace

0 commit comments

Comments
 (0)