Skip to content

Commit

Permalink
Added mouse api interface (#731)
Browse files Browse the repository at this point in the history
* Added WIP mouse support

* Fixed compilation

* Added mouse press state

* Fixed tidy

* Added support for mouse capturing (SetMouseCapture)

* Added mouse wheel support

* mouse backward & forward support

* bool IsMouseCaptured()

---------

Co-authored-by: lightmanLP <[email protected]>
  • Loading branch information
KiritoDv and lightmanLP authored Dec 10, 2024
1 parent 6351314 commit ee1c0df
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/graphic/Fast3D/Fast3dWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,36 @@ int32_t Fast3dWindow::GetPosY() {
return posY;
}

Ship::Coords Fast3dWindow::GetMousePos() {
int32_t x, y;
mWindowManagerApi->get_mouse_pos(&x, &y);
return { x, y };
}

Ship::Coords Fast3dWindow::GetMouseDelta() {
int32_t x, y;
mWindowManagerApi->get_mouse_delta(&x, &y);
return { x, y };
}

Ship::CoordsF Fast3dWindow::GetMouseWheel() {
float x, y;
mWindowManagerApi->get_mouse_wheel(&x, &y);
return { x, y };
}

bool Fast3dWindow::GetMouseState(Ship::MouseBtn btn) {
return mWindowManagerApi->get_mouse_state(static_cast<uint32_t>(btn));
}

void Fast3dWindow::SetMouseCapture(bool capture) {
mWindowManagerApi->set_mouse_capture(capture);
}

bool Fast3dWindow::IsMouseCaptured() {
return mWindowManagerApi->is_mouse_captured();
}

uint32_t Fast3dWindow::GetCurrentRefreshRate() {
uint32_t refreshRate;
mWindowManagerApi->get_active_window_refresh_rate(&refreshRate);
Expand Down
6 changes: 6 additions & 0 deletions src/graphic/Fast3D/Fast3dWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class Fast3dWindow : public Ship::Window {
uint32_t GetHeight() override;
int32_t GetPosX() override;
int32_t GetPosY() override;
Ship::Coords GetMousePos() override;
Ship::Coords GetMouseDelta() override;
Ship::CoordsF GetMouseWheel() override;
bool GetMouseState(Ship::MouseBtn btn) override;
void SetMouseCapture(bool capture) override;
bool IsMouseCaptured() override;
uint32_t GetCurrentRefreshRate() override;
bool SupportsWindowedFullscreen() override;
bool CanDisableVerticalSync() override;
Expand Down
76 changes: 76 additions & 0 deletions src/graphic/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static struct {
bool use_timer;
bool tearing_support;
bool is_vsync_enabled;
bool mouse_pressed[5];
float mouse_wheel[2];
LARGE_INTEGER previous_present_time;

void (*on_fullscreen_changed)(bool is_now_fullscreen);
Expand Down Expand Up @@ -356,6 +358,34 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par
case WM_KEYUP:
onkeyup(w_param, l_param);
break;
case WM_LBUTTONDOWN:
dxgi.mouse_pressed[0] = true;
break;
case WM_LBUTTONUP:
dxgi.mouse_pressed[0] = false;
break;
case WM_MBUTTONDOWN:
dxgi.mouse_pressed[1] = true;
break;
case WM_MBUTTONUP:
dxgi.mouse_pressed[1] = false;
break;
case WM_RBUTTONDOWN:
dxgi.mouse_pressed[2] = true;
break;
case WM_RBUTTONUP:
dxgi.mouse_pressed[2] = false;
break;
case WM_XBUTTONDOWN:
dxgi.mouse_pressed[2 + GET_XBUTTON_WPARAM(w_param)] = true;
break;
case WM_XBUTTONUP:
dxgi.mouse_pressed[2 + GET_XBUTTON_WPARAM(w_param)] = false;
break;
case WM_MOUSEWHEEL:
dxgi.mouse_wheel[0] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA;
dxgi.mouse_wheel[1] = 0;
break;
case WM_DROPFILES:
DragQueryFileA((HDROP)w_param, 0, fileName, 256);
Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, fileName);
Expand Down Expand Up @@ -485,6 +515,46 @@ static void gfx_dxgi_set_cursor_visibility(bool visible) {
}
}

static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) {
POINT p;
GetCursorPos(&p);
ScreenToClient(dxgi.h_wnd, &p);
*x = p.x;
*y = p.y;
}

static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) {
POINT p;
GetCursorPos(&p);
ScreenToClient(dxgi.h_wnd, &p);
*x = p.x - dxgi.current_width / 2;
*y = p.y - dxgi.current_height / 2;
SetCursorPos(dxgi.current_width / 2, dxgi.current_height / 2);
}

static void gfx_dxgi_get_mouse_wheel(float* x, float* y) {
*x = dxgi.mouse_wheel[0];
*y = dxgi.mouse_wheel[1];
dxgi.mouse_wheel[0] = 0;
dxgi.mouse_wheel[1] = 0;
}

static bool gfx_dxgi_get_mouse_state(uint32_t btn) {
return dxgi.mouse_pressed[btn];
}

static void gfx_dxgi_set_mouse_capture(bool capture) {
if (capture) {
SetCapture(dxgi.h_wnd);
} else {
ReleaseCapture();
}
}

static bool gfx_dxgi_is_mouse_captured() {
return (GetCapture() != NULL);
}

