controls_test_suite.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_DEBUG_RECS_BOUNDS
  37. //#define RAYGUI_DEBUG_TEXT_BOUNDS
  38. #define RAYGUI_IMPLEMENTATION
  39. //#define RAYGUI_CUSTOM_ICONS // It requires providing gui_icons.h in the same directory
  40. //#include "gui_icons.h" // External icons data provided, it can be generated with rGuiIcons tool
  41. #include "../../src/raygui.h"
  42. // raygui embedded styles
  43. #include "../styles/style_cyber.h" // raygui style: cyber
  44. #include "../styles/style_jungle.h" // raygui style: jungle
  45. #include "../styles/style_lavanda.h" // raygui style: lavanda
  46. #include "../styles/style_dark.h" // raygui style: dark
  47. #include "../styles/style_bluish.h" // raygui style: bluish
  48. #include "../styles/style_terminal.h" // raygui style: terminal
  49. //------------------------------------------------------------------------------------
  50. // Program main entry point
  51. //------------------------------------------------------------------------------------
  52. int main()
  53. {
  54. // Initialization
  55. //---------------------------------------------------------------------------------------
  56. const int screenWidth = 960;
  57. const int screenHeight = 560;
  58. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  59. SetExitKey(0);
  60. // GUI controls initialization
  61. //----------------------------------------------------------------------------------
  62. int dropdownBox000Active = 0;
  63. bool dropDown000EditMode = false;
  64. int dropdownBox001Active = 0;
  65. bool dropDown001EditMode = false;
  66. int spinner001Value = 0;
  67. bool spinnerEditMode = false;
  68. int valueBox002Value = 0;
  69. bool valueBoxEditMode = false;
  70. char textBoxText[64] = "Text box";
  71. bool textBoxEditMode = false;
  72. char textBoxMultiText[1024] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  73. bool textBoxMultiEditMode = false;
  74. int listViewScrollIndex = 0;
  75. int listViewActive = -1;
  76. int listViewExScrollIndex = 0;
  77. int listViewExActive = 2;
  78. int listViewExFocus = -1;
  79. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  80. char multiTextBoxText[256] = "Multi text box";
  81. bool multiTextBoxEditMode = false;
  82. Color colorPickerValue = RED;
  83. float sliderValue = 50.0f;
  84. float sliderBarValue = 60;
  85. float progressValue = 0.1f;
  86. bool forceSquaredChecked = false;
  87. float alphaValue = 0.5f;
  88. //int comboBoxActive = 1;
  89. int visualStyleActive = 0;
  90. int prevVisualStyleActive = 0;
  91. int toggleGroupActive = 0;
  92. int toggleSliderActive = 0;
  93. Vector2 viewScroll = { 0, 0 };
  94. //----------------------------------------------------------------------------------
  95. // Custom GUI font loading
  96. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  97. //GuiSetFont(font);
  98. bool exitWindow = false;
  99. bool showMessageBox = false;
  100. char textInput[256] = { 0 };
  101. bool showTextInputBox = false;
  102. char textInputFileName[256] = { 0 };
  103. float alpha = 1.0f;
  104. SetTargetFPS(60);
  105. //--------------------------------------------------------------------------------------
  106. // Main game loop
  107. while (!exitWindow) // Detect window close button or ESC key
  108. {
  109. // Update
  110. //----------------------------------------------------------------------------------
  111. exitWindow = WindowShouldClose();
  112. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  113. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  114. if (IsFileDropped())
  115. {
  116. FilePathList droppedFiles = LoadDroppedFiles();
  117. if ((droppedFiles.count > 0) && IsFileExtension(droppedFiles.paths[0], ".rgs")) GuiLoadStyle(droppedFiles.paths[0]);
  118. UnloadDroppedFiles(droppedFiles); // Clear internal buffers
  119. }
  120. //alpha -= 0.002f;
  121. if (alpha < 0.0f) alpha = 0.0f;
  122. if (IsKeyPressed(KEY_SPACE)) alpha = 1.0f;
  123. GuiSetAlpha(alpha);
  124. //progressValue += 0.002f;
  125. if (IsKeyPressed(KEY_LEFT)) progressValue -= 0.1f;
  126. else if (IsKeyPressed(KEY_RIGHT)) progressValue += 0.1f;
  127. if (progressValue > 1.0f) progressValue = 1.0f;
  128. else if (progressValue < 0.0f) progressValue = 0.0f;
  129. if (visualStyleActive != prevVisualStyleActive)
  130. {
  131. GuiLoadStyleDefault();
  132. switch (visualStyleActive)
  133. {
  134. case 0: break; // Default style
  135. case 1: GuiLoadStyleJungle(); break;
  136. case 2: GuiLoadStyleLavanda(); break;
  137. case 3: GuiLoadStyleDark(); break;
  138. case 4: GuiLoadStyleBluish(); break;
  139. case 5: GuiLoadStyleCyber(); break;
  140. case 6: GuiLoadStyleTerminal(); break;
  141. default: break;
  142. }
  143. GuiSetStyle(LABEL, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  144. prevVisualStyleActive = visualStyleActive;
  145. }
  146. //----------------------------------------------------------------------------------
  147. // Draw
  148. //----------------------------------------------------------------------------------
  149. BeginDrawing();
  150. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  151. // raygui: controls drawing
  152. //----------------------------------------------------------------------------------
  153. // Check all possible events that require GuiLock
  154. if (dropDown000EditMode ||
  155. dropDown001EditMode) GuiLock();
  156. // First GUI column
  157. //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  158. GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "FORCE CHECK!", &forceSquaredChecked);
  159. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  160. //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  161. if (GuiSpinner((Rectangle){ 25, 135, 125, 30 }, NULL, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
  162. if (GuiValueBox((Rectangle){ 25, 175, 125, 30 }, NULL, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
  163. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  164. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  165. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  166. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(ICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
  167. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  168. //GuiLock();
  169. GuiSetState(STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  170. GuiSetState(STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  171. GuiSetState(STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  172. GuiSetState(STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  173. GuiSetState(STATE_NORMAL);
  174. //GuiUnlock();
  175. GuiComboBox((Rectangle){ 25, 480, 125, 30 }, "default;Jungle;Lavanda;Dark;Bluish;Cyber;Terminal", &visualStyleActive);
  176. // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
  177. GuiUnlock();
  178. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  179. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  180. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  181. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  182. // Second GUI column
  183. GuiListView((Rectangle){ 165, 25, 140, 124 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, &listViewActive);
  184. GuiListViewEx((Rectangle){ 165, 162, 140, 184 }, listViewExList, 8, &listViewExScrollIndex, &listViewExActive, &listViewExFocus);
  185. //GuiToggle((Rectangle){ 165, 400, 140, 25 }, "#1#ONE", &toggleGroupActive);
  186. GuiToggleGroup((Rectangle){ 165, 360, 140, 24 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", &toggleGroupActive);
  187. //GuiDisable();
  188. GuiToggleSlider((Rectangle){ 165, 480, 140, 30 }, "ON;OFF", &toggleSliderActive);
  189. // Third GUI column
  190. GuiPanel((Rectangle){ 320, 25, 225, 140 }, "Panel Info");
  191. GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, NULL, &colorPickerValue);
  192. //GuiDisable();
  193. GuiSlider((Rectangle){ 355, 400, 165, 20 }, "TEST", TextFormat("%2.2f", sliderValue), &sliderValue, -50, 100);
  194. GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, TextFormat("%i", (int)sliderBarValue), &sliderBarValue, 0, 100);
  195. GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, TextFormat("%i%%", (int)(progressValue*100)), &progressValue, 0.0f, 1.0f);
  196. GuiEnable();
  197. // NOTE: View rectangle could be used to perform some scissor test
  198. Rectangle view = { 0 };
  199. GuiScrollPanel((Rectangle){ 560, 25, 102, 354 }, NULL, (Rectangle){ 560, 25, 300, 1200 }, &viewScroll, &view);
  200. Vector2 mouseCell = { 0 };
  201. GuiGrid((Rectangle) { 560, 25 + 180 + 195, 100, 120 }, NULL, 20, 3, &mouseCell);
  202. //GuiStatusBar((Rectangle){ 0, (float)GetScreenHeight() - 20, (float)GetScreenWidth(), 20 }, "This is a status bar");
  203. GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, NULL, &alphaValue);
  204. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL, TEXT_ALIGN_TOP); // WARNING: Word-wrap does not work as expected in case of no-top alignment
  205. GuiSetStyle(DEFAULT, TEXT_WRAP_MODE, TEXT_WRAP_WORD); // WARNING: If wrap mode enabled, text editing is not supported
  206. if (GuiTextBox((Rectangle){ 678, 25, 258, 492 }, textBoxMultiText, 1024, textBoxMultiEditMode)) textBoxMultiEditMode = !textBoxMultiEditMode;
  207. GuiSetStyle(DEFAULT, TEXT_WRAP_MODE, TEXT_WRAP_NONE);
  208. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL, TEXT_ALIGN_MIDDLE);
  209. if (showMessageBox)
  210. {
  211. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  212. 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");
  213. if ((result == 0) || (result == 2)) showMessageBox = false;
  214. else if (result == 1) exitWindow = true;
  215. }
  216. if (showTextInputBox)
  217. {
  218. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  219. int result = GuiTextInputBox((Rectangle){ (float)GetScreenWidth()/2 - 120, (float)GetScreenHeight()/2 - 60, 240, 140 }, GuiIconText(ICON_FILE_SAVE, "Save file as..."), "Introduce output file name:", "Ok;Cancel", textInput, 255, NULL);
  220. if (result == 1)
  221. {
  222. // TODO: Validate textInput value and save
  223. TextCopy(textInputFileName, textInput);
  224. }
  225. if ((result == 0) || (result == 1) || (result == 2))
  226. {
  227. showTextInputBox = false;
  228. TextCopy(textInput, "\0");
  229. }
  230. }
  231. //----------------------------------------------------------------------------------
  232. EndDrawing();
  233. //----------------------------------------------------------------------------------
  234. }
  235. // De-Initialization
  236. //--------------------------------------------------------------------------------------
  237. CloseWindow(); // Close window and OpenGL context
  238. //--------------------------------------------------------------------------------------
  239. return 0;
  240. }