~eda-qa/dhlib/main

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* <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 draw;

#if neko
//no BitmapData
import neash.display.Graphics;
import neash.geom.Matrix;
import neash.display.GradientType;
import neash.display.SpreadMethod;
import neash.display.InterpolationMethod;

typedef UInt = Int;
#elseif flash
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
#end

/**
 * NOTE: all the #if as3 nonsense is to ensure this works when converted to AS3 as well as
 * compiled directly in Haxe.  It is a bug in haxe arising from how these Enum'd String types
 * are handled.  Users of this class can simply use the Enum'd types as expected...
 */
private enum BrushType
{
	None;
	Solid( color : Color, alpha : Float );
#if !neko
	Bitmap( bd : BitmapData );
#end
#if as3
	Gradient( type : String, colors : Array<UInt>, alphas : Array<Dynamic>, ratios : Array<Dynamic>, 
		matrix :	Matrix, spreadMethod : String, interpolationMethod : String, focalPointRatio : Float );
#else
	Gradient( type : GradientType, colors : Array<UInt>, alphas : Array<Dynamic>, ratios : Array<Dynamic>, 
		matrix :	Matrix, spreadMethod : SpreadMethod, interpolationMethod : InterpolationMethod, focalPointRatio : Float );
#end
	Custom;
}

/**
 * Creates a logical brush which can be used for filling drawn objects.
 * The base class handles the basic cases but allows for a derived class
 * to be implemented.
 */
