Making the default button stand out

This commit is contained in:
noffie 2024-12-22 21:30:08 +01:00
parent 8ccaaca579
commit d375f9ca7d
2 changed files with 126 additions and 119 deletions

View file

@ -6,47 +6,47 @@
class GuiMovableWindow : public sva::GuiComponent class GuiMovableWindow : public sva::GuiComponent
{ {
private: private:
bool m_DragWindow = false; bool m_DragWindow = false;
Vector2 m_PanOffset = { 0,0 }; Vector2 m_PanOffset = { 0,0 };
protected: protected:
Rectangle m_WndRect = { 20,20, 200, 100 }; Rectangle m_WndRect = { 20,20, 200, 100 };
bool m_WindowOpen = true; bool m_WindowOpen = true;
public: public:
virtual int input() override virtual int input() override
{ {
if (!m_WindowOpen) return 0; if (!m_WindowOpen) return 0;
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !m_DragWindow) if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !m_DragWindow)
{ {
if (CheckCollisionPointRec(mouse_pos, { m_WndRect.x, m_WndRect.y, m_WndRect.width - 24, 20 })) if (CheckCollisionPointRec(mouse_pos, { m_WndRect.x, m_WndRect.y, m_WndRect.width - 24, 20 }))
{ {
m_DragWindow = true; m_DragWindow = true;
m_PanOffset = { mouse_pos.x - m_WndRect.x, mouse_pos.y - m_WndRect.y }; m_PanOffset = { mouse_pos.x - m_WndRect.x, mouse_pos.y - m_WndRect.y };
} }
} }
if (m_DragWindow) if (m_DragWindow)
{ {
m_WndRect.x = (mouse_pos.x - m_PanOffset.x); m_WndRect.x = (mouse_pos.x - m_PanOffset.x);
m_WndRect.y = (mouse_pos.y - m_PanOffset.y); m_WndRect.y = (mouse_pos.y - m_PanOffset.y);
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) m_DragWindow = false; if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) m_DragWindow = false;
} }
return rinput(mouse_pos); return rinput(mouse_pos);
} }
virtual int rinput(Vector2& mouse_position) { return 0; }; virtual int rinput(Vector2& mouse_position) { return 0; };
virtual int draw() override virtual int draw() override
{ {
if (!m_WindowOpen) return 0; if (!m_WindowOpen) return 0;
m_WindowOpen = !GuiWindowBox(m_WndRect, "Example Movable Window"); m_WindowOpen = !GuiWindowBox(m_WndRect, "Example Movable Window");
return 0; return 0;
} }
}; };
@ -58,102 +58,108 @@ class GuiMovableWindow : public sva::GuiComponent
****************************************************/ ****************************************************/
class SafeClosePopup final : public GuiMovableWindow class SafeClosePopup final : public GuiMovableWindow
{ {
Vector2 anchor03 = { 0,0 }; Vector2 anchor03 = { 0,0 };
bool m_CQB_YesButton = false; bool m_CQB_YesButton = false;
bool m_CQB_NoButton = false; bool m_CQB_NoButton = false;
public: public:
~SafeClosePopup() { ~SafeClosePopup() {
CloseWindow(); CloseWindow();
} }
void onAttach() override void onAttach() override
{ {
m_WindowOpen = false; m_WindowOpen = false;
} }
int draw() override int draw() override
{ {
if (WindowShouldClose()) if (WindowShouldClose())
{ {
if (m_WindowOpen) { if (m_WindowOpen) {
CloseWindow(); CloseWindow();
} }
else { else {
OpenWindow(); OpenWindow();
} }
onResize(); onResize();
} }
if (m_WindowOpen) if (m_WindowOpen)
{ {
GuiUnlock(); GuiUnlock();
anchor03 = { m_WndRect.x + 168, m_WndRect.y + 88 }; anchor03 = { m_WndRect.x + 168, m_WndRect.y + 88 };
m_WindowOpen = !GuiWindowBox(m_WndRect, "#191# Are you sure you want to close this program?"); m_WindowOpen = !GuiWindowBox(m_WndRect, "#191# Are you sure you want to close this program?");
m_CQB_YesButton = GuiButton({ anchor03.x + -152, anchor03.y + 32, 120, 24 }, "#112#Yes"); int border_color = GuiGetStyle(DEFAULT, BORDER_COLOR_NORMAL);
m_CQB_NoButton = GuiButton({ anchor03.x + 24, anchor03.y + 32, 120, 24 }, "#113#No"); int base_color = GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL);
GuiLabel({ anchor03.x + -104, anchor03.y + -40, 208, 24 }, "Are you sure you want to close this?"); GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, 0x7192C2FF);
GuiLabel({ anchor03.x + -56, anchor03.y + -8, 120, 24 }, "Press \"Yes\" to close"); GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, 0xBBCDD3FF);
if (m_CQB_YesButton) m_CQB_YesButton = GuiButton({ anchor03.x + -152, anchor03.y + 32, 120, 24 }, "#112#Yes");
{ GuiSetStyle(DEFAULT, BORDER_COLOR_NORMAL, border_color);
*m_WndRunning = false; GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, base_color);
return 1; m_CQB_NoButton = GuiButton({ anchor03.x + 24, anchor03.y + 32, 120, 24 }, "#113#No");
} GuiLabel({ anchor03.x + -104, anchor03.y + -40, 208, 24 }, "Are you sure you want to close this?");
if (m_CQB_NoButton) CloseWindow(); GuiLabel({ anchor03.x + -56, anchor03.y + -8, 120, 24 }, "Press \"Yes\" to close");
} if (m_CQB_YesButton)
return 0; {
} *m_WndRunning = false;
return 1;
}
if (m_CQB_NoButton) CloseWindow();
}
return 0;
}
int rinput(Vector2& mouse_position) override int rinput(Vector2& mouse_position) override
{ {
if (IsKeyReleased(KEY_ENTER)) { if (IsKeyReleased(KEY_ENTER)) {
*m_WndRunning = false; *m_WndRunning = false;
return 1; return 1;
} }
GuiLock(); GuiLock();
return 0; return 0;
} }
void onResize() override void onResize() override
{ {
anchor03 = { static_cast<float>(m_WndWidth) / 2 , static_cast<float>(m_WndHeight) / 2 }; anchor03 = { static_cast<float>(m_WndWidth) / 2 , static_cast<float>(m_WndHeight) / 2 };
m_WndRect = { anchor03.x + -168, anchor03.y + -88, 328, 160 }; m_WndRect = { anchor03.x + -168, anchor03.y + -88, 328, 160 };
} }
public: public:
void OpenWindow() { void OpenWindow() {
GuiLock(); GuiLock();
m_WindowOpen = true; m_WindowOpen = true;
} }
void CloseWindow() { void CloseWindow() {
m_WindowOpen = false; m_WindowOpen = false;
GuiUnlock(); GuiUnlock();
} }
}; };
class SettingsComponent final : public sva::GuiComponent { class SettingsComponent final : public sva::GuiComponent {
public: public:
struct { struct {
bool borderlessFullscreen; bool borderlessFullscreen;
uint32_t state; uint32_t state;
} values; } values;
void onAttach() override { void onAttach() override {
values.borderlessFullscreen = false; values.borderlessFullscreen = false;
values.state = 0; values.state = 0;
} }
int draw() override { int draw() override {
if(GuiButton({100, 100, 300, 30}, "This should not work when the safe window is open!")) { if (GuiButton({ 100, 100, 300, 30 }, "This should not work when the safe window is open!")) {
std::cout << "got pressed\n"; std::cout << "got pressed\n";
} }
return 0; return 0;
} }
}; };

