custom controls stuff

This commit is contained in:
TheZoroForce240
2024-08-29 19:48:22 +01:00
parent fc5c57f731
commit ba129a9c8c
6 changed files with 287 additions and 23 deletions
@@ -15,16 +15,19 @@ class KeybindSetting extends FlxTypedSpriteGroup<FlxSprite> {
public var p2Selected:Bool = false;
public var value:String;
public var custom:Bool = false;
public var option1:Null<FlxKey>;
public var option2:Null<FlxKey>;
public function new(x:Float, y:Float, name:String, value:String, ?sparrowIcon:String, ?sparrowAnim:String) {
public function new(x:Float, y:Float, name:String, value:String, ?sparrowIcon:String, ?sparrowAnim:String, ?custom:Bool = false) {
super();
this.value = value;
this.custom = custom;
title = new Alphabet(0, 0, name, true);
add(title);
var controlArrayP1:Array<FlxKey> = Reflect.field(Options, 'P1_${value}');
var controlArrayP2:Array<FlxKey> = Reflect.field(Options, 'P2_${value}');
var controlArrayP1:Array<FlxKey> = custom ? [Reflect.getProperty(FlxG.save.data, 'P1_${value}')] : Reflect.field(Options, 'P1_${value}');
var controlArrayP2:Array<FlxKey> = custom ? [Reflect.getProperty(FlxG.save.data, 'P2_${value}')] : Reflect.field(Options, 'P2_${value}');
option1 = controlArrayP1[0];
option2 = controlArrayP2[0];
@@ -76,19 +79,35 @@ class KeybindSetting extends FlxTypedSpriteGroup<FlxSprite> {
KeybindsOptions.instance.openSubState(new ChangeKeybindSubState(function(key:FlxKey) {
flicker.stop();
flicker.destroy();
if (p2)
Reflect.setField(Options, 'P2_$value', [option2 = key]);
else
Reflect.setField(Options, 'P1_$value', [option1 = key]);
if (!custom) {
if (p2)
Reflect.setField(Options, 'P2_$value', [option2 = key]);
else
Reflect.setField(Options, 'P1_$value', [option1 = key]);
} else {
if (p2)
Reflect.setProperty(FlxG.save.data, 'P2_$value', option2 = key);
else
Reflect.setProperty(FlxG.save.data, 'P1_$value', option1 = key);
}
updateText();
callback();
}, function() {
flicker.stop();
flicker.destroy();
if (p2)
Reflect.setField(Options, 'P2_$value', [option2 = 0]);
else
Reflect.setField(Options, 'P1_$value', [option1 = 0]);
if (!custom) {
if (p2)
Reflect.setField(Options, 'P2_$value', [option2 = 0]);
else
Reflect.setField(Options, 'P1_$value', [option1 = 0]);
} else {
if (p2)
Reflect.setProperty(FlxG.save.data, 'P2_$value', option2 = 0);
else
Reflect.setProperty(FlxG.save.data, 'P1_$value', option1 = 0);
}
updateText();
cancelCallback();
}));
}
@@ -1,13 +1,14 @@
package funkin.options.keybinds;
import flixel.util.FlxColor;
import haxe.xml.Access;
using StringTools;
class KeybindsOptions extends MusicBeatSubstate {
public static var instance:KeybindsOptions;
public var categories = [
public var categories:Array<ControlsCategory> = [
{
name: 'Notes',
settings: [
@@ -127,6 +128,9 @@ class KeybindsOptions extends MusicBeatSubstate {
FlxG.camera.follow(camFollow, LOCKON, 0.125);
}
var customCategories = loadCustomCategories();
for (i in customCategories) categories.push(i);
var k:Int = 0;
for(category in categories) {
k++;
@@ -152,8 +156,10 @@ class KeybindsOptions extends MusicBeatSubstate {
};
e.name = e.name.substring(5, e.name.length - 1);
}
if (e.sparrowIcon != null) sparrowIcon = e.sparrowIcon;
if (e.sparrowAnim != null) sparrowAnim = e.sparrowAnim;
var text = new KeybindSetting(100, k * 75, e.name, e.control, sparrowIcon, sparrowAnim);
var text = new KeybindSetting(100, k * 75, e.name, e.control, sparrowIcon, sparrowAnim, e.custom == null ? false : e.custom);
if (!isSubState)
text.bind1.color = text.bind2.color = FlxColor.BLACK;
alphabets.add(text);
@@ -199,7 +205,9 @@ class KeybindsOptions extends MusicBeatSubstate {
close();
else
FlxG.switchState(new OptionsMenu());
ControlsUtil.resetCustomControls();
Options.applyKeybinds();
ControlsUtil.loadCustomControls();
Options.save();
return;
}
@@ -248,4 +256,59 @@ class KeybindsOptions extends MusicBeatSubstate {
camFollow.setPosition(FlxG.width / 2, FlxG.height / 2);
}
}
public function loadCustomCategories() {
var customCategories:Array<ControlsCategory> = [];
var xmlPath = Paths.xml("config/controls");
for(source in [funkin.backend.assets.AssetsLibraryList.AssetSource.SOURCE, funkin.backend.assets.AssetsLibraryList.AssetSource.MODS]) {
if (Paths.assetsTree.existsSpecific(xmlPath, "TEXT", source)) {
var access:Access = null;
try {
access = new Access(Xml.parse(Paths.assetsTree.getSpecificAsset(xmlPath, "TEXT", source)).firstElement());
} catch(e) {
Logs.trace('Error while parsing controls.xml: ${Std.string(e)}', ERROR);
}
if (access != null) {
for (category in access.elements) {
if (!category.has.name) continue;
var cat:ControlsCategory = {
name: category.getAtt("name"),
settings: []
};
for (control in category.elements) {
if (control.has.menuName && control.has.saveName) {
cat.settings.push({
name: control.getAtt("menuName"),
control: control.getAtt("saveName"),
custom: true,
sparrowIcon: control.getAtt("menuIcon").getDefault(null),
sparrowAnim: control.getAtt("menuAnim").getDefault(null)
});
}
}
customCategories.push(cat);
}
}
}
}
return customCategories;
}
}
typedef KeybindSettingData = {
var name:String;
var control:String;
var ?custom:Bool;
var ?sparrowIcon:String;
var ?sparrowAnim:String;
}
typedef ControlsCategory = {
var name:String;
var settings:Array<KeybindSettingData>;
}