Starling-Framework
Starling-Framework copied to clipboard
Allow a Quad with a width/height of 0 (i.e. not throw an Error)
I'm not sure how much work this is, or even if it's feasible, but if you don't ask you don't get :)
We use a lot of quads in our game to store as bgs etc, but sometimes, due to size errors (e.g. you use a quad as a bg for a Feathers Label, but when you create it, it might not be properly validated yet, so the width/height returns 0), it's created with a width and/or height of 0, resulting in a runtime error.
From what I can tell, Starling already supports display objects with no visible area, so would it be possible to add a Quad to this, if either the width or height is 0? It saves a lot of boilerplate if( width == 0 ) width = 1 code checks everywhere (assuming that they are everywhere)
This was recently implemented as part of a fix for the following reported issue: https://github.com/Gamua/Starling-Framework/issues/487
Mhm, both alternatives had their issues as well.
Simply allowing zero, like in Starling 1.4, prevents the quad from ever being used; you can't scale anything with a zero size. And using a very small value could lead to visible quads when it's scaled up extremely (and is not very clean, either).
Thus, we decided to go for the strict route here, and throw an exception. That said: I'm willing to discuss this, if the exception causes lots of problems!
If having a zero size Quad gives no problem other than "prevents the quad from ever being used", then we could just go the route of initialising the quad when the width/height is set and it's currently 0. Something like:
private var mWidth:Number = 0.0;
private var mHeight:Number = 0.0;
private var mColor:uint = 0;
override public function get width():Number { return mWidth; }
override public function set width( n:Number ):void
{
var old:Number = mWidth;
mWidth = n;
if( old == 0.0 )
{
if( mWidth > 0.0 && mHeight > 0.0 )
_createQuad();
}
else
super.width = mWidth;
}
override public function get height():Number { return mHeight; }
override public function set height( n:Number ):void
{
var old:Number = mHeight;
mHeight = n;
if( old == 0.0 )
{
if( mWidth > 0.0 && mHeight > 0.0 )
_createQuad();
}
else
super.height = mHeight;
}
public function Quad(width:Number, height:Number, color:uint=0xffffff,
premultipliedAlpha:Boolean=true)
{
mColor = color;
mTinted = mColor != 0xffffff;
this.width = width; // NOTE: using setter
this.height = height; // NOTE: using setter
}
private function _createQuad():void
{
mVertexData = new VertexData(4, premultipliedAlpha);
mVertexData.setPosition(0, 0.0, 0.0);
mVertexData.setPosition(1, mWidth, 0.0);
mVertexData.setPosition(2, 0.0, mHeight);
mVertexData.setPosition(3, mWidth, mHeight);
mVertexData.setUniformColor(mColor);
onVertexDataChanged();
}
The reverse could also apply; if the width and height is set to 0, then clear the vertex data
Yeah, that would also be a possibility, thanks a lot for the suggestion! I'll think about this again when there's a little time. I can understand that this would be helpful, of course.
I've run into issues with this as well, and can't seem to trace the culprit that's triggering this error. Seems like it's happening in the buildArmature function of DragonBones StarlingFactory.
For now, I've just commented out the thrown error. I'd recommend maybe tracing out a warning instead of an actual error, or even adding a starling.strictMode = true/false to toggle it.