Funkin/source/funkin/ui/Prompt.hx

123 lines
2.5 KiB
Haxe
Raw Normal View History

package funkin.ui;
2021-02-18 14:58:16 -05:00
import flixel.FlxSprite;
2022-04-18 19:36:09 -04:00
import funkin.ui.AtlasText.AtlasFont;
import funkin.ui.MenuList;
2021-02-18 14:58:16 -05:00
2022-04-18 19:36:09 -04:00
/**
* Opens a yes/no dialog box as a substate over the current state.
*/
2021-02-18 14:58:16 -05:00
class Prompt extends flixel.FlxSubState
{
inline static var MARGIN = 100;
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
public var onYes:Void->Void;
public var onNo:Void->Void;
2021-02-23 19:55:24 -05:00
public var buttons:TextMenuList;
public var field:AtlasText;
2021-03-16 10:56:08 -04:00
public var back:FlxSprite;
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
var style:ButtonStyle;
2021-06-23 04:15:44 -04:00
public function new(text:String, style:ButtonStyle = Ok)
2021-02-18 14:58:16 -05:00
{
this.style = style;
2021-03-16 10:56:08 -04:00
super(0x80000000);
2021-06-23 04:15:44 -04:00
2021-02-23 19:55:24 -05:00
buttons = new TextMenuList(Horizontal);
2021-06-23 04:15:44 -04:00
2022-04-18 19:36:09 -04:00
field = new AtlasText(text, AtlasFont.BOLD);
2021-02-18 14:58:16 -05:00
field.scrollFactor.set(0, 0);
}
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
override function create()
{
super.create();
2021-06-23 04:15:44 -04:00
field.y = MARGIN;
2021-02-18 14:58:16 -05:00
field.screenCenter(X);
add(field);
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
createButtons();
add(buttons);
}
2021-06-23 04:15:44 -04:00
2021-03-16 10:56:08 -04:00
public function createBg(width:Int, height:Int, color = 0xFF808080)
{
back = new FlxSprite();
back.makeGraphic(width, height, color, false, "prompt-bg");
back.screenCenter(XY);
add(back);
2021-06-23 04:15:44 -04:00
members.unshift(members.pop()); // bring to front
2021-03-16 10:56:08 -04:00
}
2021-06-23 04:15:44 -04:00
2021-03-16 10:56:08 -04:00
public function createBgFromMargin(margin = MARGIN, color = 0xFF808080)
{
createBg(Std.int(FlxG.width - margin * 2), Std.int(FlxG.height - margin * 2), color);
}
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
public function setButtons(style:ButtonStyle)
{
if (this.style != style)
{
this.style = style;
createButtons();
}
}
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
function createButtons()
{
// destroy previous buttons
2021-06-23 04:15:44 -04:00
while (buttons.members.length > 0)
2021-02-18 14:58:16 -05:00
{
buttons.remove(buttons.members[0], true).destroy();
}
2021-06-23 04:15:44 -04:00
switch (style)
2021-02-18 14:58:16 -05:00
{
2021-06-23 04:15:44 -04:00
case Yes_No:
createButtonsHelper("yes", "no");
case Ok:
createButtonsHelper("ok");
case Custom(yes, no):
createButtonsHelper(yes, no);
case None:
buttons.exists = false;
2021-02-18 14:58:16 -05:00
};
}
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
function createButtonsHelper(yes:String, ?no:String)
{
buttons.exists = true;
// pass anonymous functions rather than the current callbacks, in case they change later
var yesButton = buttons.createItem(yes, function() onYes());
yesButton.screenCenter(X);
yesButton.y = FlxG.height - yesButton.height - MARGIN;
2021-02-18 14:58:16 -05:00
yesButton.scrollFactor.set(0, 0);
if (no != null)
{
// place right
yesButton.x = FlxG.width - yesButton.width - MARGIN;
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
var noButton = buttons.createItem(no, function() onNo());
noButton.x = MARGIN;
noButton.y = FlxG.height - noButton.height - MARGIN;
2021-02-18 14:58:16 -05:00
noButton.scrollFactor.set(0, 0);
}
}
2021-06-23 04:15:44 -04:00
2021-02-18 14:58:16 -05:00
public function setText(text:String)
{
field.text = text;
field.screenCenter(X);
}
}
enum ButtonStyle
{
2021-06-23 04:15:44 -04:00
Ok;
Yes_No;
Custom(yes:String, no:Null<String>); // Todo: more than 2
2021-02-18 14:58:16 -05:00
None;
2021-06-23 04:15:44 -04:00
}