Skip to content

Commit d844cb0

Browse files
committed
Feathers-Extension-Canvas
1 parent 4760e97 commit d844cb0

File tree

7 files changed

+337
-0
lines changed

7 files changed

+337
-0
lines changed
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Simplified BSD License
2+
======================
3+
4+
Copyright 2016 pol2095. All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
The views and conclusions contained in the software and documentation are those
28+
of the authors and should not be interpreted as representing official policies,
29+
either expressed or implied, of the copyright holders.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Feathers Extension Canvas 0.9.0
2+
3+
Say hello to [Feathers Extension Canvas](https://github.com/pol2095/Feathers-Extension-Canvas/).
4+
This project add a Canvas control to Feathers UI, a library of light-weight, skinnable, and extensible UI controls for mobile and desktop. The components run on Starling Framework and the Adobe Flash runtimes, offering blazing fast GPU powered graphics to create a smooth and responsive experience. Build completely standalone, native applications on iOS, Android, Windows, and Mac OS X, or target Adobe Flash Player in desktop browsers.<br />
5+
The Canvas supports basic vector drawing functionality.
6+
7+
## Quick Links
8+
9+
* [Website](http://pol2095.free.fr/Starling-Feathers-Extensions/)
10+
* [API Reference](http://pol2095.free.fr/Starling-Feathers-Extensions/docs/feathers/extensions/canvas/package-detail.html)
11+
* [Discussion Forum](http://forum.starling-framework.org/forum/feathers/)
12+
* [Github Project](https://github.com/pol2095/Feathers-Extension-Canvas/)
13+
14+
## Minimum Requirements
15+
16+
* [Adobe AIR 23.0](https://get.adobe.com/air/) or [Adobe Flash Player 19.0](https://get.adobe.com/fr/flashplayer/)
17+
* [Starling Framework 2.1](https://github.com/Gamua/Starling-Framework)
18+
* [Feathers Framework 3.1](https://feathersui.com/download/)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<f:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
2+
xmlns:f="library://ns.feathersui.com/mxml"
3+
theme="feathers.themes.MetalWorksDesktopTheme"
4+
creationComplete="creationCompleteHandler(event)"
5+
xmlns:components="feathers.extensions.canvas.*">
6+
7+
<f:layout>
8+
<f:VerticalLayout/>
9+
</f:layout>
10+
<f:Button label="lineTo" triggered="button_triggeredHandler(event)"/>
11+
<components:Canvas id="canvas"/>
12+
<f:Button label="clear" triggered="button2_triggeredHandler(event)"/>
13+
<f:Button label="drawCircle" triggered="button3_triggeredHandler(event)"/>
14+
<f:Button label="drawRectangle" triggered="button4_triggeredHandler(event)"/>
15+
16+
<fx:Script>
17+
<![CDATA[
18+
private function creationCompleteHandler( event:Event ):void
19+
{
20+
canvas.graphics.lineStyle(1, 0xFF0000);
21+
canvas.graphics.moveTo(20, 20);
22+
canvas.graphics.lineTo(100, 100);
23+
canvas.graphics.lineTo(200, 100);
24+
}
25+
private function button_triggeredHandler( event:Event ):void
26+
{
27+
canvas.graphics.lineTo(200, 200);
28+
}
29+
private function button2_triggeredHandler( event:Event ):void
30+
{
31+
canvas.graphics.clear();
32+
canvas.graphics.moveTo(20, 20);
33+
}
34+
private function button3_triggeredHandler( event:Event ):void
35+
{
36+
canvas.graphics.beginFill(0x00FF00);
37+
canvas.graphics.drawCircle(50, 50, 20);
38+
canvas.graphics.endFill();
39+
}
40+
private function button4_triggeredHandler( event:Event ):void
41+
{
42+
canvas.graphics.beginFill(0x0000FF);
43+
canvas.graphics.drawRectangle(50, 80, 40, 20);
44+
canvas.graphics.endFill();
45+
}
46+
]]>
47+
</fx:Script>
48+
</f:Application>

0 commit comments

Comments
 (0)