Add array option type (#241)

* Add Array option type

* Prevent nulls

* add display options

* change xml structure
This commit is contained in:
bctix
2024-02-08 16:04:41 -07:00
committed by GitHub
parent cb5fb40adb
commit 6d73a9ef4b
3 changed files with 97 additions and 3 deletions
+3 -3
View File
@@ -4,9 +4,9 @@
<checkbox id="checkboxExample" name="Checkbox example" />
<number id="numberExample" name="Number example" min="0" max="10" change="1"/>
<choice id="choiceExample" name="Choice Example">
<value name="Disabled" value="disabled" />
<value name="This Only" value="thisonly" />
<value name="All" value="all" />
<value name="Disabled" value="disabled"/>
<value name="This only" value="thisOnly"/>
<value name="All" value="all"/>
</choice>
<menu name="Submenu Example" desc="Submenu test">
+17
View File
@@ -121,6 +121,23 @@ class OptionsMenu extends TreeMenu {
continue;
}
options.push(new NumOption(name, desc, Std.parseFloat(node.att.min), Std.parseFloat(node.att.max), Std.parseFloat(node.att.change), node.att.id, null, FlxG.save.data));
case "choice":
if (!node.has.id) {
Logs.trace("A choice option requires an \"id\" for option saving.", WARNING);
continue;
}
var optionOptions:Array<Dynamic> = [];
var optionDisplayOptions:Array<String> = [];
for(choice in node.elements) {
optionOptions.push(choice.att.value);
optionDisplayOptions.push(choice.att.name);
}
if(optionOptions.length > 0)
options.push(new ArrayOption(name, desc, optionOptions, optionDisplayOptions, node.att.id, null, FlxG.save.data));
case "menu":
options.push(new TextOption(name + " >", desc, function() {
optionsTree.add(new OptionsScreen(name, desc, parseOptionsFromXML(node)));
+77
View File
@@ -0,0 +1,77 @@
package funkin.options.type;
import flixel.effects.FlxFlicker;
class ArrayOption extends OptionType {
public var selectCallback:String->Void;
private var __text:Alphabet;
private var __selectiontext:Alphabet;
public var options:Array<Dynamic>;
public var displayOptions:Array<String>;
public var currentSelection:Int;
var optionName:String;
public var parent:Dynamic;
public var text(get, set):String;
private function get_text() {return __text.text;}
private function set_text(v:String) {return __text.text = v;}
public function new(text:String, desc:String, options:Array<Dynamic>, displayOptions:Array<String>, optionName:String, ?selectCallback:String->Void = null, ?parent:Dynamic) {
super(desc);
this.selectCallback = selectCallback;
this.displayOptions = displayOptions;
this.options = options;
if (parent == null)
parent = Options;
this.parent = parent;
if(Reflect.field(parent, optionName) != null)
{
if(options.indexOf(Reflect.field(parent, optionName)) != -1)
this.currentSelection = options.indexOf(Reflect.field(parent, optionName));
else
this.currentSelection = 0;
}
this.optionName = optionName;
add(__text = new Alphabet(100, 20, text, true));
add(__selectiontext = new Alphabet(__text.width + 120, -30, formatTextOption(), false));
}
public override function draw() {
super.draw();
}
public override function onChangeSelection(change:Float):Void
{
if(currentSelection <= 0 && change == -1 || currentSelection >= options.length - 1 && change == 1 ) return;
currentSelection += Math.round(change);
__selectiontext.text = formatTextOption();
Reflect.setField(parent, optionName, options[currentSelection]);
if(selectCallback != null)
selectCallback(options[currentSelection]);
}
private function formatTextOption() {
var currentOptionString = ": ";
if((currentSelection > 0))
currentOptionString += "< ";
else
currentOptionString += " ";
currentOptionString += displayOptions[currentSelection];
if(!(currentSelection >= options.length - 1))
currentOptionString += " >";
return currentOptionString;
}
}