aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2025-11-17 04:19:01 +0000
committernathan <nathansmith@disroot.org>2025-11-17 04:19:01 +0000
commitfe39cc897fbc8d8470d790df5306550c841d95d4 (patch)
tree97243f3c23a471000163fc9e157bb145f5d92536
parent7bb57d60f8d8249eb48c9c7a7e7d3760127a0c3c (diff)
downloadFindThings-fe39cc897fbc8d8470d790df5306550c841d95d4.tar.gz
FindThings-fe39cc897fbc8d8470d790df5306550c841d95d4.tar.bz2
FindThings-fe39cc897fbc8d8470d790df5306550c841d95d4.zip
Window focus stuff
-rw-r--r--src/game.c8
-rw-r--r--src/ui.c78
-rw-r--r--src/ui.h9
3 files changed, 81 insertions, 14 deletions
diff --git a/src/game.c b/src/game.c
index 2775900..a8c4d4a 100644
--- a/src/game.c
+++ b/src/game.c
@@ -93,6 +93,14 @@ void initGame(Game* game)
(Rectangle){200.0, 200.0, 100.0, 100.0});
addWindowToWindowManager(&game->wm, window);
+ window = createFloatingWindow("test3",
+ (Rectangle){300.0, 300.0, 100.0, 100.0});
+ addWindowToWindowManager(&game->wm, window);
+
+ window = createFloatingWindow("test4",
+ (Rectangle){400.0, 400.0, 100.0, 100.0});
+ addWindowToWindowManager(&game->wm, window);
+
disableGameCursor(game);
}
diff --git a/src/ui.c b/src/ui.c
index 9f1ce74..9faabcd 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -9,7 +9,8 @@ FloatingWindow createFloatingWindow(const char* title, Rectangle rect)
.resizing = false,
.callback = NULL,
.contentSize = Vector2Zero(),
- .scroll = Vector2Zero()
+ .scroll = Vector2Zero(),
+ .hasFocus = false
};
memcpy(window.title, title, UI_WINDOW_TITLE_MAX * sizeof(char));
@@ -136,7 +137,8 @@ void updateFloatingWindowMinimized(FloatingWindow* window)
window->rect.y + closeTitleSizeDeltaHalf,
RAYGUI_WINDOW_CLOSEBUTTON_SIZE,
RAYGUI_WINDOW_CLOSEBUTTON_SIZE},
- "#120#"))
+ "#120#")
+ && window->hasFocus)
{
window->minimized = false;
}
@@ -146,7 +148,7 @@ void updateFloatingWindowNotMinimized(FloatingWindow* window)
{
window->minimized = GuiWindowBox(
(Rectangle){window->rect.x, window->rect.y, window->rect.width,
- window->rect.height}, window->title);
+ window->rect.height}, window->title) & window->hasFocus;
// Scissor and draw content within a scroll panel.
if (window->callback != NULL)
@@ -187,9 +189,9 @@ void updateFloatingWindowNotMinimized(FloatingWindow* window)
window->rect.y + window->rect.height - 20, 1, GREEN);
}
-bool updateFloatingWindow(FloatingWindow* window)
+FocusCommand updateFloatingWindow(FloatingWindow* window)
{
- bool focusOnThisWindow = false;
+ FocusCommand focus = NO_FOCUS_ACTION;
bool isMouseLeftClick = IsMouseButtonPressed(MOUSE_LEFT_BUTTON);
// Window movement and resize input and collision check.
@@ -199,18 +201,18 @@ bool updateFloatingWindow(FloatingWindow* window)
}
// Focus on window.
- if (isMouseLeftClick && CheckCollisionPointRec(GetMousePosition(),
- window->rect))
+ if (!window->hasFocus && isMouseLeftClick
+ && CheckCollisionPointRec(GetMousePosition(), window->rect))
{
- focusOnThisWindow = true;
+ focus = REQUEST_FOCUS;
}
// Window movement and resize update.
- if (window->moving)
+ if (window->moving && window->hasFocus)
{
updateFloatingWindowMoving(window);
}
- else if (window->resizing)
+ else if (window->resizing && window->hasFocus)
{
updateFloatingWindowResizing(window);
}
@@ -225,7 +227,7 @@ bool updateFloatingWindow(FloatingWindow* window)
updateFloatingWindowNotMinimized(window);
}
- return focusOnThisWindow;
+ return focus;
}
void initWindowManager(WindowManager* wm)
@@ -235,14 +237,51 @@ void initWindowManager(WindowManager* wm)
void updateWindowManager(WindowManager* wm)
{
+ Vector2 mousePosition = GetMousePosition();
int focusOnto = -1;
for (int index = 0; index < wm->windowCount; ++index)
{
- if (updateFloatingWindow(&wm->windows[index])
- && index != wm->windowCount - 1)
+ FocusCommand focus = updateFloatingWindow(&wm->windows[index]);
+
+ if (focusOnto != -1)
{
+ continue;
+ }
+
+ switch (focus)
+ {
+ case NO_FOCUS_ACTION:
+ break;
+ case REQUEST_FOCUS:
+ {
+ puts("hi");
+ 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;
}
}
@@ -256,7 +295,13 @@ 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
@@ -267,6 +312,12 @@ void addWindowToWindowManager(WindowManager* wm, FloatingWindow window)
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)
@@ -274,5 +325,6 @@ void focusOnWindow(WindowManager* wm, int windowIndex)
wm->windows[index - 1] = wm->windows[index];
}
+ window.hasFocus = true;
wm->windows[wm->windowCount - 1] = window;
}
diff --git a/src/ui.h b/src/ui.h
index e3e5918..76d9b1f 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -16,6 +16,12 @@
typedef void (*DrawWindowContentCallback)(Vector2 position, Vector2 scroll);
+typedef enum {
+ NO_FOCUS_ACTION,
+ REQUEST_FOCUS,
+ DEMAND_FOCUS
+} FocusCommand;
+
typedef struct {
char title[UI_WINDOW_TITLE_MAX];
Rectangle rect;
@@ -25,6 +31,7 @@ typedef struct {
DrawWindowContentCallback callback;
Vector2 contentSize;
Vector2 scroll;
+ bool hasFocus;
} FloatingWindow;
typedef struct {
@@ -33,7 +40,7 @@ typedef struct {
} WindowManager;
FloatingWindow createFloatingWindow(const char* title, Rectangle rect);
-bool updateFloatingWindow(FloatingWindow* window);
+FocusCommand updateFloatingWindow(FloatingWindow* window);
void initWindowManager(WindowManager* wm);
void updateWindowManager(WindowManager* wm);