Skip to content

Commit

Permalink
SpriteWrapper component
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Jan 24, 2024
1 parent 1ec470c commit e9f8805
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions haxe/ui/backend/flixel/components/SpriteWrapper.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package haxe.ui.backend.flixel.components;

import flixel.FlxSprite;
import flixel.math.FlxRect;
import haxe.ui.containers.Box;
import haxe.ui.core.Component;
import haxe.ui.geom.Size;
import haxe.ui.layouts.DefaultLayout;

@:composite(Layout)
class SpriteWrapper extends Box {
public var spriteOffsetX:Float = 0;
public var spriteOffsetY:Float = 0;

private var _sprite:FlxSprite = null;
public var sprite(get, set):FlxSprite;
private function get_sprite():FlxSprite {
return _sprite;
}
private function set_sprite(value:FlxSprite):FlxSprite {
if (_sprite != null) {
remove(_sprite);
}
_sprite = value;
add(_sprite);
invalidateComponentLayout();
return value;
}

private override function repositionChildren() {
super.repositionChildren();
if (sprite != null) {
sprite.x = spriteOffsetX + this.screenX;
sprite.y = spriteOffsetY + this.screenY;
}
}
}

@:access(haxe.ui.backend.flixel.components.SpriteWrapper)
private class Layout extends DefaultLayout {
public override function resizeChildren() {
super.resizeChildren();

var wrapper = cast(_component, SpriteWrapper);
var sprite = wrapper.sprite;
if (sprite == null) {
return super.resizeChildren();
}

sprite.origin.set(0, 0);
sprite.setGraphicSize(Std.int(innerWidth), Std.int(innerHeight));
}

public override function calcAutoSize(exclusions:Array<Component> = null):Size {
var wrapper = cast(_component, SpriteWrapper);
var sprite = wrapper.sprite;
if (sprite == null) {
return super.calcAutoSize(exclusions);
}
var size = new Size();
size.width = sprite.width + paddingLeft + paddingRight;
size.height = sprite.height + paddingTop + paddingBottom;
return size;
}
}

0 comments on commit e9f8805

Please sign in to comment.