~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/draw/Pen.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 draw;
 
7
 
 
8
#if neash
 
9
import neash.display.Graphics;
 
10
#elseif flash
 
11
import flash.display.Graphics;
 
12
#end
 
13
 
 
14
enum PenType
 
15
{
 
16
        None;
 
17
        Solid( color : Color, alpha : Float );
 
18
}
 
19
 
 
20
/**
 
21
 * Creates a logical pen which can be used for outlined drawn objects
 
22
 */
 
23
class Pen implements draw.GraphicsImplement
 
24
{
 
25
        var type : PenType;
 
26
        var width : Float;
 
27
        
 
28
        private function new( width : Float, pt : PenType )
 
29
        {
 
30
                type = pt;
 
31
                this.width = width;
 
32
        }
 
33
        
 
34
        /*final*/ public function isNonEmpty() : Bool
 
35
        {
 
36
                return type != None;
 
37
        }
 
38
        
 
39
        /*virtual*/ public function apply( graphics : Graphics )
 
40
        {
 
41
                switch( type )
 
42
                {
 
43
                        case None:
 
44
#if neash
 
45
                                //NEASH: quirk, how to reset line-style
 
46
#elseif flash
 
47
                                graphics.lineStyle();
 
48
#end
 
49
                        case Solid( color, alpha ):
 
50
                                graphics.lineStyle( width, color.asInt(), alpha );
 
51
                }
 
52
        }
 
53
        
 
54
        /*ctor*/ static public function empty() : Pen
 
55
        {
 
56
                return new Pen( 0, PenType.None );
 
57
        }
 
58
        
 
59
        /*ctor*/ static public function solid( width : Float, color : Color, ?alpha : Null<Float> ) : Pen
 
60
        {
 
61
                if( alpha == null )
 
62
                        alpha = 1;
 
63
                        
 
64
                return new Pen( width, PenType.Solid( color, alpha ) );
 
65
        }
 
66
        
 
67
        /*virtual*/ public function toString()
 
68
        {
 
69
                switch( type )
 
70
                {
 
71
                        case None:
 
72
                                return "None";
 
73
                        case Solid( color, alpha ):
 
74
                                return "Solid " + width + " " + color + " " + alpha;
 
75
                }
 
76
#if as3         
 
77
                return null;
 
78
#end
 
79
        }
 
80
        
 
81
        /*ctor*/ static public function parsed( decl : String ) : Pen 
 
82
        {
 
83
                define(`TYPE_CHECK',`PARSE_CHECK($1,"Pen",decl)')
 
84
                var parts = decl.split( " " );
 
85
                TYPE_CHECK( parts.length > 0 )
 
86
                if( parts[0] == "None" )
 
87
                        return Pen.empty();
 
88
                if( parts[0] == "Solid" )
 
89
                {
 
90
                        TYPE_CHECK( parts.length > 2 )
 
91
                        //the next three lines are to workaround a failure in haxe with Flash9 as the target
 
92
                        var alpha : Null<Float> = null;
 
93
                        if( parts.length > 3 )
 
94
                                alpha = TypeConvert.asFloat( parts[3] );
 
95
                        return Pen.solid(
 
96
                                TypeConvert.asFloat( parts[1] ),
 
97
                                TypeConvert.asColor( parts[2] ),
 
98
                                alpha
 
99
                                );
 
100
                }
 
101
                
 
102
                TYPE_CHECK( false )
 
103
                return null;
 
104
        }
 
105
}