diff options
Diffstat (limited to 'src/xplorer.c')
| -rw-r--r-- | src/xplorer.c | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/xplorer.c b/src/xplorer.c index c03a16e..6dd1bbf 100644 --- a/src/xplorer.c +++ b/src/xplorer.c | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <stdio.h> | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 12 | 12 | ||
| 13 | // TODO: Set window dimensions and font size based on pixel area density. | ||
| 13 | static const char* WindowTitle = "XPLORER"; | 14 | static const char* WindowTitle = "XPLORER"; |
| 14 | static const int DefaultWidth = 720; | 15 | static const int DefaultWidth = 720; |
| 15 | static const int DefaultHeight = 450; | 16 | static const int DefaultHeight = 450; |
| @@ -34,6 +35,9 @@ uiMouseButton ToUiButton(Uint8 button); | |||
| 34 | void MouseCoordsToUiCoords( | 35 | void MouseCoordsToUiCoords( |
| 35 | SDL_Window*, float mouse_x, float mouse_y, int* x, int* y); | 36 | SDL_Window*, float mouse_x, float mouse_y, int* x, int* y); |
| 36 | 37 | ||
| 38 | uiMouseButtonState MouseButtonStateFromFlags( | ||
| 39 | SDL_MouseButtonFlags flags, uiMouseButton button); | ||
| 40 | |||
| 37 | void CreateUi(State* state) { | 41 | void CreateUi(State* state) { |
| 38 | assert(state); | 42 | assert(state); |
| 39 | 43 | ||
| @@ -345,7 +349,7 @@ int main( | |||
| 345 | .type = uiEventMouseButton, | 349 | .type = uiEventMouseButton, |
| 346 | .mouse_button = | 350 | .mouse_button = |
| 347 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), | 351 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), |
| 348 | .state = uiMouseDown, | 352 | .button_state = uiMouseDown, |
| 349 | .mouse_position = (uiPoint){x, y}} | 353 | .mouse_position = (uiPoint){x, y}} |
| 350 | }; | 354 | }; |
| 351 | redraw = uiSendEvent(state.frame, &ui_event); | 355 | redraw = uiSendEvent(state.frame, &ui_event); |
| @@ -357,7 +361,7 @@ int main( | |||
| 357 | .type = uiEventMouseButton, | 361 | .type = uiEventMouseButton, |
| 358 | .mouse_button = | 362 | .mouse_button = |
| 359 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), | 363 | (uiMouseButtonEvent){.button = ToUiButton(event.button.button), |
| 360 | .state = uiMouseUp, | 364 | .button_state = uiMouseUp, |
| 361 | .mouse_position = (uiPoint){x, y}} | 365 | .mouse_position = (uiPoint){x, y}} |
| 362 | }; | 366 | }; |
| 363 | redraw = uiSendEvent(state.frame, &ev); | 367 | redraw = uiSendEvent(state.frame, &ev); |
| @@ -365,6 +369,11 @@ int main( | |||
| 365 | int x, y; | 369 | int x, y; |
| 366 | MouseCoordsToUiCoords( | 370 | MouseCoordsToUiCoords( |
| 367 | state.window, event.wheel.mouse_x, event.wheel.mouse_y, &x, &y); | 371 | state.window, event.wheel.mouse_x, event.wheel.mouse_y, &x, &y); |
| 372 | // TODO: Scrolling on a trackpad results in 0<event.wheel.y<1 values, | ||
| 373 | // which become 0 with integer truncation. Have the UI library handle | ||
| 374 | // float values for smooth scrolling. Though that means a lot of the UI | ||
| 375 | // input will have to be float-based. Should we handle it here in the | ||
| 376 | // client instead? That seems inconvenient. | ||
| 368 | const uiInputEvent ev = { | 377 | const uiInputEvent ev = { |
| 369 | .type = uiEventMouseScroll, | 378 | .type = uiEventMouseScroll, |
| 370 | .mouse_scroll = | 379 | .mouse_scroll = |
| @@ -372,6 +381,20 @@ int main( | |||
| 372 | .mouse_position = (uiPoint){x, y}} | 381 | .mouse_position = (uiPoint){x, y}} |
| 373 | }; | 382 | }; |
| 374 | redraw = uiSendEvent(state.frame, &ev); | 383 | redraw = uiSendEvent(state.frame, &ev); |
| 384 | } else if (event.type == SDL_EVENT_MOUSE_MOTION) { | ||
| 385 | int x, y; | ||
| 386 | MouseCoordsToUiCoords( | ||
| 387 | state.window, event.motion.x, event.motion.y, &x, &y); | ||
| 388 | const uiInputEvent ev = { | ||
| 389 | .type = uiEventMouseMove, | ||
| 390 | .mouse_move = (uiMouseMoveEvent){ | ||
| 391 | .button_state = | ||
| 392 | { | ||
| 393 | MouseButtonStateFromFlags(event.motion.state, uiLMB), | ||
| 394 | MouseButtonStateFromFlags(event.motion.state, uiRMB), | ||
| 395 | }, .mouse_position = (uiPoint){x, y}} | ||
| 396 | }; | ||
| 397 | redraw = uiSendEvent(state.frame, &ev); | ||
| 375 | } else { | 398 | } else { |
| 376 | EVENT_LOOP_PRINT("event.window.event = %d\n", event.window.event); | 399 | EVENT_LOOP_PRINT("event.window.event = %d\n", event.window.event); |
| 377 | } | 400 | } |
| @@ -417,3 +440,16 @@ void MouseCoordsToUiCoords( | |||
| 417 | *x = (int)(mouse_x * d); | 440 | *x = (int)(mouse_x * d); |
| 418 | *y = (int)(mouse_y * d); | 441 | *y = (int)(mouse_y * d); |
| 419 | } | 442 | } |
| 443 | |||
| 444 | uiMouseButtonState MouseButtonStateFromFlags( | ||
| 445 | SDL_MouseButtonFlags flags, uiMouseButton button) { | ||
| 446 | switch (button) { | ||
| 447 | case uiLMB: | ||
| 448 | return (flags & SDL_BUTTON_LMASK) ? uiMouseDown : uiMouseUp; | ||
| 449 | case uiRMB: | ||
| 450 | return (flags & SDL_BUTTON_RMASK) ? uiMouseDown : uiMouseUp; | ||
| 451 | default: | ||
| 452 | assert(false); | ||
| 453 | return uiMouseUp; | ||
| 454 | } | ||
| 455 | } | ||
