1 module examples.sdl;
2 
3 import std.datetime : Clock;
4 import std.traits : isDynamicArray;
5 import arsd.nanovega;
6 import nanogui.sdlbackend : SdlBackend;
7 import nanogui.widget : Widget;
8 import nanogui.glcanvas : GLCanvas;
9 
10 struct Model(T) if (is(T == struct))
11 {
12 	const(T)* data;
13 
14 	this(ref T t)
15 	{
16 		data = &t;
17 	}
18 
19 	auto opSlice()
20 	{
21 		return Range!T(data);
22 	}
23 
24 	struct Range(R)
25 	{
26 		@disable
27 		this();
28 
29 		private
30 		{
31 			const(R)* source;
32 			size_t idx;
33 
34 			auto helper(size_t i)
35 			{
36 				import std.conv : text;
37 				switch(i)
38 				{
39 					static foreach(j; 0..R.tupleof.length)
40 					{
41 						case j:
42 							return text((*source).tupleof[j]);
43 					}
44 					default:
45 					{
46 						assert(0);
47 					}
48 				}
49 			}
50 		}
51 
52 		this(const(R)* source)
53 		{
54 			this.source = source;
55 		}
56 
57 		auto front()
58 		{
59 			return helper(idx);
60 		}
61 
62 		auto empty() const
63 		{
64 			return idx == R.tupleof.length;
65 		}
66 
67 		void popFront()
68 		{
69 			assert(!empty);
70 			idx++;
71 		}
72 	}
73 }
74 
75 
76 struct Model(T) if (isDynamicArray!T)
77 {
78 	@disable
79 	this();
80 
81 	T data;
82 
83 	this(T t)
84 	{
85 		data = t;
86 	}
87 
88 	auto opSlice()
89 	{
90 		return Range!(typeof(data))(data, 0, data.length);
91 	}
92 
93 	struct Range(R)
94 	{
95 		@disable
96 		this();
97 
98 		private
99 		{
100 			R source;
101 			size_t from, to;
102 		}
103 
104 		this(R source, size_t from, size_t to)
105 		{
106 			this.source = source;
107 			this.from   = from;
108 			this.to     = to;
109 		}
110 
111 		auto front()
112 		{
113 			return source[from];
114 		}
115 
116 		auto empty() const
117 		{
118 			return from == to;
119 		}
120 
121 		void popFront()
122 		{
123 			assert(!empty);
124 			from++;
125 		}
126 	}
127 }
128 
129 auto model(T)(T t) if (isDynamicArray!T)
130 {
131 	return Model!T(t);
132 }
133 
134 auto model(T)(ref T t) if (is(T == struct))
135 {
136 	return Model!T(t);
137 }
138 
139 class MyGui : SdlBackend
140 {
141 	this(int w, int h, string title)
142 	{
143 		super(w, h, title);
144 	}
145 
146 	override void onVisibleForTheFirstTime()
147 	{
148 		import nanogui.screen : Screen;
149 		import nanogui.widget, nanogui.theme, nanogui.checkbox, nanogui.label, 
150 			nanogui.common, nanogui.window, nanogui.layout, nanogui.button,
151 			nanogui.popupbutton, nanogui.entypo, nanogui.popup, nanogui.vscrollpanel,
152 			nanogui.combobox, nanogui.textbox, nanogui.formhelper;
153 		
154 		{
155 			auto window = new Window(screen, "TreeView demo", true);
156 			window.position(Vector2i(400, 245));
157 			window.size = Vector2i(150, 260);
158 			// window.size = Vector2i(screen.size.x - 30, screen.size.y - 30);
159 			window.layout(new BoxLayout(Orientation.Vertical));
160 
161 			import nanogui.experimental.treeview;
162 			new TreeView!float(window, "TreeView_______", 10, null);
163 			new TreeView!float(window, "TreeView_2_____", 11, null);
164 			new TreeView!float(window, "TreeView_3_____", 12, null);
165 			new TreeView!float(window, "TreeView_4_____", 13, null);
166 			new TreeView!(float[])(window, "TreeView_5_____", [14.0f, 29, 100], null);
167 		}
168 
169 		// now we should do layout manually yet
170 		screen.performLayout(ctx);
171 	}
172 }
173 
174 void main () {
175 	auto gui = new MyGui(1000, 800, "Nanogui TreeView example");
176 	gui.run();
177 }