Skip to content

Commit

Permalink
Add array option type (#241)
Browse files Browse the repository at this point in the history
* Add Array option type

* Prevent nulls

* add display options

* change xml structure
  • Loading branch information
bctix authored Feb 8, 2024
1 parent cb5fb40 commit 6d73a9e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
6 changes: 3 additions & 3 deletions assets/data/config/options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
17 changes: 17 additions & 0 deletions source/funkin/options/OptionsMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
77 changes: 77 additions & 0 deletions source/funkin/options/type/ArrayOption.hx
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 6d73a9e

Please sign in to comment.