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;