~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/ui/test/TestActor.mhx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package ui.test;
 
7
 
 
8
import ui.anim.Actor;
 
9
import ui.PositionBox;
 
10
import ui.Menu;
 
11
 
 
12
import mathx.Point2;
 
13
 
 
14
import DrawBasicTypes;
 
15
 
 
16
typedef ActorArgs = Void -> Dynamic;
 
17
 
 
18
class TestActor extends ui.StageLayout, implements ui.Animation
 
19
{
 
20
        var actor : Actor;
 
21
        var actorClass : Class<Dynamic>;
 
22
        
 
23
        var box : PositionBox;
 
24
        var notice : ui.StaticText;
 
25
        
 
26
        var edit1 : ui.EditText;
 
27
        
 
28
        var actorButtons : ui.LinearLayout;
 
29
        var testButtons : ui.LinearLayout;
 
30
        var buttons : ui.LinearLayout;
 
31
        var status : ui.LinearLayout;
 
32
        var primary : ui.LinearLayout;
 
33
        
 
34
        var tfActor : ui.StaticText;
 
35
        var tfRatio : ui.StaticText;
 
36
        var tfZoom : ui.StaticText;
 
37
        var tfFPS : ui.StaticText;
 
38
        var tfLoad : ui.StaticText;
 
39
        
 
40
        public function new()
 
41
        {
 
42
                super();
 
43
                MixinLayout();
 
44
                layout.spacing = 0;
 
45
                ui.AnimationManager.s_addListener( this, 1.0 );
 
46
                
 
47
                primary = ui.LinearLayout.horizontal();
 
48
                layout.add( primary, ui.SizeType.Fill( 1 ) );
 
49
                
 
50
                status = ui.LinearLayout.horizontal();
 
51
                status.setBackground( ui.Box.plain( Brush.solid( Color.rgb(0.2,0.5,0.5) ) ) );
 
52
                //TODO: the need to repeat 1.5 in each line here is distressing, if you
 
53
                //don't then the StaticText won't know it's preferred size... :(
 
54
                layout.add( status, ui.SizeType.Lines( 1.5 ) );
 
55
                tfActor = ui.StaticText.singleLine( "", ui.StaticTextAlign.Left, 1.5 );
 
56
                tfRatio = ui.StaticText.singleLine( "", 1.5 );
 
57
                tfZoom = ui.StaticText.singleLine( "", 1.5 );
 
58
                tfFPS = ui.StaticText.singleLine( "xx.x fps", 1.5 );
 
59
                tfLoad = ui.StaticText.singleLine( "xxx.xx ms", 1.5 );
 
60
                
 
61
                status.add( tfActor );
 
62
                status.add( ui.Box.empty(), ui.SizeType.Fill( 0 ) );
 
63
                status.add( tfRatio );
 
64
                status.add( tfZoom );
 
65
                status.add( tfFPS );
 
66
                status.add( tfLoad );
 
67
                updateStatus();
 
68
                
 
69
                //test buttons
 
70
                testButtons = ui.LinearLayout.vertical();
 
71
                testButtons.add( ui.Button.plain( ui.Action.bind( control ), "Control" ) );
 
72
                
 
73
                //actor box
 
74
                box = new PositionBox( );
 
75
                primary.add( box, ui.SizeType.Fill( 1 ) );
 
76
                
 
77
                //combine it all together
 
78
                buttons = ui.LinearLayout.vertical();
 
79
                primary.add( buttons );
 
80
                buttons.add( testButtons );
 
81
                
 
82
                //ensure we fill the stage
 
83
                layout.setBackground( ui.Box.plain( draw.TileDesc.brushFromXML( backTile ) ) );
 
84
                
 
85
                var cl = Type.resolveClass( TestActorOptions.actorClassName );
 
86
                if( cl != null ) {
 
87
                        try
 
88
                        {
 
89
                                setupActor( cl, null );
 
90
                        }
 
91
                        catch( q : Dynamic )
 
92
                        {
 
93
                                TestActorOptions.actorClassName = null;
 
94
                        }
 
95
                }
 
96
        }
 
97
        
 
98
        public function animate( elapsed : Float ) : Bool
 
99
        {
 
100
                tfFPS.text = Math.floor( ui.AnimationManager.getInstance().lastFPS * 10 ) /10.0 + "fps";
 
101
                tfLoad.text = Math.floor( ui.AnimationManager.getInstance().lastLoad * 100000 ) / 100 + "ms";
 
102
                return true;
 
103
        }
 
104
        
 
105
        function updateStatus()
 
106
        {
 
107
                tfActor.text = TestActorOptions.actorClassName;
 
108
                tfZoom.text = Math.floor( TestActorOptions.zoom * 100 ) + "% "; //extra space until full auto-text sizing works
 
109
                
 
110
                if( TestActorOptions.ratio == null )
 
111
                        tfRatio.text = "none";
 
112
                else
 
113
                {
 
114
                        //rough check for simple ratios (TODO: lookup a good way to do this)
 
115
                        var found = false;
 
116
                        for( x in 1...6 )
 
117
                        {
 
118
                                for( y in 1...6 )
 
119
                                        if( mathx.MathUtil.equals( x/y, TestActorOptions.ratio ) )
 
120
                                        {
 
121
                                                tfRatio.text = x + ":" + y;
 
122
                                                found = true;
 
123
                                                break;
 
124
                                        }
 
125
                                if( found )
 
126
                                        break;
 
127
                        }
 
128
                                        
 
129
                        if( !found )
 
130
                                tfRatio.text = "" + TestActorOptions.ratio;
 
131
                }
 
132
                
 
133
                status.update();
 
134
        }
 
135
        
 
136
        function control()
 
137
        {
 
138
        
 
139
                var menu = Menu.popup();
 
140
                menu.addSubMenu( "Select Actor", getSelectActorMenu() );
 
141
                menu.addSubMenu( "Size Ratio", getSelectRatio() );
 
142
                
 
143
                menu.addItem( "Params", ui.Action.bind( setParams ) );
 
144
                menu.addItem( "Reset", ui.Action.bind( doActorReset ) );
 
145
                menu.addItem( "Zoom Out", ui.Action.bind1( doZoom, 0.5 ) );
 
146
                menu.addItem( "Zoom In", ui.Action.bind1( doZoom, 2 ) );
 
147
                
 
148
                menu.modalAtMouse(null,false/*gray*/);
 
149
        }
 
150
        
 
151
        function getSelectRatio()
 
152
        {
 
153
                var menu = Menu.popup();
 
154
                var ratios = [
 
155
                        [ "None", null ],
 
156
                        [ "1:1", 1.0 ],
 
157
                        [ "1:2", 1.0/2.0 ],
 
158
                        [ "1:3", 1.0/3.0 ],
 
159
                        [ "2:1", 2.0 ],
 
160
                        [ "2:3", 2.0/3.0 ],
 
161
                        [ "3:1", 3.0 ],
 
162
                        [ "3:2", 3.0/2.0 ]
 
163
                        ];
 
164
                        
 
165
                for( r in ratios )
 
166
                        menu.addItem( r[0], ui.Action.bind1( setRatio, r[1] ) );
 
167
                        
 
168
                return menu;
 
169
        }
 
170
        
 
171
        function setRatio( ratio : Null<Float> )
 
172
        {
 
173
                TestActorOptions.ratio = ratio;
 
174
                box.setRatio( TestActorOptions.ratio );
 
175
                updateStatus();
 
176
        }
 
177
        
 
178
        function getSelectActorMenu()
 
179
        {
 
180
                var menu = Menu.popup();
 
181
                //create the built-in samples
 
182
                var sub = Menu.popup();
 
183
                var samples = [
 
184
                        [ "Stacked Panels", ui.anim.lib.StackedPanels, argsStackedPanels ],
 
185
                        [ "Puzzle Piece", ui.anim.lib.PuzzlePiece, argsPuzzlePiece ],
 
186
                        [ "Gift Box", ui.anim.lib.GiftBox, argsGiftBox ],
 
187
                        [ "Bone Circle", ui.anim.lib.BoneCircle, argsBoneCircle ],
 
188
                        [ "Bone Jitter", ui.anim.lib.BoneJitter, argsBoneJitter ],
 
189
                        [ "Bone Slider", ui.anim.lib.BoneSlider, argsBoneSlider ],
 
190
                        [ "Bone Shatter", ui.anim.lib.BoneShatter, argsBoneSlider ],
 
191
                        [ "Bone Wheel", ui.anim.lib.BoneWheel, argsBoneWheel ],
 
192
                        ];
 
193
                        
 
194
                for( s in samples )
 
195
                        sub.addItem( s[0], ui.Action.bind2( selectActor, s[1], s[2] ) );
 
196
                menu.addSubMenu( "TestActor Samples", sub );
 
197
                
 
198
                //add a default for each of the actors
 
199
                sub = Menu.popup();
 
200
                for( s in ActorList.actorList )
 
201
                        sub.addItem( s[0], ui.Action.bind2( setupActor, s[1], {} ) );
 
202
                menu.addSubMenu( "Library Animations", sub );
 
203
                
 
204
                return menu;
 
205
        }
 
206
        
 
207
        function selectActor( actorClass : Class<Dynamic>, actorArgFunc : ActorArgs )
 
208
        {
 
209
                setupActor( actorClass, actorArgFunc() );
 
210
        }
 
211
        
 
212
        function setupActor( actorClass : Class<Dynamic>, actorArgs : Dynamic )
 
213
        {
 
214
                TestActorOptions.actorClassName = Type.getClassName( actorClass );
 
215
        
 
216
                this.actorClass = actorClass;
 
217
                this.actor = Type.createInstance( actorClass, [ actorArgs ] );
 
218
                var replace = new Array<ui.Widget>();
 
219
                replace.push( ui.Box.plain( Pen.solid( 0, Color.rgb(0,0,0) ) ) );
 
220
                replace.push( actor );
 
221
                box.replace( replace );
 
222
                box.setZoom( TestActorOptions.zoom );
 
223
                box.setRatio( TestActorOptions.ratio );
 
224
                
 
225
                if( actorButtons != null )
 
226
                        buttons.remove( actorButtons );
 
227
                        
 
228
                //setup buttons to control the actor
 
229
                actorButtons = ui.LinearLayout.vertical();
 
230
                actorButtons.add( ui.StaticText.singleLine( "Sequences" ) );
 
231
                for( seq in actor.getSequences() )
 
232
                                actorButtons.add( 
 
233
                                        ui.Button.plain( 
 
234
                                                ui.Action.bind3( 
 
235
                                                        actor.actorSetSequence, 
 
236
                                                        seq,
 
237
                                                        ui.Action.bind1( seqDone, seq ),
 
238
                                                        false
 
239
                                                ),
 
240
                                                seq
 
241
                                        ) 
 
242
                                );
 
243
                notice = ui.StaticText.singleLine( "" );
 
244
                actorButtons.add( notice );
 
245
                                
 
246
                actorButtons.add( ui.StaticText.singleLine( "Traits" ) );
 
247
                for( group in actor.getTraitGroups() )
 
248
                {
 
249
                        var menu = Menu.popup();
 
250
                        
 
251
                        actorButtons.add( ui.Button.plain( ui.Action.bind( function() { menu.modalAtMouse(null,false); } ), group ) );
 
252
                        for( trait in actor.getTraits(group) )
 
253
                        {
 
254
                                var numArgs = actor.getTraitNumArgs( group, trait );
 
255
                                menu.addItem( trait, ui.Action.bind3( setTrait, group, trait, numArgs ) );
 
256
                        }
 
257
                }
 
258
                
 
259
                actorButtons.add( ui.StaticText.singleLine( "Params" ) );
 
260
                actorButtons.add( edit1 = ui.EditText.singleLine() );
 
261
                
 
262
                buttons.add( actorButtons );
 
263
                primary.update();       //width of buttons may have changed
 
264
                
 
265
                updateStatus();
 
266
        }
 
267
        
 
268
        function doActorReset()
 
269
        {
 
270
                //can't be bound directly since the actor object changes
 
271
                actor.actorReset();
 
272
        }
 
273
 
 
274
        function setTrait( group : String, trait : String, numArgs : Int )
 
275
        {
 
276
                if( numArgs == 0 )
 
277
                        actor.actorSetTrait( actor.getTraitRef(group, trait) );
 
278
                else if( numArgs == 1 )
 
279
                        actor.actorSetTrait1( actor.getTraitRef(group, trait), edit1.text );    //use auto-conversions...
 
280
                else
 
281
                        Assert.unreachable();
 
282
        }
 
283
        
 
284
        function seqDone( seq : String )
 
285
        {
 
286
                notice.text = "Done: " + seq;
 
287
        }
 
288
        
 
289
        function doZoom( mul : Float )
 
290
        {
 
291
                TestActorOptions.zoom *= mul;
 
292
                box.setZoom( TestActorOptions.zoom );
 
293
                updateStatus();
 
294
        }
 
295
        
 
296
        function setParams()
 
297
        {
 
298
                var dlg = new ActorParamsDialog( actor );
 
299
                dlg.usePrefSize(stage);
 
300
                ui.Modal.asStageModal( dlg, ui.Action.bind1( dialogDone, dlg ) );
 
301
        }
 
302
        
 
303
        function dialogDone( dlg : ActorParamsDialog )
 
304
        {
 
305
                var newParams = dlg.getParams();
 
306
                if( newParams == null )
 
307
                        return; //user pressed cancel
 
308
                        
 
309
                //recreate the actor entirely since Actors do not expect their params to
 
310
                //change during their execution
 
311
                setupActor( actorClass, newParams );
 
312
        }
 
313
        
 
314
        //////////////////////////////////////////////////////////////////////
 
315
        // Sample actor arguments
 
316
        function argsStackedPanels()
 
317
        {
 
318
                return {};
 
319
        }
 
320
        
 
321
        function argsPuzzlePiece()
 
322
        {
 
323
#if false
 
324
                var image = new ui.PositionBox( ui.Box.plain( new squix.IClipBackground() ) ); 
 
325
                image.setOrigin( Point2.at( 0.5, 0.5 ) );
 
326
                image.setTranslate( Point2.at( -0.25, 0 ) );
 
327
                image.setZoom( 2 );
 
328
#end
 
329
                return 
 
330
                        { 
 
331
                                left: -1, 
 
332
                                bottom: 1,
 
333
                                //image: image
 
334
                        };
 
335
        }
 
336
        
 
337
        function argsBoneJitter()
 
338
        {
 
339
                return 
 
340
                        { 
 
341
                                item: new ui.anim.lib.Polygon( { sides: 5, fill: Brush.solid( Color.rgb(0.8,0.1,0.5) ) } ) 
 
342
                        };
 
343
        }
 
344
        
 
345
        function argsBoneSlider()
 
346
        {
 
347
                return 
 
348
                        { 
 
349
                                item: new ui.anim.lib.Polygon( { sides: 4, fill: Brush.solid( Color.rgb(0.8,0.1,0.5) ), angleOffset: 0.7854 } ) 
 
350
                        };
 
351
        }
 
352
        
 
353
        function argsGiftBox()
 
354
        {
 
355
                return
 
356
                        { 
 
357
 
 
358
                                prize: new ui.anim.lib.Polygon( { sides: 5, fill: Brush.solid( Color.rgb(0.8,0.1,0.5) ) } ) 
 
359
                        };
 
360
        }
 
361
        
 
362
        function argsBoneCircle()
 
363
        {
 
364
                return 
 
365
                        { actors: getListActors(),
 
366
                                lockRate: 2 
 
367
                        };
 
368
        }
 
369
        
 
370
        function argsBoneWheel()
 
371
        {
 
372
                return
 
373
                        { 
 
374
                                actors: getListActors().concat( getListActors() )
 
375
                        }
 
376
 
 
377
        }
 
378
        
 
379
        function getListActors()
 
380
        {
 
381
                var text = "Press Me!";
 
382
                var inner = new ui.PositionBox( ui.StaticText.singleLine( text ) );
 
383
                inner.setRatio( 4 );    //TODO: still to be done automatically by StaticText!!!
 
384
                var subActor = new ui.anim.lib.Polygon( 
 
385
                        { 
 
386
                                inner: inner, 
 
387
                                outerBulge: 1.6,
 
388
                                gutterSize: 0.25,
 
389
                                sides: 5, fill: Brush.solid( Color.rgb(0.8,0.1,0.5) )  
 
390
                        } );
 
391
                var testButton = ui.Button.actor( ui.Action.bind( function() { trace( "Clicked" ); } ), subActor );
 
392
                return  [
 
393
                        testButton,
 
394
                        new ui.anim.lib.Polygon( { sides: 3, fill: Brush.solid( Color.rgb(0.5,0.8,0.1) ) } ),
 
395
                        new ui.anim.lib.Polygon( { sides: 4, fill: Brush.solid( Color.rgb(0.1,0.5,0.8) ) } ),
 
396
                        new ui.anim.lib.Polygon( { sides: 5, fill: Brush.solid( Color.rgb(0.8,0.1,0.5) ) } )
 
397
                        ];
 
398
        }
 
399
        
 
400
        //////////////////////////////////////////////////////////////////////
 
401
        // Entry point
 
402
        static public function main()
 
403
        {
 
404
                ui.StageLayout.setup( TestActor );
 
405
        }
 
406
        
 
407
        //////////////////////////////////////////////////////////////////////
 
408
        // Inclusions
 
409
        define(`IsVerticalLayout',`')
 
410
        include(`ui/MixinLayout.ihx')
 
411
        
 
412
        static var backTile = HERE _FILE(actor_back.tile.xml);
 
413
}
 
414