~extremepopcorn/dhlib/dhlib_ep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package ui.anim.lib;

/**
 * Exposes the Button pattern for traits
 */
DEFACTOR(Polygon)

	PARAM(sides,Int,5)
	PARAM(angleOffset,Float,0.0)
	PARAM(outline,Pen,Pen.solid(1,Color.rgb(0,0,0) ))
	PARAM(fill,Brush,Brush.solid(Color.rgb(0.5,0.5,0.5)))
	PARAM(gutterSize,Float,0.1)
	PARAM(inner,Widget,null)
	PARAM(outerBulge,Float,1.0)
	PARAM(outerBulgeInvertDown,Bool,true)
	PARAM(rotateSpeed,T_Float(1.0))
	PARAM(innerSize,T_Float(0.70711),Default is cos/sin(pi/4) for the inner-square of this item, as if perfect circle)

		
	ONINIT()
	{
		if( params.inner != null )
			addChild( params.inner.getNative() );
	}
	VAR(down,Bool,false)
	VAR(mouse,Bool,false)
	
	TRAIT(Button,Up,DEFAULT)
	{
		down = false;
		taintDraw();
	}
	TRAIT(Button,Down,NONE)
	{
		down = true;
		taintDraw();
	}
	TRAIT(Mouse,Over,DEFAULT)
	{
		mouse = true;
		taintDraw();
	}
	TRAIT(Mouse,Out,NONE)
	{
		mouse = false;
		taintDraw();
	}
	
	SEQUENCE(Still,DEFAULT)
	{
		taintDraw();
		ONRESET()
		{
		}
	}
	ONSTEP()
	{
	}
		
	VAR(angleRotate,T_Float(0.0))
	/** Rotates polygon, not the inner item */
	SEQUENCE(Rotate,NONE)
	{
	}
	ONSTEP()
	{
		taintDraw();
		angleRotate += elapsed * params.rotateSpeed;
	}
	
	DRAWTRANSFORM_SCALE(0.5,0.5)
	ONDRAW()
	{
		var gfxi = GraphicsX.scaled( graphics, 
			widgetSize.x/2 * (1-params.gutterSize), 
			widgetSize.y/2 * (1-params.gutterSize));
		gfx.clear();
		
		var angle = params.angleOffset + angleRotate;
		var angleDelta = Math.PI*2 / params.sides;
		var pts = new Array<Point2>();
		var outPts = new Array<Point2>();
						
		var hasBulge = params.outerBulge != 1.0;	//exact mathc okay, specific value
		var bulgeFactor = Math.cos( 0.5 * angleDelta ) /* length from center to chord*/
			* ( down && params.outerBulgeInvertDown ? 1/params.outerBulge : params.outerBulge);
		
		for( i in 0...params.sides )
		{
			//inner edge (will be auto-rounded)
			pts.push( Point2.at( Math.cos( angle ), Math.sin( angle ) ) );
			
			//do outer edge
			if( hasBulge )
			{
				var halfAngle = angle + angleDelta/2;
				outPts.push( pts[pts.length-1] );
				outPts.push( 
					Point2.at( 
						Math.cos( halfAngle ) * bulgeFactor, 
						Math.sin( halfAngle ) * bulgeFactor
						) 
					);
			}
			
			if( i == (params.sides - 1 ) )
				angle = params.angleOffset;	//to provide perfect alignment (no floating point error)
			else
				angle += angleDelta;
		}
		if( hasBulge )
			outPts.push( outPts[0] );	//close polygon
		
		var outFill = params.fill.adjustBrightness( 0.5 );
		var inFill = params.fill;
		if( down )
		{
			var swap = outFill;
			outFill = inFill;
			inFill = swap;
		}
		
		//TODO: optimize if gutterSize = 0
		//draw primary polygon
		gfx.use( outFill );
		gfx.use( params.outline );
		if( hasBulge )
			gfx.polyCurve( outPts );
		else
			gfx.polyLine( pts, true );
		gfx.endFill();
		
		//draw inner polygon
		gfxi.use( inFill );
		gfxi.use( Pen.empty() );
		gfxi.polyRound( pts, 0.2 );
		gfxi.endFill();
		
		//CLEANUP to provide general containing logic
		if( params.inner != null )
		{
			params.inner.move( -actorSize.x * params.innerSize/2, -actorSize.y * params.innerSize/2 );
			params.inner.resize( actorSize.x * params.innerSize, actorSize.y * params.innerSize );
		}
	}
	
ENDACTOR()