diff options
Diffstat (limited to 'src/xplorer.c')
| -rw-r--r-- | src/xplorer.c | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/src/xplorer.c b/src/xplorer.c index 53badc3..db9cf7d 100644 --- a/src/xplorer.c +++ b/src/xplorer.c | |||
| @@ -11,8 +11,8 @@ | |||
| 11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 12 | 12 | ||
| 13 | static const char* WindowTitle = "XPLORER"; | 13 | static const char* WindowTitle = "XPLORER"; |
| 14 | static const int DefaultWidth = 1440; | 14 | static const int DefaultWidth = 720; |
| 15 | static const int DefaultHeight = 900; | 15 | static const int DefaultHeight = 450; |
| 16 | 16 | ||
| 17 | // #define DEBUG_EVENT_LOOP 1 | 17 | // #define DEBUG_EVENT_LOOP 1 |
| 18 | 18 | ||
| @@ -31,6 +31,9 @@ typedef struct State { | |||
| 31 | 31 | ||
| 32 | uiMouseButton ToUiButton(Uint8 button); | 32 | uiMouseButton ToUiButton(Uint8 button); |
| 33 | 33 | ||
| 34 | void MouseCoordsToUiCoords( | ||
| 35 | SDL_Window*, float mouse_x, float mouse_y, int* x, int* y); | ||
| 36 | |||
| 34 | void CreateUi(State* state) { | 37 | void CreateUi(State* state) { |
| 35 | assert(state); | 38 | assert(state); |
| 36 | 39 | ||
| @@ -244,8 +247,8 @@ bool Initialize(State* state) { | |||
| 244 | assert(state); | 247 | assert(state); |
| 245 | 248 | ||
| 246 | if ((state->window = SDL_CreateWindow( | 249 | if ((state->window = SDL_CreateWindow( |
| 247 | WindowTitle, DefaultWidth, DefaultHeight, SDL_WINDOW_RESIZABLE)) == | 250 | WindowTitle, DefaultWidth, DefaultHeight, |
| 248 | NULL) { | 251 | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY)) == NULL) { |
| 249 | return false; | 252 | return false; |
| 250 | } | 253 | } |
| 251 | 254 | ||
| @@ -337,32 +340,38 @@ int main( | |||
| 337 | } | 340 | } |
| 338 | } | 341 | } |
| 339 | } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { | 342 | } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { |
| 343 | int x, y; | ||
| 344 | MouseCoordsToUiCoords( | ||
| 345 | state.window, event.button.x, event.button.y, &x, &y); | ||
| 340 | const uiInputEvent ui_event = { | 346 | const uiInputEvent ui_event = { |
| 341 | .type = uiEventMouseButton, | 347 | .type = uiEventMouseButton, |
| 342 | .mouse_button = (uiMouseButtonEvent){ | 348 | .mouse_button = |
| 343 | .button = ToUiButton(event.button.button), | 349 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), |
| 344 | .state = uiMouseDown, | 350 | .state = uiMouseDown, |
| 345 | .mouse_position = | 351 | .mouse_position = (uiPoint){x, y}} |
| 346 | (uiPoint){.x = event.button.x, .y = event.button.y}} | ||
| 347 | }; | 352 | }; |
| 348 | redraw = uiSendEvent(state.frame, &ui_event); | 353 | redraw = uiSendEvent(state.frame, &ui_event); |
| 349 | } else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { | 354 | } else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { |
| 355 | int x, y; | ||
| 356 | MouseCoordsToUiCoords( | ||
| 357 | state.window, event.button.x, event.button.y, &x, &y); | ||
| 350 | const uiInputEvent ev = { | 358 | const uiInputEvent ev = { |
| 351 | .type = uiEventMouseButton, | 359 | .type = uiEventMouseButton, |
| 352 | .mouse_button = (uiMouseButtonEvent){ | 360 | .mouse_button = |
| 353 | .button = ToUiButton(event.button.button), | 361 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), |
| 354 | .state = uiMouseUp, | 362 | .state = uiMouseUp, |
| 355 | .mouse_position = | 363 | .mouse_position = (uiPoint){x, y}} |
| 356 | (uiPoint){.x = event.button.x, .y = event.button.y}} | ||
| 357 | }; | 364 | }; |
| 358 | redraw = uiSendEvent(state.frame, &ev); | 365 | redraw = uiSendEvent(state.frame, &ev); |
| 359 | } else if (event.type == SDL_EVENT_MOUSE_WHEEL) { | 366 | } else if (event.type == SDL_EVENT_MOUSE_WHEEL) { |
| 367 | int x, y; | ||
| 368 | MouseCoordsToUiCoords( | ||
| 369 | state.window, event.wheel.mouse_x, event.wheel.mouse_y, &x, &y); | ||
| 360 | const uiInputEvent ev = { | 370 | const uiInputEvent ev = { |
| 361 | .type = uiEventMouseScroll, | 371 | .type = uiEventMouseScroll, |
| 362 | .mouse_scroll = (uiMouseScrollEvent){ | 372 | .mouse_scroll = |
| 363 | .scroll_offset = event.wheel.y, | 373 | (uiMouseScrollEvent){.scroll_offset = (int)event.wheel.y, |
| 364 | .mouse_position = (uiPoint){.x = event.wheel.mouse_x, | 374 | .mouse_position = (uiPoint){x, y}} |
| 365 | .y = event.wheel.mouse_y}} | ||
| 366 | }; | 375 | }; |
| 367 | redraw = uiSendEvent(state.frame, &ev); | 376 | redraw = uiSendEvent(state.frame, &ev); |
| 368 | } else { | 377 | } else { |
| @@ -400,3 +409,13 @@ uiMouseButton ToUiButton(Uint8 button) { | |||
| 400 | // TODO: Buttons. | 409 | // TODO: Buttons. |
| 401 | return uiLMB; | 410 | return uiLMB; |
| 402 | } | 411 | } |
| 412 | |||
| 413 | void MouseCoordsToUiCoords( | ||
| 414 | SDL_Window* window, float mouse_x, float mouse_y, int* x, int* y) { | ||
| 415 | assert(window); | ||
| 416 | assert(x); | ||
| 417 | assert(y); | ||
| 418 | const float d = SDL_GetWindowPixelDensity(window); | ||
| 419 | *x = (int)(mouse_x * d); | ||
| 420 | *y = (int)(mouse_y * d); | ||
| 421 | } | ||
