controls_test_suite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.0 - 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-2021 Ramon Santamaria (@raysan5)
  34. *
  35. **********************************************************************************************/
  36. #include "raylib.h"
  37. #define RAYGUI_IMPLEMENTATION
  38. #define RAYGUI_SUPPORT_RICONS
  39. #include "../../src/raygui.h"
  40. #undef RAYGUI_IMPLEMENTATION // Avoid including raygui implementation again
  41. //------------------------------------------------------------------------------------
  42. // Program main entry point
  43. //------------------------------------------------------------------------------------
  44. int main()
  45. {
  46. // Initialization
  47. //---------------------------------------------------------------------------------------
  48. int screenWidth = 690;
  49. int screenHeight = 560;
  50. InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
  51. SetExitKey(0);
  52. // GUI controls initialization
  53. //----------------------------------------------------------------------------------
  54. int dropdownBox000Active = 0;
  55. bool dropDown000EditMode = false;
  56. int dropdownBox001Active = 0;
  57. bool dropDown001EditMode = false;
  58. int spinner001Value = 0;
  59. bool spinnerEditMode = false;
  60. int valueBox002Value = 0;
  61. bool valueBoxEditMode = false;
  62. char textBoxText[64] = "Text box";
  63. bool textBoxEditMode = false;
  64. int listViewScrollIndex = 0;
  65. int listViewActive = -1;
  66. int listViewExScrollIndex = 0;
  67. int listViewExActive = 2;
  68. int listViewExFocus = -1;
  69. const char *listViewExList[8] = { "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" };
  70. char multiTextBoxText[256] = "Multi text box";
  71. bool multiTextBoxEditMode = false;
  72. Color colorPickerValue = RED;
  73. int sliderValue = 50;
  74. int sliderBarValue = 60;
  75. float progressValue = 0.4f;
  76. bool forceSquaredChecked = false;
  77. float alphaValue = 0.5f;
  78. int comboBoxActive = 1;
  79. int toggleGroupActive = 0;
  80. Vector2 viewScroll = { 0, 0 };
  81. //----------------------------------------------------------------------------------
  82. // Custom GUI font loading
  83. //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
  84. //GuiSetFont(font);
  85. bool exitWindow = false;
  86. bool showMessageBox = false;
  87. char textInput[256] = { 0 };
  88. bool showTextInputBox = false;
  89. char textInputFileName[256] = { 0 };
  90. SetTargetFPS(60);
  91. //--------------------------------------------------------------------------------------
  92. // Main game loop
  93. while (!exitWindow) // Detect window close button or ESC key
  94. {
  95. // Update
  96. //----------------------------------------------------------------------------------
  97. exitWindow = WindowShouldClose();
  98. if (IsKeyPressed(KEY_ESCAPE)) showMessageBox = !showMessageBox;
  99. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_S)) showTextInputBox = true;
  100. if (IsFileDropped())
  101. {
  102. int dropFileCount = 0;
  103. char **droppedFiles = GetDroppedFiles(&dropFileCount);
  104. if ((dropFileCount > 0) && IsFileExtension(droppedFiles[0], ".rgs")) GuiLoadStyle(droppedFiles[0]);
  105. ClearDroppedFiles(); // Clear internal buffers
  106. }
  107. //----------------------------------------------------------------------------------
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  112. // raygui: controls drawing
  113. //----------------------------------------------------------------------------------
  114. if (dropDown000EditMode || dropDown001EditMode) GuiLock();
  115. else if (!dropDown000EditMode && !dropDown001EditMode) GuiUnlock();
  116. //GuiDisable();
  117. // First GUI column
  118. //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  119. forceSquaredChecked = GuiCheckBox((Rectangle){ 25, 108, 15, 15 }, "FORCE CHECK!", forceSquaredChecked);
  120. GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  121. //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, GUI_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, GUI_TEXT_ALIGN_LEFT);
  125. if (GuiTextBox((Rectangle){ 25, 215, 125, 30 }, textBoxText, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  126. GuiSetStyle(BUTTON, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  127. if (GuiButton((Rectangle){ 25, 255, 125, 30 }, GuiIconText(RICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
  128. GuiGroupBox((Rectangle){ 25, 310, 125, 150 }, "STATES");
  129. //GuiLock();
  130. GuiSetState(GUI_STATE_NORMAL); if (GuiButton((Rectangle){ 30, 320, 115, 30 }, "NORMAL")) { }
  131. GuiSetState(GUI_STATE_FOCUSED); if (GuiButton((Rectangle){ 30, 355, 115, 30 }, "FOCUSED")) { }
  132. GuiSetState(GUI_STATE_PRESSED); if (GuiButton((Rectangle){ 30, 390, 115, 30 }, "#15#PRESSED")) { }
  133. GuiSetState(GUI_STATE_DISABLED); if (GuiButton((Rectangle){ 30, 425, 115, 30 }, "DISABLED")) { }
  134. GuiSetState(GUI_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. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_LEFT);
  139. if (GuiDropdownBox((Rectangle){ 25, 65, 125, 30 }, "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
  140. GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, GUI_TEXT_ALIGN_CENTER);
  141. if (GuiDropdownBox((Rectangle){ 25, 25, 125, 30 }, "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
  142. // Second GUI column
  143. listViewActive = GuiListView((Rectangle){ 165, 25, 140, 140 }, "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, listViewActive);
  144. listViewExActive = GuiListViewEx((Rectangle){ 165, 180, 140, 200 }, listViewExList, 8, &listViewExFocus, &listViewExScrollIndex, listViewExActive);
  145. toggleGroupActive = GuiToggleGroup((Rectangle){ 165, 400, 140, 25 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", toggleGroupActive);
  146. // Third GUI column
  147. if (GuiTextBoxMulti((Rectangle){ 320, 25, 225, 140 }, multiTextBoxText, 256, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode;
  148. colorPickerValue = GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, colorPickerValue);
  149. sliderValue = GuiSlider((Rectangle){ 355, 400, 165, 20 }, "TEST", TextFormat("%2.2f", (float)sliderValue), sliderValue, -50, 100);
  150. sliderBarValue = GuiSliderBar((Rectangle){ 320, 430, 200, 20 }, NULL, TextFormat("%i", (int)sliderBarValue), sliderBarValue, 0, 100);
  151. progressValue = GuiProgressBar((Rectangle){ 320, 460, 200, 20 }, NULL, NULL, progressValue, 0, 1);
  152. // NOTE: View rectangle could be used to perform some scissor test
  153. Rectangle view = GuiScrollPanel((Rectangle){ 560, 25, 100, 160 }, (Rectangle){ 560, 25, 200, 400 }, &viewScroll);
  154. GuiStatusBar((Rectangle){ 0, GetScreenHeight() - 20, GetScreenWidth(), 20 }, "This is a status bar");
  155. alphaValue = GuiColorBarAlpha((Rectangle){ 320, 490, 200, 30 }, alphaValue);
  156. if (showMessageBox)
  157. {
  158. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  159. int result = GuiMessageBox((Rectangle){ GetScreenWidth()/2 - 125, GetScreenHeight()/2 - 50, 250, 100 }, GuiIconText(RICON_EXIT, "Close Window"), "Do you really want to exit?", "Yes;No");
  160. if ((result == 0) || (result == 2)) showMessageBox = false;
  161. else if (result == 1) exitWindow = true;
  162. }
  163. if (showTextInputBox)
  164. {
  165. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  166. int result = GuiTextInputBox((Rectangle){ GetScreenWidth()/2 - 120, GetScreenHeight()/2 - 60, 240, 140 }, GuiIconText(RICON_FILE_SAVE, "Save file as..."), "Introduce a save file name", "Ok;Cancel", textInput);
  167. if (result == 1)
  168. {
  169. // TODO: Validate textInput value and save
  170. strcpy(textInputFileName, textInput);
  171. }
  172. if ((result == 0) || (result == 1) || (result == 2))
  173. {
  174. showTextInputBox = false;
  175. strcpy(textInput, "\0");
  176. }
  177. }
  178. //----------------------------------------------------------------------------------
  179. EndDrawing();
  180. //----------------------------------------------------------------------------------
  181. }
  182. // De-Initialization
  183. //--------------------------------------------------------------------------------------
  184. CloseWindow(); // Close window and OpenGL context
  185. //--------------------------------------------------------------------------------------
  186. return 0;
  187. }