diff options
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index e8e99cf..0a17a05 100644 --- a/src/utils.c +++ b/src/utils.c @@ -43,6 +43,139 @@ void updateFloatWindow(FloatingWindow* window, const char* title) #ifndef RAYGUI_WINDOW_CLOSEBUTTON_SIZE #define RAYGUI_WINDOW_CLOSEBUTTON_SIZE 18 #endif + + int closeTitleSizeDeltaHalf = (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT - + RAYGUI_WINDOW_CLOSEBUTTON_SIZE) / 2; + + // Window movement and resize 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.0, + window->rect.y + window->rect.height - 20.0, + 20.0, + 20.0 + }; + + if (CheckCollisionPointRec(mousePosition, titleCollisionRect)) + { + window->moving = true; + } + else if (!window->minimized + && CheckCollisionPointRec(mousePosition, resizeCollisionRect)) + { + window->resizing = true; + } + } + + // Window movement and resize update. + if (window->moving) + { + Vector2 mouseDelta = GetMouseDelta(); + window->rect.x += mouseDelta.x; + window->rect.y += mouseDelta.y; + + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) + { + window->moving = false; + + // Clamp window position on screen. + window->rect.x = Clamp(window->rect.x, 0.0, + GetScreenWidth() - window->rect.width); + window->rect.y = Clamp(window->rect.y, 0.0, + GetScreenHeight() - + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT); + } + } + else if (window->resizing) + { + 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; + } + + // TODO: Add window size clamp. + + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) + { + window->resizing = true; + } + } + + if (window->minimized) + { + GuiStatusBar( + (Rectangle){window->rect.x, window->rect.y, window->rect.width, + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT}, + title); + + window->minimized = !GuiButton( + (Rectangle){window->rect.x + window->rect.width - + RAYGUI_WINDOW_CLOSEBUTTON_SIZE - + closeTitleSizeDeltaHalf, + RAYGUI_WINDOW_CLOSEBUTTON_SIZE, + RAYGUI_WINDOW_CLOSEBUTTON_SIZE}, + "#120#"); + } + else + { + window->minimized = GuiWindowBox( + (Rectangle){window->rect.x, window->rect.y, window->rect.width, + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT}, + title); + + 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.y; + + 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(); + } + } + + GuiDrawIcon(71, window->rect.x + window->rect.width - 20, + window->rect.y + window->rect.height - 20, 1, WHITE); + } } // Why does the universe feel strange to exist in? |
