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:
@@ -13,7 +13,7 @@ class App {
|
|||||||
public:
|
public:
|
||||||
App();
|
App();
|
||||||
~App();
|
~App();
|
||||||
void handle_cursor_position(float xpos, float ypos);
|
void handle_mouse_move(float xpos, float ypos, float xrel, float yrel);
|
||||||
void handle_sdl_key(SDL_Event& e);
|
void handle_sdl_key(SDL_Event& e);
|
||||||
void handle_sdl_mouse_button(SDL_Event& e);
|
void handle_sdl_mouse_button(SDL_Event& e);
|
||||||
void handle_window_focus(bool focused);
|
void handle_window_focus(bool focused);
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public:
|
|||||||
void camera_init(ClientPlayer* player);
|
void camera_init(ClientPlayer* player);
|
||||||
void hot_reload();
|
void hot_reload();
|
||||||
void reset_camera();
|
void reset_camera();
|
||||||
void update_cursor_position_camera(double xpos, double ypos);
|
void update_cursor_position_camera(float offset_x, float offset_y);
|
||||||
|
|
||||||
const glm::mat4 get_camera_lookat() const;
|
const glm::mat4 get_camera_lookat() const;
|
||||||
const glm::vec3& get_camera_pos() const;
|
const glm::vec3& get_camera_pos() const;
|
||||||
|
|||||||
@@ -11,8 +11,10 @@ namespace Cubed {
|
|||||||
struct MouseMoveEvent {
|
struct MouseMoveEvent {
|
||||||
float xpos;
|
float xpos;
|
||||||
float ypos;
|
float ypos;
|
||||||
|
float xrel;
|
||||||
MouseMoveEvent(float x, float y) : xpos(x), ypos(y) {}
|
float yrel;
|
||||||
|
MouseMoveEvent(float x, float y, float dx, float dy)
|
||||||
|
: xpos(x), ypos(y), xrel(dx), yrel(dy) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MouseButtonEvent {
|
struct MouseButtonEvent {
|
||||||
|
|||||||
@@ -41,14 +41,17 @@ public:
|
|||||||
|
|
||||||
void set_vsync(bool enable);
|
void set_vsync(bool enable);
|
||||||
|
|
||||||
|
int width() const;
|
||||||
|
int height() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_mouse_enable = true;
|
bool m_mouse_enable = true;
|
||||||
bool m_imgui_init = false;
|
bool m_imgui_init = false;
|
||||||
bool m_game_running = false;
|
bool m_game_running = false;
|
||||||
SDL_Window* m_window;
|
SDL_Window* m_window;
|
||||||
SDL_GLContext m_context;
|
SDL_GLContext m_context;
|
||||||
int m_window_width;
|
int m_window_width = 0;
|
||||||
int m_window_height;
|
int m_window_height = 0;
|
||||||
Config& m_config;
|
Config& m_config;
|
||||||
Camera* m_camera = nullptr;
|
Camera* m_camera = nullptr;
|
||||||
bool m_imgui_enable = false;
|
bool m_imgui_enable = false;
|
||||||
|
|||||||
@@ -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) {
|
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);
|
handle_sdl_mouse_button(e);
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_MOUSE_MOTION:
|
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;
|
break;
|
||||||
case SDL_EVENT_WINDOW_RESIZED:
|
case SDL_EVENT_WINDOW_RESIZED:
|
||||||
handle_window_resize(e.window.data1, e.window.data2);
|
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::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) {
|
if (m_firse_mouse) {
|
||||||
m_last_mouse_x = xpos;
|
|
||||||
m_last_mouse_y = ypos;
|
|
||||||
m_firse_mouse = false;
|
m_firse_mouse = false;
|
||||||
return;
|
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");
|
ASSERT_MSG(m_player, "nullptr");
|
||||||
m_player->update_front_vec(offset_x, offset_y);
|
m_player->update_front_vec(offset_x, offset_y);
|
||||||
}
|
}
|
||||||
@@ -174,7 +167,7 @@ bool Camera::handle_key_event(const KeyEvent& e) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool Camera::handle_mouse_move_event(const MouseMoveEvent& e) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
static int windowed_xpos = 0, windowed_ypos = 0;
|
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) {}
|
Window::Window(Config& config) : m_config(config) {}
|
||||||
|
|
||||||
@@ -193,11 +193,12 @@ void Window::set_fullscreen(bool full) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Window::enable_mouse() {
|
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;
|
m_mouse_enable = true;
|
||||||
}
|
}
|
||||||
void Window::disable_mouse() {
|
void Window::disable_mouse() {
|
||||||
SDL_SetWindowRelativeMouseMode(m_window, false);
|
SDL_SetWindowRelativeMouseMode(m_window, true);
|
||||||
if (m_camera) {
|
if (m_camera) {
|
||||||
m_camera->reset_camera();
|
m_camera->reset_camera();
|
||||||
}
|
}
|
||||||
@@ -231,7 +232,8 @@ void Window::set_vsync(bool enable) {
|
|||||||
Logger::error("VSync Fail: {}", SDL_GetError());
|
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() {
|
void Window::imgui_init() {
|
||||||
float main_scale = SDL_GetWindowDisplayScale(m_window);
|
float main_scale = SDL_GetWindowDisplayScale(m_window);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user