popup test

This commit is contained in:
Yoshman29
2023-03-15 23:35:15 +01:00
parent d72558c427
commit 4a15cff151
12 changed files with 277 additions and 107 deletions
+16
View File
@@ -16,5 +16,21 @@ class UIDebugState extends UIState {
add(new UIButton(10, 70, "Test button", function() {
trace("Hello, World!");
}, 120, 32));
add(new UIButton(10, 110, "Warning test", function() {
openSubState(new UIWarningSubstate("Test", "This is a test message", [
{
label: "Alt. Choice",
onClick: function(t) {
}
},
{
label: "OK",
onClick: function(t) {
}
}
]));
}, 120, 32));
}
}
+11 -106
View File
@@ -2,36 +2,26 @@ package funkin.editors.ui;
import flixel.graphics.frames.FlxFrame;
class UIButton extends UISprite {
public var bWidth:Int = 120;
public var bHeight:Int = 20;
class UIButton extends UISpliceSprite {
public var callback:Void->Void = null;
public var field:UIText;
public function resize(w:Int, h:Int) {
bWidth = w;
bHeight = h;
field.fieldWidth = w;
}
public override function new(x:Float, y:Float, text:String, callback:Void->Void, w:Float = 120, h:Float = 32) {
super(x, y);
frames = CoolUtil.loadFrames(Paths.image('editors/ui/button'));
public override function new(x:Float, y:Float, text:String, callback:Void->Void, w:Int = 120, h:Int = 32) {
super(x, y, w, h, 'editors/ui/button');
this.callback = callback;
members.push(field = new UIText(x, y, w, text));
field.borderStyle = OUTLINE;
field.borderColor = 0x88000000;
field.borderSize = 1;
field.alignment = CENTER;
field.fieldWidth = w;
}
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 resize(w:Int, h:Int) {
super.resize(w, h);
if (field != null)
field.fieldWidth = w;
}
public override function onHovered() {
@@ -41,93 +31,8 @@ class UIButton extends UISprite {
}
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.drawSuper();
// TOP
frame = top;
setPosition(x + topleft.frame.width, y);
__setSize(bWidth - topleft.frame.width - topright.frame.width, top.frame.height);
super.drawSuper();
// TOP RIGHT
frame = topright;
setPosition(x + bWidth - topright.frame.width, y);
__setSize(topright.frame.width, topright.frame.height);
super.drawSuper();
// MIDDLE LEFT
frame = middleleft;
setPosition(x, y + top.frame.height);
__setSize(middleleft.frame.width, bHeight - topleft.frame.height - bottomleft.frame.height);
super.drawSuper();
// 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.drawSuper();
// 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.drawSuper();
// BOTTOM LEFT
frame = bottomleft;
setPosition(x, y + bHeight - bottom.frame.height);
__setSize(bottomleft.frame.width, bottomleft.frame.height);
super.drawSuper();
// 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.drawSuper();
// BOTTOM RIGHT
frame = bottomright;
setPosition(x + bWidth - bottomright.frame.width, y + bHeight - bottom.frame.height);
__setSize(bottomright.frame.width, bottomright.frame.height);
super.drawSuper();
}
setPosition(x, y);
field.follow(this, 0, ((bHeight - field.height) / 2));
super.drawMembers();
}
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();
framesOffset = hovered ? (pressed ? 18 : 9) : 0;
field.follow(this, x, (bHeight - field.height) / 2);
super.draw();
}
}
+117
View File
@@ -0,0 +1,117 @@
package funkin.editors.ui;
import flixel.graphics.frames.FlxFrame;
class UISpliceSprite extends UISprite {
public var bWidth:Int = 120;
public var bHeight:Int = 20;
public var framesOffset:Int = 0;
public function new(x:Float, y:Float, w:Int, h:Int, path:String) {
super(x, y);
frames = CoolUtil.loadFrames(Paths.image(path));
resize(w, h);
}
public override function updateButton() {
__rect.x = x;
__rect.y = y;
__rect.width = bWidth;
__rect.height = bHeight;
UIState.state.updateRectButtonHandler(this, __rect, onHovered);
}
public function resize(w:Int, h:Int) {
bWidth = w;
bHeight = h;
}
public override function draw() {
var x:Float = this.x;
var y:Float = this.y;
var topleft:FlxFrame = frames.frames[framesOffset];
var top:FlxFrame = frames.frames[framesOffset + 1];
var topright:FlxFrame = frames.frames[framesOffset + 2];
var middleleft:FlxFrame = frames.frames[framesOffset + 3];
var middle:FlxFrame = frames.frames[framesOffset + 4];
var middleright:FlxFrame = frames.frames[framesOffset + 5];
var bottomleft:FlxFrame = frames.frames[framesOffset + 6];
var bottom:FlxFrame = frames.frames[framesOffset + 7];
var bottomright:FlxFrame = frames.frames[framesOffset + 8];
@:privateAccess {
// TOP LEFT
frame = topleft;
setPosition(x, y);
__setSize(topleft.frame.width, topleft.frame.height);
super.drawSuper();
// TOP
frame = top;
setPosition(x + topleft.frame.width, y);
__setSize(bWidth - topleft.frame.width - topright.frame.width, top.frame.height);
super.drawSuper();
// TOP RIGHT
frame = topright;
setPosition(x + bWidth - topright.frame.width, y);
__setSize(topright.frame.width, topright.frame.height);
super.drawSuper();
// MIDDLE LEFT
frame = middleleft;
setPosition(x, y + top.frame.height);
__setSize(middleleft.frame.width, bHeight - topleft.frame.height - bottomleft.frame.height);
super.drawSuper();
// 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.drawSuper();
// 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.drawSuper();
// BOTTOM LEFT
frame = bottomleft;
setPosition(x, y + bHeight - bottom.frame.height);
__setSize(bottomleft.frame.width, bottomleft.frame.height);
super.drawSuper();
// 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.drawSuper();
// BOTTOM RIGHT
frame = bottomright;
setPosition(x + bWidth - bottomright.frame.width, y + bHeight - bottom.frame.height);
__setSize(bottomright.frame.width, bottomright.frame.height);
super.drawSuper();
}
setPosition(x, y);
super.drawMembers();
}
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();
}
}
@@ -0,0 +1,72 @@
package funkin.editors.ui;
import flixel.tweens.FlxTween;
import flixel.tweens.FlxEase;
import funkin.shaders.CustomShader;
class UIWarningSubstate extends MusicBeatSubstate {
var camShaders:Array<FlxCamera> = [];
var blurShader:CustomShader = new CustomShader("editorBlur");
var title:String;
var message:String;
var buttons:Array<WarningButton>;
var titleSpr:UIText;
var messageSpr:UIText;
public override function onSubstateOpen() {
super.onSubstateOpen();
parent.persistentUpdate = false;
parent.persistentDraw = true;
}
public override function create() {
for(c in FlxG.cameras.list) {
camShaders.push(c);
c.addShader(blurShader);
}
camera = new FlxCamera();
camera.bgColor = 0;
camera.alpha = 0;
camera.zoom = 0.1;
FlxG.cameras.add(camera, false);
var spr = new UISpliceSprite(0, 0, 560, 280, "editors/ui/warning-popup");
spr.x = (FlxG.width - spr.bWidth) / 2;
spr.y = (FlxG.height - spr.bHeight) / 2;
add(spr);
add(titleSpr = new UIText(spr.x + 25, spr.y, spr.bWidth - 50, title, 15, -1));
titleSpr.y = spr.y + ((30 - titleSpr.height) / 2);
add(messageSpr = new UIText(spr.x + 10, spr.y + 40, spr.bWidth - 20, message, 15, 0xFF000000));
FlxTween.tween(camera, {alpha: 1}, 0.25, {ease: FlxEase.cubeOut});
FlxTween.tween(camera, {zoom: 1}, 0.66, {ease: FlxEase.elasticOut});
}
public override function destroy() {
super.destroy();
for(e in camShaders)
e.removeShader(blurShader);
FlxG.cameras.remove(camera);
}
public function new(title:String, message:String, buttons:Array<WarningButton>) {
super();
this.title = title;
this.message = message;
this.buttons = buttons;
}
}
typedef WarningCamShader = {
var cam:FlxCamera;
}
typedef WarningButton = {
var label:String;
var onClick:UIWarningSubstate->Void;
}
+8
View File
@@ -242,4 +242,12 @@ class MusicBeatState extends FlxUIState implements IBeatReceiver
super.onFocusLost();
call("onFocusLost");
}
public override function resetSubState() {
super.resetSubState();
if (subState != null && subState is MusicBeatSubstate) {
cast(subState, MusicBeatSubstate).parent = this;
cast(subState, MusicBeatSubstate).onSubstateOpen();
}
}
}
+14
View File
@@ -243,4 +243,18 @@ class MusicBeatSubstate extends FlxSubState implements IBeatReceiver
super.onFocusLost();
call("onFocusLost");
}
public var parent:FlxState;
public function onSubstateOpen() {
}
public override function resetSubState() {
super.resetSubState();
if (subState != null && subState is MusicBeatSubstate) {
cast(subState, MusicBeatSubstate).parent = this;
cast(subState, MusicBeatSubstate).onSubstateOpen();
}
}
}