diff --git a/engine/core/include/imgui_backend.hpp b/engine/core/include/imgui_backend.hpp index f2a509c..79966f4 100644 --- a/engine/core/include/imgui_backend.hpp +++ b/engine/core/include/imgui_backend.hpp @@ -9,8 +9,12 @@ namespace prism { void render(int index); - void process_key_down(unsigned int key_code); + void process_mouse_down(int button); + void process_key_down(unsigned int key_code); void process_key_up(unsigned int key_code); + + private: + bool mouse_buttons[3] = {}; }; } \ No newline at end of file diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index 0573a62..a03daa7 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -479,6 +479,8 @@ void engine::process_key_up(const unsigned int keyCode) { void engine::process_mouse_down(const int button, const prism::Offset offset) { if(current_screen != nullptr && button == 0) current_screen->process_mouse(offset.x, offset.y); + + imgui->process_mouse_down(button); } void engine::push_event(const std::string_view name, const std::string_view data) { diff --git a/engine/core/src/imgui_backend.cpp b/engine/core/src/imgui_backend.cpp index df24c8c..ea93270 100644 --- a/engine/core/src/imgui_backend.cpp +++ b/engine/core/src/imgui_backend.cpp @@ -186,9 +186,11 @@ void imgui_backend::begin_frame(const float delta_time) { io.MousePos = ImVec2(static_cast(x), static_cast(y)); } - io.MouseDown[0] = platform::get_mouse_button_down(0); - io.MouseDown[1] = platform::get_mouse_button_down(1); - + io.MouseDown[0] = mouse_buttons[0] || platform::get_mouse_button_down(0); // left + io.MouseDown[1] = mouse_buttons[1] || platform::get_mouse_button_down(1); // right + io.MouseDown[2] = mouse_buttons[2] || platform::get_mouse_button_down(2); // middle + mouse_buttons[0] = mouse_buttons[1] = mouse_buttons[2] = false; + ImGui::NewFrame(); } @@ -217,3 +219,7 @@ void imgui_backend::process_key_up(unsigned int key_code) { io.KeysDown[key_code] = false; } + +void imgui_backend::process_mouse_down(int button) { + mouse_buttons[button] = true; +} \ No newline at end of file diff --git a/platforms/sdl/main.cpp.in b/platforms/sdl/main.cpp.in index 65bbc84..9193bc6 100644 --- a/platforms/sdl/main.cpp.in +++ b/platforms/sdl/main.cpp.in @@ -222,7 +222,20 @@ prism::Offset platform::get_screen_cursor_position() { } bool platform::get_mouse_button_down(const int button) { - return SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT + button); + Uint8 sdl_button = SDL_BUTTON_LEFT; + switch(button) { + case 0: + sdl_button = SDL_BUTTON_LEFT; + break; + case 1: + sdl_button = SDL_BUTTON_RIGHT; + break; + case 2: + sdl_button = SDL_BUTTON_MIDDLE; + break; + } + + return (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(sdl_button)) != 0; } float mouse_wheel_x, mouse_wheel_y; @@ -288,6 +301,22 @@ int main(int argc, char* argv[]) { mouse_wheel_y = event.wheel.y; } break; + case SDL_MOUSEBUTTONDOWN: + { + int engine_button = 0; + if(event.button.button == SDL_BUTTON_RIGHT) + engine_button = 1; + else if(event.button.button == SDL_BUTTON_MIDDLE) + engine_button = 2; + + engine->process_mouse_down(engine_button, {0, 0}); + } + break; + case SDL_KEYDOWN: + { + engine->process_key_down(event.key.keysym.scancode); + } + break; case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_RESIZED) { auto window = get_window_by_sdl_id(event.window.windowID);