Skip to content

Commit 1351cf5

Browse files
committed
Feathers-Extension-Canvas-Starling-2.1
1 parent d844cb0 commit 1351cf5

File tree

6 files changed

+1127
-0
lines changed

6 files changed

+1127
-0
lines changed
351 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 feathers.controls.ScrollContainer;
10+
11+
/**
12+
* The Canvas supports basic vector drawing functionality.
13+
*
14+
* @see feathers.extensions.canvas.CanvasDisplayObject
15+
*/
16+
public class Canvas extends ScrollContainer
17+
{
18+
private var canvas:CanvasControl = new CanvasControl();
19+
20+
public function Canvas()
21+
{
22+
super();
23+
this.addChild( canvas );
24+
}
25+
26+
/**
27+
* The display object that contains the drawing methods.
28+
*/
29+
public function get graphics():CanvasDisplayObject
30+
{
31+
return canvas.graphics;
32+
}
33+
}
34+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package feathers.extensions.canvas
2+
{
3+
import feathers.core.FeathersControl;
4+
import starling.events.Event;
5+
6+
/**
7+
* The Canvas supports basic vector drawing functionality.
8+
*
9+
* @see feathers.extensions.canvas.CanvasDisplayObject
10+
*/
11+
public class CanvasControl extends FeathersControl
12+
{
13+
private var canvas:CanvasDisplayObject = new CanvasDisplayObject();
14+
15+
public function CanvasControl()
16+
{
17+
super();
18+
canvas.addEventListener(Event.ADDED, invalidateHandler);
19+
canvas.addEventListener(Event.REMOVED, invalidateHandler);
20+
this.addChild( canvas );
21+
}
22+
23+
private function invalidateHandler():void
24+
{
25+
this.invalidate(INVALIDATION_FLAG_LAYOUT);
26+
}
27+
28+
/**
29+
* The display object that contains the drawing methods.
30+
*/
31+
public function get graphics():CanvasDisplayObject
32+
{
33+
return canvas;
34+
}
35+
/**
36+
* @private
37+
*/
38+
override protected function draw():void
39+
{
40+
this.autoSizeIfNeeded();
41+
}
42+
/**
43+
* @private
44+
*/
45+
protected function autoSizeIfNeeded():Boolean
46+
{
47+
var needsWidth:Boolean = isNaN(this.explicitWidth);
48+
var needsHeight:Boolean = isNaN(this.explicitHeight);
49+
if(!needsWidth && !needsHeight)
50+
{
51+
return false;
52+
}
53+
54+
var newWidth:Number = this.explicitWidth;
55+
if(needsWidth)
56+
{
57+
newWidth = this.graphics.bounds.x + this.graphics.bounds.width;
58+
}
59+
var newHeight:Number = this.explicitHeight;
60+
if(needsHeight)
61+
{
62+
newHeight = this.graphics.bounds.y + this.graphics.bounds.height;
63+
}
64+
65+
return this.setSizeInternal(newWidth, newHeight, false);
66+
}
67+
68+
/**
69+
* @private
70+
*/
71+
override public function dispose():void
72+
{
73+
canvas.removeEventListener(Event.ADDED, invalidateHandler);
74+
canvas.removeEventListener(Event.REMOVED, invalidateHandler);
75+
super.dispose();
76+
}
77+
}
78+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)