1+ /*
2+ Copyright 2016 pol2095. All Rights Reserved.
3+
4+ This program is free software. You can redistribute and/or modify it in
5+ accordance with the terms of the accompanying license agreement.
6+ */
7+ package feathers.extensions.canvas
8+ {
9+ import starling.display.Canvas ;
10+ import starling.display.Quad ;
11+ import starling.filters.FragmentFilter ;
12+ import starling.geom.Polygon ;
13+ import starling.utils.Align ;
14+ import starling.display.MeshBatch ;
15+ import starling.events.Event ;
16+
17+ /**
18+ * The Canvas supports basic vector drawing functionality.
19+ *
20+ * @see starling.display.Canvas
21+ */
22+ public class CanvasDisplayObject extends Canvas
23+ {
24+
25+ private var _thickness : Number = 1 ;
26+ private var _color : uint = 0 ;
27+ private var _alpha : Number = 1 ;
28+ private var _fromX : Number ;
29+ private var _fromY : Number ;
30+ private var _toX : Number ;
31+ private var _toY : Number ;
32+ private var line : Quad;
33+ /**
34+ * @private
35+ */
36+ public var batch : MeshBatch;
37+ private var fragmentFilter : FragmentFilter;
38+
39+ public function CanvasDisplayObject ()
40+ {
41+ super ();
42+ batch = new MeshBatch ()
43+ fragmentFilter = new FragmentFilter ();
44+ fragmentFilter. antiAliasing = 4 ;
45+ this . filter = fragmentFilter;
46+ }
47+
48+ /**
49+ * Specifies a line style used for subsequent calls to Graphics methods such as the lineTo().
50+ *
51+ * @param thickness An integer that indicates the thickness of the line in points
52+ *
53+ * @param color A hexadecimal color value of the line
54+ *
55+ * @param alpha A number that indicates the alpha value of the color of the line
56+ *
57+ */
58+ public function lineStyle (thickness :Number = 1 , color :uint = 0 , alpha :Number = 1 ):void
59+ {
60+ _thickness = thickness ;
61+ _color = color ;
62+ _alpha = alpha ;
63+ }
64+
65+ /**
66+ * Moves the current drawing position to (x, y).
67+ *
68+ * @param x A number that indicates the horizontal position relative to the registration point of the parent display object (in pixels).
69+ *
70+ * @param y A number that indicates the vertical position relative to the registration point of the parent display object (in pixels).
71+ *
72+ */
73+ public function moveTo (x :Number , y :Number ):void
74+ {
75+ _fromX = x ;
76+ _fromY = y ;
77+ }
78+
79+ /**
80+ * Draws a line using the current line style from the current drawing position to (x, y)
81+ *
82+ * @param x A number that indicates the horizontal position relative to the registration point of the parent display object (in pixels).
83+ *
84+ * @param y A number that indicates the vertical position relative to the registration point of the parent display object (in pixels).
85+ *
86+ */
87+ public function lineTo (x :Number , y :Number ):void
88+ {
89+ if ( this . getChildIndex ( batch ) == - 1 )
90+ {
91+ addChild ( batch );
92+ }
93+ {
94+ dispatchEvent (new Event ( Event . ADDED ));
95+ }
96+
97+ _toX = x ;
98+ _toY = y ;
99+
100+ line = new Quad (_thickness , _thickness , _color );
101+
102+ var fXOffset: Number = _toX - _fromX ;
103+ var fYOffset: Number = _toY - _fromY ;
104+ var len: Number = Math . sqrt (fXOffset * fXOffset + fYOffset * fYOffset);
105+ fXOffset = fXOffset * _thickness / (len * 2 );
106+ fYOffset = fYOffset * _thickness / (len * 2 );
107+
108+ line. setVertexPosition(2 , _fromX + fYOffset, _fromY - fXOffset);
109+ line. setVertexPosition(1 , _toX - fYOffset, _toY + fXOffset);
110+ line. setVertexPosition(0 , _toX + fYOffset, _toY - fXOffset);
111+ line. setVertexPosition(3 , _fromX - fYOffset, _fromY + fXOffset);
112+
113+ line. alpha = _alpha ;
114+
115+ batch. addMesh (line);
116+
117+ _fromX = x ;
118+ _fromY = y ;
119+ }
120+
121+ /**
122+ * @private
123+ */
124+ override public function clear ():void
125+ {
126+ batch. clear ();
127+ super . clear ();
128+ }
129+ }
130+ }
0 commit comments