View file

@ -59,7 +59,7 @@ int main(int argc, char** argv)
ClearWindowState(ConfigFlags::FLAG_WINDOW_TRANSPARENT); ClearWindowState(ConfigFlags::FLAG_WINDOW_TRANSPARENT);
SetWindowState(ConfigFlags::FLAG_WINDOW_ALWAYS_RUN); SetWindowState(ConfigFlags::FLAG_WINDOW_ALWAYS_RUN);
SetWindowState(ConfigFlags::FLAG_WINDOW_RESIZABLE); SetWindowState(ConfigFlags::FLAG_WINDOW_RESIZABLE);
SetWindowFocused(); SetWindowFocused();
@ -74,10 +74,9 @@ int main(int argc, char** argv)
int run_result = 0; int run_result = 0;
ComponentStack::push<SettingsComponent>(); ComponentStack::push<SettingsComponent>();
// always on top...
// always on top...
SafeClosePopup safe_close_popup; SafeClosePopup safe_close_popup;
safe_close_popup.attach(&is_running); safe_close_popup.attach(&is_running);
safe_close_popup.wndrsize(ComponentStack::s_WndWidth, ComponentStack::s_WndHeight); safe_close_popup.wndrsize(ComponentStack::s_WndWidth, ComponentStack::s_WndHeight);
@ -114,6 +113,8 @@ int main(int argc, char** argv)
switch (run_result) switch (run_result)
{ {
case 1:
return 0;
case 2: case 2:
spdlog::warn("Program exiting abnormally."); spdlog::warn("Program exiting abnormally.");
break; break;
@ -121,5 +122,5 @@ int main(int argc, char** argv)
break; break;
} }
return 0; return run_result;
} }