From 6d73a9ef4b576217b2b7a33aa37be4137efd2030 Mon Sep 17 00:00:00 2001 From: bctix <51544115+bctix@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:04:41 -0500 Subject: [PATCH] Add array option type (#241) * Add Array option type * Prevent nulls * add display options * change xml structure --- assets/data/config/options.xml | 6 +- source/funkin/options/OptionsMenu.hx | 17 +++++ source/funkin/options/type/ArrayOption.hx | 77 +++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 source/funkin/options/type/ArrayOption.hx diff --git a/assets/data/config/options.xml b/assets/data/config/options.xml index e14baa09..888288d7 100644 --- a/assets/data/config/options.xml +++ b/assets/data/config/options.xml @@ -4,9 +4,9 @@ - - - + + + diff --git a/source/funkin/options/OptionsMenu.hx b/source/funkin/options/OptionsMenu.hx index b0546bc6..186e3249 100644 --- a/source/funkin/options/OptionsMenu.hx +++ b/source/funkin/options/OptionsMenu.hx @@ -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 = []; + var optionDisplayOptions:Array = []; + + 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))); diff --git a/source/funkin/options/type/ArrayOption.hx b/source/funkin/options/type/ArrayOption.hx new file mode 100644 index 00000000..edbc4e72 --- /dev/null +++ b/source/funkin/options/type/ArrayOption.hx @@ -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; + public var displayOptions:Array; + + 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, displayOptions:Array, 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; + } +} \ No newline at end of file