diff options
| author | nathan <nathansmith@disroot.org> | 2025-11-06 11:04:42 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2025-11-06 11:04:42 +0000 |
| commit | 6fea2be59237f6d43c45dbda95f62346779b21e6 (patch) | |
| tree | 62123859bed12985af4cb3475fb1575b70a41eb6 /src | |
| parent | 485584966a5592eec6fd8b3b815cb23f26984733 (diff) | |
| download | FindThings-6fea2be59237f6d43c45dbda95f62346779b21e6.tar.gz FindThings-6fea2be59237f6d43c45dbda95f62346779b21e6.tar.bz2 FindThings-6fea2be59237f6d43c45dbda95f62346779b21e6.zip | |
Cleaned up floating window code
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui.c | 308 | ||||
| -rw-r--r-- | src/ui.h | 10 | ||||
| -rw-r--r-- | src/utils.h | 2 |
3 files changed, 174 insertions, 146 deletions
@@ -13,182 +13,202 @@ FloatingWindow createFloatingWindow() }; } -void updateFloatingWindow(FloatingWindow* window, const char* title) +void floatingWindowTransformCollisionCheck(FloatingWindow* window) { -#ifndef RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT -#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24 -#endif - -#ifndef RAYGUI_WINDOW_CLOSEBUTTON_SIZE -#define RAYGUI_WINDOW_CLOSEBUTTON_SIZE 18 -#endif - int closeTitleSizeDeltaHalf = (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT - RAYGUI_WINDOW_CLOSEBUTTON_SIZE) / 2; + Vector2 mousePosition = GetMousePosition(); + + Rectangle titleCollisionRect = (Rectangle){ + window->rect.x, + window->rect.y, + window->rect.width - (RAYGUI_WINDOW_CLOSEBUTTON_SIZE + + closeTitleSizeDeltaHalf), + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT + }; - // Window movement and resize input and collision check. - if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !window->moving - && !window->resizing) - { - Vector2 mousePosition = GetMousePosition(); - - Rectangle titleCollisionRect = (Rectangle){ - window->rect.x, - window->rect.y, - window->rect.width - (RAYGUI_WINDOW_CLOSEBUTTON_SIZE - + closeTitleSizeDeltaHalf), - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT - }; - - Rectangle resizeCollisionRect = (Rectangle){ - window->rect.x + window->rect.width - 20, - window->rect.y + window->rect.height - 20, - 20, - 20 - }; + Rectangle resizeCollisionRect = (Rectangle){ + window->rect.x + window->rect.width - 20, + window->rect.y + window->rect.height - 20, + 20, + 20 + }; - if (CheckCollisionPointRec(mousePosition, titleCollisionRect)) - { - window->moving = true; - } - else if (!window->minimized - && CheckCollisionPointRec(mousePosition, resizeCollisionRect)) - { - window->resizing = true; - } + if (CheckCollisionPointRec(mousePosition, titleCollisionRect)) + { + window->moving = true; } - - // Window movement and resize update. - if (window->moving) + else if (!window->minimized + && CheckCollisionPointRec(mousePosition, resizeCollisionRect)) { - Vector2 mouseDelta = GetMouseDelta(); - window->rect.x += mouseDelta.x; - window->rect.y += mouseDelta.y; - - if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) - { - window->moving = false; - - // Clamp window position keep it inside the application area. - if (window->rect.x < 0) - { - window->rect.x = 0; - } - else if (window->rect.x > GetScreenWidth() - window->rect.width) - { - window->rect.x = GetScreenWidth() - window->rect.width; - } - - if (window->rect.y < 0) - { - window->rect.y = 0; - } - else if (window->rect.y > GetScreenHeight()) - { - window->rect.y = GetScreenHeight() - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT; - } - } + window->resizing = true; } - else if (window->resizing) +} + +void updateFloatingWindowMoving(FloatingWindow* window) +{ + Vector2 mouseDelta = GetMouseDelta(); + window->rect.x += mouseDelta.x; + window->rect.y += mouseDelta.y; + + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) { - Vector2 mousePosition = GetMousePosition(); - - if (mousePosition.x > window->rect.x) - { - window->rect.width = mousePosition.x - window->rect.x; - } - if (mousePosition.y > window->rect.y) - { - window->rect.height = mousePosition.y - window->rect.y; - } + window->moving = false; - // Clamp window size to an arbitrary minimum value and the window size as - // the maximum. - if (window->rect.width < 100) + // Clamp window position keep it inside the application area. + if (window->rect.x < 0) { - window->rect.width = 100; + window->rect.x = 0; } - else if (window->rect.width > GetScreenWidth()) + else if (window->rect.x > GetScreenWidth() - window->rect.width) { - window->rect.width = GetScreenWidth(); + window->rect.x = GetScreenWidth() - window->rect.width; } - - if (window->rect.height < 100) + + if (window->rect.y < 0) { - window->rect.height = 100; + window->rect.y = 0; } - else if (window->rect.height > GetScreenHeight()) + else if (window->rect.y > GetScreenHeight()) { - window->rect.height = GetScreenHeight(); + window->rect.y = GetScreenHeight() - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT; } + } +} - if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) - { - window->resizing = false; - } +void updateFloatingWindowResizing(FloatingWindow* window) +{ + Vector2 mousePosition = GetMousePosition(); + + if (mousePosition.x > window->rect.x) + { + window->rect.width = mousePosition.x - window->rect.x; + } + if (mousePosition.y > window->rect.y) + { + window->rect.height = mousePosition.y - window->rect.y; } - // window and content drawing with scissor and scroll area - if(window->minimized) + // Clamp window size to an arbitrary minimum value and the window size as + // the maximum. + if (window->rect.width < 100) + { + window->rect.width = 100; + } + else if (window->rect.width > GetScreenWidth()) { - GuiStatusBar( + window->rect.width = GetScreenWidth(); + } + + if (window->rect.height < 100) + { + window->rect.height = 100; + } + else if (window->rect.height > GetScreenHeight()) + { + window->rect.height = GetScreenHeight(); + } + + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) + { + window->resizing = false; + } +} + +void updateFloatingWindowMinimized(FloatingWindow* window, const char* title) +{ + int closeTitleSizeDeltaHalf = (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT - + RAYGUI_WINDOW_CLOSEBUTTON_SIZE) / 2; + + GuiStatusBar( (Rectangle){window->rect.x, window->rect.y, window->rect.width, RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT}, title); - if (GuiButton( - (Rectangle){window->rect.x + window->rect.width - - RAYGUI_WINDOW_CLOSEBUTTON_SIZE - - closeTitleSizeDeltaHalf, - window->rect.y + closeTitleSizeDeltaHalf, - RAYGUI_WINDOW_CLOSEBUTTON_SIZE, - RAYGUI_WINDOW_CLOSEBUTTON_SIZE}, - "#120#")) + if (GuiButton( + (Rectangle){window->rect.x + window->rect.width + - RAYGUI_WINDOW_CLOSEBUTTON_SIZE + - closeTitleSizeDeltaHalf, + window->rect.y + closeTitleSizeDeltaHalf, + RAYGUI_WINDOW_CLOSEBUTTON_SIZE, + RAYGUI_WINDOW_CLOSEBUTTON_SIZE}, + "#120#")) + { + window->minimized = false; + } +} + +void updateFloatingWindowNotMinimized(FloatingWindow* window, const char* title) +{ + window->minimized = GuiWindowBox( + (Rectangle){window->rect.x, window->rect.y, window->rect.width, + window->rect.height}, title); + + // Scissor and draw content within a scroll panel. + if (window->callback != NULL) + { + Rectangle scissor; + + GuiScrollPanel( + (Rectangle){ + window->rect.x, window->rect.y + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT, + window->rect.width, + window->rect.height - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT}, + NULL, + (Rectangle){ + window->rect.x, window->rect.y, + window->contentSize.x, window->contentSize.y}, + &window->scroll, + &scissor); + + bool requireScissor = window->rect.width < window->contentSize.x + || window->rect.height < window->contentSize.x; + + if (requireScissor) { - window->minimized = false; + BeginScissorMode(scissor.x, scissor.y, scissor.width, scissor.height); } - } else { - window->minimized = GuiWindowBox( - (Rectangle){window->rect.x, window->rect.y, window->rect.width, - window->rect.height}, title); + window->callback((Vector2){window->rect.x, window->rect.y}, + window->scroll); - // Scissor and draw content within a scroll panel. - if (window->callback != NULL) + if (requireScissor) { - Rectangle scissor; - - GuiScrollPanel( - (Rectangle){ - window->rect.x, window->rect.y + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT, - window->rect.width, - window->rect.height - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT}, - NULL, - (Rectangle){ - window->rect.x, window->rect.y, - window->contentSize.x, window->contentSize.y}, - &window->scroll, - &scissor); - - bool requireScissor = window->rect.width < window->contentSize.x - || window->rect.height < window->contentSize.x; - - if (requireScissor) - { - BeginScissorMode(scissor.x, scissor.y, scissor.width, scissor.height); - } - - window->callback((Vector2){window->rect.x, window->rect.y}, - window->scroll); - - if (requireScissor) - { - EndScissorMode(); - } + EndScissorMode(); } + } - // Draw the resize button/icon. - GuiDrawIcon(71, window->rect.x + window->rect.width - 20, - window->rect.y + window->rect.height - 20, 1, GREEN); + // Draw the resize button/icon. + GuiDrawIcon(71, window->rect.x + window->rect.width - 20, + window->rect.y + window->rect.height - 20, 1, GREEN); +} + +void updateFloatingWindow(FloatingWindow* window, const char* title) +{ + // Window movement and resize input and collision check. + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !window->moving + && !window->resizing) + { + floatingWindowTransformCollisionCheck(window); + } + + // Window movement and resize update. + if (window->moving) + { + updateFloatingWindowMoving(window); + } + else if (window->resizing) + { + updateFloatingWindowResizing(window); + } + + // window and content drawing with scissor and scroll area + if(window->minimized) + { + updateFloatingWindowMinimized(window, title); + } + else + { + updateFloatingWindowNotMinimized(window, title); } } @@ -3,6 +3,16 @@ #ifndef UI_H #define UI_H +#ifndef RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT +#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24 +#endif + +#ifndef RAYGUI_WINDOW_CLOSEBUTTON_SIZE +#define RAYGUI_WINDOW_CLOSEBUTTON_SIZE 18 +#endif + +typedef void (*DrawWindowContentCallback)(Vector2 position, Vector2 scroll); + typedef struct { Rectangle rect; bool minimized; diff --git a/src/utils.h b/src/utils.h index 96261b9..75e27fd 100644 --- a/src/utils.h +++ b/src/utils.h @@ -56,8 +56,6 @@ #define RANDOM_DIRECTION_UNITS 4096 -typedef void (*DrawWindowContentCallback)(Vector2 position, Vector2 scroll); - // Typedef hackery. typedef struct Game Game; typedef struct World World; |
