Fix up SDL2 mouse and key handling for imgui
This commit is contained in:
parent
401961be5a
commit
2ddc1e1794
4 changed files with 46 additions and 5 deletions
|
@ -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] = {};
|
||||
};
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -186,9 +186,11 @@ void imgui_backend::begin_frame(const float delta_time) {
|
|||
io.MousePos = ImVec2(static_cast<float>(x), static_cast<float>(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;
|
||||
}
|
|
@ -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);
|
||||
|
|
Reference in a new issue