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

add world-space FlxCamera rotation #3245

Closed
wants to merge 1 commit into from
Closed
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
84 changes: 74 additions & 10 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ private typedef FlxDrawItem = flixel.graphics.tile.FlxDrawQuadsItem;
* |-> `_scrollRect:Sprite` (which is used for cropping camera's graphic, mostly in tile render mode)
* |-> `_flashBitmap:Bitmap` (its bitmapData property is buffer BitmapData, this var is used in blit render mode.
* Everything is rendered on buffer in blit render mode)
* |-> `canvas:Sprite` (its graphics is used for rendering objects in tile render mode)
* |-> `debugLayer:Sprite` (this sprite is used in tile render mode for rendering debug info, like bounding boxes)
* |-> `_rotationCanvas:Sprite` (used for rendering rotated camera in tile render mode)
* |-> `canvas:Sprite` (its graphics is used for rendering objects in tile render mode)
* |-> `debugLayer:Sprite` (this sprite is used in tile render mode for rendering debug info, like bounding boxes)
*/
class FlxCamera extends FlxBasic
{
Expand Down Expand Up @@ -362,6 +363,11 @@ class FlxCamera extends FlxBasic
*/
public var angle(default, set):Float = 0;

/**
* The camera's world rotation in degrees.
*/
public var rotation(default, set):Float = 0;

/**
* The color tint of the camera display.
*/
Expand Down Expand Up @@ -531,16 +537,24 @@ class FlxCamera extends FlxBasic
/**
* Sprite used for actual rendering in tile render mode (instead of `_flashBitmap` for blitting).
* Its graphics is used as a drawing surface for `drawTriangles()` and `drawTiles()` methods.
* It is a child of `_scrollRect` `Sprite` (which trims graphics that should be invisible).
* It is a child of `_rotationCanvas` `Sprite` (which itself is a child of `scrollRect` `Sprite`, which trims graphics
* that should be invisible).
* Its position is modified by `updateInternalSpritePositions()`, which is called on camera's resize and scale events.
*/
public var canvas:Sprite;

/**
* Internal, used for rendering rotated camera in tile render mode.
* It is a child of `scrollRect` `Sprite` (which trims graphics that should be invisible).
*/
var _rotationCanvas:Sprite;

#if FLX_DEBUG
/**
* Sprite for visual effects (flash and fade) and drawDebug information
* (bounding boxes are drawn on it) for tile render mode.
* It is a child of `_scrollRect` `Sprite` (which trims graphics that should be invisible).
* It is a child of `_rotationCanvas` `Sprite` (which itself is a child of `scrollRect` `Sprite`, which trims graphics
* that should be invisible).
* Its position is modified by `updateInternalSpritePositions()`, which is called on camera's resize and scale events.
*/
public var debugLayer:Sprite;
Expand Down Expand Up @@ -1060,12 +1074,15 @@ class FlxCamera extends FlxBasic
}
else
{
_rotationCanvas = new Sprite();
_scrollRect.addChild(_rotationCanvas);

canvas = new Sprite();
_scrollRect.addChild(canvas);
_rotationCanvas.addChild(canvas);

#if FLX_DEBUG
debugLayer = new Sprite();
_scrollRect.addChild(debugLayer);
_rotationCanvas.addChild(debugLayer);
#end
}

Expand Down Expand Up @@ -1099,12 +1116,14 @@ class FlxCamera extends FlxBasic
}
else
{
FlxDestroyUtil.removeChild(_scrollRect, _rotationCanvas);

#if FLX_DEBUG
FlxDestroyUtil.removeChild(_scrollRect, debugLayer);
FlxDestroyUtil.removeChild(_rotationCanvas, debugLayer);
debugLayer = null;
#end

FlxDestroyUtil.removeChild(_scrollRect, canvas);
FlxDestroyUtil.removeChild(_rotationCanvas, canvas);
if (canvas != null)
{
for (i in 0...canvas.numChildren)
Expand All @@ -1122,6 +1141,8 @@ class FlxCamera extends FlxBasic
_blitMatrix = null;
_helperMatrix = null;
_helperPoint = null;

_rotationCanvas = null;
}

_bounds = null;
Expand Down Expand Up @@ -2020,6 +2041,23 @@ class FlxCamera extends FlxBasic
return Angle;
}

function set_rotation(Rotation:Float):Float
{
rotation = Rotation;

@:privateAccess if (!FlxG.renderBlit) {
_rotationCanvas.__transform.identity();
_rotationCanvas.__transform.translate(-width / 2, -height / 2);
_rotationCanvas.__transform.rotate(Rotation * Math.PI / 180);
_rotationCanvas.__transform.translate(width / 2, height / 2);
}

calcMarginX();
calcMarginY();

return Rotation;
}

function set_color(Color:FlxColor):FlxColor
{
color = Color;
Expand Down Expand Up @@ -2095,12 +2133,38 @@ class FlxCamera extends FlxBasic

inline function calcMarginX():Void
{
viewMarginX = 0.5 * width * (scaleX - initialZoom) / scaleX;
if (!FlxG.renderBlit && rotation % 360 != 0)
{
final rotatedBounds:FlxRect = FlxRect.weak(0, 0, width, height);
rotatedBounds.getRotatedBounds(rotation, null, rotatedBounds);

final rotatedScaleX:Float = width / rotatedBounds.width * scaleX;
viewMarginX = 0.5 * width * (rotatedScaleX - initialZoom) / rotatedScaleX;

rotatedBounds.putWeak();
}
else
{
viewMarginX = 0.5 * width * (scaleX - initialZoom) / scaleX;
}
}

inline function calcMarginY():Void
{
viewMarginY = 0.5 * height * (scaleY - initialZoom) / scaleY;
if (!FlxG.renderBlit && rotation % 360 != 0)
{
final rotatedBounds:FlxRect = FlxRect.weak(0, 0, width, height);
rotatedBounds.getRotatedBounds(rotation, null, rotatedBounds);

final rotatedScaleY:Float = height / rotatedBounds.height * scaleY;
viewMarginY = 0.5 * height * (rotatedScaleY - initialZoom) / rotatedScaleY;

rotatedBounds.putWeak();
}
else
{
viewMarginY = 0.5 * height * (scaleY - initialZoom) / scaleY;
}
}

static inline function get_defaultCameras():Array<FlxCamera>
Expand Down