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 
40 import raylib;
41 
42 version = RAYGUI_IMPLEMENTATION;
43 //#define RAYGUI_CUSTOM_ICONS     // It requires providing gui_icons.h in the same directory
44 //#include "gui_icons.h"          // External icons data provided, it can be generated with rGuiIcons tool
45 import raygui;
46 
47 public import core.stdc.string;             // Required for: strcpy()
48 
49 //------------------------------------------------------------------------------------
50 // Program main entry point
51 //------------------------------------------------------------------------------------
52 int main() {
53     // Initialization
54     //---------------------------------------------------------------------------------------
55     const(int) screenWidth = 690;
56     const(int) screenHeight = 560;
57 
58     InitWindow(screenWidth, screenHeight, "raygui - controls test suite");
59     SetExitKey(0);
60 
61     // GUI controls initialization
62     //----------------------------------------------------------------------------------
63     int dropdownBox000Active = 0;
64     bool dropDown000EditMode = false;
65 
66     int dropdownBox001Active = 0;
67     bool dropDown001EditMode = false;
68 
69     int spinner001Value = 0;
70     bool spinnerEditMode = false;
71 
72     int valueBox002Value = 0;
73     bool valueBoxEditMode = false;
74 
75     char[64] textBoxText = "Text box";
76     bool textBoxEditMode = false;
77 
78     int listViewScrollIndex = 0;
79     int listViewActive = -1;
80 
81     int listViewExScrollIndex = 0;
82     int listViewExActive = 2;
83     int listViewExFocus = -1;
84     const(char)*[8] listViewExList = [ "This", "is", "a", "list view", "with", "disable", "elements", "amazing!" ];
85 
86     char[256] multiTextBoxText = "Multi text box";
87     bool multiTextBoxEditMode = false;
88     Color colorPickerValue = Colors.RED;
89 
90     int sliderValue = 50;
91     int sliderBarValue = 60;
92     float progressValue = 0.4f;
93 
94     bool forceSquaredChecked = false;
95 
96     float alphaValue = 0.5f;
97 
98     int comboBoxActive = 1;
99 
100     int toggleGroupActive = 0;
101 
102     Vector2 viewScroll = { 0, 0 };
103     //----------------------------------------------------------------------------------
104 
105     // Custom GUI font loading
106     //Font font = LoadFontEx("fonts/rainyhearts16.ttf", 12, 0, 0);
107     //GuiSetFont(font);
108 
109     bool exitWindow = false;
110     bool showMessageBox = false;
111 
112     char[256] textInput = 0;
113     bool showTextInputBox = false;
114 
115     char[256] textInputFileName = 0;
116 
117     SetTargetFPS(60);
118     //--------------------------------------------------------------------------------------
119 
120     // Main game loop
121     while (!exitWindow)    // Detect window close button or ESC key
122     {
123         // Update
124         //----------------------------------------------------------------------------------
125         exitWindow = WindowShouldClose();
126 
127         if (IsKeyPressed(KeyboardKey.KEY_ESCAPE)) showMessageBox = !showMessageBox;
128 
129         if (IsKeyDown(KeyboardKey.KEY_LEFT_CONTROL) && IsKeyPressed(KeyboardKey.KEY_S)) showTextInputBox = true;
130 
131         if (IsFileDropped())
132         {
133             FilePathList droppedFiles = LoadDroppedFiles();
134 
135             if ((droppedFiles.count > 0) && IsFileExtension(droppedFiles.paths[0], ".rgs")) GuiLoadStyle(droppedFiles.paths[0]);
136 
137             UnloadDroppedFiles(droppedFiles);    // Clear internal buffers
138         }
139         //----------------------------------------------------------------------------------
140 
141         // Draw
142         //----------------------------------------------------------------------------------
143         BeginDrawing();
144 
145             ClearBackground(GetColor(GuiGetStyle(GuiControl.DEFAULT, GuiDefaultProperty.BACKGROUND_COLOR)));
146 
147             // raygui: controls drawing
148             //----------------------------------------------------------------------------------
149             if (dropDown000EditMode || dropDown001EditMode) GuiLock();
150             else if (!dropDown000EditMode && !dropDown001EditMode) GuiUnlock();
151             //GuiDisable();
152 
153             // First GUI column
154             //GuiSetStyle(CHECKBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
155             forceSquaredChecked = GuiCheckBox(Rectangle( 25, 108, 15, 15 ), "FORCE CHECK!", forceSquaredChecked);
156 
157             GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
158             //GuiSetStyle(VALUEBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
159             if (GuiSpinner(Rectangle( 25, 135, 125, 30 ), null, &spinner001Value, 0, 100, spinnerEditMode)) spinnerEditMode = !spinnerEditMode;
160             if (GuiValueBox(Rectangle( 25, 175, 125, 30 ), null, &valueBox002Value, 0, 100, valueBoxEditMode)) valueBoxEditMode = !valueBoxEditMode;
161             GuiSetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
162             if (GuiTextBox(Rectangle( 25, 215, 125, 30 ), textBoxText.ptr, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
163 
164             GuiSetStyle(BUTTON, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
165 
166             if (GuiButton(Rectangle( 25, 255, 125, 30 ), GuiIconText(ICON_FILE_SAVE, "Save File"))) showTextInputBox = true;
167 
168             GuiGroupBox(Rectangle( 25, 310, 125, 150 ), "STATES");
169             //GuiLock();
170             GuiSetState(STATE_NORMAL); if (GuiButton(Rectangle( 30, 320, 115, 30 ), "NORMAL")) { }
171             GuiSetState(STATE_FOCUSED); if (GuiButton(Rectangle( 30, 355, 115, 30 ), "FOCUSED")) { }
172             GuiSetState(STATE_PRESSED); if (GuiButton(Rectangle( 30, 390, 115, 30 ), "#15#PRESSED")) { }
173             GuiSetState(STATE_DISABLED); if (GuiButton(Rectangle( 30, 425, 115, 30 ), "DISABLED")) { }
174             GuiSetState(STATE_NORMAL);
175             //GuiUnlock();
176 
177             comboBoxActive = GuiComboBox(Rectangle( 25, 470, 125, 30 ), "ONE;TWO;THREE;FOUR", comboBoxActive);
178 
179             // NOTE: GuiDropdownBox must draw after any other control that can be covered on unfolding
180             GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);
181             if (GuiDropdownBox(Rectangle( 25, 65, 125, 30 ), "#01#ONE;#02#TWO;#03#THREE;#04#FOUR", &dropdownBox001Active, dropDown001EditMode)) dropDown001EditMode = !dropDown001EditMode;
182 
183             GuiSetStyle(DROPDOWNBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
184             if (GuiDropdownBox(Rectangle( 25, 25, 125, 30 ), "ONE;TWO;THREE", &dropdownBox000Active, dropDown000EditMode)) dropDown000EditMode = !dropDown000EditMode;
185 
186             // Second GUI column
187             listViewActive = GuiListView(Rectangle( 165, 25, 140, 140 ), "Charmander;Bulbasaur;#18#Squirtel;Pikachu;Eevee;Pidgey", &listViewScrollIndex, listViewActive);
188             listViewExActive = GuiListViewEx(Rectangle( 165, 180, 140, 200 ), listViewExList.ptr, 8, &listViewExFocus, &listViewExScrollIndex, listViewExActive);
189 
190             toggleGroupActive = GuiToggleGroup(Rectangle( 165, 400, 140, 25 ), "#1#ONE\n#3#TWO\n#8#THREE\n#23#", toggleGroupActive);
191 
192             // Third GUI column
193             if (GuiTextBoxMulti(Rectangle( 320, 25, 225, 140 ), multiTextBoxText.ptr, 256, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode;
194             colorPickerValue = GuiColorPicker(Rectangle( 320, 185, 196, 192 ), null, colorPickerValue);
195 
196             sliderValue = cast(int)GuiSlider(Rectangle( 355, 400, 165, 20 ), "TEST", TextFormat("%2.2f", cast(float)sliderValue), sliderValue, -50, 100);
197             sliderBarValue = cast(int)GuiSliderBar(Rectangle( 320, 430, 200, 20 ), null, TextFormat("%i", cast(int)sliderBarValue), sliderBarValue, 0, 100);
198             progressValue = GuiProgressBar(Rectangle( 320, 460, 200, 20 ), null, null, progressValue, 0, 1);
199 
200             // NOTE: View rectangle could be used to perform some scissor test
201             Rectangle view = GuiScrollPanel(Rectangle( 560, 25, 100, 160 ), null, Rectangle( 560, 25, 200, 400 ), &viewScroll);
202 
203             GuiPanel(Rectangle( 560, 25 + 180, 100, 160 ), "Panel Info");
204 
205             GuiGrid(Rectangle( 560, 25 + 180 + 180, 100, 120 ), null, 20, 2);
206 
207             GuiStatusBar(Rectangle( 0, cast(float)GetScreenHeight() - 20, cast(float)GetScreenWidth(), 20 ), "This is a status bar");
208 
209             alphaValue = GuiColorBarAlpha(Rectangle( 320, 490, 200, 30 ), null, alphaValue);
210 
211             if (showMessageBox)
212             {
213                 DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(Colors.RAYWHITE, 0.8f));
214                 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");
215 
216                 if ((result == 0) || (result == 2)) showMessageBox = false;
217                 else if (result == 1) exitWindow = true;
218             }
219 
220             if (showTextInputBox)
221             {
222                 DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(Colors.RAYWHITE, 0.8f));
223                 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);
224 
225                 if (result == 1)
226                 {
227                     // TODO: Validate textInput value and save
228 
229                     strcpy(textInputFileName.ptr, textInput.ptr);
230                 }
231 
232                 if ((result == 0) || (result == 1) || (result == 2))
233                 {
234                     showTextInputBox = false;
235                     strcpy(textInput.ptr, "\0");
236                 }
237             }
238             //----------------------------------------------------------------------------------
239 
240         EndDrawing();
241         //----------------------------------------------------------------------------------
242     }
243 
244     // De-Initialization
245     //--------------------------------------------------------------------------------------
246     CloseWindow();        // Close window and OpenGL context
247     //--------------------------------------------------------------------------------------
248 
249     return 0;
250 }