-
Notifications
You must be signed in to change notification settings - Fork 472
Add FlxG.centerGraphic() and camera.centerGraphic() #3329
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
base: 6.2.0
Are you sure you want to change the base?
Changes from 8 commits
ec3b8c5
c4a41ff
55ace96
5603edd
a593d5a
f720a2b
ea9c93f
5b47432
cbf91ce
e3f9d55
7628284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1873,6 +1873,71 @@ class FlxCamera extends FlxBasic | |
setScale(scaleX, scaleY); | ||
} | ||
|
||
/** | ||
* Centers `FlxSprite` by graphic size in this camera view, either by the x axis, y axis, or both. | ||
* | ||
* @param sprite The sprite to center. | ||
* @param axes On what axes to center the sprite (e.g. `X`, `Y`, `XY`) - default is both. | ||
* @return Centered sprite for chaining. | ||
* @since TBA | ||
*/ | ||
public function center<T:FlxSprite>(sprite:T, axes:FlxAxes = XY):T | ||
{ | ||
// We need to disable these flags to get accurate graphic bounds | ||
final pixelPerfectPosition = sprite.pixelPerfectPosition; | ||
final pixelPerfectRender = sprite.pixelPerfectRender; | ||
sprite.pixelPerfectPosition = false; | ||
@:bypassAccessor sprite.pixelPerfectRender = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make a new helper that allows us to get the unrounded value, or a new arg in the existing one that allows us to ignore this field. this is not the first case where pxPerfPos has been a hassle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I think about this, maybe this should NOT attempt to avoid pixelPerfectRender and position, perhaps the unit test was failing because it should have accounted for this? Can you elaborate on why it failed and why centering should not honor pixelperfectRendering? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unit tests were actually fine, the problem was discovered in a test project, where grapthic is centered every frame. For now i removed this workaround. Adding another argument to functions will work, but will cause problems with function override. Should this be a concern? My test project: package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.util.FlxAxes;
class PlayState extends flixel.FlxState
{
var graphic:FlxSprite;
var centerMode = FlxAxes.XY;
var rorate = false;
override public function create()
{
super.create();
FlxG.cameras.bgColor = flixel.util.FlxColor.GRAY;
graphic = new FlxSprite().makeGraphic(20, 20);
graphic.scrollFactor.set(2, 2);
graphic.origin.set(20, 20);
graphic.offset.set(200, 200);
graphic.scale.set(2, 4);
add(graphic);
FlxG.watch.add(FlxG.camera, "scroll", "camera");
FlxG.watch.add(this, "graphic", "graphic");
FlxG.watch.add(graphic, "pixelPerfectPosition", "pixelPerfectPosition");
FlxG.watch.add(graphic, "pixelPerfectRender", "pixelPerfectRender");
}
override public function update(elapsed:Float)
{
super.update(elapsed);
var mult = 10.0;
if (FlxG.keys.pressed.SHIFT)
mult *= 10;
if (FlxG.keys.pressed.A)
FlxG.camera.scroll.x -= mult * elapsed;
if (FlxG.keys.pressed.D)
FlxG.camera.scroll.x += mult * elapsed;
if (FlxG.keys.pressed.W)
FlxG.camera.scroll.y -= mult * elapsed;
if (FlxG.keys.pressed.S)
FlxG.camera.scroll.y += mult * elapsed;
mult /= 100;
if (FlxG.keys.pressed.Q)
FlxG.camera.zoom += mult * elapsed;
if (FlxG.keys.pressed.E)
FlxG.camera.zoom -= mult * elapsed;
if (FlxG.keys.justPressed.Z)
centerMode = FlxAxes.fromBools(!centerMode.x, centerMode.y);
if (FlxG.keys.justPressed.X)
centerMode = FlxAxes.fromBools(centerMode.x, !centerMode.y);
if (FlxG.keys.justPressed.C)
graphic.pixelPerfectPosition = !graphic.pixelPerfectPosition;
if (FlxG.keys.justPressed.V)
graphic.pixelPerfectRender = !graphic.pixelPerfectRender;
if (FlxG.keys.justPressed.R)
rorate = !rorate;
if (rorate)
graphic.angle += elapsed;
FlxG.camera.centerGraphic(graphic, centerMode);
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an example to reproduce the shaking effect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Shaking appears when camera is moved or sprite is rotated. |
||
|
||
final graphicBounds = sprite.getScreenBounds(null, this); | ||
|
||
if (axes.x) | ||
{ | ||
final offset = sprite.x - graphicBounds.x; | ||
sprite.x = (width - graphicBounds.width) / 2 + offset; | ||
} | ||
|
||
if (axes.y) | ||
{ | ||
final offset = sprite.y - graphicBounds.y; | ||
sprite.y = (height - graphicBounds.height) / 2 + offset; | ||
} | ||
|
||
// Now we set flags back to their original state | ||
sprite.pixelPerfectPosition = pixelPerfectPosition; | ||
@:bypassAccessor sprite.pixelPerfectRender = pixelPerfectRender; | ||
graphicBounds.put(); | ||
return sprite; | ||
} | ||
|
||
/** | ||
* Centers `FlxObject` by hitbox size in this camera view, either by the x axis, y axis, or both. | ||
* | ||
* @param object The object to center. | ||
* @param axes On what axes to center the object (e.g. `X`, `Y`, `XY`) - default is both. | ||
* @return Centered object for chaining. | ||
* @since TBA | ||
*/ | ||
public function centerHitbox<T:FlxObject>(object:T, axes:FlxAxes = XY):T | ||
{ | ||
final hitbox = object.getHitbox(); | ||
|
||
if (axes.x) | ||
{ | ||
final offset = object.x - hitbox.x; | ||
object.x = scroll.x + (width - hitbox.width) / 2 + offset; | ||
} | ||
|
||
if (axes.y) | ||
{ | ||
final offset = object.y - hitbox.y; | ||
object.y = scroll.y + (height - hitbox.height) / 2 + offset; | ||
} | ||
|
||
hitbox.put(); | ||
richTrash21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return object; | ||
} | ||
|
||
/** | ||
* The size and position of this camera's margins, via `viewMarginLeft`, `viewMarginTop`, `viewWidth` | ||
* and `viewHeight`. | ||
|
Uh oh!
There was an error while loading. Please reload this page.