From 732edd976427963b231cfb93f36fdfd66830500a Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 22 Feb 2019 18:33:35 +0100 Subject: [PATCH] REDESIGNED: GuiMessageBox() --- .../controls_test_suite/controls_test_suite.c | 19 +++++++++++++-- src/raygui.h | 23 +++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/examples/controls_test_suite/controls_test_suite.c b/examples/controls_test_suite/controls_test_suite.c index 22cf8b3..006d939 100644 --- a/examples/controls_test_suite/controls_test_suite.c +++ b/examples/controls_test_suite/controls_test_suite.c @@ -39,6 +39,7 @@ int main() int screenHeight = 560; InitWindow(screenWidth, screenHeight, "raygui - controls test suite"); + SetExitKey(0); // GUI controls initialization //---------------------------------------------------------------------------------- @@ -88,16 +89,21 @@ int main() // Custom GUI font loading //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0); //GuiFont(font); + + bool exitWindow = false; + bool showMessageBox = false; SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!exitWindow) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- - // TODO: Implement required update logic + exitWindow = WindowShouldClose(); + + if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = true; //---------------------------------------------------------------------------------- // Draw @@ -160,6 +166,15 @@ int main() GuiSetStyle(DEFAULT, INNER_PADDING, 10); GuiStatusBar((Rectangle){ 0, GetScreenHeight() - 20, GetScreenWidth(), 20 }, "This is a status bar"); GuiSetStyle(DEFAULT, INNER_PADDING, 2); + + if (showMessageBox) + { + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.7f)); + int message = GuiMessageBox((Rectangle){ GetScreenWidth()/2 - 125, GetScreenHeight()/2 - 50, 250, 100 }, "Closing rTexPacker", "Do you really want to exit?", "Yes;No"); + + if ((message == 0) || (message == 2)) showMessageBox = false; + else if (message == 1) exitWindow = true; + } //GuiEnable(); GuiUnlock(); diff --git a/src/raygui.h b/src/raygui.h index 194b5c7..c650676 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -2704,9 +2704,18 @@ RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *windowTitle, const cha const char **buttonsText = GuiTextSplit(buttons, &buttonsCount, NULL); Vector2 textSize = MeasureTextEx(GetFontDefault(), message, GuiGetStyle(DEFAULT, TEXT_SIZE), 1); - Rectangle textBounds = { bounds.x + bounds.width/2 - textSize.x/2, bounds.y + bounds.height/2 - textSize.y/2 + 12, textSize.x, textSize.y }; + + Rectangle textBounds = { 0 }; + textBounds.x = bounds.x + bounds.width/2 - textSize.x/2; + textBounds.y = bounds.y + WINDOW_STATUSBAR_HEIGHT + (bounds.height - WINDOW_STATUSBAR_HEIGHT)/4 - textSize.y/2; + textBounds.width = textSize.x; + textBounds.height = textSize.y; - Rectangle buttonBounds = { bounds.x + MESSAGEBOX_BUTTON_PADDING, bounds.y + bounds.height/2 - MESSAGEBOX_BUTTON_PADDING - MESSAGEBOX_BUTTON_HEIGHT, bounds.width - MESSAGEBOX_BUTTON_PADDING*2, MESSAGEBOX_BUTTON_HEIGHT }; + Rectangle buttonBounds = { 0 }; + buttonBounds.x = bounds.x + MESSAGEBOX_BUTTON_PADDING; + buttonBounds.y = bounds.y + bounds.height/2 + bounds.height/4 - MESSAGEBOX_BUTTON_HEIGHT/2; + buttonBounds.width = (bounds.width - MESSAGEBOX_BUTTON_PADDING*(buttonsCount + 1))/buttonsCount; + buttonBounds.height = MESSAGEBOX_BUTTON_HEIGHT; // Draw control //-------------------------------------------------------------------- @@ -2717,12 +2726,16 @@ RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *windowTitle, const cha GuiLabel(textBounds, message); GuiSetStyle(LABEL, TEXT_ALIGNMENT, prevTextAlignment); + prevTextAlignment = GuiGetStyle(BUTTON, TEXT_ALIGNMENT); + GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER); + for (int i = 0; i < buttonsCount; i++) { - //buttonBounds.x = - - if (GuiButton(buttonBounds, buttonsText[i]) clicked = i + 1; + if (GuiButton(buttonBounds, buttonsText[i])) clicked = i + 1; + buttonBounds.x += (buttonBounds.width + MESSAGEBOX_BUTTON_PADDING); } + + GuiSetStyle(BUTTON, TEXT_ALIGNMENT, prevTextAlignment); //-------------------------------------------------------------------- return clicked;