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

rename viewOffsetX to viewMarginX, plus many more #2714

Merged
merged 2 commits into from
Jan 17, 2023
Merged
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
235 changes: 192 additions & 43 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,66 @@ class FlxCamera extends FlxBasic
/**
* The zoom level of this camera. `1` = 1:1, `2` = 2x zoom, etc.
* Indicates how far the camera is zoomed in.
* Note: Changing this property from it's initial value will change properties like:
* `viewX`, `viewY`, `viewWidth`, `viewHeight` and many others. Cameras always zoom in to
* their center, meaning as you zoom in, the view is cut off on all sides.
*/
public var zoom(default, set):Float;

/**
* Difference between native size of camera and zoomed size, divided in half
* Needed to do occlusion of objects when zoom != initialZoom
*/
var viewOffsetX(default, null):Float = 0;
/** The margin cut off on the left and right by the camera zooming in (or out), in world space */
public var viewMarginX(default, null):Float;

var viewOffsetY(default, null):Float = 0;
/** The margin cut off on the top and bottom by the camera zooming in (or out), in world space */
public var viewMarginY(default, null):Float;

/**
* The size of the camera plus view offset.
* These variables are used for object visibility checks.
*/
var viewOffsetWidth(default, null):Float = 0;
// deprecated vars

var viewOffsetHeight(default, null):Float = 0;
@:deprecated("use viewMarginLeft or viewMarginX")
var viewOffsetX(get, set):Float;
@:deprecated("use viewMarginTop or viewMarginY")
var viewOffsetY(get, set):Float;
@:deprecated("use viewMarginLeft or viewMarginX")
var viewOffsetWidth(get, never):Float;
@:deprecated("use viewMarginTop or viewMarginY")
var viewOffsetHeight(get, never):Float;

/**
* Dimensions of area visible at current camera zoom.
*/
var viewWidth(default, null):Float = 0;
// delegates

/** The margin cut off on the left by the camera zooming in (or out), in world space */
public var viewMarginLeft(get, never):Float;

/** The margin cut off on the top by the camera zooming in (or out), in world space */
public var viewMarginTop(get, never):Float;

/** The margin cut off on the right by the camera zooming in (or out), in world space */
public var viewMarginRight(get, never):Float;

/** The margin cut off on the bottom by the camera zooming in (or out), in world space */
public var viewMarginBottom(get, never):Float;

/** The size of the camera's view, in world space. */
public var viewWidth(get, never):Float;

var viewHeight(default, null):Float = 0;
/** The size of the camera's view, in world space. */
public var viewHeight(get, never):Float;

/** The left of the camera's view, in world space. */
public var viewX(get, never):Float;

/** The top of the camera's view, in world space. */
public var viewY(get, never):Float;

/** The left of the camera's view, in world space. */
public var viewLeft(get, never):Float;

/** The top of the camera's view, in world space. */
public var viewTop(get, never):Float;

/** The right side of the camera's view, in world space. */
public var viewRight(get, never):Float;

/** The bottom side of the camera's view, in world space. */
public var viewBottom(get, never):Float;

