2022-09-07 23:07:08 +00:00
|
|
|
package funkin.ui.haxeui;
|
|
|
|
|
2022-12-17 20:19:42 +00:00
|
|
|
import haxe.ui.components.CheckBox;
|
2023-03-01 02:06:09 +00:00
|
|
|
import haxe.ui.containers.menus.MenuCheckBox;
|
2023-10-28 23:56:34 +00:00
|
|
|
import haxe.ui.containers.menus.MenuItem;
|
2022-09-07 23:07:08 +00:00
|
|
|
import haxe.ui.core.Component;
|
2022-10-07 04:37:21 +00:00
|
|
|
import haxe.ui.core.Screen;
|
2023-11-07 09:04:22 +00:00
|
|
|
import funkin.ui.MusicBeatState;
|
2022-10-07 04:37:21 +00:00
|
|
|
import haxe.ui.events.MouseEvent;
|
2023-03-01 02:06:09 +00:00
|
|
|
import haxe.ui.events.UIEvent;
|
|
|
|
import haxe.ui.RuntimeComponentBuilder;
|
2022-10-07 04:37:21 +00:00
|
|
|
import lime.app.Application;
|
2022-09-07 23:07:08 +00:00
|
|
|
|
|
|
|
class HaxeUIState extends MusicBeatState
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
public var component:Component;
|
|
|
|
|
|
|
|
var _componentKey:String;
|
|
|
|
|
|
|
|
public function new(key:String)
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
_componentKey = key;
|
|
|
|
}
|
|
|
|
|
2023-03-01 02:06:09 +00:00
|
|
|
override function create():Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
super.create();
|
|
|
|
|
|
|
|
if (component == null) component = buildComponent(_componentKey);
|
|
|
|
if (component != null) add(component);
|
|
|
|
}
|
|
|
|
|
2023-03-01 02:06:09 +00:00
|
|
|
public function buildComponent(assetPath:String):Component
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return RuntimeComponentBuilder.fromAsset(assetPath);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
Application.current.window.alert('Error building component "$assetPath": $e', 'HaxeUI Parsing Error');
|
|
|
|
// trace('[ERROR] Failed to build component from asset: ' + assetPath);
|
|
|
|
// trace(e);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently active context menu.
|
|
|
|
*/
|
|
|
|
public var contextMenu:Component;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when right clicking on a component, to display a context menu.
|
|
|
|
*/
|
|
|
|
function showContextMenu(assetPath:String, xPos:Float, yPos:Float):Component
|
|
|
|
{
|
|
|
|
if (contextMenu != null) contextMenu.destroy();
|
|
|
|
|
|
|
|
contextMenu = buildComponent(assetPath);
|
|
|
|
|
|
|
|
if (contextMenu != null)
|
|
|
|
{
|
|
|
|
// Move the context menu to the mouse position.
|
|
|
|
contextMenu.left = xPos;
|
|
|
|
contextMenu.top = yPos;
|
|
|
|
Screen.instance.addComponent(contextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return contextMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a context menu to display when right clicking.
|
|
|
|
* @param component Only display the menu when clicking this component. If null, display the menu when right clicking anywhere.
|
|
|
|
* @param assetPath The asset path to the context menu XML.
|
|
|
|
*/
|
|
|
|
public function registerContextMenu(target:Null<Component>, assetPath:String):Void
|
|
|
|
{
|
|
|
|
if (target == null)
|
|
|
|
{
|
2023-03-01 02:06:09 +00:00
|
|
|
Screen.instance.registerEvent(MouseEvent.RIGHT_CLICK, function(e:MouseEvent) {
|
2023-01-23 03:25:45 +00:00
|
|
|
showContextMenu(assetPath, e.screenX, e.screenY);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-01 02:06:09 +00:00
|
|
|
target.registerEvent(MouseEvent.RIGHT_CLICK, function(e:MouseEvent) {
|
2023-01-23 03:25:45 +00:00
|
|
|
showContextMenu(assetPath, e.screenX, e.screenY);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an onClick listener to a HaxeUI menu bar item.
|
|
|
|
*/
|
2023-03-01 02:06:09 +00:00
|
|
|
function addUIClickListener(key:String, callback:MouseEvent->Void):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.onClick = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 05:04:50 +00:00
|
|
|
/**
|
|
|
|
* Add an onRightClick listener to a HaxeUI menu bar item.
|
|
|
|
*/
|
|
|
|
function addUIRightClickListener(key:String, callback:MouseEvent->Void):Void
|
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.onRightClick = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 02:06:09 +00:00
|
|
|
function setComponentText(key:String, text:String):Void
|
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.text = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 23:56:34 +00:00
|
|
|
function setComponentShortcutText(key:String, text:String):Void
|
|
|
|
{
|
|
|
|
var target:MenuItem = findComponent(key, MenuItem);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.shortcutText = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 07:12:34 +00:00
|
|
|
function addTooltip(key:String, text:String):Void
|
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.tooltip = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Add an onChange listener to a HaxeUI input component such as a slider or text field.
|
|
|
|
*/
|
2023-03-01 02:06:09 +00:00
|
|
|
function addUIChangeListener(key:String, callback:UIEvent->Void):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target.onChange = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a HaxeUI component.
|
|
|
|
* Usually modifies the text of a label or value of a text field.
|
|
|
|
*/
|
|
|
|
function setUIValue<T>(key:String, value:T):T
|
|
|
|
{
|
|
|
|
var target:Component = findComponent(key);
|
|
|
|
if (target == null)
|
|
|
|
{
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate menu item: $key');
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return target.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a HaxeUI checkbox,
|
|
|
|
* since that's on 'selected' instead of 'value'.
|
|
|
|
*/
|
|
|
|
public function setUICheckboxSelected<T>(key:String, value:Bool):Bool
|
|
|
|
{
|
|
|
|
var targetA:CheckBox = findComponent(key, CheckBox);
|
|
|
|
|
|
|
|
if (targetA != null)
|
|
|
|
{
|
|
|
|
return targetA.selected = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetB:MenuCheckBox = findComponent(key, MenuCheckBox);
|
|
|
|
if (targetB != null)
|
|
|
|
{
|
|
|
|
return targetB.selected = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gracefully handle the case where the item can't be located.
|
|
|
|
trace('WARN: Could not locate check box: $key');
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findComponent<T:Component>(criteria:String = null, type:Class<T> = null, recursive:Null<Bool> = null, searchType:String = "id"):Null<T>
|
|
|
|
{
|
|
|
|
if (component == null) return null;
|
|
|
|
|
|
|
|
return component.findComponent(criteria, type, recursive, searchType);
|
|
|
|
}
|
|
|
|
|
2023-03-01 02:06:09 +00:00
|
|
|
override function destroy():Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
if (component != null) remove(component);
|
|
|
|
component = null;
|
|
|
|
|
|
|
|
super.destroy();
|
|
|
|
}
|
2022-09-07 23:07:08 +00:00
|
|
|
}
|