Mouse work

This commit is contained in:
Stepanov Igor 2025-10-05 12:16:09 +03:00
parent 433a2eae15
commit 51927ee350
5 changed files with 141 additions and 79 deletions

View file

@ -6,8 +6,10 @@
#include <SDL3/SDL_keyboard.h> #include <SDL3/SDL_keyboard.h>
#include <SDL3/SDL_keycode.h> #include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_mouse.h> #include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_video.h>
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "window/detail/window_sdl.hpp"
#include "window/input.hpp" #include "window/input.hpp"
static debug::Logger logger("input"); static debug::Logger logger("input");
@ -24,7 +26,7 @@ Keycode input_util::keycode_from(const std::string& name) {
return static_cast<Keycode>(SDL_GetKeyFromName(name.c_str())); return static_cast<Keycode>(SDL_GetKeyFromName(name.c_str()));
} }
input_sdl::input_sdl() { input_sdl::input_sdl(window_sdl& window) : window(window) {
input_util::initialize(); input_util::initialize();
} }
@ -42,28 +44,26 @@ void input_sdl::pollEvents() {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
window.setShouldClose(true);
break; break;
case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_DOWN:
logger.info() << "Keyboard button: " << event.key.key; logger.info() << "Keyboard button: " << event.key.scancode;
if (event.key.key >= keys_buffer_size) { prevPressed = keys[event.key.scancode];
// Win key return 1073742051 keys[event.key.scancode] = true;
break; frames[event.key.scancode] = currentFrame;
}
prevPressed = keys[event.key.key];
keys[event.key.key] = true;
frames[event.key.key] = currentFrame;
if (!prevPressed) { if (!prevPressed) {
keyCallbacks[static_cast<Keycode>(event.key.key)].notify(); keyCallbacks[static_cast<Keycode>(event.key.scancode)]
.notify();
} }
pressedKeys.push_back(static_cast<Keycode>(event.key.key)); pressedKeys.push_back(static_cast<Keycode>(event.key.scancode));
break; break;
case SDL_EVENT_KEY_UP: case SDL_EVENT_KEY_UP:
if (event.key.key >= keys_buffer_size) { if (event.key.scancode >= keys_buffer_size) {
// Win key return 1073742051 // Win key return 1073742051
break; break;
} }
keys[event.key.key] = false; keys[event.key.scancode] = false;
frames[event.key.key] = currentFrame; frames[event.key.scancode] = currentFrame;
break; break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_DOWN:
prevPressed = keys[event.button.button + mouse_keys_offset]; prevPressed = keys[event.button.button + mouse_keys_offset];
@ -84,6 +84,8 @@ void input_sdl::pollEvents() {
cursor = {event.motion.x, event.motion.y}; cursor = {event.motion.x, event.motion.y};
delta = {event.motion.xrel, event.motion.yrel}; delta = {event.motion.xrel, event.motion.yrel};
break; break;
case SDL_EVENT_WINDOW_RESIZED:
break;
} }
} }
for (auto& [_, binding] : bindings.getAll()) { for (auto& [_, binding] : bindings.getAll()) {
@ -102,7 +104,6 @@ void input_sdl::pollEvents() {
newstate = clicked(static_cast<Mousecode>(binding.code)); newstate = clicked(static_cast<Mousecode>(binding.code));
break; break;
} }
if (newstate) { if (newstate) {
if (!binding.state) { if (!binding.state) {
binding.state = true; binding.state = true;
@ -135,7 +136,7 @@ bool input_sdl::pressed(Keycode key) const {
if (keycode < 0 || keycode >= keys_buffer_size) { if (keycode < 0 || keycode >= keys_buffer_size) {
return false; return false;
} }
if (keys[keycode]) logger.info() << "lol"; if (keys[keycode]) logger.info() << "kek";
return keys[keycode]; return keys[keycode];
} }
bool input_sdl::jpressed(Keycode keycode) const { bool input_sdl::jpressed(Keycode keycode) const {
@ -162,13 +163,9 @@ bool input_sdl::isCursorLocked() const {
} }
void input_sdl::toggleCursor() { void input_sdl::toggleCursor() {
// cursorDrag = false; cursorDrag = false;
// if (cursorLocked) { SDL_SetWindowMouseGrab(window.getSdlWindow(), cursorLocked);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); cursorLocked = !cursorLocked;
// } else {
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// }
// cursorLocked = !cursorLocked;
} }
Bindings& input_sdl::getBindings() { Bindings& input_sdl::getBindings() {

View file

@ -1,16 +1,17 @@
#include <SDL3/SDL_keycode.h> #include <SDL3/SDL_keycode.h>
#include "window/input.hpp"
#include "window/input.hpp"
#include "window/window.hpp" #include "window/window.hpp"
struct SDL_Window; struct window_sdl;
class input_sdl final : public Input { class input_sdl final : public Input {
public: public:
inline static constexpr short keys_buffer_size = 1036; inline static constexpr short mouse_keys_offset = 512;
inline static constexpr short mouse_keys_offset = 1024; inline static constexpr short keys_buffer_size =
mouse_keys_offset + sizeof(mousecodes_all) / sizeof(mousecodes_all[0]);
input_sdl(); input_sdl(window_sdl& window);
void pollEvents() override; void pollEvents() override;
@ -51,4 +52,6 @@ private:
std::vector<uint> codepoints; std::vector<uint> codepoints;
std::vector<Keycode> pressedKeys; std::vector<Keycode> pressedKeys;
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks; std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
window_sdl& window;
}; };

View file

@ -3,6 +3,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_error.h> #include <SDL3/SDL_error.h>
#include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_timer.h> #include <SDL3/SDL_timer.h>
#include <SDL3/SDL_version.h> #include <SDL3/SDL_version.h>
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
@ -239,6 +240,62 @@ void window_sdl::setShouldClose(bool flag) {
} }
void window_sdl::setCursor(CursorShape shape) { void window_sdl::setCursor(CursorShape shape) {
SDL_Cursor* cursor;
switch (shape) {
case CursorShape::ARROW:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_DEFAULT
);
break;
case CursorShape::TEXT:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_TEXT
);
break;
case CursorShape::CROSSHAIR:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_CROSSHAIR
);
break;
case CursorShape::POINTER:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_POINTER
);
break;
case CursorShape::EW_RESIZE:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_EW_RESIZE
);
break;
case CursorShape::NS_RESIZE:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_NS_RESIZE
);
break;
case CursorShape::NWSE_RESIZE:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_NWSE_RESIZE
);
break;
case CursorShape::NESW_RESIZE:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_NESW_RESIZE
);
break;
case CursorShape::ALL_RESIZE:
// ??
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_NWSE_RESIZE
);
break;
case CursorShape::NOT_ALLOWED:
cursor = SDL_CreateSystemCursor(
SDL_SystemCursor::SDL_SYSTEM_CURSOR_NOT_ALLOWED
);
break;
SDL_SetCursor(cursor);
SDL_DestroyCursor(cursor);
}
} }
void window_sdl::toggleFullscreen() { void window_sdl::toggleFullscreen() {
fullscreen = !fullscreen; fullscreen = !fullscreen;
@ -305,6 +362,9 @@ std::unique_ptr<ImageData> window_sdl::takeScreenshot() {
[[nodiscard]] bool window_sdl::isValid() const { [[nodiscard]] bool window_sdl::isValid() const {
return isSuccessfull; return isSuccessfull;
} }
[[nodiscard]] SDL_Window *window_sdl::getSdlWindow() const {
return window;
}
std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>> Window::initialize( std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>> Window::initialize(
DisplaySettings *settings, std::string title DisplaySettings *settings, std::string title
@ -313,7 +373,7 @@ std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>> Window::initialize(
if (!window->isValid()) { if (!window->isValid()) {
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
auto input = std::make_unique<input_sdl>(); auto input = std::make_unique<input_sdl>(*window);
return {std::move(window), std::move(input)}; return {std::move(window), std::move(input)};
} }

View file

@ -13,6 +13,7 @@ public:
window_sdl &operator=(window_sdl &&) = default; window_sdl &operator=(window_sdl &&) = default;
void swapBuffers() const noexcept override; void swapBuffers() const noexcept override;
bool isMaximized() const override; bool isMaximized() const override;
bool isFocused() const override; bool isFocused() const override;
bool isIconified() const override; bool isIconified() const override;
@ -38,6 +39,7 @@ public:
std::unique_ptr<ImageData> takeScreenshot() override; std::unique_ptr<ImageData> takeScreenshot() override;
[[nodiscard]] bool isValid() const override; [[nodiscard]] bool isValid() const override;
[[nodiscard]] SDL_Window* getSdlWindow() const;
private: private:
bool isSuccessfull = true; bool isSuccessfull = true;
bool maximized = false; bool maximized = false;

View file

@ -15,14 +15,14 @@ enum class BindType { BIND = 0, REBIND = 1 };
/// @brief Represents sdl scancode values. /// @brief Represents sdl scancode values.
enum class Keycode : std::int32_t { enum class Keycode : std::int32_t {
SPACE = 0x00000020u, SPACE = 44,
APOSTROPHE = 0x00000027u, APOSTROPHE = 52,
COMMA = 0x0000002cu, COMMA = 54,
MINUS = 0x0000002du, MINUS = 45,
PERIOD = 0x0000002eu, PERIOD = 55,
SLASH = 0x0000002fu, SLASH = 56,
NUM_0 = 0x40000059u, NUM_0 = 39,
NUM_1, NUM_1 = 30,
NUM_2, NUM_2,
NUM_3, NUM_3,
NUM_4, NUM_4,
@ -31,9 +31,9 @@ enum class Keycode : std::int32_t {
NUM_7, NUM_7,
NUM_8, NUM_8,
NUM_9, NUM_9,
SEMICOLON = 0x0000003bu, SEMICOLON = 51,
EQUAL = 0x0000003du, EQUAL = 46,
A = 0x00000061u, A = 4,
B, B,
C, C,
D, D,
@ -59,29 +59,29 @@ enum class Keycode : std::int32_t {
X, X,
Y, Y,
Z, Z,
LEFT_BRACKET = 0x0000005bu, LEFT_BRACKET = 47,
BACKSLASH = 0x0000005cu, BACKSLASH = 49,
RIGHT_BRACKET = 0x0000005du, RIGHT_BRACKET = 48,
GRAVE_ACCENT = 0x00000060u, GRAVE_ACCENT = 53,
ESCAPE = 0x0000001bu, ESCAPE = 41,
ENTER = 0x0000000du, ENTER = 40,
TAB = 0x00000009u, TAB = 43,
BACKSPACE = 0x00000008u, BACKSPACE = 42,
INSERT = 0x40000049u, INSERT = 73,
DELETE = 0x0000007fu, DELETE = 76,
LEFT = 0x40000050u, LEFT = 80,
RIGHT = 0x4000004fu, RIGHT = 79,
DOWN = 0x40000051u, DOWN = 81,
UP = 0x40000052u, UP = 82,
PAGE_UP = 0x4000004bu, PAGE_UP = 75,
PAGE_DOWN = 0x4000004eu, PAGE_DOWN = 78,
HOME = 0x4000004au, HOME = 74,
END = 0x4000004du, END = 77,
CAPS_LOCK = 0x40000039u, CAPS_LOCK = 57,
NUM_LOCK = 0x40000053u, NUM_LOCK = 83,
PRINT_SCREEN = 0x40000046u, PRINT_SCREEN = 70,
PAUSE = 0x40000048u, PAUSE = 72,
F1 = 0x4000003au, F1 = 58,
F2, F2,
F3, F3,
F4, F4,
@ -93,27 +93,27 @@ enum class Keycode : std::int32_t {
F10, F10,
F11, F11,
F12, F12,
LEFT_SHIFT = 0x400000e1u, LEFT_SHIFT = 225,
LEFT_CONTROL = 0x400000e0u, LEFT_CONTROL = 224,
LEFT_ALT = 0x400000e2u, LEFT_ALT = 226,
// LEFT_SUPER = 343, LEFT_SUPER = 227,
RIGHT_SHIFT = 0x400000e5u, RIGHT_SHIFT = 229,
RIGHT_CONTROL = 0x400000e4u, RIGHT_CONTROL = 228,
RIGHT_ALT = 0x400000e6u, RIGHT_ALT = 230,
// RIGHT_SUPER = 347, RIGHT_SUPER = 231,
MENU = 0x40000076u, MENU = 118,
UNKNOWN = 0x00000000u UNKNOWN = 0
}; };
/// @brief Represents sdl mouse button IDs. /// @brief Represents sdl mouse button IDs.
/// @details There is a subset of sdl mouse button IDs. /// @details There is a subset of sdl mouse button IDs.
enum class Mousecode : int { enum class Mousecode : int {
BUTTON_1 = 0, // Left mouse button BUTTON_1 = 1, // Left mouse button
BUTTON_2 = 1, // Right mouse button BUTTON_2 = 2, // Right mouse button
BUTTON_3 = 2, // Middle mouse button BUTTON_3 = 3, // Middle mouse button
BUTTON_4 = 3, // Side mouse button 1 BUTTON_4 = 4, // Side mouse button 1
BUTTON_5 = 4, // Side mouse button 2 BUTTON_5 = 5, // Side mouse button 2
UNKNOWN = -1, UNKNOWN = 0,
}; };
inline Mousecode mousecodes_all[] { inline Mousecode mousecodes_all[] {