controls_test_suite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*******************************************************************************************
  2. *
  3. * raygui - controls test suite
  4. *
  5. * TEST CONTROLS:
  6. * - GuiDropdownBox()
  7. * - GuiValueBox()
  8. * - GuiSpinner()
  9. * - GuiTextBox()
  10. * - GuiTextBoxMulti()
  11. * - GuiListView()
  12. *
  13. * DEPENDENCIES:
  14. * raylib 2.1 - Windowing/input management and drawing.
  15. * raygui 2.0 - Immediate-mode GUI controls.
  16. *
  17. * COMPILATION (Windows - MinGW):
  18. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  19. *
  20. * LICENSE: zlib/libpng
  21. *
  22. * Copyright (c) 2019 raylib technologies (@raylibtech)
  23. *
  24. **********************************************************************************************/
  25. #include "raylib.h"
  26. #define RAYGUI_IMPLEMENTATION
  27. #define RAYGUI_SUPPORT_RICONS
  28. #include "../../src/raygui.h"
  29. #undef RAYGUI_IMPLEMENTATION // Avoid including raygui implementation again
  30. //------------------------------------------------------------------------------------
  31. // Program main entry point
  32. //------------------------------------------------------------------------------------
  33. int main()
  34. {
  35. // Initialization
  36. //---------------------------------------------------------------------------------------
  37. int screenWidth = 690;
  38. int screenHeight = 560;
  39. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  40. SetExitKey(0);
  41. // GUI controls initialization
  42. //----------------------------------------------------------------------------------
  43. int dropdownBox000Active = 0;
  44. bool dropDown000EditMode = false;
  45. int dropdownBox001Active = 0;
  46. bool dropDown001EditMode = false;
  47. int spinner001Value = 0;
  48. bool spinnerEditMode = false;
  49. int valueBox002Value = 0;
  50. bool valueBoxEditMode = false;
  51. char textBoxText[64] = "Text box";
  52. bool textBoxEditMode = false;
  53. int listViewScrollIndex = 0;
  54. int listViewActive = -1;
  55. int listViewExScrollIndex = 0;
  56. int listViewExActive = 2;
  57. int listViewExFocus = -1;
  58. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  59. int listViewExElementsEnable[8] = { 1, 0, 1, 1, 0, 0, 1 };
  60. char multiTextBoxText[141] = "Multi text box";
  61. bool multiTextBoxEditMode = false;
  62. Color colorPickerValue = RED;
  63. int sliderValue = 50;
  64. int sliderBarValue = 60;
  65. float progressValue = 0.4f;
  66. bool forceSquaredChecked = false;
  67. float alphaValue = 0.5f;
  68. int comboBoxActive = 1;
  69. int toggleGroupActive = 0;
  70. Vector2 viewScroll = { 0, 0 };
  71. //----------------------------------------------------------------------------------
  72. // Custom GUI font loading
  73. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  74. //GuiFont(font);
  75. bool exitWindow = false;
  76. bool showMessageBox = false;
  77. char textInput[256] = { 0 };
  78. bool showTextInputBox = false;
  79. char textInputFileName[256] = { 0 };
  80. SetTargetFPS(60);
  81. //--------------------------------------------------------------------------------------
  82. // Main game loop
  83. while (!exitWindow) // Detect window close button or ESC key
  84. {
  85. // Update
  86. //----------------------------------------------------------------------------------
  87. exitWindow = WindowShouldClose();
  88. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  89. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  90. if (IsFileDropped())
  91. {
  92. int dropsCount = 0;
  93. char **droppedFiles = GetDroppedFiles(&dropsCount);
  94. if ((dropsCount > 0) && IsFileExtension(droppedFiles[0], ".rgs")) GuiLoadStyle(droppedFiles[0]);
  95. ClearDroppedFiles(); // Clear internal buffers
  96. }
  97. //----------------------------------------------------------------------------------
  98. // Draw
  99. //----------------------------------------------------------------------------------
  100. BeginDrawing();
  101. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  102. // raygui: controls drawing
  103. //----------------------------------------------------------------------------------
  104. if (dropDown000EditMode || dropDown001EditMode) GuiLock();
  105. //GuiDisable();
  106. // First GUI column
  107. forceSquaredChecked = GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "Force Square", forceSquaredChecked);
  108. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  109. if (GuiSpinner((Rectangle){ 25, 135, 125, 30 }, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
  110. if (GuiValueBox((Rectangle){ 25, 175, 125, 30 }, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
  111. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  112. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  113. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  114. //if (GuiButton((Rectangle){ 25, 255, 125, 30 }, "#05#Open File")) { };
  115. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(RICON_FILE_OPEN, "Open File"))) { };
  116. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  117. GuiLock();
  118. GuiState(GUI_STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  119. GuiState(GUI_STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  120. GuiState(GUI_STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  121. GuiState(GUI_STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  122. GuiState(GUI_STATE_NORMAL);
  123. GuiUnlock();
  124. comboBoxActive = GuiComboBox((Rectangle){ 25, 470, 125, 30 }, "ONE;TWO;THREE;FOUR", comboBoxActive);
  125. // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
  126. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  127. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  128. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  129. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  130. // Second GUI column
  131. listViewActive = GuiListView((Rectangle){ 165, 25, 140, 140 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, listViewActive);
  132. listViewExActive = GuiListViewEx((Rectangle){ 165, 180, 140, 200 }, listViewExList, 8, &listViewExFocus, &listViewExScrollIndex, listViewExActive);
  133. toggleGroupActive = GuiToggleGroup((Rectangle){ 165, 400, 140, 25 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", toggleGroupActive);
  134. // Third GUI column
  135. if (GuiTextBoxMulti((Rectangle){ 320, 25, 225, 140 }, multiTextBoxText, 141, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode;
  136. colorPickerValue = GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, colorPickerValue);
  137. sliderValue = GuiSlider((Rectangle){ 370, 400, 200, 20 }, "#49#TEST", sliderValue, -50, 100, true);
  138. sliderBarValue = GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, sliderBarValue, 0, 100, true);
  139. progressValue = GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, progressValue, 0, 1, true);
  140. Rectangle view = GuiScrollPanel((Rectangle){ 560, 25, 100, 160 }, (Rectangle){ 560, 25, 200, 400 }, &viewScroll);
  141. GuiSetStyle(DEFAULT, INNER_PADDING, 10);
  142. GuiStatusBar((Rectangle){ 0, GetScreenHeight() - 20, GetScreenWidth(), 20 }, "This is a status bar");
  143. GuiSetStyle(DEFAULT, INNER_PADDING, 2);
  144. alphaValue = GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, alphaValue);
  145. if (showMessageBox)
  146. {
  147. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  148. 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");
  149. if ((result == 0) || (result == 2)) showMessageBox = false;
  150. else if (result == 1) exitWindow = true;
  151. }
  152. if (showTextInputBox)
  153. {
  154. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  155. int result = GuiTextInputBox((Rectangle){ GetScreenWidth()/2 - 120, GetScreenHeight()/2 - 60, 240, 120 }, GuiIconText(RICON_FILE_SAVE, "Save file as..."), textInput, "Ok;Cancel");
  156. if (result == 1)
  157. {
  158. // TODO: Validate textInput value and save
  159. strcpy(textInputFileName, textInput);
  160. }
  161. if ((result == 0) || (result == 1) || (result == 2))
  162. {
  163. showTextInputBox = false;
  164. strcpy(textInput, "\0");
  165. }
  166. }
  167. GuiUnlock();
  168. //----------------------------------------------------------------------------------
  169. EndDrawing();
  170. //----------------------------------------------------------------------------------
  171. }
  172. // De-Initialization
  173. //--------------------------------------------------------------------------------------
  174. CloseWindow(); // Close window and OpenGL context
  175. //--------------------------------------------------------------------------------------
  176. return 0;
  177. }