Skip to content

Commit

Permalink
Keybind & Dropdown Work
Browse files Browse the repository at this point in the history
  • Loading branch information
dodad-2 committed Feb 22, 2024
1 parent c9a4840 commit 1f53541
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Mod.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using MelonLoader;
using QList;

[assembly: MelonInfo(typeof(Mod), "QList", "0.1.1", "dodad")]
[assembly: MelonInfo(typeof(Mod), "QList", "0.1.2", "dodad")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonPriority(-99)]

Expand Down
127 changes: 117 additions & 10 deletions OptionTypes/OptionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace QList.OptionTypes;
using UnityEngine;
using Newtonsoft.Json;

// TODO
// Each option needs to have standardized logic concerning GetValue, SetValue and Actions

public abstract class BaseOption
{
protected static readonly string defaultCategory = "Options";
Expand Down Expand Up @@ -325,7 +328,10 @@ private void OnValueChanged(object oldValue, object newValue)
var newValueString = newValue.ToString();

if (newValueString == null)
{
Log.LogOutput($"{this.GetType()}.OnValueChanged: Could not parse '{Name}'", Log.LogLevel.Warning);
return;
}

currentValue = newValueString;

Expand Down Expand Up @@ -361,7 +367,7 @@ public virtual void Click()
OnClick?.Invoke(this);
}
}
public class KeybindOption : BaseOption // TODO: currentValue needs to be the serialized version of currentKeybind
public class KeybindOption : BaseOption
{
public new Action<KeybindOption>? OnValueChangedOption;
public Action<KeybindOption>? OnKeybindDown;
Expand All @@ -381,8 +387,8 @@ public KeybindOption(MelonPreferences_Entry? entry = null, KeyCode[]? currentVal

if (this.entry != null)
{
//this.onEntryValueChangedUntyped = new LemonAction<object, object>(OnValueChanged);
//this.entry.OnEntryValueChangedUntyped.Subscribe(onEntryValueChangedUntyped);
this.onEntryValueChangedUntyped = new LemonAction<object, object>(OnValueChanged);
this.entry.OnEntryValueChangedUntyped.Subscribe(onEntryValueChangedUntyped);

KeyCode[]? boxedArray = null;
var json = this.entry.BoxedValue.ToString();
Expand All @@ -403,16 +409,24 @@ public override void SetValue(object newValue)
{
KeyCode[] newValueArray = new KeyCode[0];

if (newValue != null)
newValueArray = (KeyCode[])newValue;
if (newValue == null)
{
Log.LogOutput($"{this.GetType()}.OnValueChanged: Could not parse null value '{Name}'", Log.LogLevel.Warning); // TODO ensure this change doesn't cause problems (no longer nullable)
return;
}

if (entry != null)
entry.BoxedValue = JsonConvert.SerializeObject(newValueArray);
newValueArray = (KeyCode[])newValue;

var oldValue = currentValue;
currentValue = newValueArray;
if (newValueArray == null)
{
Log.LogOutput($"{this.GetType()}.OnValueChanged: Could not parse '{Name}'", Log.LogLevel.Warning);
return;
}

this.OnValueChanged(oldValue, currentValue);
if (entry != null)
entry.BoxedValue = JsonConvert.SerializeObject(newValueArray);
else
this.OnValueChanged(currentValue == null ? new KeyCode[0] : currentValue, newValueArray);
}
public override void OnUpdate()
{
Expand Down Expand Up @@ -453,7 +467,100 @@ private void OnValueChanged(object oldValue, object newValue)
if (newValue == null)
return;

KeyCode[]? newValueArray = null;

try
{
newValueArray = (KeyCode[])newValue;
}
catch
{
var json = newValue.ToString();

try
{
if (json != null)
newValueArray = JsonConvert.DeserializeObject<KeyCode[]>(json);
}
catch (Exception e)
{
Log.LogOutput($"{this.GetType()}.OnValueChanged: Could not parse '{Name}', {e}", Log.LogLevel.Warning);
return;
}
}

if (newValueArray == null)
{
Log.LogOutput($"{this.GetType()}.OnValueChanged: Could not parse '{Name}'", Log.LogLevel.Warning);
return;
}

currentValue = newValueArray;

this.OnValueChangedUntyped?.Invoke(oldValue, newValue);
this.OnValueChangedOption?.Invoke(this);
}
}
public class DropdownOption<T> : BaseOption
{
public new Action<DropdownOption<T>>? OnValueChangedOption;
internal string[]? optionList;
internal string? currentValue;

public DropdownOption(MelonPreferences_Entry? entry = null, string? currentValue = null, string[]? optionList = null) : base(entry)
{
this.entry = entry;
this.onEntryValueChangedUntyped = new LemonAction<object, object>(OnValueChanged);

if (currentValue != null)
this.currentValue = currentValue;

if (this.entry != null)
{
this.entry.OnEntryValueChangedUntyped.Subscribe(onEntryValueChangedUntyped);

var boxedString = this.entry.BoxedValue.ToString();

if (boxedString != null)
this.currentValue = boxedString;
}

if (optionList != null)
this.optionList = optionList;
}

public string GetValue()
{
if (currentValue == null)
return "";

return currentValue;
}
public string[] GetOptions()
{
if (optionList == null)
return new string[0];

return optionList;
}
public override void SetValue(object newValue)
{
string newString = "";

if (newValue != null)
newString = (string)newValue;

if (entry != null)
entry.BoxedValue = newString;

var oldValue = currentValue == null ? "" : currentValue;
currentValue = newString;

this.OnValueChanged(oldValue, newString);
}
private void OnValueChanged(object oldValue, object newValue)
{
this.OnValueChangedUntyped?.Invoke(oldValue, newValue); // TODO melon preference changes need to properly update KeybindOption and DropdownOption's currentValue
this.OnValueChangedOption?.Invoke(this);
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Option types are located at `QList.OptionTypes`. Currently available:

# Changelog

**0.1.2**

- Options: `KeybindOption` properly updates when the MelonPreference value is changed

**0.1.1**

- Mod Options Window: Reopens to last mod while ingame
Expand Down
1 change: 0 additions & 1 deletion UI/OptionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ private void OnButtonClick()
//[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void OnKeybindButtonClick()
{
Log.LogOutput($"OnKeybindButtonClick");
var keybindButton = option as KeybindOption;

if (keybindButton == null)
Expand Down

0 comments on commit 1f53541

Please sign in to comment.