2
0

controls_test_suite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * - GuiTextBoxMulti()
  16. * - GuiColorPicker()
  17. * - GuiSlider()
  18. * - GuiSliderBar()
  19. * - GuiProgressBar()
  20. * - GuiColorBarAlpha()
  21. * - GuiScrollPanel()
  22. *
  23. *
  24. * DEPENDENCIES:
  25. * raylib 4.0 - Windowing/input management and drawing.
  26. * raygui 3.2 - Immediate-mode GUI controls.
  27. *
  28. * COMPILATION (Windows - MinGW):
  29. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  30. *
  31. * LICENSE: zlib/libpng
  32. *
  33. * Copyright (c) 2016-2023 Ramon Santamaria (@raysan5)
  34. *
  35. **********************************************************************************************/
  36. #include "raylib.h"
  37. #define RAYGUI_IMPLEMENTATION
  38. //#define RAYGUI_CUSTOM_ICONS // It requires providing gui_icons.h in the same directory
  39. //#include "gui_icons.h" // External icons data provided, it can be generated with rGuiIcons tool
  40. #include "../../src/raygui.h"
  41. #include <string.h> // Required for: strcpy()
  42. //------------------------------------------------------------------------------------
  43. // Program main entry point
  44. //------------------------------------------------------------------------------------
  45. int main()
  46. {
  47. // Initialization
  48. //---------------------------------------------------------------------------------------
  49. const int screenWidth = 690;
  50. const int screenHeight = 560;
  51. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  52. SetExitKey(0);
  53. // GUI controls initialization
  54. //----------------------------------------------------------------------------------
  55. int dropdownBox000Active = 0;
  56. bool dropDown000EditMode = false;
  57. int dropdownBox001Active = 0;
  58. bool dropDown001EditMode = false;
  59. int spinner001Value = 0;
  60. bool spinnerEditMode = false;
  61. int valueBox002Value = 0;
  62. bool valueBoxEditMode = false;
  63. char textBoxText[64] = "Text box";
  64. bool textBoxEditMode = false;
  65. int listViewScrollIndex = 0;
  66. int listViewActive = -1;
  67. int listViewExScrollIndex = 0;
  68. int listViewExActive = 2;
  69. int listViewExFocus = -1;
  70. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  71. char multiTextBoxText[256] = "Multi text box";
  72. bool multiTextBoxEditMode = false;
  73. Color colorPickerValue = RED;
  74. int sliderValue = 50;
  75. int sliderBarValue = 60;
  76. float progressValue = 0.4f;
  77. bool forceSquaredChecked = false;
  78. float alphaValue = 0.5f;
  79. int comboBoxActive = 1;
  80. int toggleGroupActive = 0;
  81. Vector2 viewScroll = { 0, 0 };
  82. //----------------------------------------------------------------------------------
  83. // Custom GUI font loading
  84. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  85. //GuiSetFont(font);
  86. bool exitWindow = false;
  87. bool showMessageBox = false;
  88. char textInput[256] = { 0 };
  89. bool showTextInputBox = false;
  90. char textInputFileName[256] = { 0 };
  91. SetTargetFPS(60);
  92. //--------------------------------------------------------------------------------------
  93. // Main game loop
  94. while (!exitWindow) // Detect window close button or ESC key
  95. {
  96. // Update
  97. //----------------------------------------------------------------------------------
  98. exitWindow = WindowShouldClose();
  99. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  100. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  101. if (IsFileDropped())
  102. {
  103. FilePathList droppedFiles = LoadDroppedFiles();
  104. if ((droppedFiles.count > 0) && IsFileExtension(droppedFiles.paths[0], ".rgs")) GuiLoadStyle(droppedFiles.paths[0]);
  105. UnloadDroppedFiles(droppedFiles); // Clear internal buffers
  106. }
  107. //----------------------------------------------------------------------------------
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  112. // raygui: controls drawing
  113. //----------------------------------------------------------------------------------
  114. // Check all possible events that require GuiLock
  115. if (dropDown000EditMode ||
  116. dropDown001EditMode) GuiLock();
  117. // First GUI column
  118. //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  119. forceSquaredChecked = GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "FORCE CHECK!", forceSquaredChecked);
  120. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  121. //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  122. if (GuiSpinner((Rectangle){ 25, 135, 125, 30 }, NULL, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
  123. if (GuiValueBox((Rectangle){ 25, 175, 125, 30 }, NULL, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
  124. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  125. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  126. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  127. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(ICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
  128. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  129. //GuiLock();
  130. GuiSetState(STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  131. GuiSetState(STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  132. GuiSetState(STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  133. GuiSetState(STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  134. GuiSetState(STATE_NORMAL);
  135. //GuiUnlock();
  136. comboBoxActive = GuiComboBox((Rectangle){ 25, 470, 125, 30 }, "ONE;TWO;THREE;FOUR", comboBoxActive);
  137. // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
  138. GuiUnlock();
  139. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
  140. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  141. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
  142. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  143. // Second GUI column
  144. listViewActive = GuiListView((Rectangle){ 165, 25, 140, 140 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, listViewActive);
  145. listViewExActive = GuiListViewEx((Rectangle){ 165, 180, 140, 200 }, listViewExList, 8, &listViewExFocus, &listViewExScrollIndex, listViewExActive);
  146. toggleGroupActive = GuiToggleGroup((Rectangle){ 165, 400, 140, 25 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", toggleGroupActive);
  147. // Third GUI column
  148. if (GuiTextBoxMulti((Rectangle){ 320, 25, 225, 140 }, multiTextBoxText, 256, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode;
  149. colorPickerValue = GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, NULL, colorPickerValue);
  150. sliderValue = GuiSlider((Rectangle){ 355, 400, 165, 20 }, "TEST", TextFormat("%2.2f", (float)sliderValue), sliderValue, -50, 100);
  151. sliderBarValue = GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, TextFormat("%i", (int)sliderBarValue), sliderBarValue, 0, 100);
  152. progressValue = GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, NULL, progressValue, 0, 1);
  153. // NOTE: View rectangle could be used to perform some scissor test
  154. Rectangle view = GuiScrollPanel((Rectangle){ 560, 25, 100, 160 }, NULL, (Rectangle){ 560, 25, 200, 400 }, &viewScroll);
  155. GuiPanel((Rectangle){ 560, 25 + 180, 100, 160 }, "Panel Info");
  156. GuiGrid((Rectangle) { 560, 25 + 180 + 180, 100, 120 }, NULL, 20, 2);
  157. GuiStatusBar((Rectangle){ 0, (float)GetScreenHeight() - 20, (float)GetScreenWidth(), 20 }, "This is a status bar");
  158. alphaValue = GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, NULL, alphaValue);
  159. if (showMessageBox)
  160. {
  161. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  162. 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");
  163. if ((result == 0) || (result == 2)) showMessageBox = false;
  164. else if (result == 1) exitWindow = true;
  165. }
  166. if (showTextInputBox)
  167. {
  168. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  169. 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);
  170. if (result == 1)
  171. {
  172. // TODO: Validate textInput value and save
  173. strcpy(textInputFileName, textInput);
  174. }
  175. if ((result == 0) || (result == 1) || (result == 2))
  176. {
  177. showTextInputBox = false;
  178. strcpy(textInput, "\0");
  179. }
  180. }
  181. //----------------------------------------------------------------------------------
  182. EndDrawing();
  183. //----------------------------------------------------------------------------------
  184. }
  185. // De-Initialization
  186. //--------------------------------------------------------------------------------------
  187. CloseWindow(); // Close window and OpenGL context
  188. //--------------------------------------------------------------------------------------
  189. return 0;
  190. }