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

cam rotation support #2

Draft
wants to merge 1 commit into
base: dev-6.0.0
Choose a base branch
from
Draft
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
77 changes: 69 additions & 8 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 @@ -339,6 +340,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 @@ -510,6 +516,11 @@ class FlxCamera extends FlxBasic
*/
public var canvas:Sprite;

/**
* Internal, used for rendering rotated camera in tile render mode.
*/
var _rotationCanvas:Sprite;

#if FLX_DEBUG
/**
* Sprite for visual effects (flash and fade) and drawDebug information
Expand Down Expand Up @@ -1037,12 +1048,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 @@ -1076,12 +1090,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 @@ -1099,6 +1115,8 @@ class FlxCamera extends FlxBasic
_blitMatrix = null;
_helperMatrix = null;
_helperPoint = null;

_rotationCanvas = null;
}

_bounds = null;
Expand Down Expand Up @@ -1962,6 +1980,23 @@ class FlxCamera extends FlxBasic
flashSprite.rotation = Angle;
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
{
Expand Down Expand Up @@ -2032,12 +2067,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