static void gfx_dxgi_set_fullscreen(bool enable) {
toggle_borderless_window_full_screen(enable, true);
}
Expand Down Expand Up @@ -912,6 +982,12 @@ extern "C" struct GfxWindowManagerAPI gfx_dxgi_api = { gfx_dxgi_init,
gfx_dxgi_set_fullscreen,
gfx_dxgi_get_active_window_refresh_rate,
gfx_dxgi_set_cursor_visibility,
gfx_dxgi_get_mouse_pos,
gfx_dxgi_get_mouse_delta,
gfx_dxgi_get_mouse_wheel,
gfx_dxgi_get_mouse_state,
gfx_dxgi_set_mouse_capture,
gfx_dxgi_is_mouse_captured,
gfx_dxgi_get_dimensions,
gfx_dxgi_handle_events,
gfx_dxgi_start_frame,
Expand Down
39 changes: 39 additions & 0 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static SDL_GLContext ctx;
static SDL_Renderer* renderer;
static int sdl_to_lus_table[512];
static bool vsync_enabled = true;
static float mouse_wheel_x = 0.0f;
static float mouse_wheel_y = 0.0f;
// OTRTODO: These are redundant. Info can be queried from SDL.
static int window_width = DESIRED_SCREEN_WIDTH;
static int window_height = DESIRED_SCREEN_HEIGHT;
Expand Down Expand Up @@ -438,6 +440,33 @@ static void gfx_sdl_set_cursor_visibility(bool visible) {
}
}

static void gfx_sdl_get_mouse_pos(int32_t* x, int32_t* y) {
SDL_GetMouseState(x, y);
}

static void gfx_sdl_get_mouse_delta(int32_t* x, int32_t* y) {
SDL_GetRelativeMouseState(x, y);
}

static void gfx_sdl_get_mouse_wheel(float* x, float* y) {
*x = mouse_wheel_x;
*y = mouse_wheel_y;
mouse_wheel_x = 0.0f;
mouse_wheel_y = 0.0f;
}

static bool gfx_sdl_get_mouse_state(uint32_t btn) {
return SDL_GetMouseState(NULL, NULL) & (1 << btn);
}

static void gfx_sdl_set_mouse_capture(bool capture) {
SDL_SetRelativeMouseMode(static_cast<SDL_bool>(capture));
}

static bool gfx_sdl_is_mouse_captured() {
return (SDL_GetRelativeMouseMode() == SDL_TRUE);
}

static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bool (*on_key_up)(int scancode),
void (*on_all_keys_up)(void)) {
on_key_down_callback = on_key_down;
Expand Down Expand Up @@ -494,6 +523,10 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) {
case SDL_KEYUP:
gfx_sdl_onkeyup(event.key.keysym.scancode);
break;
case SDL_MOUSEWHEEL:
mouse_wheel_x = event.wheel.x;
mouse_wheel_y = event.wheel.y;
break;
#endif
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
Expand Down Expand Up @@ -625,6 +658,12 @@ struct GfxWindowManagerAPI gfx_sdl = { gfx_sdl_init,
gfx_sdl_set_fullscreen,
gfx_sdl_get_active_window_refresh_rate,
gfx_sdl_set_cursor_visibility,
gfx_sdl_get_mouse_pos,
gfx_sdl_get_mouse_delta,
gfx_sdl_get_mouse_wheel,
gfx_sdl_get_mouse_state,
gfx_sdl_set_mouse_capture,
gfx_sdl_is_mouse_captured,
gfx_sdl_get_dimensions,
gfx_sdl_handle_events,
gfx_sdl_start_frame,
Expand Down
6 changes: 6 additions & 0 deletions src/graphic/Fast3D/gfx_window_manager_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ struct GfxWindowManagerAPI {
void (*set_fullscreen)(bool enable);
void (*get_active_window_refresh_rate)(uint32_t* refresh_rate);
void (*set_cursor_visibility)(bool visible);
void (*get_mouse_pos)(int32_t* x, int32_t* y);
void (*get_mouse_delta)(int32_t* x, int32_t* y);
void (*get_mouse_wheel)(float* x, float* y);
bool (*get_mouse_state)(uint32_t btn);
void (*set_mouse_capture)(bool capture);
bool (*is_mouse_captured)();
void (*get_dimensions)(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY);
void (*handle_events)(void);
bool (*start_frame)(void);
Expand Down
18 changes: 18 additions & 0 deletions src/window/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
namespace Ship {
enum class WindowBackend { FAST3D_DXGI_DX11, FAST3D_SDL_OPENGL, FAST3D_SDL_METAL, WINDOW_BACKEND_COUNT };

enum class MouseBtn { LEFT, MIDDLE, RIGHT, BACKWARD, FORWARD, MOUSE_BTN_COUNT };

struct Coords {
int32_t x;
int32_t y;
};

struct CoordsF {
float x;
float y;
};

class Config;

class Window {
Expand All @@ -30,6 +42,12 @@ class Window {
virtual uint32_t GetHeight() = 0;
virtual int32_t GetPosX() = 0;
virtual int32_t GetPosY() = 0;
virtual Coords GetMousePos() = 0;
virtual Coords GetMouseDelta() = 0;
virtual CoordsF GetMouseWheel() = 0;
virtual bool GetMouseState(MouseBtn btn) = 0;
virtual void SetMouseCapture(bool capture) = 0;
virtual bool IsMouseCaptured() = 0;
virtual uint32_t GetCurrentRefreshRate() = 0;
virtual bool SupportsWindowedFullscreen() = 0;
virtual bool CanDisableVerticalSync() = 0;
Expand Down

0 comments on commit ee1c0df

Please sign in to comment.