/**
* Helper matrix object. Used in blit render mode when camera's zoom is less than initialZoom
Expand Down Expand Up @@ -702,7 +737,7 @@ class FlxCamera extends FlxBasic
}
else
{
_helperMatrix.translate(-viewOffsetX, -viewOffsetY);
_helperMatrix.translate(-viewMarginLeft, -viewMarginTop);
buffer.draw(pixels, _helperMatrix, null, blend, null, (smoothing || antialiasing));
}
}
Expand Down Expand Up @@ -736,8 +771,8 @@ class FlxCamera extends FlxBasic
}
else
{
_helperPoint.x = destPoint.x - Std.int(viewOffsetX);
_helperPoint.y = destPoint.y - Std.int(viewOffsetY);
_helperPoint.x = destPoint.x - Std.int(viewMarginLeft);
_helperPoint.y = destPoint.y - Std.int(viewMarginTop);
buffer.copyPixels(pixels, sourceRect, _helperPoint, null, null, true);
}
}
Expand Down Expand Up @@ -821,7 +856,7 @@ class FlxCamera extends FlxBasic
else
{
_helperMatrix.identity();
_helperMatrix.translate(-viewOffsetX, -viewOffsetY);
_helperMatrix.translate(-viewMarginLeft, -viewMarginTop);
}

buffer.draw(trianglesSprite, _helperMatrix);
Expand Down Expand Up @@ -866,7 +901,7 @@ class FlxCamera extends FlxBasic
{
if (FlxG.renderBlit)
{
rect.offset(-viewOffsetX, -viewOffsetY);
rect.offset(-viewMarginLeft, -viewMarginTop);

if (_useBlitMatrix)
{
Expand All @@ -889,7 +924,7 @@ class FlxCamera extends FlxBasic
{
if (FlxG.renderBlit)
{
point.subtract(viewOffsetX, viewOffsetY);
point.subtract(viewMarginLeft, viewMarginTop);

if (_useBlitMatrix)
point.scale(zoom);
Expand Down Expand Up @@ -1566,7 +1601,7 @@ class FlxCamera extends FlxBasic
targetGraphics.beginFill(Color, FxAlpha);
// i'm drawing rect with these parameters to avoid light lines at the top and left of the camera,
// which could appear while cameras fading
targetGraphics.drawRect(viewOffsetX - 1, viewOffsetY - 1, viewWidth + 2, viewHeight + 2);
targetGraphics.drawRect(viewMarginLeft - 1, viewMarginTop - 1, viewWidth + 2, viewHeight + 2);
targetGraphics.endFill();
}
}
Expand Down Expand Up @@ -1636,7 +1671,7 @@ class FlxCamera extends FlxBasic
inline function updateBlitMatrix():Void
{
_blitMatrix.identity();
_blitMatrix.translate(-viewOffsetX, -viewOffsetY);
_blitMatrix.translate(-viewMarginLeft, -viewMarginTop);
_blitMatrix.scale(scaleX, scaleY);

_useBlitMatrix = (scaleX < initialZoom) || (scaleY < initialZoom);
Expand Down Expand Up @@ -1735,8 +1770,8 @@ class FlxCamera extends FlxBasic
}
}

calcOffsetX();
calcOffsetY();
calcMarginX();
calcMarginY();

updateScrollRect();
updateInternalSpritePositions();
Expand All @@ -1755,15 +1790,34 @@ class FlxCamera extends FlxBasic
}

/**
* The size and position of this camera's screen
* The size and position of this camera's margins, via `viewMarginLeft`, `viewMarginTop`, `viewWidth`
* and `viewHeight`.
*
* Notes: Deprecated, in 4.11.0 this was made public, but the wording is confusing.
* In flixel 6.0.0 this will be changed to use `viewX`, `viewY`, `viewWidth` and `viewHeight`,
* meaning, this will return the world coordinates of the camera.
* @since 4.11.0
*/
@deprecated("getViewMarginRect")
public function getViewRect(?rect:FlxRect)
{
if (rect == null)
rect = FlxRect.get();

return rect.set(viewOffsetX, viewOffsetY, viewOffsetWidth - viewOffsetX, viewOffsetHeight - viewOffsetY);
return rect.set(viewMarginLeft, viewMarginTop, viewWidth, viewHeight);
}

/**
* The size and position of this camera's margins, via `viewMarginLeft`, `viewMarginTop`, `viewWidth`
* and `viewHeight`.
* @since 5.2.0
*/
public function getViewMarginRect(?rect:FlxRect)
{
if (rect == null)
rect = FlxRect.get();

return rect.set(viewMarginLeft, viewMarginTop, viewWidth, viewHeight);
}

