~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/ui/anim/lib/Polygon.ahx

  • 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.anim.lib;
 
7
 
 
8
/**
 
9
 * Exposes the Button pattern for traits
 
10
 */
 
11
DEFACTOR(Polygon)
 
12
 
 
13
        PARAM(sides,Int,5)
 
14
        PARAM(angleOffset,Float,0.0)
 
15
        PARAM(outline,Pen,Pen.solid(1,Color.rgb(0,0,0) ))
 
16
        PARAM(fill,Brush,Brush.solid(Color.rgb(0.5,0.5,0.5)))
 
17
        PARAM(gutterSize,Float,0.1)
 
18
        PARAM(inner,Widget,null)
 
19
        PARAM(outerBulge,Float,1.0)
 
20
        PARAM(outerBulgeInvertDown,Bool,true)
 
21
        PARAM(rotateSpeed,T_Float(1.0))
 
22
        PARAM(innerSize,T_Float(0.70711),Default is cos/sin(pi/4) for the inner-square of this item, as if perfect circle)
 
23
 
 
24
                
 
25
        ONINIT()
 
26
        {
 
27
                if( params.inner != null )
 
28
                        addChild( params.inner.getNative() );
 
29
        }
 
30
        VAR(down,Bool,false)
 
31
        VAR(mouse,Bool,false)
 
32
        
 
33
        TRAIT(Button,Up,DEFAULT)
 
34
        {
 
35
                down = false;
 
36
                taintDraw();
 
37
        }
 
38
        TRAIT(Button,Down,NONE)
 
39
        {
 
40
                down = true;
 
41
                taintDraw();
 
42
        }
 
43
        TRAIT(Mouse,Over,DEFAULT)
 
44
        {
 
45
                mouse = true;
 
46
                taintDraw();
 
47
        }
 
48
        TRAIT(Mouse,Out,NONE)
 
49
        {
 
50
                mouse = false;
 
51
                taintDraw();
 
52
        }
 
53
        
 
54
        SEQUENCE(Still,DEFAULT)
 
55
        {
 
56
                taintDraw();
 
57
                ONRESET()
 
58
                {
 
59
                }
 
60
        }
 
61
        ONSTEP()
 
62
        {
 
63
        }
 
64
                
 
65
        VAR(angleRotate,T_Float(0.0))
 
66
        /** Rotates polygon, not the inner item */
 
67
        SEQUENCE(Rotate,NONE)
 
68
        {
 
69
        }
 
70
        ONSTEP()
 
71
        {
 
72
                taintDraw();
 
73
                angleRotate += elapsed * params.rotateSpeed;
 
74
        }
 
75
        
 
76
        DRAWTRANSFORM_SCALE(0.5,0.5)
 
77
        ONDRAW()
 
78
        {
 
79
                var gfxi = GraphicsX.scaled( graphics, 
 
80
                        widgetSize.x/2 * (1-params.gutterSize), 
 
81
                        widgetSize.y/2 * (1-params.gutterSize));
 
82
                gfx.clear();
 
83
                
 
84
                var angle = params.angleOffset + angleRotate;
 
85
                var angleDelta = Math.PI*2 / params.sides;
 
86
                var pts = new Array<Point2>();
 
87
                var outPts = new Array<Point2>();
 
88
                                                
 
89
                var hasBulge = params.outerBulge != 1.0;        //exact mathc okay, specific value
 
90
                var bulgeFactor = Math.cos( 0.5 * angleDelta ) /* length from center to chord*/
 
91
                        * ( down && params.outerBulgeInvertDown ? 1/params.outerBulge : params.outerBulge);
 
92
                
 
93
                for( i in 0...params.sides )
 
94
                {
 
95
                        //inner edge (will be auto-rounded)
 
96
                        pts.push( Point2.at( Math.cos( angle ), Math.sin( angle ) ) );
 
97
                        
 
98
                        //do outer edge
 
99
                        if( hasBulge )
 
100
                        {
 
101
                                var halfAngle = angle + angleDelta/2;
 
102
                                outPts.push( pts[pts.length-1] );
 
103
                                outPts.push( 
 
104
                                        Point2.at( 
 
105
                                                Math.cos( halfAngle ) * bulgeFactor, 
 
106
                                                Math.sin( halfAngle ) * bulgeFactor
 
107
                                                ) 
 
108
                                        );
 
109
                        }
 
110
                        
 
111
                        if( i == (params.sides - 1 ) )
 
112
                                angle = params.angleOffset;     //to provide perfect alignment (no floating point error)
 
113
                        else
 
114
                                angle += angleDelta;
 
115
                }
 
116
                if( hasBulge )
 
117
                        outPts.push( outPts[0] );       //close polygon
 
118
                
 
119
                var outFill = params.fill.adjustBrightness( 0.5 );
 
120
                var inFill = params.fill;
 
121
                if( down )
 
122
                {
 
123
                        var swap = outFill;
 
124
                        outFill = inFill;
 
125
                        inFill = swap;
 
126
                }
 
127
                
 
128
                //TODO: optimize if gutterSize = 0
 
129
                //draw primary polygon
 
130
                gfx.use( outFill );
 
131
                gfx.use( params.outline );
 
132
                if( hasBulge )
 
133
                        gfx.polyCurve( outPts );
 
134
                else
 
135
                        gfx.polyLine( pts, true );
 
136
                gfx.endFill();
 
137
                
 
138
                //draw inner polygon
 
139
                gfxi.use( inFill );
 
140
                gfxi.use( Pen.empty() );
 
141
                gfxi.polyRound( pts, 0.2 );
 
142
                gfxi.endFill();
 
143
                
 
144
                //CLEANUP to provide general containing logic
 
145
                if( params.inner != null )
 
146
                {
 
147
                        params.inner.move( -actorSize.x * params.innerSize/2, -actorSize.y * params.innerSize/2 );
 
148
                        params.inner.resize( actorSize.x * params.innerSize, actorSize.y * params.innerSize );
 
149
                }
 
150
        }
 
151
        
 
152
ENDACTOR()
 
 
b'\\ No newline at end of file'