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

View file

@ -1,16 +1,17 @@
#include <SDL3/SDL_keycode.h>
#include "window/input.hpp"
#include "window/input.hpp"
#include "window/window.hpp"
struct SDL_Window;
struct window_sdl;
class input_sdl final : public Input {
public:
inline static constexpr short keys_buffer_size = 1036;
inline static constexpr short mouse_keys_offset = 1024;
inline static constexpr short mouse_keys_offset = 512;
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;
@ -51,4 +52,6 @@ private:
std::vector<uint> codepoints;
std::vector<Keycode> pressedKeys;
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
window_sdl& window;
};

View file

@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_video.h>
@ -239,6 +240,62 @@ void window_sdl::setShouldClose(bool flag) {
}
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() {
fullscreen = !fullscreen;
@ -305,6 +362,9 @@ std::unique_ptr<ImageData> window_sdl::takeScreenshot() {
[[nodiscard]] bool window_sdl::isValid() const {
return isSuccessfull;
}
[[nodiscard]] SDL_Window *window_sdl::getSdlWindow() const {
return window;
}
std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>> Window::initialize(
DisplaySettings *settings, std::string title
@ -313,7 +373,7 @@ std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>> Window::initialize(
if (!window->isValid()) {
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)};
}

View file

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

View file

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