fix button
This commit is contained in:
@@ -13,5 +13,8 @@ class UIDebugState extends UIState {
|
||||
|
||||
add(new UICheckbox(10, 10, "Test unchecked", false));
|
||||
add(new UICheckbox(10, 40, "Test checked", true));
|
||||
add(new UIButton(10, 70, "Test button", function() {
|
||||
trace("Hello, World!");
|
||||
}, 120, 20));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,12 @@ package funkin.editors.ui;
|
||||
import funkin.ui.FunkinText;
|
||||
|
||||
class UICheckbox extends UISprite {
|
||||
public var checked:Bool = false;
|
||||
public var onChecked:Bool->Void = null;
|
||||
|
||||
public var field:FunkinText;
|
||||
public var check:FlxSprite;
|
||||
|
||||
public var checked:Bool = false;
|
||||
|
||||
var hovered:Bool = false;
|
||||
var pressed:Bool = false;
|
||||
public function new(x:Float, y:Float, text:String, checked:Bool = false, w:Int = 0) {
|
||||
super(x, y);
|
||||
loadGraphic(Paths.image('editors/ui/checkbox'), true, 20, 20);
|
||||
@@ -29,8 +28,6 @@ class UICheckbox extends UISprite {
|
||||
public override function update(elapsed:Float) {
|
||||
// ANIMATION HANDLING
|
||||
animation.play(hovered ? (pressed ? "pressed" : "hover") : "normal");
|
||||
hovered = false;
|
||||
pressed = false;
|
||||
|
||||
// CHECKMARK HANDLING
|
||||
check.alpha = checked ? 1 : 0;
|
||||
@@ -56,9 +53,7 @@ class UICheckbox extends UISprite {
|
||||
}
|
||||
|
||||
public override function onHovered() {
|
||||
hovered = true;
|
||||
if (FlxG.mouse.pressed)
|
||||
pressed = true;
|
||||
super.onHovered();
|
||||
if (FlxG.mouse.justReleased) {
|
||||
// clicked
|
||||
checked = !checked;
|
||||
@@ -66,6 +61,8 @@ class UICheckbox extends UISprite {
|
||||
|
||||
if (Options.editorSFX)
|
||||
CoolUtil.playMenuSFX(checked ? CHECKED : UNCHECKED, 0.5);
|
||||
if (onChecked != null)
|
||||
onChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@ class UISprite extends FlxSprite {
|
||||
|
||||
private var __oldDefCams:Array<FlxCamera>;
|
||||
|
||||
var hovered:Bool = false;
|
||||
var pressed:Bool = false;
|
||||
|
||||
public override function update(elapsed:Float) {
|
||||
super.update(elapsed);
|
||||
updateButton();
|
||||
@@ -25,6 +28,9 @@ class UISprite extends FlxSprite {
|
||||
|
||||
FlxCamera._defaultCameras = __oldDefCams;
|
||||
}
|
||||
|
||||
hovered = false;
|
||||
pressed = false;
|
||||
}
|
||||
|
||||
public override function draw() {
|
||||
@@ -56,6 +62,8 @@ class UISprite extends FlxSprite {
|
||||
* Called whenever the sprite is being hovered by the mouse.
|
||||
*/
|
||||
public function onHovered() {
|
||||
|
||||
hovered = true;
|
||||
if (FlxG.mouse.pressed)
|
||||
pressed = true;
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,6 @@ class UIState extends MusicBeatState {
|
||||
updateRectButtonHandler(spr, spr.__rect, buttonHandler);
|
||||
}
|
||||
|
||||
|
||||
public function updateRectButtonHandler(spr:UISprite, rect:FlxRect, buttonHandler:Void->Void) {
|
||||
var pos = FlxG.mouse.getScreenPosition(camera);
|
||||
for(camera in spr.__lastDrawCameras) {
|
||||
|
||||
Reference in New Issue
Block a user