Files
CodenameEngine/source/funkin/editors/ui/UIButton.hx
T
2023-03-15 13:46:30 +01:00

122 lines
4.1 KiB
Haxe

package funkin.editors.ui;
import flixel.graphics.frames.FlxFrame;
class UIButton extends UISprite {
public var bWidth:Int = 120;
public var bHeight:Int = 20;
public var callback:Void->Void = null;
public function resize(w:Int, h:Int) {
bWidth = w;
bHeight = h;
}
public override function new(x:Float, y:Float, t:String, callback:Void->Void, w:Float = 120, h:Float = 20) {
super(x, y);
frames = CoolUtil.loadFrames(Paths.image('editors/ui/button'));
this.callback = callback;
}
public override function updateButton() {
__rect.x = x;
__rect.y = y;
__rect.width = bWidth;
__rect.height = bHeight;
UIState.state.updateRectButtonHandler(this, __rect, onHovered);
}
public override function onHovered() {
super.onHovered();
if (FlxG.mouse.justReleased && callback != null)
callback();
}
public override function draw() {
var x:Float = this.x;
var y:Float = this.y;
var offset:Int = hovered ? (pressed ? 18 : 9) : 0;
var topleft:FlxFrame = frames.frames[offset];
var top:FlxFrame = frames.frames[offset + 1];
var topright:FlxFrame = frames.frames[offset + 2];
var middleleft:FlxFrame = frames.frames[offset + 3];
var middle:FlxFrame = frames.frames[offset + 4];
var middleright:FlxFrame = frames.frames[offset + 5];
var bottomleft:FlxFrame = frames.frames[offset + 6];
var bottom:FlxFrame = frames.frames[offset + 7];
var bottomright:FlxFrame = frames.frames[offset + 8];
@:privateAccess {
// TOP LEFT
frame = topleft;
setPosition(x, y);
__setSize(topleft.frame.width, topleft.frame.height);
super.draw();
// TOP
frame = top;
setPosition(x + topleft.frame.width, y);
__setSize(bWidth - topleft.frame.width - topright.frame.width, top.frame.height);
super.draw();
// TOP RIGHT
frame = topright;
setPosition(x + bWidth - topright.frame.width, y);
__setSize(topright.frame.width, topright.frame.height);
super.draw();
// MIDDLE LEFT
frame = middleleft;
setPosition(x, y + top.frame.height);
__setSize(middleleft.frame.width, bHeight - topleft.frame.height - bottomleft.frame.height);
super.draw();
// MIDDLE
frame = middle;
setPosition(x + topleft.frame.width, y + top.frame.height);
__setSize(bWidth - middleleft.frame.width - middleright.frame.width, bHeight - topleft.frame.height - bottomleft.frame.height);
super.draw();
// MIDDLE RIGHT
frame = middleright;
setPosition(x + bWidth - topright.frame.width, y + top.frame.height);
__setSize(middleright.frame.width, bHeight - topleft.frame.height - bottomleft.frame.height);
super.draw();
// BOTTOM LEFT
frame = bottomleft;
setPosition(x, y + bHeight - bottom.frame.height);
__setSize(bottomleft.frame.width, bottomleft.frame.height);
super.draw();
// BOTTOM
frame = bottom;
setPosition(x + bottomleft.frame.width, y + bHeight - bottom.frame.height);
__setSize(bWidth - bottomleft.frame.width - bottomright.frame.width, bottom.frame.height);
super.draw();
// BOTTOM RIGHT
frame = bottomright;
setPosition(x + bWidth - bottomright.frame.width, y + bHeight - bottom.frame.height);
__setSize(bottomright.frame.width, bottomright.frame.height);
super.draw();
}
setPosition(x, y);
}
private function __setSize(Width:Float, Height:Float) {
var newScaleX:Float = Width / frameWidth;
var newScaleY:Float = Height / frameHeight;
scale.set(newScaleX, newScaleY);
if (Width <= 0)
scale.x = newScaleY;
else if (Height <= 0)
scale.y = newScaleX;
updateHitbox();
}
}