root/trunk/luigi/examples/example1_gld.d

Revision 46, 9.9 kB (checked in by baxissimo, 2 years ago)

Added top level add_blah_callback calls in gui.d to hook the system-level input routines. May rethink this a bit still. That may be wanted in some cases (reshape, or recording all mouse motion for example) , but probably more useful would be routines that get called as a _fallback_ when no gui widget intercepts the event.

Fixed disconnect in signalobj.d

Added disconnect routines to adapter.d

Fixed dxut to work without Derelict.

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