mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(input): use relative mouse motion and simplify camera mouse update
This commit is contained in:
@@ -136,9 +136,9 @@ void App::handle_argument(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
void App::handle_cursor_position(float xpos, float ypos) {
|
||||
void App::handle_mouse_move(float xpos, float ypos, float xrel, float yrel) {
|
||||
|
||||
m_scene_manager.handle_event(MouseMoveEvent{xpos, ypos});
|
||||
m_scene_manager.handle_event(MouseMoveEvent{xpos, ypos, xrel, yrel});
|
||||
}
|
||||
|
||||
void App::handle_sdl_key(SDL_Event& e) {
|
||||
@@ -651,7 +651,7 @@ void App::handle_sdl_event(SDL_Event& e) {
|
||||
handle_sdl_mouse_button(e);
|
||||
break;
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
handle_cursor_position(e.motion.x, e.motion.y);
|
||||
handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel);
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
handle_window_resize(e.window.data1, e.window.data2);
|
||||
|
||||
@@ -69,19 +69,12 @@ void Camera::hot_reload() {}
|
||||
|
||||
void Camera::reset_camera() { m_firse_mouse = true; }
|
||||
|
||||
void Camera::update_cursor_position_camera(double xpos, double ypos) {
|
||||
void Camera::update_cursor_position_camera(float offset_x, float offset_y) {
|
||||
if (m_firse_mouse) {
|
||||
m_last_mouse_x = xpos;
|
||||
m_last_mouse_y = ypos;
|
||||
m_firse_mouse = false;
|
||||
return;
|
||||
}
|
||||
|
||||
float offset_x = xpos - m_last_mouse_x;
|
||||
float offset_y = m_last_mouse_y - ypos;
|
||||
|
||||
m_last_mouse_x = xpos;
|
||||
m_last_mouse_y = ypos;
|
||||
ASSERT_MSG(m_player, "nullptr");
|
||||
m_player->update_front_vec(offset_x, offset_y);
|
||||
}
|
||||
@@ -174,7 +167,7 @@ bool Camera::handle_key_event(const KeyEvent& e) {
|
||||
return false;
|
||||
}
|
||||
bool Camera::handle_mouse_move_event(const MouseMoveEvent& e) {
|
||||
update_cursor_position_camera(e.xpos, e.ypos);
|
||||
update_cursor_position_camera(e.xrel, -e.yrel);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
namespace Cubed {
|
||||
|
||||
static int windowed_xpos = 0, windowed_ypos = 0;
|
||||
static int windowed_width = 800, windowed_height = 600;
|
||||
static int windowed_width = 1280, windowed_height = 720;
|
||||
|
||||
Window::Window(Config& config) : m_config(config) {}
|
||||
|
||||
@@ -193,11 +193,12 @@ void Window::set_fullscreen(bool full) {
|
||||
}
|
||||
}
|
||||
void Window::enable_mouse() {
|
||||
SDL_SetWindowRelativeMouseMode(m_window, true);
|
||||
SDL_WarpMouseInWindow(m_window, width() / 2, height() / 2);
|
||||
SDL_SetWindowRelativeMouseMode(m_window, false);
|
||||
m_mouse_enable = true;
|
||||
}
|
||||
void Window::disable_mouse() {
|
||||
SDL_SetWindowRelativeMouseMode(m_window, false);
|
||||
SDL_SetWindowRelativeMouseMode(m_window, true);
|
||||
if (m_camera) {
|
||||
m_camera->reset_camera();
|
||||
}
|
||||
@@ -231,7 +232,8 @@ void Window::set_vsync(bool enable) {
|
||||
Logger::error("VSync Fail: {}", SDL_GetError());
|
||||
}
|
||||
}
|
||||
|
||||
int Window::width() const { return m_window_width; }
|
||||
int Window::height() const { return m_window_height; }
|
||||
void Window::imgui_init() {
|
||||
float main_scale = SDL_GetWindowDisplayScale(m_window);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user