Skip to content

Commit

Permalink
added 'Mesh.defaultStyleFactory' (closes #878)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Jun 20, 2016
1 parent 37e87f7 commit 2585fa2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
46 changes: 43 additions & 3 deletions starling/src/starling/display/Mesh.as
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package starling.display
import starling.textures.Texture;
import starling.utils.MatrixUtil;
import starling.utils.MeshUtil;
import starling.utils.execute;

use namespace starling_internal;

Expand Down Expand Up @@ -50,6 +51,7 @@ package starling.display
/** @private */ internal var _pixelSnapping:Boolean;

private static var sDefaultStyle:Class = MeshStyle;
private static var sDefaultStyleFactory:Function = null;

/** Creates a new mesh with the given vertices and indices.
* If you don't pass a style, an instance of <code>MeshStyle</code> will be created
Expand Down Expand Up @@ -105,14 +107,16 @@ package starling.display
* Do not use the same style instance on multiple objects! Instead, make use of
* <code>style.clone()</code> to assign an identical style to multiple meshes.</p>
*
* @param meshStyle the style to assign. If <code>null</code>, an instance of
* a standard <code>MeshStyle</code> will be created.
* @param meshStyle the style to assign. If <code>null</code>, the default
* style will be created.
* @param mergeWithPredecessor if enabled, all attributes of the previous style will be
* be copied to the new one, if possible.
* @see #defaultStyle
* @see #defaultStyleFactory
*/
public function setStyle(meshStyle:MeshStyle=null, mergeWithPredecessor:Boolean=true):void
{
if (meshStyle == null) meshStyle = new sDefaultStyle() as MeshStyle;
if (meshStyle == null) meshStyle = createDefaultMeshStyle();
else if (meshStyle == _style) return;
else if (meshStyle.target) meshStyle.target.setStyle();

Expand All @@ -126,6 +130,22 @@ package starling.display
_style.setTarget(this, _vertexData, _indexData);
}

private function createDefaultMeshStyle():MeshStyle
{
var meshStyle:MeshStyle;

if (sDefaultStyleFactory != null)
{
if (sDefaultStyleFactory.length == 0) meshStyle = sDefaultStyleFactory();
else meshStyle = sDefaultStyleFactory(this);
}

if (meshStyle == null)
meshStyle = new sDefaultStyle() as MeshStyle;

return meshStyle;
}

// vertex manipulation

/** The position of the vertex at the specified index, in the mesh's local coordinate
Expand Down Expand Up @@ -256,5 +276,25 @@ package starling.display
{
sDefaultStyle = value;
}

/** A factory method that is used to create the 'MeshStyle' for a mesh if no specific
* style is provided. That's useful if you are creating a hierarchy of objects, all
* of which need to have a certain style. Different to the <code>defaultStyle</code>
* property, this method allows plugging in custom logic and passing arguments to the
* constructor. Return <code>null</code> to fall back to the default behavior (i.e.
* to instantiate <code>defaultStyle</code>). The <code>mesh</code>-parameter is optional
* and may be omitted.
*
* <listing>
* Mesh.defaultStyleFactory = function(mesh:Mesh):MeshStyle
* {
* return new ColorizeMeshStyle(Math.random() * 0xffffff);
* }</listing>
*/
public static function get defaultStyleFactory():Function { return sDefaultStyleFactory; }
public static function set defaultStyleFactory(value:Function):void
{
sDefaultStyleFactory = value;
}
}
}
44 changes: 44 additions & 0 deletions tests/src/tests/rendering/MeshStyleTest.as
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ package tests.rendering
import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertNull;
import org.flexunit.asserts.assertTrue;

import starling.display.Mesh;
import starling.display.Quad;
import starling.events.EnterFrameEvent;
import starling.events.Event;
Expand Down Expand Up @@ -83,5 +85,47 @@ package tests.rendering
++eventCount;
}
}

[Test]
public function testDefaultStyle():void
{
var origStyle:Class = Mesh.defaultStyle;
var quad:Quad = new Quad(100, 100);
assertTrue(quad.style is origStyle);

Mesh.defaultStyle = MockStyle;

quad = new Quad(100, 100);
assertTrue(quad.style is MockStyle);

Mesh.defaultStyle = origStyle;
}

[Test]
public function testDefaultStyleFactory():void
{
var quad:Quad;
var origStyle:Class = Mesh.defaultStyle;
var origStyleFactory:Function = Mesh.defaultStyleFactory;

Mesh.defaultStyleFactory = function(mesh:Mesh):MeshStyle { return new MockStyle(); };
quad = new Quad(100, 100);
assertTrue(quad.style is MockStyle);

Mesh.defaultStyleFactory = function():MeshStyle { return null; };
quad = new Quad(100, 100);
assertTrue(quad.style is origStyle);

Mesh.defaultStyleFactory = null;
quad = new Quad(100, 100);
assertTrue(quad.style is origStyle);

Mesh.defaultStyle = origStyle;
Mesh.defaultStyleFactory = origStyleFactory;
}
}
}

import starling.styles.MeshStyle;

class MockStyle extends MeshStyle { }

0 comments on commit 2585fa2

Please sign in to comment.