Преглед на файлове

REDESIGNED: GuiMessageBox()

Ray преди 6 години
родител
ревизия
732edd9764
променени са 2 файла, в които са добавени 35 реда и са изтрити 7 реда
  1. 17 2
      examples/controls_test_suite/controls_test_suite.c
  2. 18 5
      src/raygui.h

+ 17 - 2
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();

+ 18 - 5
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;