app.d 11 KB

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