diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/messageArea.c | 7 | ||||
| -rw-r--r-- | src/messageArea.h | 3 | ||||
| -rw-r--r-- | src/ui.c | 194 | ||||
| -rw-r--r-- | src/ui.h | 19 | ||||
| -rw-r--r-- | src/utils.c | 144 | ||||
| -rw-r--r-- | src/utils.h | 12 |
6 files changed, 218 insertions, 161 deletions
diff --git a/src/messageArea.c b/src/messageArea.c index bf2c70d..21fea03 100644 --- a/src/messageArea.c +++ b/src/messageArea.c @@ -4,12 +4,11 @@ void initMessageArea(MessageArea* messageArea) { memset(messageArea->text, 0, sizeof(char) * MESSAGE_AREA_MAX); memcpy(messageArea->text, "meowmeowmeowmeowmeowmeowmeowmeowmweowiwiweioo", sizeof("meowmeowmeowmeowmeowmeowmeowmeowmweowiwiweioo")); - messageArea->bounds = (Rectangle){100.0, 100.0, 100.0, 100.0}; + + messageArea->window = createFloatingWindow(); } void updateMessageArea(MessageArea* messageArea, Game* game) { - DrawRectangleRec(messageArea->bounds, - GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR))); - GuiLabel(messageArea->bounds, messageArea->text); + updateFloatingWindow(&messageArea->window, "meow"); } diff --git a/src/messageArea.h b/src/messageArea.h index ec34452..1fd0a70 100644 --- a/src/messageArea.h +++ b/src/messageArea.h @@ -1,4 +1,5 @@ #include "utils.h" +#include "ui.h" #ifndef MESSAGE_AREA_H #define MESSAGE_AREA_H @@ -7,7 +8,7 @@ typedef struct { char text[MESSAGE_AREA_MAX]; - Rectangle bounds; + FloatingWindow window; } MessageArea; void initMessageArea(MessageArea* messageArea); diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..72956a5 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,194 @@ +#include "ui.h" + +FloatingWindow createFloatingWindow() +{ + return (FloatingWindow){ + .rect = (Rectangle){0.0, 0.0, 100.0, 100.0}, + .minimized = false, + .moving = false, + .resizing = false, + .callback = NULL, + .contentSize = Vector2Zero(), + .scroll = Vector2Zero() + }; +} + +void updateFloatingWindow(FloatingWindow* window, const char* title) +{ +#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; + + // 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 + }; + + 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 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; + } + } + } + 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; + } + + // 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()) + { + 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; + } + } + + // window and content drawing with scissor and scroll area + if(window->minimized) + { + 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#")) + { + window->minimized = false; + } + + } else { + 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) + { + BeginScissorMode(scissor.x, scissor.y, scissor.width, scissor.height); + } + + window->callback((Vector2){window->rect.x, window->rect.y}, + window->scroll); + + if (requireScissor) + { + EndScissorMode(); + } + } + + // Draw the resize button/icon. + GuiDrawIcon(71, window->rect.x + window->rect.width - 20, + window->rect.y + window->rect.height - 20, 1, GREEN); + } +} diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..80c669f --- /dev/null +++ b/src/ui.h @@ -0,0 +1,19 @@ +#include "utils.h" + +#ifndef UI_H +#define UI_H + +typedef struct { + Rectangle rect; + bool minimized; + bool moving; + bool resizing; + DrawWindowContentCallback callback; + Vector2 contentSize; + Vector2 scroll; +} FloatingWindow; + +FloatingWindow createFloatingWindow(); +void updateFloatingWindow(FloatingWindow* window, const char* title); + +#endif diff --git a/src/utils.c b/src/utils.c index 0a17a05..02d7388 100644 --- a/src/utils.c +++ b/src/utils.c @@ -34,148 +34,4 @@ Vector3 randomDirection3(int seed, int* nextSeed) return Vector3Normalize(direction); } -void updateFloatWindow(FloatingWindow* window, const char* title) -{ -#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; - - // 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? diff --git a/src/utils.h b/src/utils.h index eba0333..96261b9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -67,19 +67,7 @@ typedef enum FTError { FTSUCCESS = 0 } FTError; -typedef struct { - Rectangle rect; - bool minimized; - bool moving; - bool resizing; - DrawWindowContentCallback callback; - Vector2 contentSize; - Vector2 scroll; -} FloatingWindow; - Vector2 randomDirection2(int seed, int* nextSeed); Vector3 randomDirection3(int seed, int* nextSeed); -void updateFloatWindow(FloatingWindow* window, const char* title); - #endif |
