| 1 |
/* |
|---|
| 2 |
--------------------------------------------------------------------- |
|---|
| 3 |
luigi/example1.d -- An example program using the Luigi user |
|---|
| 4 |
interface library. |
|---|
| 5 |
|
|---|
| 6 |
An example/test program for Luigi. |
|---|
| 7 |
Uses the GLFW windowing toolkit via luigi.adapter.glfw. |
|---|
| 8 |
--------------------------------------------------------------------- |
|---|
| 9 |
Copyright (C) 2006 William V. Baxter III |
|---|
| 10 |
|
|---|
| 11 |
This software is provided 'as-is', without any express or implied |
|---|
| 12 |
warranty. In no event will the authors be held liable for any |
|---|
| 13 |
damages arising from the use of this software. |
|---|
| 14 |
|
|---|
| 15 |
Permission is granted to anyone to use this software for any |
|---|
| 16 |
purpose, including commercial applications, and to alter it and |
|---|
| 17 |
redistribute it freely, subject to the following restrictions: |
|---|
| 18 |
|
|---|
| 19 |
1. The origin of this software must not be misrepresented; you must |
|---|
| 20 |
not claim that you wrote the original software. If you use this |
|---|
| 21 |
software in a product, an acknowledgment in the product |
|---|
| 22 |
documentation would be appreciated but is not required. |
|---|
| 23 |
2. Altered source versions must be plainly marked as such, and must |
|---|
| 24 |
not be misrepresented as being the original software. |
|---|
| 25 |
3. This notice may not be removed or altered from any source distribution. |
|---|
| 26 |
|
|---|
| 27 |
William Baxter wbaxter@gmail.com |
|---|
| 28 |
|
|---|
| 29 |
--------------------------------------------------------------------- |
|---|
| 30 |
Written in the D programming language (http://www.digitalmars.com/d) |
|---|
| 31 |
--------------------------------------------------------------------- |
|---|
| 32 |
*/ |
|---|
| 33 |
import luigui = luigi.gui; |
|---|
| 34 |
import luigi.adapters.glfw; |
|---|
| 35 |
static import luigi.themes.std; |
|---|
| 36 |
static import luigi.themes.dxut; |
|---|
| 37 |
|
|---|
| 38 |
import derelict.glfw.glfw; |
|---|
| 39 |
import derelict.opengl.gl; |
|---|
| 40 |
import derelict.opengl.glu; |
|---|
| 41 |
import std.string; |
|---|
| 42 |
import std.stdio : writefln; |
|---|
| 43 |
|
|---|
| 44 |
static this() |
|---|
| 45 |
{ |
|---|
| 46 |
DerelictGL.load(); |
|---|
| 47 |
DerelictGLU.load(); |
|---|
| 48 |
DerelictGLFW.load(); |
|---|
| 49 |
|
|---|
| 50 |
// Initialise GLFW |
|---|
| 51 |
glfwInit(); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
static ~this() |
|---|
| 55 |
{ |
|---|
| 56 |
// Close OpenGL window and terminate GLFW |
|---|
| 57 |
glfwTerminate(); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
void usage(char[] name) |
|---|
| 61 |
{ |
|---|
| 62 |
writefln("Usage: %s [-nosync]", name); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
luigui.Overlay create_gui(char[] theme) |
|---|
| 66 |
{ |
|---|
| 67 |
luigui.Overlay gui; |
|---|
| 68 |
with(luigui) { |
|---|
| 69 |
Luigi().adapter = GLFWAdapter(); |
|---|
| 70 |
if (theme == "dxut" || theme == "dx") { |
|---|
| 71 |
Luigi().add_resource_location(".."); |
|---|
| 72 |
Luigi().theme = new luigi.themes.dxut.DXUTTheme(); |
|---|
| 73 |
} |
|---|
| 74 |
gui = new Overlay; |
|---|
| 75 |
|
|---|
| 76 |
gui.arranger = new BorderArranger(Gaps(5)); |
|---|
| 77 |
|
|---|
| 78 |
alias BorderArranger.Region Region; |
|---|
| 79 |
alias Alignment A; |
|---|
| 80 |
|
|---|
| 81 |
auto eastGroup = gui.add_arranged(new GridPanel(1,0, Gaps(2)), Region.East); |
|---|
| 82 |
auto westGroup = gui.add_arranged(new GridPanel(2,0, Gaps(2)), Region.West); |
|---|
| 83 |
auto northGroup = gui.add_arranged(new FlowPanel, Region.North); |
|---|
| 84 |
auto southGroup = gui.add_arranged(new FlowPanel, Region.South); |
|---|
| 85 |
|
|---|
| 86 |
auto rgrp = new RadioGroup; |
|---|
| 87 |
|
|---|
| 88 |
with (westGroup) { |
|---|
| 89 |
auto b20 = add_arranged(new Button("Tool0")); |
|---|
| 90 |
auto b21 = add_arranged(new Button("Tool1")); |
|---|
| 91 |
auto b22 = add_arranged(new Button("Tool2")); |
|---|
| 92 |
auto b23 = add_arranged(new Button("Tool3")); |
|---|
| 93 |
auto b24 = add_arranged(new Button("LongNameTool4")); |
|---|
| 94 |
auto b25 = add_arranged(new Button("Tool5")); |
|---|
| 95 |
auto b26 = add_arranged(new Button("Tool6")); |
|---|
| 96 |
auto b27 = add_arranged(new Button("Tool7")); |
|---|
| 97 |
auto b28 = add_arranged(new Button("Tool8")); |
|---|
| 98 |
auto b29 = add_arranged(new Button("Tool9")); |
|---|
| 99 |
|
|---|
| 100 |
rgrp.multiadd(b20,b21,b22,b23,b24,b25,b26,b27,b28,b29); |
|---|
| 101 |
rgrp.value = 0; |
|---|
| 102 |
rgrp.selection_changed ~= (int t) { |
|---|
| 103 |
writefln("Tool %s now selected", t); |
|---|
| 104 |
}; |
|---|
| 105 |
|
|---|
| 106 |
auto s01 = add_arranged(new Slider); |
|---|
| 107 |
auto s02 = add_arranged(new Slider); |
|---|
| 108 |
s02.vertical = true; |
|---|
| 109 |
s01.precision = 0.1; |
|---|
| 110 |
s02.precision = 0.1; |
|---|
| 111 |
|
|---|
| 112 |
s01.value_changed.connect2nd( (double d){ writefln("Slider now %s",d); } ); |
|---|
| 113 |
s02.value_changed.connect2nd( (double d){ writefln("Slider now %s",d); } ); |
|---|
| 114 |
s02.value_changed.connect2nd(&s01.value); |
|---|
| 115 |
s01.value_changed.connect2nd(&s02.value); |
|---|
| 116 |
|
|---|
| 117 |
/* |
|---|
| 118 |
auto sb01 = add_arranged(new ScrollBar); |
|---|
| 119 |
auto sb02 = add_arranged(new ScrollBar); |
|---|
| 120 |
sb02.vertical = true; |
|---|
| 121 |
sb01.precision = 1; |
|---|
| 122 |
sb02.precision = 1; |
|---|
| 123 |
*/ |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
with(eastGroup) { |
|---|
| 127 |
auto b0 = add_widget(new Button("Luigi")); b0.disable(); |
|---|
| 128 |
auto b1 = add_widget(new Button("Is")); rgrp.add(b1,5); |
|---|
| 129 |
auto b2 = add_widget(new Button("My")); rgrp.add(b2,7); |
|---|
| 130 |
auto b3 = add_widget(new Button("Pal"));rgrp.add(b3,9); |
|---|
| 131 |
auto b4 = add_widget(new Button("Hide West")); |
|---|
| 132 |
b4.is_toggle = true; |
|---|
| 133 |
b4.clicked.connect( &westGroup.toggle_shown ); |
|---|
| 134 |
b4.value_changed ~= (Widget w, bool v){ |
|---|
| 135 |
Button b = cast(Button)w; assert(b); |
|---|
| 136 |
if (v) b.label = "Show West"; |
|---|
| 137 |
else b.label = "Hide West"; |
|---|
| 138 |
b.get_root.arrange(); |
|---|
| 139 |
}; |
|---|
| 140 |
|
|---|
| 141 |
auto onoff = new RadioGroup; |
|---|
| 142 |
auto r0 = add_widget(new RadioButton("On")); |
|---|
| 143 |
auto r1 = add_widget(new RadioButton("Off")); |
|---|
| 144 |
onoff.multiadd(r0,1, r1,0); |
|---|
| 145 |
onoff.value = 1; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
with(southGroup) { |
|---|
| 149 |
auto l0 = add_widget(new Label( "First Name:")); |
|---|
| 150 |
auto t0 = add_widget(new TextField("Hello")); |
|---|
| 151 |
auto l1 = add_widget(new Label( "Last Name:")); |
|---|
| 152 |
auto t1 = add_widget(new TextField("Rumpelstiltskin")); |
|---|
| 153 |
auto b4 = add_widget(new Button("Click me!")); |
|---|
| 154 |
auto l2 = add_widget(new Label("Disabled label")); |
|---|
| 155 |
l2.disable; |
|---|
| 156 |
b4.clicked ~= (Widget w){ writefln("Ouch! Not so hard!"); }; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
with (northGroup) { |
|---|
| 160 |
auto b10 = add_widget(new Button("File")); |
|---|
| 161 |
auto b11 = add_widget(new Button("Edit")); |
|---|
| 162 |
auto b12 = add_widget(new Button("View")); |
|---|
| 163 |
auto b13 = add_widget(new Button("Favorites")); |
|---|
| 164 |
auto b14 = add_widget(new Button("Tools")); |
|---|
| 165 |
auto b15 = add_widget(new Button("Options")); |
|---|
| 166 |
auto c15 = add_widget(new Checkbox("Check it out!")); |
|---|
| 167 |
auto b16 = add_widget(new Button("Help")); |
|---|
| 168 |
|
|---|
| 169 |
c15.value_changed ~= (Widget w, bool onoff) { |
|---|
| 170 |
writefln(onoff?"checked!":"unchecked!"); |
|---|
| 171 |
}; |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
return gui; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
void main(char[][] argv) |
|---|
| 178 |
{ |
|---|
| 179 |
bool dosync = true; |
|---|
| 180 |
char[] theme = "dxut"; |
|---|
| 181 |
|
|---|
| 182 |
//---------------- PARSE ARGS ----------------------- |
|---|
| 183 |
for( int i=1; i<argv.length; i++ ) { |
|---|
| 184 |
auto a = argv[i]; |
|---|
| 185 |
if (a == "-nosync") dosync = false; |
|---|
| 186 |
else if (a == "-help") { usage(argv[0]); return; } |
|---|
| 187 |
else if (a == "-theme") { |
|---|
| 188 |
theme = argv[++i]; |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
//---------------- SETUP ----------------------- |
|---|
| 193 |
// Open OpenGL window |
|---|
| 194 |
if (!glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW )) |
|---|
| 195 |
return; |
|---|
| 196 |
|
|---|
| 197 |
// Enable sticky keys |
|---|
| 198 |
glfwEnable( GLFW_STICKY_KEYS ); |
|---|
| 199 |
glfwEnable( GLFW_KEY_REPEAT ); |
|---|
| 200 |
|
|---|
| 201 |
// Disable vertical sync if requested (on cards that support it) |
|---|
| 202 |
if (!dosync) glfwSwapInterval( 0 ); |
|---|
| 203 |
|
|---|
| 204 |
// Set the background color |
|---|
| 205 |
glClearColor( 66/255., 75/255., 121/255., 0 ); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
//---------------- CREATE GUI ---------------------- |
|---|
| 209 |
luigui.Overlay gui = create_gui(theme); |
|---|
| 210 |
|
|---|
| 211 |
//---------------- HELPERS ------------------------- |
|---|
| 212 |
int frames = 0; |
|---|
| 213 |
real t0 = glfwGetTime(); |
|---|
| 214 |
|
|---|
| 215 |
//---------------- |
|---|
| 216 |
// Calculate and display FPS (frames per second), returns current time |
|---|
| 217 |
real show_fps() |
|---|
| 218 |
{ |
|---|
| 219 |
real t = glfwGetTime(); |
|---|
| 220 |
if( (t-t0) > 1.0 || frames == 0 ) |
|---|
| 221 |
{ |
|---|
| 222 |
real fps = frames / (t-t0); |
|---|
| 223 |
char[] titlestr = format("Spinning Triangle (%0.1f FPS)", fps); |
|---|
| 224 |
if (dosync) { titlestr ~= " [vsync]"; } |
|---|
| 225 |
glfwSetWindowTitle( toStringz(titlestr) ); |
|---|
| 226 |
t0 = t; |
|---|
| 227 |
frames = 0; |
|---|
| 228 |
} |
|---|
| 229 |
frames ++; |
|---|
| 230 |
return t; |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
//---------------- |
|---|
| 234 |
// Setup up open gl matrices based on window size |
|---|
| 235 |
void setup_view_matrices() |
|---|
| 236 |
{ |
|---|
| 237 |
// Get window size (may be different than the requested size) |
|---|
| 238 |
int width, height; |
|---|
| 239 |
glfwGetWindowSize( &width, &height ); |
|---|
| 240 |
height = height > 0 ? height : 1; |
|---|
| 241 |
|
|---|
| 242 |
//writefln("Size = (%d, %d)", width,height); |
|---|
| 243 |
|
|---|
| 244 |
// Set viewport |
|---|
| 245 |
glViewport( 0, height, width, -height ); |
|---|
| 246 |
|
|---|
| 247 |
// Select and setup the projection matrix |
|---|
| 248 |
glMatrixMode( GL_PROJECTION ); |
|---|
| 249 |
glLoadIdentity(); |
|---|
| 250 |
gluPerspective( 65.0f, cast(GLfloat)width/height, 1.0f,100.0f ); |
|---|
| 251 |
|
|---|
| 252 |
// Select and setup the modelview matrix |
|---|
| 253 |
glMatrixMode( GL_MODELVIEW ); |
|---|
| 254 |
glLoadIdentity(); |
|---|
| 255 |
gluLookAt( 0.0f, 1.0f, 0.0f, // Eye-position |
|---|
| 256 |
0.0f, 20.0f, 0.0f, // View-point |
|---|
| 257 |
0.0f, 0.0f, 1.0f ); // Up-vector |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
//---------------- MAIN LOOP ------------------------- |
|---|
| 261 |
bool running=true; |
|---|
| 262 |
while (running) |
|---|
| 263 |
{ |
|---|
| 264 |
int x,y; |
|---|
| 265 |
glfwGetMousePos( &x, &y ); |
|---|
| 266 |
|
|---|
| 267 |
real t = show_fps(); |
|---|
| 268 |
|
|---|
| 269 |
setup_view_matrices(); |
|---|
| 270 |
|
|---|
| 271 |
glClear( GL_COLOR_BUFFER_BIT ); |
|---|
| 272 |
|
|---|
| 273 |
glTranslatef(-4,14,4 ); |
|---|
| 274 |
glBegin( GL_TRIANGLES ); |
|---|
| 275 |
glColor3f( 1, 0, 0 ); glVertex3f( -5, 0, -4 ); |
|---|
| 276 |
glColor3f( 0, 1, 0 ); glVertex3f( 5, 0, -4 ); |
|---|
| 277 |
glColor3f( 0, 0, 1 ); glVertex3f( 0, 0, 6); |
|---|
| 278 |
glEnd(); |
|---|
| 279 |
glTranslatef( 4,-14,-4 ); |
|---|
| 280 |
|
|---|
| 281 |
// Draw a rotating colorful triangle |
|---|
| 282 |
glTranslatef( 0, 14, 0 ); |
|---|
| 283 |
glRotatef( t*100, 0,1,0 ); |
|---|
| 284 |
glRotatef( 0.3*x, 1,0,0); |
|---|
| 285 |
glRotatef( 0.3*y, 0,0,1); |
|---|
| 286 |
glBegin( GL_TRIANGLES ); |
|---|
| 287 |
glColor3f( 1, 0, 0 ); glVertex3f( -5, 0, -4 ); |
|---|
| 288 |
glColor3f( 0, 1, 0 ); glVertex3f( 5, 0, -4 ); |
|---|
| 289 |
glColor3f( 0, 0, 1 ); glVertex3f( 0, 0, 6 ); |
|---|
| 290 |
glEnd(); |
|---|
| 291 |
|
|---|
| 292 |
gui.draw(); |
|---|
| 293 |
|
|---|
| 294 |
glfwSwapBuffers(); |
|---|
| 295 |
|
|---|
| 296 |
running = |
|---|
| 297 |
!glfwGetKey( GLFW_KEY_ESC ) && |
|---|
| 298 |
glfwGetWindowParam( GLFW_OPENED ); |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
} |
|---|