/**
Expand All @@ -1773,8 +1827,8 @@ class FlxCamera extends FlxBasic
*/
public inline function containsPoint(point:FlxPoint, width:Float = 0, height:Float = 0):Bool
{
var contained = (point.x + width > viewOffsetX) && (point.x < viewOffsetWidth)
&& (point.y + height > viewOffsetY) && (point.y < viewOffsetHeight);
var contained = (point.x + width > viewMarginLeft) && (point.x < viewMarginRight)
&& (point.y + height > viewMarginTop) && (point.y < viewMarginBottom);
point.putWeak();
return contained;
}
Expand All @@ -1785,8 +1839,8 @@ class FlxCamera extends FlxBasic
*/
public inline function containsRect(rect:FlxRect):Bool
{
var contained = (rect.right > viewOffsetX) && (rect.x < viewOffsetWidth)
&& (rect.bottom > viewOffsetY) && (rect.y < viewOffsetHeight);
var contained = (rect.right > viewMarginLeft) && (rect.x < viewMarginRight)
&& (rect.bottom > viewMarginTop) && (rect.y < viewMarginBottom);
rect.putWeak();
return contained;
}
Expand All @@ -1801,7 +1855,7 @@ class FlxCamera extends FlxBasic
if (width != Value && Value > 0)
{
width = Value;
calcOffsetX();
calcMarginX();
updateFlashOffset();
updateScrollRect();
updateInternalSpritePositions();
Expand All @@ -1816,7 +1870,7 @@ class FlxCamera extends FlxBasic
if (height != Value && Value > 0)
{
height = Value;
calcOffsetY();
calcMarginY();
updateFlashOffset();
updateScrollRect();
updateInternalSpritePositions();
Expand Down Expand Up @@ -1921,18 +1975,20 @@ class FlxCamera extends FlxBasic
return this.visible = visible;
}

inline function calcOffsetX():Void
@:deprecated("Use calcMarginX")
inline function calcOffsetX():Void calcMarginX();

@:deprecated("Use calcMarginY")
inline function calcOffsetY():Void calcMarginY();

inline function calcMarginX():Void
{
viewOffsetX = 0.5 * width * (scaleX - initialZoom) / scaleX;
viewOffsetWidth = width - viewOffsetX;
viewWidth = width - 2 * viewOffsetX;
viewMarginX = 0.5 * width * (scaleX - initialZoom) / scaleX;
}

inline function calcOffsetY():Void
inline function calcMarginY():Void
{
viewOffsetY = 0.5 * height * (scaleY - initialZoom) / scaleY;
viewOffsetHeight = height - viewOffsetY;
viewHeight = height - 2 * viewOffsetY;
viewMarginY = 0.5 * height * (scaleY - initialZoom) / scaleY;
}

static inline function get_defaultCameras():Array<FlxCamera>
Expand All @@ -1944,6 +2000,99 @@ class FlxCamera extends FlxBasic
{
return _defaultCameras = value;
}

inline function get_viewMarginLeft():Float
{
return viewMarginX;
}

inline function get_viewMarginTop():Float
{
return viewMarginY;
}

inline function get_viewMarginRight():Float
{
return width - viewMarginX;
}

inline function get_viewMarginBottom():Float
{
return height - viewMarginY;
}

inline function get_viewWidth():Float
{
return width - viewMarginX * 2;
}

inline function get_viewHeight():Float
{
return height - viewMarginY * 2;
}

inline function get_viewX():Float
{
return camera.scroll.x + viewMarginX;
}

inline function get_viewY():Float
{
return camera.scroll.y + viewMarginY;
}

inline function get_viewLeft():Float
{
return viewX;
}

inline function get_viewTop():Float
{
return viewY;
}

inline function get_viewRight():Float
{
return camera.scroll.x + viewMarginRight;
}

inline function get_viewBottom():Float
{
return camera.scroll.x + viewMarginBottom;
}

// deprecated vars

inline function get_viewOffsetX():Float
{
return viewMarginX;
}

inline function set_viewOffsetX(value:Float):Float
{
return viewMarginX = value;
}

inline function get_viewOffsetY():Float
{
return viewMarginY;
}

inline function set_viewOffsetY(value:Float):Float
{
return viewMarginY = value;
}

inline function get_viewOffsetWidth():Float
{
return viewMarginRight;
}

inline function get_viewOffsetHeight():Float
{
return viewMarginBottom;
}

}

enum FlxCameraFollowStyle
Expand Down
4 changes: 2 additions & 2 deletions flixel/input/FlxPointer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class FlxPointer
if (point == null)
point = FlxPoint.get();

point.x = (_globalScreenX - Camera.x) / Camera.zoom + Camera.viewOffsetX;
point.y = (_globalScreenY - Camera.y) / Camera.zoom + Camera.viewOffsetY;
point.x = (_globalScreenX - Camera.x) / Camera.zoom + Camera.viewMarginX;
point.y = (_globalScreenY - Camera.y) / Camera.zoom + Camera.viewMarginY;

return point;
}
Expand Down
2 changes: 1 addition & 1 deletion flixel/path/FlxPath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ class FlxPath implements IFlxDestroyable
_point.y = nextNode.y - (camera.scroll.y * object.scrollFactor.y);

if (FlxG.renderBlit)
_point.subtract(camera.viewOffsetX, camera.viewOffsetY);
_point.subtract(camera.viewMarginX, camera.viewMarginY);

gfx.lineTo(_point.x + lineOffset, _point.y + lineOffset);
}
Expand Down
4 changes: 2 additions & 2 deletions flixel/tile/FlxTilemap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,8 @@ class FlxTilemap extends FlxBaseTilemap<FlxTile>
}

// Copy tile images into the tile buffer
_point.x = (camera.scroll.x * scrollFactor.x) - x - offset.x + camera.viewOffsetX; // modified from getScreenPosition()
_point.y = (camera.scroll.y * scrollFactor.y) - y - offset.y + camera.viewOffsetY;
_point.x = (camera.scroll.x * scrollFactor.x) - x - offset.x + camera.viewMarginX; // modified from getScreenPosition()
_point.y = (camera.scroll.y * scrollFactor.y) - y - offset.y + camera.viewMarginY;

var screenXInTiles:Int = Math.floor(_point.x / scaledTileWidth);
var screenYInTiles:Int = Math.floor(_point.y / scaledTileHeight);
Expand Down
Loading