From b2b752870967b191d9d8732f56df7c34fe37fea2 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 2 Aug 2019 17:19:00 +0200 Subject: [PATCH] ADDED: GuiTextInputBox() --- .../controls_test_suite/controls_test_suite.c | 21 ++++++- src/raygui.h | 55 +++++++++++++++---- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/examples/controls_test_suite/controls_test_suite.c b/examples/controls_test_suite/controls_test_suite.c index 0725947..b6c4a91 100644 --- a/examples/controls_test_suite/controls_test_suite.c +++ b/examples/controls_test_suite/controls_test_suite.c @@ -95,6 +95,9 @@ int main() bool exitWindow = false; bool showMessageBox = false; + + char textInput[256] = { 0 }; + bool showTextInputBox = false; SetTargetFPS(60); //-------------------------------------------------------------------------------------- @@ -108,6 +111,8 @@ int main() if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox; + if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true; + if (IsFileDropped()) { int dropsCount = 0; @@ -188,10 +193,20 @@ int main() if (showMessageBox) { DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f)); - int message = GuiMessageBox((Rectangle){ GetScreenWidth()/2 - 125, GetScreenHeight()/2 - 50, 250, 100 }, GuiIconText(RICON_EXIT, "Closing rTexPacker"), "Do you really want to exit?", "Yes;No"); + int result = GuiMessageBox((Rectangle){ GetScreenWidth()/2 - 125, GetScreenHeight()/2 - 50, 250, 100 }, GuiIconText(RICON_EXIT, "Closing rTexPacker"), "Do you really want to exit?", "Yes;No"); - if ((message == 0) || (message == 2)) showMessageBox = false; - else if (message == 1) exitWindow = true; + if ((result == 0) || (result == 2)) showMessageBox = false; + else if (result == 1) exitWindow = true; + } + + if (showTextInputBox) + { + char textInputTemp[256] = { 0 }; + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f)); + int result = GuiTextInputBox((Rectangle){ GetScreenWidth()/2 - 120, GetScreenHeight()/2 - 60, 240, 120 }, GuiIconText(RICON_FILE_SAVE, "Save file as..."), textInputTemp, "Ok;Cancel"); + + if ((result == 0) || (result == 2)) showTextInputBox = false; + else if (result == 1) strcpy(textInput, textInputTemp); } //GuiEnable(); diff --git a/src/raygui.h b/src/raygui.h index 8a50fe2..afdec76 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -440,7 +440,7 @@ RAYGUIDEF Vector2 GuiGrid(Rectangle bounds, float spacing, int subdivs); RAYGUIDEF bool GuiListView(Rectangle bounds, const char *text, int *active, int *scrollIndex, bool editMode); // List View control, returns selected list element index RAYGUIDEF bool GuiListViewEx(Rectangle bounds, const char **text, int count, int *enabled, int *active, int *focus, int *scrollIndex, bool editMode); // List View with extended parameters RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *windowTitle, const char *message, const char *buttons); // Message Box control, displays a message -RAYGUIDEF int GuiTextInputBox(Rectangle bounds, const char *windowTitle, const char *message, char *text, const char *buttons); // Text Input Box control, ask for text +RAYGUIDEF int GuiTextInputBox(Rectangle bounds, const char *windowTitle, char *text, const char *buttons); // Text Input Box control, ask for text RAYGUIDEF Color GuiColorPicker(Rectangle bounds, Color color); // Color Picker control // Styles loading functions @@ -3878,21 +3878,20 @@ RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *windowTitle, const cha int buttonsCount = 0; const char **buttonsText = GuiTextSplit(buttons, &buttonsCount, NULL); + Rectangle buttonBounds = { 0 }; + buttonBounds.x = bounds.x + MESSAGEBOX_BUTTON_PADDING; + buttonBounds.y = bounds.y + bounds.height - MESSAGEBOX_BUTTON_HEIGHT - MESSAGEBOX_BUTTON_PADDING; + buttonBounds.width = (bounds.width - MESSAGEBOX_BUTTON_PADDING*(buttonsCount + 1))/buttonsCount; + buttonBounds.height = MESSAGEBOX_BUTTON_HEIGHT; Vector2 textSize = MeasureTextEx(guiFont, message, GuiGetStyle(DEFAULT, TEXT_SIZE), 1); 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.y = bounds.y + WINDOW_STATUSBAR_HEIGHT + (bounds.height - WINDOW_STATUSBAR_HEIGHT - MESSAGEBOX_BUTTON_HEIGHT - MESSAGEBOX_BUTTON_PADDING)/2 - textSize.y/2; textBounds.width = textSize.x; textBounds.height = textSize.y; - 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 //-------------------------------------------------------------------- if (GuiWindowBox(bounds, windowTitle)) clicked = 0; @@ -3918,11 +3917,45 @@ RAYGUIDEF int GuiMessageBox(Rectangle bounds, const char *windowTitle, const cha } // Text Input Box control, ask for text -RAYGUIDEF int GuiTextInputBox(Rectangle bounds, const char *windowTitle, const char *message, char *text, const char *buttons) +RAYGUIDEF int GuiTextInputBox(Rectangle bounds, const char *windowTitle, char *text, const char *buttons) { - int btnIndex = -1; + #define TEXTINPUTBOX_BUTTON_HEIGHT 24 + #define TEXTINPUTBOX_BUTTON_PADDING 10 + #define TEXTINPUTBOX_HEIGHT 30 - // TODO: GuiTextInputBox() + int btnIndex = -1; + + int buttonsCount = 0; + const char **buttonsText = GuiTextSplit(buttons, &buttonsCount, NULL); + Rectangle buttonBounds = { 0 }; + buttonBounds.x = bounds.x + TEXTINPUTBOX_BUTTON_PADDING; + buttonBounds.y = bounds.y + bounds.height - TEXTINPUTBOX_BUTTON_HEIGHT - TEXTINPUTBOX_BUTTON_PADDING; + buttonBounds.width = (bounds.width - TEXTINPUTBOX_BUTTON_PADDING*(buttonsCount + 1))/buttonsCount; + buttonBounds.height = TEXTINPUTBOX_BUTTON_HEIGHT; + + Rectangle textBoxBounds = { 0 }; + textBoxBounds.x = bounds.x + TEXTINPUTBOX_BUTTON_PADDING; + textBoxBounds.y = bounds.y + WINDOW_STATUSBAR_HEIGHT + (bounds.height - WINDOW_STATUSBAR_HEIGHT - TEXTINPUTBOX_BUTTON_HEIGHT - TEXTINPUTBOX_BUTTON_PADDING)/2 - TEXTINPUTBOX_HEIGHT/2; + textBoxBounds.width = bounds.width - TEXTINPUTBOX_BUTTON_PADDING*2; + textBoxBounds.height = TEXTINPUTBOX_HEIGHT; + + // Draw control + //-------------------------------------------------------------------- + if (GuiWindowBox(bounds, windowTitle)) btnIndex = 0; + + GuiTextBox(textBoxBounds, text, 256, true); + + int prevBtnTextAlignment = GuiGetStyle(BUTTON, TEXT_ALIGNMENT); + GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER); + + for (int i = 0; i < buttonsCount; i++) + { + if (GuiButton(buttonBounds, buttonsText[i])) btnIndex = i + 1; + buttonBounds.x += (buttonBounds.width + MESSAGEBOX_BUTTON_PADDING); + } + + GuiSetStyle(BUTTON, TEXT_ALIGNMENT, prevBtnTextAlignment); + //-------------------------------------------------------------------- return btnIndex; }