From a0d31fa794cb4d805b83f52495dfb4cb8a0ae861 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 24 Nov 2025 01:04:49 -0700 Subject: Ditching windowed ui --- src/game.c | 59 +-------- src/game.h | 4 +- src/settings.c | 5 +- src/settings.h | 5 +- src/ui.c | 395 --------------------------------------------------------- src/ui.h | 58 --------- 6 files changed, 14 insertions(+), 512 deletions(-) delete mode 100644 src/ui.c delete mode 100644 src/ui.h diff --git a/src/game.c b/src/game.c index 4ada5de..6f87639 100644 --- a/src/game.c +++ b/src/game.c @@ -45,35 +45,13 @@ void resetScreenScale(Game* game) } } -FocusCommand testFloatingWindowCallback(FloatingWindow* window, Game* game) -{ - float x = window->rect.x + window->scroll.x; - float y = window->rect.y + window->scroll.y; - - DrawText("test", x + 10, y + 10 + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT, 15, - BLUE); - - // Content size test. - if (window->contentSize.x != 0.0) - { - DrawText("content size test", x + 150.0, y + 150.0, 20, RED); - } - - // Button test. - if (GuiButton((Rectangle){x + 10.0, y + 50.0, 40.0, 20.0}, "A")) - { - puts("hi"); - } - - return NO_FOCUS_ACTION; -} - void initGame(Game* game) { game->sceneId = GAME_SCENE; // Settings. game->settings = defaultSettings(); + game->isCrossHairEnabled = game->settings.isCrossHairEnabledDefault; // Window. InitWindow(game->settings.windowWidth, game->settings.windowHeight, @@ -104,31 +82,6 @@ void initGame(Game* game) game->player.camera.fovy = game->settings.fov; game->player.position = Vector3Scale(game->world.size, 0.5); - // Window manager. - initWindowManager(&game->wm); - - FloatingWindow window = createFloatingWindow("test1", - (Rectangle){0.0, 0.0, - 100.0, 100.0}); - addWindowToWindowManager(&game->wm, window); - - window = createFloatingWindow("test2",\ - (Rectangle){200.0, 200.0, 100.0, 100.0}); - window.callback = testFloatingWindowCallback; - addWindowToWindowManager(&game->wm, window); - - window = createFloatingWindow("test3", - (Rectangle){300.0, 300.0, 100.0, 100.0}); - window.callback = testFloatingWindowCallback; - window.contentSize = (Vector2){500.0, 500.0}; - addWindowToWindowManager(&game->wm, window); - - window = createFloatingWindow("test4", - (Rectangle){400.0, 400.0, 100.0, 100.0}); - window.callback = testFloatingWindowCallback; - window.contentSize = (Vector2){500.0, 500.0}; - addWindowToWindowManager(&game->wm, window); - disableGameCursor(game); } @@ -229,9 +182,13 @@ void updateGameScene(Game* game) drawGameScreen(game); - updateWindowManager(&game->wm, game); + // Cross hair. + if (IsKeyPressed(game->settings.toggleCrossHairKey)) + { + game->isCrossHairEnabled = !game->isCrossHairEnabled; + } - if (!game->isCursorEnabled) + if (game->isCrossHairEnabled) { drawCrosshair(game->settings.crossHairSize, game->settings.crossHairThickness, @@ -284,12 +241,10 @@ void enableGameCursor(Game* game) { game->isCursorEnabled = true; EnableCursor(); - enableWindowManager(&game->wm); } void disableGameCursor(Game* game) { game->isCursorEnabled = false; DisableCursor(); - disableWindowManager(&game->wm); } diff --git a/src/game.h b/src/game.h index 0524c25..d917d70 100644 --- a/src/game.h +++ b/src/game.h @@ -4,7 +4,6 @@ #include "player.h" #include "world.h" #include "entity.h" -#include "ui.h" #ifndef GAME_H #define GAME_H @@ -26,8 +25,7 @@ struct Game { SceneId sceneId; bool isCursorEnabled; - - WindowManager wm; + bool isCrossHairEnabled; struct { RenderTexture render; diff --git a/src/settings.c b/src/settings.c index 7720c64..3171811 100644 --- a/src/settings.c +++ b/src/settings.c @@ -14,7 +14,7 @@ Settings defaultSettings() .gamma = 0.6, .colorCount = 11.0, .maxUpdateDistance = 255.0, - .nonEditWindowAlpha = 255.0 * 0.75, + .isCrossHairEnabledDefault = true, .crossHairSize = 8.0, .crossHairThickness = 3.0, .crossHairColor = BLUE, @@ -23,7 +23,8 @@ Settings defaultSettings() .backwardKey = KEY_S, .rightKey = KEY_D, .leftKey = KEY_A, - .toggleCursorKey = KEY_C, + .toggleCursorKey = KEY_LEFT_ALT, + .toggleCrossHairKey = KEY_C, .interactKey = KEY_E }; } diff --git a/src/settings.h b/src/settings.h index 5a37ab9..468f402 100644 --- a/src/settings.h +++ b/src/settings.h @@ -27,8 +27,8 @@ typedef struct { // Update. float maxUpdateDistance; - // UI. - float nonEditWindowAlpha; // Alpha for windows in non-editable mode. + // Cross hair. + bool isCrossHairEnabledDefault; float crossHairSize; float crossHairThickness; Color crossHairColor; @@ -40,6 +40,7 @@ typedef struct { KeyboardKey rightKey; KeyboardKey leftKey; KeyboardKey toggleCursorKey; + KeyboardKey toggleCrossHairKey; KeyboardKey interactKey; } Settings; diff --git a/src/ui.c b/src/ui.c deleted file mode 100644 index 758bd17..0000000 --- a/src/ui.c +++ /dev/null @@ -1,395 +0,0 @@ -#include "ui.h" -#include "game.h" - -FloatingWindow createFloatingWindow(const char* title, Rectangle rect) -{ - FloatingWindow window = (FloatingWindow){ - .rect = rect, - .minimized = false, - .moving = false, - .resizing = false, - .callback = NULL, - .contentSize = Vector2Zero(), - .scroll = Vector2Zero(), - .hasFocus = false, - .data = NULL - }; - - memcpy(window.title, title, UI_WINDOW_TITLE_MAX * sizeof(char)); - - return window; -} - -void floatingWindowTransformCollisionCheck(FloatingWindow* window) -{ - 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 - }; - - 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; - } -} - -void updateFloatingWindowMoving(FloatingWindow* window) -{ - 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; - } - } -} - -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; - } - - // 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; - } -} - -void updateFloatingWindowMinimized(FloatingWindow* window) -{ - 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}, - window->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; - } -} - -FocusCommand updateFloatingWindowNotMinimized(FloatingWindow* window, - WindowManager* wm, Game* game) -{ - FocusCommand focus = NO_FOCUS_ACTION; - - if (wm->enabled) - { - window->minimized = GuiWindowBox( - (Rectangle){window->rect.x, window->rect.y, window->rect.width, - window->rect.height}, window->title); - } - - // Scissor and draw content within a scroll panel. - if (window->callback != NULL) - { - Rectangle scissor; - - // Handle scrolling. - if (wm->enabled) - { - 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); - } - else - { - scissor = (Rectangle){ - window->rect.x, - window->rect.y + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT, - window->rect.width, - window->rect.height - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT - }; - - Color color = GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)); - color.a = game->settings.nonEditWindowAlpha; - - DrawRectangleRec(scissor, color); - } - - 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); - } - - focus = window->callback(window, game); - - if (requireScissor) - { - EndScissorMode(); - } - } - - // Draw the resize button/icon. - if (wm->enabled) - { - GuiDrawIcon(71, window->rect.x + window->rect.width - 20, - window->rect.y + window->rect.height - 20, 1, GREEN); - } - - return focus; -} - -FocusCommand updateFloatingWindow(FloatingWindow* window, WindowManager* wm, - Game* game) -{ - FocusCommand focus = NO_FOCUS_ACTION; - bool isMouseLeftClick = IsMouseButtonPressed(MOUSE_LEFT_BUTTON); - - if (wm->enabled && !window->hasFocus) - { - GuiSetState(STATE_DISABLED); - } - - // Window movement and resize input and collision check. - if (isMouseLeftClick && !window->moving && !window->resizing) - { - floatingWindowTransformCollisionCheck(window); - } - - // Focus on window. - if (!window->hasFocus && isMouseLeftClick - && CheckCollisionPointRec(GetMousePosition(), window->rect)) - { - focus = REQUEST_FOCUS; - } - - // Window movement and resize update. - if (window->moving && window->hasFocus) - { - updateFloatingWindowMoving(window); - } - else if (window->resizing && window->hasFocus) - { - updateFloatingWindowResizing(window); - } - - // Window and content drawing with scissor and scroll area. - if(window->minimized) - { - if (wm->enabled) - { - updateFloatingWindowMinimized(window); - } - } - else - { - FocusCommand result = updateFloatingWindowNotMinimized(window, wm, game); - focus = result == NO_FOCUS_ACTION ? focus : result; - } - - if (wm->enabled && !window->hasFocus) - { - GuiSetState(STATE_NORMAL); - } - - return focus; -} - -void initWindowManager(WindowManager* wm) -{ - memset(wm, 0, sizeof(WindowManager)); - wm->enabled = true; -} - -void updateWindowManager(WindowManager* wm, Game* game) -{ - Vector2 mousePosition = GetMousePosition(); - int focusOnto = -1; - - if (!wm->enabled) - { - GuiSetState(STATE_DISABLED); - } - - for (int index = 0; index < wm->windowCount; ++index) - { - FocusCommand focus = updateFloatingWindow(&wm->windows[index], wm, game); - - if (focusOnto != -1) - { - continue; - } - - switch (focus) - { - case NO_FOCUS_ACTION: - break; - case REQUEST_FOCUS: - { - bool canFocus = true; - - // Windows ontop of it prevent focus. - for (int innerIndex = index + 1; innerIndex < wm->windowCount; - ++innerIndex) - { - if (CheckCollisionPointRec(mousePosition, - wm->windows[innerIndex].rect)) - { - canFocus = false; - break; - } - } - - if (canFocus) - { - focusOnto = index; - } - } - - break; - case DEMAND_FOCUS: - focusOnto = index; - break; - default: - break; - } - } - - if (!wm->enabled) - { - GuiSetState(STATE_NORMAL); - } - - if (focusOnto != -1) - { - focusOnWindow(wm, focusOnto); - } -} - -void addWindowToWindowManager(WindowManager* wm, FloatingWindow window) -{ - if (wm->windowCount < UI_WINDOW_MAX) - { - if (wm->windowCount > 0) - { - wm->windows[wm->windowCount - 1].hasFocus = false; - } - - wm->windows[wm->windowCount] = window; - wm->windows[wm->windowCount].hasFocus = true; - ++wm->windowCount; - } - else - { - TraceLog(LOG_ERROR, "'wm->windowCount' went over the limit"); - } -} - -void focusOnWindow(WindowManager* wm, int windowIndex) -{ - if (wm->windowCount <= 1) - { - return; - } - - wm->windows[wm->windowCount - 1].hasFocus = false; - FloatingWindow window = wm->windows[windowIndex]; - - for (int index = windowIndex + 1; index < wm->windowCount; ++index) - { - wm->windows[index - 1] = wm->windows[index]; - } - - window.hasFocus = true; - wm->windows[wm->windowCount - 1] = window; -} - -void enableWindowManager(WindowManager* wm) -{ - wm->enabled = true; -} - -void disableWindowManager(WindowManager* wm) -{ - wm->enabled = false; -} diff --git a/src/ui.h b/src/ui.h deleted file mode 100644 index 30853fb..0000000 --- a/src/ui.h +++ /dev/null @@ -1,58 +0,0 @@ -#include "utils.h" - -#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 - -#define UI_WINDOW_TITLE_MAX 32 -#define UI_WINDOW_MAX 4 - -typedef struct FloatingWindow FloatingWindow; -typedef enum FocusCommand FocusCommand; -typedef FocusCommand (*DrawWindowContentCallback)(FloatingWindow* window, - Game* game); - -enum FocusCommand { - NO_FOCUS_ACTION, - REQUEST_FOCUS, - DEMAND_FOCUS -}; - -struct FloatingWindow { - char title[UI_WINDOW_TITLE_MAX]; - Rectangle rect; - bool minimized; - bool moving; - bool resizing; - DrawWindowContentCallback callback; - Vector2 contentSize; - Vector2 scroll; - bool hasFocus; - void* data; -}; - -typedef struct { - FloatingWindow windows[UI_WINDOW_MAX]; - int windowCount; - bool enabled; -} WindowManager; - -FloatingWindow createFloatingWindow(const char* title, Rectangle rect); -FocusCommand updateFloatingWindow(FloatingWindow* window, WindowManager* wm, - Game* game); - -void initWindowManager(WindowManager* wm); -void updateWindowManager(WindowManager* wm, Game* game); -void addWindowToWindowManager(WindowManager* wm, FloatingWindow window); -void focusOnWindow(WindowManager* wm, int windowIndex); -void enableWindowManager(WindowManager* wm); -void disableWindowManager(WindowManager* wm); - -#endif -- cgit v1.2.3