-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement FlxSprite.skew
#3241
base: dev
Are you sure you want to change the base?
Implement FlxSprite.skew
#3241
Conversation
test project: SkewedSpriteTest.zip |
I wonder if we should just add a matrix field rather than just skew, and then add a skew method to FlxMatrix |
This seems like the ideal solution |
figured it out edit:
it looks like the matrix skewing have to be called every time drawComplex is, i'm not sure how i would work around that without an extra field for skew |
Here's an example of what I'm suggesting. they are implemented in extending classes, but I'm imagining adding these fields to package states;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.math.FlxAngle;
import flixel.math.FlxMatrix;
import flixel.text.FlxText;
class SpriteMatrixTestState extends flixel.FlxState
{
final sprite = new MatrixSprite("assets/images/haxe.png");
final skewedSprite = new flixel.addons.effects.FlxSkewedSprite("assets/images/haxe.png");
var scaleX = 1.0;
var scaleY = 1.0;
var skewX = 0.0;
var skewY = 0.0;
var tX = 0.0;
var tY = 0.0;
public function new()
{
super();
sprite.screenCenter();
sprite.x -= sprite.width;
add(sprite);
skewedSprite.screenCenter();
skewedSprite.x += sprite.width;
add(skewedSprite);
final text = new FlxText("MatrixSprite", 16);
text.x = sprite.x;
text.y = sprite.y + sprite.height * 1.5;
add(text);
final text = new FlxText("FlxSkewedSprite", 16);
text.x = skewedSprite.x;
text.y = skewedSprite.y + skewedSprite.height * 1.5;
add(text);
FlxG.watch.addFunction("scale", ()->'( x: $scaleX | y: $scaleY )');
FlxG.watch.addFunction("skew", ()->'( x: $skewX | y: $skewY )');
FlxG.watch.addFunction("t", ()->'( x: $tX | y: $tY )');
FlxG.log.notice
( "ARROWS: Translate\n"
+ "ARROWS + SHIFT: Scale\n"
+ "ARROWS + ALT: Skew"
);
FlxG.debugger.visible = true;
}
override function update(elapsed:Float)
{
super.update(elapsed);
final L = FlxG.keys.pressed.LEFT;
final R = FlxG.keys.pressed.RIGHT;
final U = FlxG.keys.pressed.UP;
final D = FlxG.keys.pressed.DOWN;
if (FlxG.keys.pressed.SHIFT)
{
if (L) scaleX -= 0.01;
if (R) scaleX += 0.01;
if (U) scaleY += 0.01;
if (D) scaleY -= 0.01;
}
else if (FlxG.keys.pressed.ALT)
{
if (L) skewX -= 0.01;
if (R) skewX += 0.01;
if (U) skewY -= 0.01;
if (D) skewY += 0.01;
}
else
{
if (L) tX -= 1;
if (R) tX += 1;
if (U) tY -= 1;
if (D) tY += 1;
}
sprite.transform.identity();
sprite.transform.skewRadians(skewX * Math.PI / 2, skewY * Math.PI / 2);
sprite.transform.scale(scaleX, scaleY);
sprite.transform.translate(tX, tY);
skewedSprite.skew.set(skewX * 90, skewY * 90);
}
}
class SkewMatrix extends FlxMatrix
{
public inline function isIdentity()
{
return equals(openfl.geom.Matrix.__identity);
}
public function skewRadians(skewX:Float, skewY:Float)
{
b = Math.tan(skewY);
c = Math.tan(skewX);
}
public inline function skewDegrees(skewX:Float, skewY:Float)
{
skewRadians(skewY * FlxAngle.TO_RAD, skewX * FlxAngle.TO_RAD);
}
}
class MatrixSprite extends flixel.FlxSprite
{
public var transform:SkewMatrix = new SkewMatrix();
override function isSimpleRenderBlit(?camera:FlxCamera):Bool
{
return transform.isIdentity() || super.isSimpleRenderBlit(camera);
}
override function drawComplex(camera:FlxCamera)
{
_frame.prepareMatrix(_matrix, ANGLE_0, checkFlipX(), checkFlipY());
_matrix.translate(-origin.x, -origin.y);
_matrix.scale(scale.x, scale.y);
if (bakedRotationAngle <= 0)
{
updateTrig();
if (angle != 0)
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
}
_matrix.concat(transform);
getScreenPosition(_point, camera).subtractPoint(offset);
_point.add(origin.x, origin.y);
_matrix.translate(_point.x, _point.y);
if (isPixelPerfectRender(camera))
{
_matrix.tx = Math.floor(_matrix.tx);
_matrix.ty = Math.floor(_matrix.ty);
}
camera.drawPixels(_frame, framePixels, _matrix, colorTransform, blend, antialiasing, shader);
}
} |
👍 |
CI is still failing, on flash it can't find |
Also, one thing I forgot to ask, why is it that you want this new feature in FlxSprite, instead of just using FlxSkewedSprite? |
FlxSkewedSprite was the only class i used from flixel-addons, and it felt weird to ship a project of mine with an extra dependency for one class |
Is this something you've needed?
Based on what? Anecdotally, I really never see people requesting this. The reason I'm hesitant about this is that FlxSprite is already pretty bloated, I wanna transition to a more compositional system for flixel, rather than adding every feature that someone might need into FlxBasic, FlxObject and FlxSprite. Right now it's pretty easy to add this feature to a FlxSprite extending class, rather than adding extra memory to every FlxSprite for something that: A. Doesn't seem widely used and B. Can be implemented in a few different ways (We've discussed 2 valid solutions but I've considered others, namely a third that I would like in my projects, but I don't think others would like). In the end, we need to consider what problem a change like this fixes, and the only problems I see are:
1 seems like a non-issue and 2 is trivial, especially since there's already an example implementation to copy. Thoughts? |
yes if it is too much trouble to merge here, i can result to the extra extension |
you mean the "example implementation to copy" i mentioned? that example is FlxSkewedSprite
"bloat" doesn't mean "features that serve no purpose", it can mean a lot of ad-hoc solutions that most people don't need, making it harder to implement your own features or different implementations of existing features. This is why moving to composition style system can be really beneficial, such a system would allow you to make a skew component that can be attached to either a FlxSprite or a FlxText or what have you |
i meant what features in FlxSprite make it difficult to add new functionality (and where you might need to add newadjust said functionality) im not too familiar with the concept of composition based programming, ill look into it more (Edit from GK, i accidentally edited your post instead of replying to it, hopefully I've reverted that edit) |
Similarly to how having a |
Closes: #3238
Implementation of the
skew
property fromflixel.addons.effects.FlxSkewedSprite
.This implementation is missing
matrixExposed
andtransformationMatrix
. I can't really see a use case for these variables, nor have I seen them used in any project.If this is merged:
FlxSkewedSprite
, and remove some conflicting fields.FlxSkewedSprite
demo, or adjust it to useFlxSprite.skew
.