controls_test_suite.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-2024 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. #include "../styles/style_candy.h"
  50. #include "../styles/style_cherry.h"
  51. #include "../styles/style_ashes.h"
  52. #include "../styles/style_enefete.h"
  53. #include "../styles/style_sunny.h"
  54. #include "../styles/style_amber.h"
  55. //------------------------------------------------------------------------------------
  56. // Program main entry point
  57. //------------------------------------------------------------------------------------
  58. int main()
  59. {
  60. // Initialization
  61. //---------------------------------------------------------------------------------------
  62. const int screenWidth = 960;
  63. const int screenHeight = 560;
  64. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  65. SetExitKey(0);
  66. // GUI controls initialization
  67. //----------------------------------------------------------------------------------
  68. int dropdownBox000Active = 0;
  69. bool dropDown000EditMode = false;
  70. int dropdownBox001Active = 0;
  71. bool dropDown001EditMode = false;
  72. int spinner001Value = 0;
  73. bool spinnerEditMode = false;
  74. int valueBox002Value = 0;
  75. bool valueBoxEditMode = false;
  76. char textBoxText[64] = "Text box";
  77. bool textBoxEditMode = false;
  78. 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.\n\nThisisastringlongerthanexpectedwithoutspacestotestcharbreaksforthosecases,checkingifworkingasexpected.\n\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  79. bool textBoxMultiEditMode = false;
  80. int listViewScrollIndex = 0;
  81. int listViewActive = -1;
  82. int listViewExScrollIndex = 0;
  83. int listViewExActive = 2;
  84. int listViewExFocus = -1;
  85. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  86. Color colorPickerValue = RED;
  87. float sliderValue = 50.0f;
  88. float sliderBarValue = 60;
  89. float progressValue = 0.1f;
  90. bool forceSquaredChecked = false;
  91. float alphaValue = 0.5f;
  92. //int comboBoxActive = 1;
  93. int visualStyleActive = 0;
  94. int prevVisualStyleActive = 0;
  95. int toggleGroupActive = 0;
  96. int toggleSliderActive = 0;
  97. Vector2 viewScroll = { 0, 0 };
  98. //----------------------------------------------------------------------------------
  99. // Custom GUI font loading
  100. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  101. //GuiSetFont(font);
  102. bool exitWindow = false;
  103. bool showMessageBox = false;
  104. char textInput[256] = { 0 };
  105. char textInputFileName[256] = { 0 };
  106. bool showTextInputBox = false;
  107. float alpha = 1.0f;
  108. // DEBUG: Testing how those two properties affect all controls!
  109. //GuiSetStyle(DEFAULT, TEXT_PADDING, 0);
  110. //GuiSetStyle(DEFAULT, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  111. SetTargetFPS(60);
  112. //--------------------------------------------------------------------------------------
  113. // Main game loop
  114. while (!exitWindow) // Detect window close button or ESC key
  115. {
  116. // Update
  117. //----------------------------------------------------------------------------------
  118. exitWindow = WindowShouldClose();
  119. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  120. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  121. if (IsFileDropped())
  122. {
  123. FilePathList droppedFiles = LoadDroppedFiles();
  124. if ((droppedFiles.count > 0) && IsFileExtension(droppedFiles.paths[0], ".rgs")) GuiLoadStyle(droppedFiles.paths[0]);
  125. UnloadDroppedFiles(droppedFiles); // Clear internal buffers
  126. }
  127. //alpha -= 0.002f;
  128. if (alpha < 0.0f) alpha = 0.0f;
  129. if (IsKeyPressed(KEY_SPACE)) alpha = 1.0f;
  130. GuiSetAlpha(alpha);
  131. //progressValue += 0.002f;
  132. if (IsKeyPressed(KEY_LEFT)) progressValue -= 0.1f;
  133. else if (IsKeyPressed(KEY_RIGHT)) progressValue += 0.1f;
  134. if (progressValue > 1.0f) progressValue = 1.0f;
  135. else if (progressValue < 0.0f) progressValue = 0.0f;
  136. if (visualStyleActive != prevVisualStyleActive)
  137. {
  138. GuiLoadStyleDefault();
  139. switch (visualStyleActive)
  140. {
  141. case 0: break; // Default style
  142. case 1: GuiLoadStyleJungle(); break;
  143. case 2: GuiLoadStyleLavanda(); break;
  144. case 3: GuiLoadStyleDark(); break;
  145. case 4: GuiLoadStyleBluish(); break;
  146. case 5: GuiLoadStyleCyber(); break;
  147. case 6: GuiLoadStyleTerminal(); break;
  148. case 7: GuiLoadStyleCandy(); break;
  149. case 8: GuiLoadStyleCherry(); break;
  150. case 9: GuiLoadStyleAshes(); break;
  151. case 10: GuiLoadStyleEnefete(); break;
  152. case 11: GuiLoadStyleSunny(); break;
  153. case 12: GuiLoadStyleAmber(); break;
  154. default: break;
  155. }
  156. GuiSetStyle(LABEL, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  157. prevVisualStyleActive = visualStyleActive;
  158. }
  159. //----------------------------------------------------------------------------------
  160. // Draw
  161. //----------------------------------------------------------------------------------
  162. BeginDrawing();
  163. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  164. // raygui: controls drawing
  165. //----------------------------------------------------------------------------------
  166. // Check all possible events that require GuiLock
  167. if (dropDown000EditMode || dropDown001EditMode) GuiLock();
  168. // First GUI column
  169. //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  170. GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "FORCE CHECK!", &forceSquaredChecked);
  171. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  172. //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  173. if (GuiSpinner((Rectangle){ 25, 135, 125, 30 }, NULL, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
  174. if (GuiValueBox((Rectangle){ 25, 175, 125, 30 }, NULL, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
  175. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  176. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  177. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  178. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(ICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
  179. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  180. //GuiLock();
  181. GuiSetState(STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  182. GuiSetState(STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  183. GuiSetState(STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  184. GuiSetState(STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  185. GuiSetState(STATE_NORMAL);
  186. //GuiUnlock();
  187. GuiComboBox((Rectangle){ 25, 480, 125, 30 }, "default;Jungle;Lavanda;Dark;Bluish;Cyber;Terminal;Candy;Cherry;Ashes;Enefete;Sunny;Amber", &visualStyleActive);
  188. // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
  189. GuiUnlock();
  190. GuiSetStyle(DROPDOWNBOX, TEXT_PADDING, 4);
  191. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  192. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  193. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  194. GuiSetStyle(DROPDOWNBOX, TEXT_PADDING, 0);
  195. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  196. // Second GUI column
  197. //GuiSetStyle(LISTVIEW, LIST_ITEMS_BORDER_NORMAL, 1);
  198. GuiListView((Rectangle){ 165, 25, 140, 124 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, &listViewActive);
  199. GuiListViewEx((Rectangle){ 165, 162, 140, 184 }, listViewExList, 8, &listViewExScrollIndex, &listViewExActive, &listViewExFocus);
  200. GuiSetStyle(LISTVIEW, LIST_ITEMS_BORDER_NORMAL, 0);
  201. //GuiToggle((Rectangle){ 165, 400, 140, 25 }, "#1#ONE", &toggleGroupActive);
  202. GuiToggleGroup((Rectangle){ 165, 360, 140, 24 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", &toggleGroupActive);
  203. //GuiDisable();
  204. GuiSetStyle(SLIDER, SLIDER_PADDING, 2);
  205. GuiToggleSlider((Rectangle){ 165, 480, 140, 30 }, "ON;OFF", &toggleSliderActive);
  206. GuiSetStyle(SLIDER, SLIDER_PADDING, 0);
  207. // Third GUI column
  208. GuiPanel((Rectangle){ 320, 25, 225, 140 }, "Panel Info");
  209. GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, NULL, &colorPickerValue);
  210. //GuiDisable();
  211. GuiSlider((Rectangle){ 355, 400, 165, 20 }, "TEST", TextFormat("%2.2f", sliderValue), &sliderValue, -50, 100);
  212. GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, TextFormat("%i", (int)sliderBarValue), &sliderBarValue, 0, 100);
  213. GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, TextFormat("%i%%", (int)(progressValue*100)), &progressValue, 0.0f, 1.0f);
  214. GuiEnable();
  215. // NOTE: View rectangle could be used to perform some scissor test
  216. Rectangle view = { 0 };
  217. GuiScrollPanel((Rectangle){ 560, 25, 102, 354 }, NULL, (Rectangle){ 560, 25, 300, 1200 }, &viewScroll, &view);
  218. Vector2 mouseCell = { 0 };
  219. GuiGrid((Rectangle) { 560, 25 + 180 + 195, 100, 120 }, NULL, 20, 3, &mouseCell);
  220. GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, NULL, &alphaValue);
  221. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL, TEXT_ALIGN_TOP); // WARNING: Word-wrap does not work as expected in case of no-top alignment
  222. GuiSetStyle(DEFAULT, TEXT_WRAP_MODE, TEXT_WRAP_WORD); // WARNING: If wrap mode enabled, text editing is not supported
  223. if (GuiTextBox((Rectangle){ 678, 25, 258, 492 }, textBoxMultiText, 1024, textBoxMultiEditMode)) textBoxMultiEditMode = !textBoxMultiEditMode;
  224. GuiSetStyle(DEFAULT, TEXT_WRAP_MODE, TEXT_WRAP_NONE);
  225. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT_VERTICAL, TEXT_ALIGN_MIDDLE);
  226. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  227. GuiStatusBar((Rectangle){ 0, (float)GetScreenHeight() - 20, (float)GetScreenWidth(), 20 }, "This is a status bar");
  228. GuiSetStyle(DEFAULT, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  229. //GuiSetStyle(STATUSBAR, TEXT_INDENTATION, 20);
  230. if (showMessageBox)
  231. {
  232. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  233. 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");
  234. if ((result == 0) || (result == 2)) showMessageBox = false;
  235. else if (result == 1) exitWindow = true;
  236. }
  237. if (showTextInputBox)
  238. {
  239. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  240. 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);
  241. if (result == 1)
  242. {
  243. // TODO: Validate textInput value and save
  244. TextCopy(textInputFileName, textInput);
  245. }
  246. if ((result == 0) || (result == 1) || (result == 2))
  247. {
  248. showTextInputBox = false;
  249. TextCopy(textInput, "\0");
  250. }
  251. }
  252. //----------------------------------------------------------------------------------
  253. EndDrawing();
  254. //----------------------------------------------------------------------------------
  255. }
  256. // De-Initialization
  257. //--------------------------------------------------------------------------------------
  258. CloseWindow(); // Close window and OpenGL context
  259. //--------------------------------------------------------------------------------------
  260. return 0;
  261. }