Skip to content

Commit

Permalink
Merge branch 'v2.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
j3k0 committed Sep 13, 2021
2 parents fa446f6 + dff6420 commit 61c8277
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Why?

Why another extension, when there's already the [Starling Graphics Extension](https://github.com/StarlingGraphics/Starling-Extension-Graphics)?

Well, one draw-back of the Graphics extension is that it doesn't allow to `flatten` your sprites. Which will result
in poor performances, especially if you have complex static objects that could have been pre-rendered.
The original functionality of these classes was to avoid the draw-back of the Graphics extension not allowing you to `flatten` your sprites. Which resulted in poor performances, especially if you have complex static objects that could have been pre-rendered.

So in short, this extension has less features, but is compatible with `flatten`, which will help you write more efficient code.
In Starling 2 `flatten` has been removed. Starling developers have noted the `flatten` feature might come back in the future.

If all you need are Lines, Circles and custom Polygons, then do not look any further.


Shapes
------

Expand All @@ -28,7 +28,7 @@ Shapes
A Poly4 represents an abitrary 4-sided polygon with a uniform color or a color gradient.

```as3
Poly4(p1:Point, p2:Point, p3:Point, p4:Point, color:uint=0xffffff, premultipliedAlpha:Boolean=true)
Poly4(p1:Point, p2:Point, p3:Point, p4:Point, color:uint=0xffffff)
```

