controls_test_suite.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*******************************************************************************************
  2. *
  3. * raygui - controls test suite
  4. *
  5. * TEST CONTROLS:
  6. * - GuiDropdownBox()
  7. * - GuiCheckBox()
  8. * - GuiSpinner()
  9. * - GuiValueBox()
  10. * - GuiTextBox()
  11. * - GuiButton()
  12. * - GuiComboBox()
  13. * - GuiListView()
  14. * - GuiToggleGroup()
  15. * - GuiColorPicker()
  16. * - GuiSlider()
  17. * - GuiSliderBar()
  18. * - GuiProgressBar()
  19. * - GuiColorBarAlpha()
  20. * - GuiScrollPanel()
  21. *
  22. *
  23. * DEPENDENCIES:
  24. * raylib 4.5 - Windowing/input management and drawing
  25. * raygui 3.5 - Immediate-mode GUI controls with custom styling and icons
  26. *
  27. * COMPILATION (Windows - MinGW):
  28. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  29. *
  30. * LICENSE: zlib/libpng
  31. *
  32. * Copyright (c) 2016-2023 Ramon Santamaria (@raysan5)
  33. *
  34. **********************************************************************************************/
  35. #include "raylib.h"
  36. #define RAYGUI_IMPLEMENTATION
  37. //#define RAYGUI_CUSTOM_ICONS // It requires providing gui_icons.h in the same directory
  38. //#include "gui_icons.h" // External icons data provided, it can be generated with rGuiIcons tool
  39. #include "../../src/raygui.h"
  40. // raygui embedded styles
  41. #include "../styles/style_cyber.h" // raygui style: cyber
  42. #include "../styles/style_jungle.h" // raygui style: jungle
  43. #include "../styles/style_lavanda.h" // raygui style: lavanda
  44. #include "../styles/style_dark.h" // raygui style: dark
  45. #include "../styles/style_bluish.h" // raygui style: bluish
  46. #include "../styles/style_terminal.h" // raygui style: terminal
  47. //------------------------------------------------------------------------------------
  48. // Program main entry point
  49. //------------------------------------------------------------------------------------
  50. int main()
  51. {
  52. // Initialization
  53. //---------------------------------------------------------------------------------------
  54. const int screenWidth = 690;
  55. const int screenHeight = 560;
  56. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  57. SetExitKey(0);
  58. // GUI controls initialization
  59. //----------------------------------------------------------------------------------
  60. int dropdownBox000Active = 0;
  61. bool dropDown000EditMode = false;
  62. int dropdownBox001Active = 0;
  63. bool dropDown001EditMode = false;
  64. int spinner001Value = 0;
  65. bool spinnerEditMode = false;
  66. int valueBox002Value = 0;
  67. bool valueBoxEditMode = false;
  68. char textBoxText[64] = "Text box";
  69. bool textBoxEditMode = false;
  70. int listViewScrollIndex = 0;
  71. int listViewActive = -1;
  72. int listViewExScrollIndex = 0;
  73. int listViewExActive = 2;
  74. int listViewExFocus = -1;
  75. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  76. char multiTextBoxText[256] = "Multi text box";
  77. bool multiTextBoxEditMode = false;
  78. Color colorPickerValue = RED;
  79. float sliderValue = 50.0f;
  80. float sliderBarValue = 60;
  81. float progressValue = 0.1f;
  82. bool forceSquaredChecked = false;
  83. float alphaValue = 0.5f;
  84. //int comboBoxActive = 1;
  85. int visualStyleActive = 0;
  86. int prevVisualStyleActive = 0;
  87. int toggleGroupActive = 0;
  88. int toggleSliderActive = 0;
  89. Vector2 viewScroll = { 0, 0 };
  90. //----------------------------------------------------------------------------------
  91. // Custom GUI font loading
  92. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  93. //GuiSetFont(font);
  94. bool exitWindow = false;
  95. bool showMessageBox = false;
  96. char textInput[256] = { 0 };
  97. bool showTextInputBox = false;
  98. char textInputFileName[256] = { 0 };
  99. float alpha = 1.0f;
  100. SetTargetFPS(60);
  101. //--------------------------------------------------------------------------------------
  102. // Main game loop
  103. while (!exitWindow) // Detect window close button or ESC key
  104. {
  105. // Update
  106. //----------------------------------------------------------------------------------
  107. exitWindow = WindowShouldClose();
  108. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  109. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  110. if (IsFileDropped())
  111. {
  112. FilePathList droppedFiles = LoadDroppedFiles();
  113. if ((droppedFiles.count > 0) && IsFileExtension(droppedFiles.paths[0], ".rgs")) GuiLoadStyle(droppedFiles.paths[0]);
  114. UnloadDroppedFiles(droppedFiles); // Clear internal buffers
  115. }
  116. //alpha -= 0.002f;
  117. if (alpha < 0.0f) alpha = 0.0f;
  118. if (IsKeyPressed(KEY_SPACE)) alpha = 1.0f;
  119. GuiSetAlpha(alpha);
  120. //progressValue += 0.002f;
  121. if (IsKeyPressed(KEY_LEFT)) progressValue -= 0.1f;
  122. else if (IsKeyPressed(KEY_RIGHT)) progressValue += 0.1f;
  123. if (progressValue > 1.0f) progressValue = 1.0f;
  124. else if (progressValue < 0.0f) progressValue = 0.0f;
  125. if (visualStyleActive != prevVisualStyleActive)
  126. {
  127. GuiLoadStyleDefault();
  128. switch (visualStyleActive)
  129. {
  130. case 0: break; // Default style
  131. case 1: GuiLoadStyleJungle(); break;
  132. case 2: GuiLoadStyleLavanda(); break;
  133. case 3: GuiLoadStyleDark(); break;
  134. case 4: GuiLoadStyleBluish(); break;
  135. case 5: GuiLoadStyleCyber(); break;
  136. case 6: GuiLoadStyleTerminal(); break;
  137. default: break;
  138. }
  139. GuiSetStyle(LABEL, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  140. prevVisualStyleActive = visualStyleActive;
  141. }
  142. //----------------------------------------------------------------------------------
  143. // Draw
  144. //----------------------------------------------------------------------------------
  145. BeginDrawing();
  146. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  147. // raygui: controls drawing
  148. //----------------------------------------------------------------------------------
  149. // Check all possible events that require GuiLock
  150. if (dropDown000EditMode ||
  151. dropDown001EditMode) GuiLock();
  152. // First GUI column
  153. //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  154. GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "FORCE CHECK!", &forceSquaredChecked);
  155. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  156. //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  157. if (GuiSpinner((Rectangle){ 25, 135, 125, 30 }, NULL, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
  158. if (GuiValueBox((Rectangle){ 25, 175, 125, 30 }, NULL, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
  159. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  160. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  161. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  162. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(ICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
  163. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  164. //GuiLock();
  165. GuiSetState(STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  166. GuiSetState(STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  167. GuiSetState(STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  168. GuiSetState(STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  169. GuiSetState(STATE_NORMAL);
  170. //GuiUnlock();
  171. GuiComboBox((Rectangle){ 25, 480, 125, 30 }, "default;Jungle;Lavanda;Dark;Bluish;Cyber;Terminal", &visualStyleActive);
  172. // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
  173. GuiUnlock();
  174. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  175. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  176. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  177. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  178. // Second GUI column
  179. GuiListView((Rectangle){ 165, 25, 140, 124 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, &listViewActive);
  180. GuiListViewEx((Rectangle){ 165, 162, 140, 184 }, listViewExList, 8, &listViewExScrollIndex, &listViewExActive, &listViewExFocus);
  181. //GuiToggle((Rectangle){ 165, 400, 140, 25 }, "#1#ONE", &toggleGroupActive);
  182. GuiToggleGroup((Rectangle){ 165, 360, 140, 24 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", &toggleGroupActive);
  183. //GuiDisable();
  184. GuiToggleSlider((Rectangle){ 165, 480, 140, 30 }, "ON;OFF", &toggleSliderActive);
  185. // Third GUI column
  186. GuiPanel((Rectangle){ 320, 25, 225, 140 }, "Panel Info");
  187. GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, NULL, &colorPickerValue);
  188. //GuiDisable();
  189. GuiSlider((Rectangle){ 355, 400, 165, 20 }, "TEST", TextFormat("%2.2f", sliderValue), &sliderValue, -50, 100);
  190. GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, TextFormat("%i", (int)sliderBarValue), &sliderBarValue, 0, 100);
  191. GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, TextFormat(" %i%%", (int)(progressValue*100)), &progressValue, 0.0f, 1.0f);
  192. GuiEnable();
  193. // NOTE: View rectangle could be used to perform some scissor test
  194. Rectangle view = { 0 };
  195. GuiScrollPanel((Rectangle){ 560, 25, 102, 354 }, NULL, (Rectangle){ 560, 25, 300, 1200 }, &viewScroll, &view);
  196. Vector2 mouseCell = { 0 };
  197. GuiGrid((Rectangle) { 560, 25 + 180 + 195, 100, 120 }, NULL, 20, 2, &mouseCell);
  198. GuiStatusBar((Rectangle){ 0, (float)GetScreenHeight() - 20, (float)GetScreenWidth(), 20 }, "This is a status bar");
  199. GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, NULL, &alphaValue);
  200. if (showMessageBox)
  201. {
  202. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  203. int result = GuiMessageBox((Rectangle){ (float)GetScreenWidth()/2 - 125, (float)GetScreenHeight()/2 - 50, 250, 100 }, GuiIconText(ICON_EXIT, "Close Window"), "Do you really want to exit?", "Yes;No");
  204. if ((result == 0) || (result == 2)) showMessageBox = false;
  205. else if (result == 1) exitWindow = true;
  206. }
  207. if (showTextInputBox)
  208. {
  209. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  210. int result = GuiTextInputBox((Rectangle){ (float)GetScreenWidth()/2 - 120, (float)GetScreenHeight()/2 - 60, 240, 140 }, "Save", GuiIconText(ICON_FILE_SAVE, "Save file as..."), "Ok;Cancel", textInput, 255, NULL);
  211. if (result == 1)
  212. {
  213. // TODO: Validate textInput value and save
  214. TextCopy(textInputFileName, textInput);
  215. }
  216. if ((result == 0) || (result == 1) || (result == 2))
  217. {
  218. showTextInputBox = false;
  219. TextCopy(textInput, "\0");
  220. }
  221. }
  222. //----------------------------------------------------------------------------------
  223. EndDrawing();
  224. //----------------------------------------------------------------------------------
  225. }
  226. // De-Initialization
  227. //--------------------------------------------------------------------------------------
  228. CloseWindow(); // Close window and OpenGL context
  229. //--------------------------------------------------------------------------------------
  230. return 0;
  231. }