/*interface*/ class Brush implements draw.GraphicsImplement
{
	/**
	 * Determines where there is a fill or not
	 */
	/*abstract*/ public function isNonEmpty() : Bool
	{
		Assert.pureVirtual();
		return false;
	}
	
	/**
	 * Applies the brush to the graphics object, will be used
	 * to draw the next objects
	 */
	/*abstract*/ public function apply( graphics : Graphics ) : Void
	{
		Assert.pureVirtual();
	}
	
	/*abstract*/ public function toString() : String
	{
		Assert.pureVirtual();
		return "";
	}
	
	/**
	 * Creates a new brush with the brightness adjusted by the given
	 * factor.
	 *
	 * @return	[out] the new brush, or the original brush if not possible
	 */
	/*abstract*/ public function adjustBrightness( factor : Float ) : Brush
	{
		Assert.pureVirtual();
		return null;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////
	// Constructors
	
	/*ctor*/ static public function empty() : Brush
	{
		return new BrushImpl( BrushType.None );
	}
	
	/*ctor*/ static public function solid( color : Color, ?alpha : Float ) : Brush
	{
		if( alpha == null )
			alpha = 1;
			
		return new BrushImpl( BrushType.Solid( color, alpha ) );
	}
	
	/**
	 * In flash clear brushes are needed to draw areas where the mouse can be
	 * accepted.
	 */
	/*ctor*/ static public function clear( ) : Brush
	{
		return new BrushImpl( BrushType.Solid( Color.rgb(0,0,0), 0 ) );
	}
	
#if !neko
	/*ctor*/ static public function bitmap( bd : BitmapData ) : Brush
	{
		return new BrushImpl( BrushType.Bitmap( bd ) );
	}
#end
	

	/*ctor*/ static public function parsed( decl : String ) : Brush
	{
		define(`TYPE_CHECK',`PARSE_CHECK($1,"Brush",decl)')
		var parts = decl.split( " " );
		TYPE_CHECK( parts.length > 0 )
		if( parts[0] == "None" )
			return Brush.empty();
		if( parts[0] == "Solid" )
		{
			TYPE_CHECK( parts.length >= 2 )
			//HAXE: the next three lines are to workaround a failure in haxe with Flash9 as the target
			var alpha : Null<Float> = null;
			if( parts.length > 2 )
				alpha = TypeConvert.asFloat( parts[2] );
			return Brush.solid(
				TypeConvert.asColor( parts[1] ),
				alpha
				);				
		}
		if( parts[0] == "Multi" )
			return MultiBrush.parse( decl );
		
		TYPE_CHECK( false )
		return null;
	}
	
	/**
	 * A more convenient way to create simple linear gradients.
	 *
	 * @param angle [in] the orientation of the gradient
	 * @param colors [in] the set of colors to use
	 * @param lengths [in] the length of each section (must be same length as colors).
	 *		The max length should be the size of the gradient you need, for example,
	 *		if you wish to fill a window of height 100, then 100 should be maximum.
	 *    May be one element longer than colors, in which case the last item is used
	 *		to determine the width, but not a gradient transition point.
	 * @param alphas [in] (Default=1) array of alphas for each section
	 * @param translate [in] (Default=none] offset the start point of the gradient, which is
	 *		usually 0,0 aligned in the sprite.
	 */
	static public function linearGradient( angle : Float, colors : Array<Color>, 
		lengths : Array<Dynamic>, ?alphas : Array<Float>, ?translate : mathx.Point2 )
	{
		var maxLen = lengths[lengths.length-1];
		
		var m = new Matrix();
		m.createGradientBox( maxLen, maxLen, angle );	//strange, one API that actually uses radians.
		if( translate != null )
			m.translate( translate.x, translate.y );
		
		var cints = new Array<UInt>();
		for( c in colors )
			cints.push( c.asInt() );
			
		var ratios = new Array<UInt>();
		for( i in 0...colors.length )
			ratios.push( Math.round( lengths[i] / maxLen * 255 ) );
			
		return gradient( GradientType.LINEAR, cints, alphas, ratios, m );
	}
	
#if as3
	/*ctor*/ static public function gradient( type : Dynamic, 
		colors : Array<UInt>, alphas : Array<Dynamic>, ratios : Array<Dynamic>, 
		?matrix : Matrix, 
		?spreadMethod : Dynamic,
		?interpolationMethod : Dynamic,
		?focalPointRatio : Float ) : Brush
#else
	/*ctor*/ static public function gradient( type : GradientType, 
		colors : Array<UInt>, alphas : Array<Dynamic>, ratios : Array<Dynamic>, 
		?matrix : Matrix, 
		?spreadMethod : Dynamic /*SpreadMethod*/, //HAXE: see StringObjectEnum.hx
		?interpolationMethod : Dynamic /*InterpolationMethod*/,
		?focalPointRatio : Float ) : Brush
#end
	{
		if( spreadMethod == null )
#if as3
			spreadMethod = "PAD";
#else
			//HAXE: this compiles to a VerifyError at the moment, I don't know a workaround!!!!
			spreadMethod = SpreadMethod.PAD;
#end

		if( interpolationMethod == null )
#if as3
			interpolationMethod = "`RGB'";
#else
			interpolationMethod = InterpolationMethod.`RGB';
#end
		if( focalPointRatio == null )
			focalPointRatio = 0;
			
		return new BrushImpl( BrushType.Gradient( untyped type, colors, alphas, ratios, matrix, untyped spreadMethod, untyped interpolationMethod, focalPointRatio ) );
	}
}


/**
 * Creates a logical brush which can be used for filling drawn objects.
 * The base class handles the basic cases but allows for a derived class
 * to be implemented.
 */
private class BrushImpl extends /*implements*/ Brush
{
	var type : BrushType;
	
	public function new( bt : BrushType )
	{
		type = bt;
	}
	
	/**
	 * Determines where there is a fill or not
	 */
	/*final*/ override public function isNonEmpty() : Bool
	{
		return type != None;
	}
	
	/**
	 * Applies the brush to the graphics object, will be used
	 * to draw the next objects
	 */
	/*virtual*/ override public function apply( graphics : Graphics )
	{
		switch( type )
		{
			case None:
				graphics.endFill();	//hmm, finished last elements, is that desired...?
			case Solid( color, alpha ):
				graphics.beginFill( color.asInt(), alpha );
#if !neko				
			case Bitmap( bd ):
				graphics.beginBitmapFill( bd );
#end				
			case Gradient( type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio ):
				graphics.beginGradientFill( untyped type, 
					colors, alphas, ratios, matrix, 
					untyped spreadMethod, 
					untyped interpolationMethod, 
					focalPointRatio );
			case Custom:
				Assert.pureVirtual();
		}
	}
	
	/*virtual*/ override public function toString() : String
	{
		switch( type )
		{
			case None:
				return "None";
			case Solid( color, alpha ):
				return "Solid " + color + " " + alpha;
#if !neko
			case Bitmap( bd ):
				return "Bitmap " + bd;	//who knows what that returns...
#end				
			case Gradient( type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio ):
				return "Gradient !!!";	//upsupported 
			case Custom:
				Assert.unimplemented();
				return null;
		}
#if as3		
		return null;
#end
	}
	
	/**
	 * Creates a new brush with the brightness adjusted by the given
	 * factor.
	 *
	 * @return	[out] the new brush, or the original brush if not possible
	 */
	/*virtual*/ override public function adjustBrightness( factor : Float ) : Brush
	{
		switch( type )
		{
			case None:
				return this;
			case Solid( color, alpha ):
				return Brush.solid( color.adjustBrightness( factor ), alpha );
#if !neko
			case Bitmap( bd ):
				return this;
#end
			case Gradient( type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio ):
				return this;	//could be done actually, simply apply to each color...
			case Custom:
				Assert.unimplemented();
				return null;
		}
#if as3		
		return null;
#end
	}
}