Skip to content
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

Fix angle, scale and origin on FlxStrip #2973

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,25 @@ class FlxCamera extends FlxBasic
}
}

// backwards compatibility for FlxClothSprite
#if flash
public function drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>,
?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void
{
drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, position, 0, null, null, blend, repeat, smoothing, transform, shader);
}
#else
public function drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>,
?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false,
?transform:ColorTransform, ?shader:FlxShader):Void
{
drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, position, angle, scale, origin, blend, repeat, smoothing, transform, shader);
}
#end

public function drawTrianglesAdvanced(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>,
?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false,
?transform:ColorTransform, ?shader:FlxShader):Void
{
if (FlxG.renderBlit)
{
Expand Down Expand Up @@ -932,13 +949,13 @@ class FlxCamera extends FlxBasic
var isColored:Bool = (colors != null && colors.length != 0);

#if !flash
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
final hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
isColored = isColored || (transform != null && transform.hasRGBMultipliers());
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds, transform);
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds, transform);
#else
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds);
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTrianglesAdvanced(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds);
#end
}
}
Expand Down
5 changes: 3 additions & 2 deletions flixel/FlxStrip.hx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class FlxStrip extends FlxSprite

getScreenPosition(_point, camera).subtractPoint(offset);
#if !flash
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing, colorTransform, shader);
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, colorTransform,
shader);
#else
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing);
camera.drawTrianglesAdvanced(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, null);
#end
}
}
Expand Down
52 changes: 48 additions & 4 deletions flixel/graphics/tile/FlxDrawTrianglesItem.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package flixel.graphics.tile;

import flixel.math.FlxAngle;
import flixel.FlxCamera;
import flixel.graphics.frames.FlxFrame;
import flixel.graphics.tile.FlxDrawBaseItem.FlxDrawItemType;
import flixel.math.FlxMatrix;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.math.FlxMath;
import flixel.system.FlxAssets.FlxShader;
import flixel.util.FlxColor;
import openfl.display.Graphics;
Expand All @@ -21,6 +23,8 @@ typedef DrawData<T> = #if (flash || openfl >= "4.0.0") openfl.Vector<T> #else Ar
class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
{
static var point:FlxPoint = FlxPoint.get();
static var size:FlxPoint = FlxPoint.get();
static var origin:FlxPoint = FlxPoint.get();
static var rect:FlxRect = FlxRect.get();

#if !flash
Expand Down Expand Up @@ -136,18 +140,38 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
#end
}

#if flash
public function addTriangles(vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint,
?cameraBounds:FlxRect #if !flash , ?transform:ColorTransform #end):Void
?cameraBounds:FlxRect):Void
{
addTrianglesAdvanced(vertices, indices, uvtData, colors, position, 0, null, null, cameraBounds, null);
}
#else
public function addTriangles(vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint,
angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, ?cameraBounds:FlxRect, ?transform:ColorTransform):Void
{
addTrianglesAdvanced(vertices, indices, uvtData, colors, position, angle, scale, originPoint, cameraBounds, transform);
}
#end

public function addTrianglesAdvanced(vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint,
angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint, ?cameraBounds:FlxRect, ?transform:ColorTransform):Void
{
if (position == null)
position = point.set();

if (size == null)
scale = size.set(1, 1);

if (originPoint == null)
originPoint = origin.set();

if (cameraBounds == null)
cameraBounds = rect.set(0, 0, FlxG.width, FlxG.height);

var verticesLength:Int = vertices.length;
var prevVerticesLength:Int = this.vertices.length;
var numberOfVertices:Int = Std.int(verticesLength / 2);
var numberOfVertices:Int = verticesLength >> 1;
var prevIndicesLength:Int = this.indices.length;
var prevUVTDataLength:Int = this.uvtData.length;
var prevColorsLength:Int = this.colors.length;
Expand All @@ -157,10 +181,30 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
var i:Int = 0;
var currentVertexPosition:Int = prevVerticesLength;

var cos:Float = 1.0;
var sin:Float = 0.0;
if (angle != 0)
{
cos = FlxMath.fastCos(angle * FlxAngle.TO_RAD);
sin = FlxMath.fastSin(angle * FlxAngle.TO_RAD);
}

while (i < verticesLength)
{
tempX = position.x + vertices[i];
tempY = position.y + vertices[i + 1];
var vertX:Float = (vertices[i] * scale.x) - originPoint.x;
var vertY:Float = (vertices[i + 1] * scale.y) - originPoint.y;

if (angle != 0)
{
final vx:Float = vertX;
final vy:Float = vertY;

vertX = (vx * cos) + (vy * -sin);
vertY = (vx * sin) + (vy * cos);
}

tempX = position.x + vertX;
tempY = position.y + vertY;

this.vertices[currentVertexPosition++] = tempX;
this.vertices[currentVertexPosition++] = tempY;
Expand Down
Loading