mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
working input API
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/12 11:14:24 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 19:34:45 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace mlx::core
|
||||
inline void getMousePos(int* x, int* y) noexcept;
|
||||
inline void mouseMove(void* win, int x, int y) noexcept;
|
||||
|
||||
inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept;
|
||||
inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
|
||||
|
||||
inline constexpr void enableAutoRepeat() noexcept;
|
||||
inline constexpr void disableAutoRepeat() noexcept;
|
||||
|
||||
@@ -27,9 +27,9 @@ namespace mlx::core
|
||||
SDL_FlushEvent(SDL_MOUSEMOTION);
|
||||
}
|
||||
|
||||
void Application::onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept
|
||||
void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
|
||||
{
|
||||
_in->onEvent(event, funct_ptr, param);
|
||||
_in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param);
|
||||
}
|
||||
|
||||
constexpr void Application::enableAutoRepeat() noexcept
|
||||
@@ -53,6 +53,7 @@ namespace mlx::core
|
||||
void* Application::newGraphicsSuport(std::size_t w, std::size_t h, std::string title)
|
||||
{
|
||||
_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, std::move(title), _graphics.size()));
|
||||
_in->addWindow(_graphics.back()->getWindow());
|
||||
return static_cast<void*>(&_graphics.back()->getID());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/12 11:16:52 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 19:34:22 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -69,9 +69,9 @@ extern "C"
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_on_event(void* mlx, int event, int (*funct_ptr)(int, void*), void* param)
|
||||
int mlx_on_event(void* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param)
|
||||
{
|
||||
static_cast<mlx::core::Application*>(mlx)->onEvent(event, funct_ptr, param);
|
||||
static_cast<mlx::core::Application*>(mlx)->onEvent(win, event, funct_ptr, param);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/12 11:31:28 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 19:54:20 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -31,57 +31,6 @@ namespace mlx
|
||||
|
||||
while(SDL_PollEvent(&_event))
|
||||
{
|
||||
if(_event.window.event == SDL_WINDOWEVENT_CLOSE)
|
||||
_end = true;
|
||||
|
||||
switch(_event.type)
|
||||
{
|
||||
case SDL_KEYDOWN:
|
||||
{
|
||||
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::down);
|
||||
if(_events_hooks[0].hook)
|
||||
_events_hooks[0].hook(_event.key.keysym.scancode, _events_hooks[0].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_KEYUP:
|
||||
{
|
||||
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::up);
|
||||
if(_events_hooks[1].hook)
|
||||
_events_hooks[1].hook(_event.key.keysym.scancode, _events_hooks[1].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
_mouse[_event.button.button] = static_cast<uint8_t>(action::down);
|
||||
if(_events_hooks[2].hook)
|
||||
_events_hooks[2].hook(_event.button.button, _events_hooks[2].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
_mouse[_event.button.button] = static_cast<uint8_t>(action::up);
|
||||
if(_events_hooks[3].hook)
|
||||
_events_hooks[3].hook(_event.button.button, _events_hooks[3].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
{
|
||||
switch(_event.window.event)
|
||||
{
|
||||
//case
|
||||
|
||||
default : break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
if(_event.type == SDL_MOUSEMOTION)
|
||||
{
|
||||
_x = _event.motion.x;
|
||||
@@ -90,6 +39,105 @@ namespace mlx
|
||||
_xRel = _event.motion.xrel;
|
||||
_yRel = _event.motion.yrel;
|
||||
}
|
||||
|
||||
uint32_t id = _event.window.windowID;
|
||||
if(!_events_hooks.count(id))
|
||||
continue;
|
||||
|
||||
switch(_event.type)
|
||||
{
|
||||
case SDL_KEYDOWN:
|
||||
{
|
||||
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::down);
|
||||
if(_events_hooks[id][0].hook)
|
||||
_events_hooks[id][0].hook(_event.key.keysym.scancode, _events_hooks[id][0].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_KEYUP:
|
||||
{
|
||||
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::up);
|
||||
if(_events_hooks[id][1].hook)
|
||||
_events_hooks[id][1].hook(_event.key.keysym.scancode, _events_hooks[id][1].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
_mouse[_event.button.button] = static_cast<uint8_t>(action::down);
|
||||
if(_events_hooks[id][2].hook)
|
||||
_events_hooks[id][2].hook(_event.button.button, _events_hooks[id][2].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
_mouse[_event.button.button] = static_cast<uint8_t>(action::up);
|
||||
if(_events_hooks[id][3].hook)
|
||||
_events_hooks[id][3].hook(_event.button.button, _events_hooks[id][3].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
{
|
||||
switch(_event.window.event)
|
||||
{
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(0, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MOVED:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(1, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MINIMIZED:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(2, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(3, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_ENTER:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_LEAVE:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(5, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
{
|
||||
if(_events_hooks[id][4].hook)
|
||||
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
|
||||
break;
|
||||
}
|
||||
|
||||
default : break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/12 13:47:02 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 20:02:44 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "window.h"
|
||||
|
||||
@@ -53,21 +54,26 @@ namespace mlx
|
||||
inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; }
|
||||
inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; }
|
||||
|
||||
inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept
|
||||
inline void addWindow(std::shared_ptr<MLX_Window> window)
|
||||
{
|
||||
_events_hooks[event].hook = funct_ptr;
|
||||
_events_hooks[event].param = param;
|
||||
_windows[window->getID()] = window;
|
||||
_events_hooks[window->getID()] = {};
|
||||
}
|
||||
|
||||
inline void onEvent(uint32_t id, int event, int (*funct_ptr)(int, void*), void* param) noexcept
|
||||
{
|
||||
_events_hooks[id][event].hook = funct_ptr;
|
||||
_events_hooks[id][event].param = param;
|
||||
}
|
||||
|
||||
~Input() = default;
|
||||
|
||||
private:
|
||||
std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<MLX_Window>> _windows;
|
||||
std::unordered_map<uint32_t, std::array<Hook, 5>> _events_hooks;
|
||||
SDL_Event _event;
|
||||
std::array<uint8_t, 8> _mouse;
|
||||
std::vector<class* MLX_Window> _window;
|
||||
|
||||
std::array<Hook, 5> _events_hooks;
|
||||
|
||||
int _x = 0;
|
||||
int _y = 0;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/02 15:39:59 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 18:45:05 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,11 +20,17 @@ namespace mlx
|
||||
_win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
|
||||
if(!_win)
|
||||
core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError());
|
||||
_id = SDL_GetWindowID(_win);
|
||||
}
|
||||
|
||||
MLX_Window::~MLX_Window()
|
||||
void MLX_Window::destroy() noexcept
|
||||
{
|
||||
if(_win)
|
||||
SDL_DestroyWindow(_win);
|
||||
}
|
||||
|
||||
MLX_Window::~MLX_Window()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/02 15:39:53 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/12 19:06:24 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -26,6 +26,9 @@ namespace mlx
|
||||
inline SDL_Window* getNativeWindow() const noexcept { return _win; }
|
||||
inline int getWidth() const noexcept { return _width; }
|
||||
inline int getHeight() const noexcept { return _height; }
|
||||
inline uint32_t getID() const noexcept { return _id; }
|
||||
|
||||
void destroy() noexcept;
|
||||
|
||||
~MLX_Window();
|
||||
|
||||
@@ -33,6 +36,7 @@ namespace mlx
|
||||
SDL_Window* _win = nullptr;
|
||||
int _width = 0;
|
||||
int _height = 0;
|
||||
uint32_t _id = -1;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user