It inherits directly from starling.display.Quad, so all fancy coloring options of Quads can be used with Poly4.
Expand All @@ -41,7 +41,7 @@ A Ring represents a ring (what else?), see [this image](http://sugabetic.files.w

constructor:
```as3
Ring(innerRadius:Number, outerRadius:Number, color:uint=0xffffff, premultipliedAlpha:Boolean=true)
Ring(innerRadius:Number, outerRadius:Number, color:uint=0xffffff)
```

It's built using a set of polygons. Number of vertices is relative to the outer radius.
Expand All @@ -54,7 +54,7 @@ A Disk is like a ring but without a hole in the middle.

constructor:
```as3
Disk(radius:Number, color:uint=0xffffff, premultipliedAlpha:Boolean=true)
Disk(radius:Number, color:uint=0xffffff)
```

It's actually a ring with innerRadius set to 0.
Expand All @@ -67,7 +67,7 @@ A Line represents a segment with a thickness and uniform color or a color gradie

constructor:
```as3
Line(from:Point, to:Point, thickness:Number, color:uint, premultipliedAlpha:Boolean = true) {
Line(from:Point, to:Point, thickness:Number, color:uint)
```

It inherit from the Poly4 class which means you can setup per-vertex color.
Expand Down
86 changes: 71 additions & 15 deletions src/starling/display/Poly4.as
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
// =================================================================================================
package starling.display
{
import flash.geom.Matrix;
import flash.geom.Matrix3D;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Vector3D;

import starling.core.RenderSupport;
import starling.utils.VertexData;
import starling.display.Quad;
import starling.display.Mesh;
import starling.rendering.IndexData;
import starling.rendering.VertexData;
import starling.styles.MeshStyle;

/** A Poly4 represents an abitrary quad with a uniform color or a color gradient.
*
Expand All @@ -35,25 +35,73 @@ package starling.display
*
* @see Image
*/
public class Poly4 extends Quad
public class Poly4 extends Mesh
{
private var _bounds:Rectangle;
private var _lowerRight:Point;
private var _p1:Point;
private var _p2:Point;
private var _p3:Point;
private var _p4:Point;

// helper objects
private static var sPoint3D:Vector3D = new Vector3D();
private static var sMatrix:Matrix = new Matrix();
private static var sMatrix3D:Matrix3D = new Matrix3D();

public function Poly4(p1:Point, p2:Point, p3:Point, p4:Point, color:uint=0xffffff, premultipliedAlpha:Boolean=true)
{
_p1 = p1;
_p2 = p2;
_p3 = p3;
_p4 = p4;

var xmin:Number = Math.min(p1.x,p2.x,p3.x,p4.x);
var ymin:Number = Math.min(p1.y,p2.y,p3.y,p4.y);
var xmax:Number = Math.max(p1.x,p2.x,p3.x,p4.x);
var ymax:Number = Math.max(p1.y,p2.y,p3.y,p4.y);
super(xmax-xmin,ymax-ymin,color,premultipliedAlpha);
mVertexData.setPosition(0, p1.x - xmin, p1.y - ymin);
mVertexData.setPosition(1, p2.x - xmin, p2.y - ymin);
mVertexData.setPosition(2, p3.x - xmin, p3.y - ymin);
mVertexData.setPosition(3, p4.x - xmin, p4.y - ymin);
onVertexDataChanged();
x = xmin;
y = ymin;
_bounds = new Rectangle(0, 0, xmax - xmin, ymax - ymin);
_lowerRight = new Point(xmax - xmin, ymax - ymin);

var vertexData:VertexData = new VertexData(MeshStyle.VERTEX_FORMAT, 4);
var indexData:IndexData = new IndexData(6);
super(vertexData, indexData);

setupVertices();
this.color = color;
this.x = xmin;
this.y = ymin;
}

protected function setupVertices():void {
var posAttr:String = "position";
var texAttr:String = "texCoords";
var vertexData:VertexData = this.vertexData;
var indexData:IndexData = this.indexData;

indexData.numIndices = 0;
indexData.addQuad(0, 1, 2, 3);
if (vertexData.numVertices != 4) {
vertexData.numVertices = 4;
vertexData.trim();
}

var xmin:Number = Math.min(_p1.x,_p2.x,_p3.x,_p4.x);
var ymin:Number = Math.min(_p1.y,_p2.y,_p3.y,_p4.y);
var xmax:Number = Math.max(_p1.x,_p2.x,_p3.x,_p4.x);
var ymax:Number = Math.max(_p1.y,_p2.y,_p3.y,_p4.y);
var width:Number = xmax - xmin;
var height:Number = ymax - ymin;
_lowerRight = new Point(width, height);
vertexData.setPoint(0, posAttr, _p1.x - xmin, _p1.y - ymin);
vertexData.setPoint(1, posAttr, _p2.x - xmin, _p2.y - ymin);
vertexData.setPoint(2, posAttr, _p3.x - xmin, _p3.y - ymin);
vertexData.setPoint(3, posAttr, _p4.x - xmin, _p4.y - ymin);
vertexData.setPoint(0, texAttr, _p1.x / width, _p1.y / height);
vertexData.setPoint(1, texAttr, _p2.x / width, _p2.y / height);
vertexData.setPoint(2, texAttr, _p3.x / width, _p3.y / height);
vertexData.setPoint(3, texAttr, _p4.x / width, _p4.y / height);
setRequiresRedraw();
}

public override function getBounds(targetSpace:DisplayObject, resultRect:Rectangle=null):Rectangle
Expand All @@ -79,5 +127,13 @@ package starling.display

return resultRect;
}

/** @inheritDoc */
override public function hitTest(localPoint:Point):DisplayObject
{
if (!visible || !touchable || !hitTestMask(localPoint)) return null;
else if (_bounds.containsPoint(localPoint)) return this;
else return null;
}
}
}
}
19 changes: 7 additions & 12 deletions src/starling/display/Ring.as
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@
// =================================================================================================
package starling.display
{
import flash.geom.Matrix;
import flash.geom.Matrix3D;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Vector3D;

import starling.core.RenderSupport;
import starling.utils.VertexData;
import starling.display.Sprite;
import starling.display.Quad;

/** A Disk represents a circle filled with a uniform color. */
/** A Disk represents a circle filled with a uniform color.
*
* TODO: Reimplement using Mesh */
public class Ring extends Sprite
{
private var _innerRadius:Number;
Expand Down Expand Up @@ -70,9 +64,10 @@ package starling.display
}
}

public override function hitTest(localPoint:Point, forTouch:Boolean = false):DisplayObject {
// on a touch test, invisible or untouchable objects cause the test to fail
if (forTouch && (!visible || !touchable)) return null;
/** @inheritDoc */
override public function hitTest(localPoint:Point):DisplayObject
{
if (!visible || !touchable || !hitTestMask(localPoint)) return null;
var vx:Number = localPoint.x - _outerRadius;
var vy:Number = localPoint.y - _outerRadius;
var l2:Number = vx*vx + vy*vy;
Expand Down

0 comments on commit 61c8277

Please sign in to comment.