1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2025-09-05 13:27:39 +00:00
Funkin/source/funkin/ui/options/items/EnumPreferenceItem.hx

92 lines
2.3 KiB
Haxe
Raw Permalink Normal View History

2024-07-01 11:42:50 +00:00
package funkin.ui.options.items;
2025-02-13 01:35:37 +00:00
import funkin.ui.TextMenuList.TextMenuItem;
2024-07-01 11:42:50 +00:00
import funkin.ui.AtlasText;
import funkin.input.Controls;
#if mobile
import funkin.util.SwipeUtil;
#end
2024-07-01 11:42:50 +00:00
/**
* Preference item that allows the player to pick a value from an enum (list of values)
*/
2025-02-20 22:49:35 +00:00
class EnumPreferenceItem<T> extends TextMenuItem
2024-07-01 11:42:50 +00:00
{
function controls():Controls
{
return PlayerSettings.player1.controls;
}
public var lefthandText:AtlasText;
2025-02-20 22:49:35 +00:00
public var currentKey:String;
public var onChangeCallback:Null<String->T->Void>;
public var map:Map<String, T>;
2024-07-01 11:42:50 +00:00
public var keys:Array<String> = [];
var index = 0;
2025-02-20 22:49:35 +00:00
public function new(x:Float, y:Float, name:String, map:Map<String, T>, defaultKey:String, ?callback:String->T->Void)
2024-07-01 11:42:50 +00:00
{
super(x, y, name, function() {
2025-02-20 22:49:35 +00:00
var value = map.get(this.currentKey);
callback(this.currentKey, value);
2024-07-01 11:42:50 +00:00
});
updateHitbox();
this.map = map;
2025-02-20 22:49:35 +00:00
this.currentKey = defaultKey;
2024-07-01 11:42:50 +00:00
this.onChangeCallback = callback;
var i:Int = 0;
for (key in map.keys())
{
2025-02-20 22:49:35 +00:00
var value:T = map[key];
2024-07-01 11:42:50 +00:00
this.keys.push(key);
2025-02-20 22:49:35 +00:00
if (this.currentKey == key) index = i;
2024-07-01 11:42:50 +00:00
i += 1;
}
lefthandText = new AtlasText(x + 15, y, formatted(defaultKey), AtlasFont.DEFAULT);
this.fireInstantly = true;
2024-07-01 11:42:50 +00:00
}
override function update(elapsed:Float):Void
{
super.update(elapsed);
// var fancyTextFancyColor:Color;
if (selected)
{
var shouldDecrease:Bool = controls().UI_LEFT_P #if mobile || SwipeUtil.justSwipedLeft #end;
var shouldIncrease:Bool = controls().UI_RIGHT_P #if mobile || SwipeUtil.justSwipedRight #end;
2024-07-01 11:42:50 +00:00
if (shouldDecrease) index -= 1;
if (shouldIncrease) index += 1;
if (index > keys.length - 1) index = 0;
if (index < 0) index = keys.length - 1;
2025-02-20 22:49:35 +00:00
currentKey = keys[index];
2024-07-01 11:42:50 +00:00
if (onChangeCallback != null && (shouldIncrease || shouldDecrease))
{
2025-02-20 22:49:35 +00:00
var value = map.get(currentKey);
onChangeCallback(currentKey, value);
2024-07-01 11:42:50 +00:00
}
}
2025-02-20 22:49:35 +00:00
lefthandText.text = formatted(currentKey);
2024-07-01 11:42:50 +00:00
}
2025-02-20 22:49:35 +00:00
function formatted(key:String):String
2024-07-01 11:42:50 +00:00
{
// FIXME: Can't add arrows around the text because the font doesn't support < >
// var leftArrow:String = selected ? '<' : '';
// var rightArrow:String = selected ? '>' : '';
2025-02-20 22:49:35 +00:00
return '${key}';
2024-07-01 11:42:50 +00:00
}
}