Fix mouse pos detect

This commit is contained in:
Stepanov Igor 2025-10-05 02:51:05 +03:00
parent 2293f6f5fa
commit 433a2eae15
4 changed files with 264 additions and 165 deletions

View file

@ -3,65 +3,119 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_clipboard.h> #include <SDL3/SDL_clipboard.h>
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.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 <unordered_map>
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "window/input.hpp"
static debug::Logger logger("input"); static debug::Logger logger("input");
std::string input_util::to_string(Keycode code) { std::string input_util::to_string(Keycode code) {
return {}; int icode_repr = static_cast<int>(code);
const char* name = SDL_GetKeyName(icode_repr);
logger.info() << icode_repr << ": " << name;
return std::string(name);
}
Keycode input_util::keycode_from(const std::string& name) {
logger.info() << name << ": " << SDL_GetKeyFromName(name.c_str());
return static_cast<Keycode>(SDL_GetKeyFromName(name.c_str()));
} }
input_sdl::input_sdl() { input_sdl::input_sdl() {
input_util::initialize();
} }
void input_sdl::pollEvents() { void input_sdl::pollEvents() {
/* static */ SDL_Event event; delta.x = 0.0f;
delta.y = 0.0f;
scroll = 0;
currentFrame++;
codepoints.clear();
pressedKeys.clear();
bool prevPressed = false;
static SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
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;
if (!prevPressed) {
keyCallbacks[static_cast<Keycode>(event.key.key)].notify();
}
pressedKeys.push_back(static_cast<Keycode>(event.key.key));
break;
case SDL_EVENT_KEY_UP:
if (event.key.key >= keys_buffer_size) {
// Win key return 1073742051
break;
}
keys[event.key.key] = false;
frames[event.key.key] = currentFrame;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
prevPressed = keys[event.button.button + mouse_keys_offset];
keys[event.button.button + mouse_keys_offset] = true;
frames[event.button.button + mouse_keys_offset] = currentFrame;
if (!prevPressed) {
keyCallbacks[static_cast<Keycode>(
event.button.button + mouse_keys_offset
)]
.notify();
}
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
keys[event.button.button + mouse_keys_offset] = false;
frames[event.button.button + mouse_keys_offset] = currentFrame;
break;
case SDL_EVENT_MOUSE_MOTION:
cursor = {event.motion.x, event.motion.y};
delta = {event.motion.xrel, event.motion.yrel};
break;
}
} }
// delta.x = 0.0f; for (auto& [_, binding] : bindings.getAll()) {
// delta.y = 0.0f; if (!binding.enabled) {
// scroll = 0; binding.state = false;
// currentFrame++; continue;
// codepoints.clear(); }
// pressedKeys.clear(); binding.justChanged = false;
// glfwPollEvents();
// for (auto& [_, binding] : bindings.getAll()) { bool newstate = false;
// if (!binding.enabled) { switch (binding.type) {
// binding.state = false; case InputType::KEYBOARD:
// continue; newstate = pressed(static_cast<Keycode>(binding.code));
// } break;
// binding.justChanged = false; case InputType::MOUSE:
newstate = clicked(static_cast<Mousecode>(binding.code));
break;
}
// bool newstate = false; if (newstate) {
// switch (binding.type) { if (!binding.state) {
// case InputType::KEYBOARD: binding.state = true;
// newstate = pressed(static_cast<Keycode>(binding.code)); binding.justChanged = true;
// break; binding.onactived.notify();
// case InputType::MOUSE: }
// newstate = clicked(static_cast<Mousecode>(binding.code)); } else {
// break; if (binding.state) {
// } binding.state = false;
binding.justChanged = true;
// if (newstate) { }
// if (!binding.state) { }
// binding.state = true; }
// binding.justChanged = true;
// binding.onactived.notify();
// }
// } else {
// if (binding.state) {
// binding.state = false;
// binding.justChanged = true;
// }
// }
// }
} }
const char* input_sdl::getClipboardText() const { const char* input_sdl::getClipboardText() const {
@ -73,29 +127,38 @@ void input_sdl::setClipboardText(const char* text) {
} }
int input_sdl::getScroll() { int input_sdl::getScroll() {
return {}; return scroll;
} }
bool input_sdl::pressed(Keycode key) const { bool input_sdl::pressed(Keycode key) const {
return {}; int keycode = static_cast<int>(key);
if (keycode < 0 || keycode >= keys_buffer_size) {
return false;
}
if (keys[keycode]) logger.info() << "lol";
return keys[keycode];
} }
bool input_sdl::jpressed(Keycode keycode) const { bool input_sdl::jpressed(Keycode keycode) const {
return {}; return pressed(keycode) &&
frames[static_cast<int>(keycode)] == currentFrame;
} }
bool input_sdl::clicked(Mousecode code) const { bool input_sdl::clicked(Mousecode code) const {
return {}; return pressed(
static_cast<Keycode>(mouse_keys_offset + static_cast<int>(code))
);
} }
bool input_sdl::jclicked(Mousecode code) const { bool input_sdl::jclicked(Mousecode code) const {
return {}; return clicked(code) &&
frames[static_cast<int>(code) + mouse_keys_offset] == currentFrame;
} }
CursorState input_sdl::getCursor() const { CursorState input_sdl::getCursor() const {
return {isCursorLocked(), {}, {}}; return {isCursorLocked(), cursor, delta};
} }
bool input_sdl::isCursorLocked() const { bool input_sdl::isCursorLocked() const {
return {}; return cursorLocked;
} }
void input_sdl::toggleCursor() { void input_sdl::toggleCursor() {
@ -117,7 +180,7 @@ const Bindings& input_sdl::getBindings() const {
} }
ObserverHandler input_sdl::addKeyCallback(Keycode key, KeyCallback callback) { ObserverHandler input_sdl::addKeyCallback(Keycode key, KeyCallback callback) {
return {}; return keyCallbacks[key].add(std::move(callback));
} }
const std::vector<Keycode>& input_sdl::getPressedKeys() const { const std::vector<Keycode>& input_sdl::getPressedKeys() const {

View file

@ -1,9 +1,15 @@
#include <SDL3/SDL_keycode.h>
#include "window/input.hpp" #include "window/input.hpp"
#include "window/window.hpp"
struct SDL_Window; struct SDL_Window;
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 = 1024;
input_sdl(); input_sdl();
void pollEvents() override; void pollEvents() override;
@ -33,7 +39,16 @@ public:
const std::vector<Keycode>& getPressedKeys() const override; const std::vector<Keycode>& getPressedKeys() const override;
const std::vector<uint>& getCodepoints() const override; const std::vector<uint>& getCodepoints() const override;
private: private:
glm::vec2 delta;
glm::vec2 cursor;
std::int32_t scroll = 0;
std::uint32_t currentFrame = 0;
std::uint32_t frames[keys_buffer_size] {};
bool keys[keys_buffer_size] {};
bool cursorLocked = false;
bool cursorDrag = false;
Bindings bindings; Bindings bindings;
std::vector<uint> codepoints; std::vector<uint> codepoints;
std::vector<Keycode> pressedKeys; std::vector<Keycode> pressedKeys;
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
}; };

View file

@ -4,14 +4,35 @@
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
#include <SDL3/SDL_mouse.h>
debug::Logger logger("input"); debug::Logger logger("input");
static std::unordered_map<int, std::string> keynames {};
static std::unordered_map<int, std::string> buttonsnames {};
static std::unordered_map<std::string, int> mousecodes {
{"left", SDL_BUTTON_LEFT},
{"right", SDL_BUTTON_RIGHT},
{"middle", SDL_BUTTON_MIDDLE},
{"side1", SDL_BUTTON_X1},
{"side2", SDL_BUTTON_X2},
};
std::string input_util::get_name(Mousecode code) { std::string input_util::get_name(Mousecode code) {
return {}; const auto found = buttonsnames.find(static_cast<int>(code));
if (found == buttonsnames.end()) {
return "unknown";
}
return found->second;
} }
std::string input_util::get_name(Keycode code) { std::string input_util::get_name(Keycode code) {
return {}; const auto found = keynames.find(static_cast<int>(code));
if (found == keynames.end()) {
return "unknown";
}
return found->second;
} }
void Binding::reset(InputType type, int code) { void Binding::reset(InputType type, int code) {
@ -27,18 +48,28 @@ void Binding::reset(Mousecode code) {
reset(InputType::MOUSE, static_cast<int>(code)); reset(InputType::MOUSE, static_cast<int>(code));
} }
void input_util::initialize() {
}
Keycode input_util::keycode_from(const std::string& name) {
return {};
}
Mousecode input_util::mousecode_from(const std::string& name) { Mousecode input_util::mousecode_from(const std::string& name) {
return {}; return {};
} }
void input_util::initialize() {
// for (int i = 0; i <= 9; i++) {
// keycodes[std::to_string(i)] = GLFW_KEY_0 + i;
// }
// for (int i = 0; i < 25; i++) {
// keycodes["f" + std::to_string(i + 1)] = GLFW_KEY_F1 + i;
// }
// for (char i = 'a'; i <= 'z'; i++) {
// keycodes[std::string({i})] = GLFW_KEY_A - 'a' + i;
// }
// for (const auto& entry : keycodes) {
// keynames[entry.second] = entry.first;
// }
for (const auto& entry : mousecodes) {
buttonsnames[entry.second] = entry.first;
}
}
std::string input_util::to_string(Mousecode code) { std::string input_util::to_string(Mousecode code) {
switch (code) { switch (code) {
case Mousecode::BUTTON_1: case Mousecode::BUTTON_1:
@ -49,9 +80,6 @@ std::string input_util::to_string(Mousecode code) {
return "MMB"; return "MMB";
case Mousecode::BUTTON_4: case Mousecode::BUTTON_4:
case Mousecode::BUTTON_5: case Mousecode::BUTTON_5:
case Mousecode::BUTTON_6:
case Mousecode::BUTTON_7:
case Mousecode::BUTTON_8:
return "XButton " + std::to_string( return "XButton " + std::to_string(
static_cast<int>(code) - static_cast<int>(code) -
static_cast<int>(Mousecode::BUTTON_3) static_cast<int>(Mousecode::BUTTON_3)

View file

@ -1,123 +1,118 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <string>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <string>
#include "util/HandlersList.hpp" #include "util/HandlersList.hpp"
namespace dv { namespace dv {
class value; class value;
} }
enum class BindType { enum class BindType { BIND = 0, REBIND = 1 };
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 = 32, SPACE = 0x00000020u,
APOSTROPHE = 39, APOSTROPHE = 0x00000027u,
COMMA = 44, COMMA = 0x0000002cu,
MINUS = 45, MINUS = 0x0000002du,
PERIOD = 46, PERIOD = 0x0000002eu,
SLASH = 47, SLASH = 0x0000002fu,
NUM_0 = 48, NUM_0 = 0x40000059u,
NUM_1 = 49, NUM_1,
NUM_2 = 50, NUM_2,
NUM_3 = 51, NUM_3,
NUM_4 = 52, NUM_4,
NUM_5 = 53, NUM_5,
NUM_6 = 54, NUM_6,
NUM_7 = 55, NUM_7,
NUM_8 = 56, NUM_8,
NUM_9 = 57, NUM_9,
SEMICOLON = 59, SEMICOLON = 0x0000003bu,
EQUAL = 61, EQUAL = 0x0000003du,
A = 65, A = 0x00000061u,
B = 66, B,
C = 67, C,
D = 68, D,
E = 69, E,
F = 70, F,
G = 71, G,
H = 72, H,
I = 73, I,
J = 74, J,
K = 75, K,
L = 76, L,
M = 77, M,
N = 78, N,
O = 79, O,
P = 80, P,
Q = 81, Q,
R = 82, R,
S = 83, S,
T = 84, T,
U = 85, U,
V = 86, V,
W = 87, W,
X = 88, X,
Y = 89, Y,
Z = 90, Z,
LEFT_BRACKET = 91, LEFT_BRACKET = 0x0000005bu,
BACKSLASH = 92, BACKSLASH = 0x0000005cu,
RIGHT_BRACKET = 93, RIGHT_BRACKET = 0x0000005du,
GRAVE_ACCENT = 96, GRAVE_ACCENT = 0x00000060u,
ESCAPE = 256, ESCAPE = 0x0000001bu,
ENTER = 257, ENTER = 0x0000000du,
TAB = 258, TAB = 0x00000009u,
BACKSPACE = 259, BACKSPACE = 0x00000008u,
INSERT = 260, INSERT = 0x40000049u,
DELETE = 261, DELETE = 0x0000007fu,
LEFT = 263, LEFT = 0x40000050u,
RIGHT = 262, RIGHT = 0x4000004fu,
DOWN = 264, DOWN = 0x40000051u,
UP = 265, UP = 0x40000052u,
PAGE_UP = 266, PAGE_UP = 0x4000004bu,
PAGE_DOWN = 267, PAGE_DOWN = 0x4000004eu,
HOME = 268, HOME = 0x4000004au,
END = 269, END = 0x4000004du,
CAPS_LOCK = 280, CAPS_LOCK = 0x40000039u,
NUM_LOCK = 282, NUM_LOCK = 0x40000053u,
PRINT_SCREEN = 283, PRINT_SCREEN = 0x40000046u,
PAUSE = 284, PAUSE = 0x40000048u,
F1 = 290, F1 = 0x4000003au,
F2 = 291, F2,
F3 = 292, F3,
F4 = 293, F4,
F5 = 294, F5,
F6 = 295, F6,
F7 = 296, F7,
F8 = 297, F8,
F9 = 298, F9,
F10 = 299, F10,
F11 = 300, F11,
F12 = 301, F12,
LEFT_SHIFT = 340, LEFT_SHIFT = 0x400000e1u,
LEFT_CONTROL = 341, LEFT_CONTROL = 0x400000e0u,
LEFT_ALT = 342, LEFT_ALT = 0x400000e2u,
LEFT_SUPER = 343, // LEFT_SUPER = 343,
RIGHT_SHIFT = 344, RIGHT_SHIFT = 0x400000e5u,
RIGHT_CONTROL = 345, RIGHT_CONTROL = 0x400000e4u,
RIGHT_ALT = 346, RIGHT_ALT = 0x400000e6u,
RIGHT_SUPER = 347, // RIGHT_SUPER = 347,
MENU = 348, MENU = 0x40000076u,
UNKNOWN = -1 UNKNOWN = 0x00000000u
}; };
/// @brief Represents glfw3 mouse button IDs. /// @brief Represents sdl mouse button IDs.
/// @details There is a subset of glfw3 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 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button BUTTON_3 = 2, // Middle mouse button
BUTTON_4 = 3, // Side mouse button 1 BUTTON_4 = 3, // Side mouse button 1
BUTTON_5 = 4, // Side mouse button 2 BUTTON_5 = 4, // Side mouse button 2
BUTTON_6 = 5, // Side mouse button 3
BUTTON_7 = 6, // Side mouse button 4
BUTTON_8 = 7, // Side mouse button 5
UNKNOWN = -1, UNKNOWN = -1,
}; };
@ -127,14 +122,10 @@ inline Mousecode mousecodes_all[] {
Mousecode::BUTTON_3, Mousecode::BUTTON_3,
Mousecode::BUTTON_4, Mousecode::BUTTON_4,
Mousecode::BUTTON_5, Mousecode::BUTTON_5,
Mousecode::BUTTON_6,
Mousecode::BUTTON_7,
Mousecode::BUTTON_8
}; };
namespace input_util { namespace input_util {
void initialize(); void initialize();
Keycode keycode_from(const std::string& name); Keycode keycode_from(const std::string& name);
Mousecode mousecode_from(const std::string& name); Mousecode mousecode_from(const std::string& name);
@ -285,7 +276,9 @@ public:
virtual const Bindings& getBindings() const = 0; virtual const Bindings& getBindings() const = 0;
virtual ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) = 0; virtual ObserverHandler addKeyCallback(
Keycode key, KeyCallback callback
) = 0;
virtual const std::vector<Keycode>& getPressedKeys() const = 0; virtual const std::vector<Keycode>& getPressedKeys() const = 0;
virtual const std::vector<uint>& getCodepoints() const = 0; virtual const std::vector<uint>& getCodepoints() const = 0;