1 module examples.arsd;
2 
3 import std.datetime : Clock;
4 
5 import arsd.simpledisplay;
6 import arsd.nanovega;
7 import nanogui.arsdbackend : ArsdBackend;
8 
9 class MyGui : ArsdBackend
10 {
11 	this(int w, int h, string title)
12 	{
13 		super(w, h, title);
14 	}
15 
16 	override void onVisibleForTheFirstTime()
17 	{
18 		import nanogui.screen : Screen;
19 		import nanogui.widget, nanogui.theme, nanogui.checkbox, nanogui.label, 
20 			nanogui.common, nanogui.window, nanogui.layout, nanogui.button,
21 			nanogui.popupbutton, nanogui.entypo, nanogui.popup, nanogui.vscrollpanel,
22 			nanogui.combobox, nanogui.textbox;
23 		
24 		{
25 			auto window = new Window(screen, "Button demo");
26 			window.position(Vector2i(15, 15));
27 			window.size = Vector2i(screen.size.x - 30, screen.size.y - 30);
28 			window.layout(new GroupLayout());
29 
30 			new Label(window, "Push buttons", "sans-bold");
31 
32 			auto checkbox = new CheckBox(window, "Checkbox #1", (bool value){ simple_window.redrawOpenGlSceneNow(); });
33 			checkbox.position = Vector2i(100, 190);
34 			checkbox.size = checkbox.preferredSize(ctx);
35 			checkbox.checked = true;
36 
37 			auto label = new Label(window, "Label");
38 			label.position = Vector2i(100, 300);
39 			label.size = label.preferredSize(ctx);
40 
41 			Popup popup;
42 
43 			auto btn = new Button(window, "Button");
44 			btn.callback = () { 
45 				popup.children[0].visible = !popup.children[0].visible; 
46 				label.caption = popup.children[0].visible ? 
47 					"Popup label is visible" : "Popup label isn't visible";
48 			};
49 
50 			auto popupBtn = new PopupButton(window, "PopupButton", Entypo.ICON_EXPORT);
51 			popup = popupBtn.popup;
52 			popup.layout(new GroupLayout());
53 			new Label(popup, "Arbitrary widgets can be placed here");
54 			new CheckBox(popup, "A check box", null);
55 
56 			window.tooltip = "Button demo tooltip";
57 		}
58 
59 		{
60 			auto window = new Window(screen, "Button group example");
61 			window.position(Vector2i(220, 15));
62 			window.layout(new GroupLayout());
63 
64 			auto buttonGroup = ButtonGroup();
65 
66 			auto btn = new Button(window, "RadioButton1");
67 			btn.flags = Button.Flags.RadioButton;
68 			btn.buttonGroup = buttonGroup;
69 			btn.tooltip = "Radio button ONE";
70 			buttonGroup ~= btn;
71 
72 			btn = new Button(window, "RadioButton2");
73 			btn.flags = Button.Flags.RadioButton;
74 			btn.buttonGroup = buttonGroup;
75 			btn.tooltip = "Radio button TWO";
76 			buttonGroup ~= btn;
77 
78 			btn = new Button(window, "RadioButton3");
79 			btn.flags = Button.Flags.RadioButton;
80 			btn.buttonGroup = buttonGroup;
81 			btn.tooltip = "Radio button THREE";
82 			buttonGroup ~= btn;
83 
84 			window.tooltip = "Radio button group tooltip";
85 		}
86 
87 		{
88 			auto window = new Window(screen, "Button with image window");
89 			window.position(Vector2i(400, 15));
90 			window.layout(new GroupLayout());
91 
92 			auto image = ctx.createImage("resources/icons/start.jpeg", [NVGImageFlags.ClampToBorderX, NVGImageFlags.ClampToBorderY]);
93 			auto btn = new Button(window, "Start", image);
94 			// some optional height, not font size, not icon height
95 			btn.fixedHeight = 130;
96 
97 			// yet another Button with the same image but default size
98 			new Button(window, "Start", image);
99 
100 			window.tooltip = "Window with button that has image as an icon";
101 		}
102 
103 		{
104 			auto window = new Window(screen, "Combobox window");
105 			window.position(Vector2i(600, 15));
106 			window.layout(new GroupLayout());
107 
108 			new Label(window, "Message dialog", "sans-bold");
109 			import std.algorithm : map;
110 			import std.range : iota;
111 			import std.array : array;
112 			import std.conv : text;
113 			auto items = 15.iota.map!(a=>text("items", a)).array;
114 			auto cb = new ComboBox(window, items);
115 			cb.cursor = Cursor.Hand;
116 			cb.tooltip = "This widget has custom cursor value - Cursor.Hand";
117 
118 			window.tooltip = "Window with ComboBox tooltip";
119 		}
120 
121 		{
122 			int width      = 400;
123 			int half_width = width / 2;
124 			int height     = 200;
125 
126 			auto window = new Window(screen, "All Icons");
127 			window.position(Vector2i(0, 400));
128 			window.fixedSize(Vector2i(width, height));
129 
130 			// attach a vertical scroll panel
131 			auto vscroll = new VScrollPanel(window);
132 			vscroll.fixedSize(Vector2i(width, height));
133 
134 			// vscroll should only have *ONE* child. this is what `wrapper` is for
135 			auto wrapper = new Widget(vscroll);
136 			wrapper.fixedSize(Vector2i(width, height));
137 			wrapper.layout(new GridLayout());// defaults: 2 columns
138 
139 			foreach(i; 0..100)
140 			{
141 				import std.conv : text;
142 				auto item = new Button(wrapper, "item" ~ i.text, Entypo.ICON_AIRCRAFT_TAKE_OFF);
143 				item.iconPosition(Button.IconPosition.Left);
144 				item.fixedWidth(half_width);
145 			}
146 		}
147 
148 		{
149 			auto asian_theme = new Theme(ctx);
150 
151 			{
152 				// sorta hack because loading font in ctx results in
153 				// conflicting font id
154 				auto nvg2 = nvgCreateContext(NVGContextFlag.Debug);
155 				scope(exit) nvg2.kill;
156 				nvg2.createFont("chihaya", "./resources/fonts/n_chihaya_font.ttf");
157 				ctx.addFontsFrom(nvg2);
158 				asian_theme.mFontNormal = ctx.findFont("chihaya");
159 			}
160 
161 			auto window = new Window(screen, "Textbox window");
162 			window.position = Vector2i(750, 15);
163 			window.fixedSize = Vector2i(200, 350);
164 			window.layout(new GroupLayout());
165 			window.tooltip = "Window with TextBoxes";
166 
167 			auto tb = new TextBox(window, "Россия");
168 			tb.editable = true;
169 
170 			tb = new TextBox(window, "England");
171 			tb.editable = true;
172 
173 			tb = new TextBox(window, "日本");
174 			tb.theme = asian_theme;
175 			tb.editable = true;
176 
177 			tb = new TextBox(window, "中国");
178 			tb.theme = asian_theme;
179 			tb.editable = true;
180 		}
181 		
182 		// now we should do layout manually yet
183 		screen.performLayout(ctx);
184 	}
185 }
186 
187 void main () {
188 	
189 	auto gui = new MyGui(1000, 800, "Nanogui using arsd.simpledisplay");
190 	gui.run();
191 }