Skip to content

Commit

Permalink
Add KeyboardShortcut support in options
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Dec 14, 2023
1 parent 40e687d commit dd2cc40
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions NebulaPatcher/Patches/Dynamic/UIOptionWindow_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using BepInEx.Configuration;
using HarmonyLib;
using NebulaModel;
using NebulaModel.Attributes;
using NebulaModel.Logger;
Expand Down Expand Up @@ -165,7 +166,6 @@ public static void _OnCreate_Postfix(UIOptionWindow __instance)
checkboxTemplate = contentTemplate.Find("fullscreen").GetComponent<RectTransform>();
comboBoxTemplate = contentTemplate.Find("resolution").GetComponent<RectTransform>();
sliderTemplate = contentTemplate.Find("dofblur").GetComponent<RectTransform>();

inputTemplate = Object.Instantiate(checkboxTemplate, listContent, false);
Object.Destroy(inputTemplate.Find("CheckBox").gameObject);
RectTransform inputField = Object.Instantiate(UIRoot.instance.saveGameWindow.transform.Find("input-filename/InputField").GetComponent<RectTransform>(), inputTemplate, false);
Expand Down Expand Up @@ -305,6 +305,10 @@ private static void AddMultiplayerOptionsProperties()
{
CreateEnumControl(displayAttr, descriptionAttr, prop, anchorPosition, container);
}
else if (prop.PropertyType == typeof(BepInEx.Configuration.KeyboardShortcut))
{
CreateHotkeyControl(displayAttr, descriptionAttr, prop, anchorPosition, container);
}
else
{
Log.Warn($"MultiplayerOption property \"${prop.Name}\" of type \"{prop.PropertyType}\" not supported.");
Expand Down Expand Up @@ -396,7 +400,7 @@ private static void CreateNumberControl(DisplayNameAttribute control, Descriptio
{
try
{
TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType);
System.ComponentModel.TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType);
System.IComparable value = (System.IComparable)converter.ConvertFromString(str);

if (rangeAttr != null)
Expand Down Expand Up @@ -479,6 +483,48 @@ private static void CreateEnumControl(DisplayNameAttribute control, DescriptionA
};
}

private static void CreateHotkeyControl(DisplayNameAttribute control, DescriptionAttribute descriptionAttr, PropertyInfo prop, Vector2 anchorPosition, Transform container)
{
UICharacterLimitAttribute characterLimitAttr = prop.GetCustomAttribute<UICharacterLimitAttribute>();
UIContentTypeAttribute contentTypeAttr = prop.GetCustomAttribute<UIContentTypeAttribute>();

RectTransform element = Object.Instantiate(inputTemplate, container, false);
SetupUIElement(element, control, descriptionAttr, prop, anchorPosition);

InputField input = element.GetComponentInChildren<InputField>();
if (characterLimitAttr != null)
{
input.characterLimit = characterLimitAttr.Max;
}
if (contentTypeAttr != null)
{
input.contentType = contentTypeAttr.ContentType;
}

input.onValueChanged.RemoveAllListeners();
input.onValueChanged.AddListener((value) => {
if (!string.IsNullOrEmpty(value))
{
KeyboardShortcut hotkey = KeyboardShortcut.Deserialize(value);
if (hotkey.Equals(KeyboardShortcut.Empty))
{
// Show text color in red when the shortcut is not valid
input.textComponent.color = Color.red;
}
else
{
input.textComponent.color = Color.white;
prop.SetValue(tempMultiplayerOptions, hotkey, null);
}
}
});

tempToUICallbacks[prop.Name] = () =>
{
input.text = ((KeyboardShortcut)prop.GetValue(tempMultiplayerOptions, null)).ToString();
};
}

private static void SetupUIElement(RectTransform element, DisplayNameAttribute display, DescriptionAttribute descriptionAttr, PropertyInfo prop, Vector2 anchorPosition)
{
element.gameObject.SetActive(true);
Expand Down

0 comments on commit dd2cc40

Please sign in to comment.