Skip to content

Commit

Permalink
keybind config
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Oct 16, 2023
1 parent b2e9986 commit 0adf4b5
Show file tree
Hide file tree
Showing 20 changed files with 917 additions and 56 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Added `gotopickup` command to quickly go to a pickup location (useful for finding story items)
- Added `dump` command to dump various data (like `dump items` for a list of all items)
- Fix settings getting combined when exiting via escape key
- Fix settings getting combined when exiting via escape key
- Fix boot.txt not being able to execute custom registered commands
- Added easier control over generated settings screens
45 changes: 45 additions & 0 deletions RedLoader/Preferences/ConfigCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,50 @@ public ConfigEntry<T> CreateEntry<T>(string identifier, T default_value, string

return entry;
}

public KeybindConfigEntry CreateKeybindEntry(string identifier, string key_name, string display_name = null,
string description = null, bool is_hidden = false, bool dont_save_default = false, Preferences.ValueValidator validator = null, string oldIdentifier = null)
{
if (string.IsNullOrEmpty(identifier))
throw new Exception("identifier is null or empty when calling CreateEntry");

if (display_name == null)
display_name = identifier;

var entry = GetKeybindEntry(identifier);
if (entry != null)
throw new Exception($"Calling CreateEntry for { display_name } when it Already Exists");

// if (validator != null && !validator.IsValid(default_value))
// throw new ArgumentException($"Default value '{default_value}' is invalid according to the provided ValueValidator!");

if (oldIdentifier != null)
{
if (HasEntry(oldIdentifier))
throw new Exception($"Unable to rename '{oldIdentifier}' when it got already loaded");

RenameEntry(oldIdentifier, identifier);
}

entry = new KeybindConfigEntry(
identifier,
display_name,
description,
is_hidden,
dont_save_default,
this,
key_name,
validator);

Preferences.IO.File currentFile = File;
if (currentFile == null)
currentFile = ConfigSystem.DefaultFile;
currentFile.SetupEntryFromRawValue(entry);

Entries.Add(entry);

return entry;
}

public bool DeleteEntry(string identifier)
{
Expand Down Expand Up @@ -106,6 +150,7 @@ public ConfigEntry GetEntry(string identifier)
return Entries.Find(x => x.Identifier.Equals(identifier));
}
public ConfigEntry<T> GetEntry<T>(string identifier) => (ConfigEntry<T>)GetEntry(identifier);
public KeybindConfigEntry GetKeybindEntry(string identifier) => (KeybindConfigEntry)GetEntry(identifier);
public bool HasEntry(string identifier) => GetEntry(identifier) != null;

public void SetFilePath(string filepath) => SetFilePath(filepath, true, true);
Expand Down
24 changes: 24 additions & 0 deletions RedLoader/Preferences/ConfigEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,28 @@ internal void SetDefaultValue(T value)
DontRegisterChanges = true;
}
}

public class KeybindConfigEntry : ConfigEntry<string>
{
public string KeyboardControlPath => "<keyboard>/" + Value;

public KeybindConfigEntry(
string identifier,
string displayName,
string description,
bool isHidden,
bool dontSaveDefault,
ConfigCategory category,
string value,
ValueValidator validator)
: base(identifier,
displayName,
description,
isHidden,
dontSaveDefault,
category,
value,
validator)
{ }
}
}
17 changes: 17 additions & 0 deletions RedLoader/Preferences/ConfigSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ public static ConfigEntry<T> CreateEntry<T>(string category_identifier, string e

return category.CreateEntry(entry_identifier, default_value, display_name, description, is_hidden, dont_save_default, validator);
}

public static KeybindConfigEntry CreateKeybindEntry(string category_identifier, string entry_identifier, string key_name,
string display_name = null, string description = null, bool is_hidden = false, bool dont_save_default = false,
ValueValidator validator = null)
{
if (string.IsNullOrEmpty(category_identifier))
throw new Exception("category_identifier is null or empty when calling CreateEntry");

if (string.IsNullOrEmpty(entry_identifier))
throw new Exception("entry_identifier is null or empty when calling CreateEntry");

ConfigCategory category = GetCategory(entry_identifier);
if (category == null)
category = CreateCategory(category_identifier);

return category.CreateKeybindEntry(entry_identifier, key_name, display_name, description, is_hidden, dont_save_default, validator);
}

public static ConfigCategory GetCategory(string identifier)
{
Expand Down
15 changes: 15 additions & 0 deletions RedLoader/Utils/Observable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ public void Set(T value)
{
Value = value;
}

/// <summary>
/// Removes all event listeners.
/// </summary>
public void RemoveEvents()
{
OnValueChanged = null;
}
}

public static class ObservableExtensions
{
/// <summary>
/// Creates a new observable with the config value.
/// Also registers <see cref="Observable{T}.OnValueChanged"/> so that the config entry is updated when the observalble value changes.
/// </summary>
/// <param name="configEntry"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Observable<T> ToObservable<T>(this ConfigEntry<T> configEntry)
{
var observable = new Observable<T>(configEntry.Value);
Expand Down
11 changes: 9 additions & 2 deletions SonsGameManager/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Construction;
using RedLoader;
using RedLoader.Utils;
using Sons.Ai.Vail;
using Sons.Gameplay;
using Sons.Gui;
using Sons.Items.Core;
Expand All @@ -27,6 +28,7 @@ public Core()
{
Instance = this;
OnUpdateCallback = OnUpdate;
OnCommandsRegisteredCallback = OnCommandsRegistered;
}

protected override void OnInitializeMod()
Expand Down Expand Up @@ -103,13 +105,18 @@ protected override void OnGameStart()
{
RepositioningUtils.Manager.SetSkipPlaceAnimations(true);
}


PanelBlur.SetupBlur();

// -- Enable Bow Trajectory --
// if (Config.EnableBowTrajectory.Value)
// {
// BowTrajectory.Init();
// }

}

private void OnCommandsRegistered()
{
LoadBootFile();
}

Expand Down
12 changes: 7 additions & 5 deletions SonsGameManager/ModSettingsUi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RedLoader;
using Sons.Ai.Vail;
using SonsSdk;
using SUI;
using UnityEngine;
Expand All @@ -15,7 +16,7 @@ public class ModSettingsUi
private static readonly BackgroundDefinition MainBg = new(
"#fff",
GetBackgroundSprite(EBackground.Sons),
Image.Type.Sliced);
Image.Type.Simple);

private static readonly BackgroundDefinition ButtonBg = new(
ColorFromString("#191A23"),
Expand Down Expand Up @@ -51,17 +52,18 @@ public static void Create()
.Dock(EDockType.Top).Size(-100, 3).Position(0, -100));

_mainContainer = SScrollContainer.Margin(100,100,120,130).As<SScrollContainerOptions>();
_mainContainer.ContainerObject.Padding(2, 4, 0, 0);
panel.Add(_mainContainer);

panel.Add(SBgButton
.Background(ButtonBg).Background("#990508").Ppu(3)
.Pivot(1, 0).Anchor(AnchorType.BottomRight).Position(-40, 40).Size(300, 60)
.RichText("Back " + SpriteText("arrow_right")).FontSize(20).Notify(Close));
.Pivot(0, 0).Anchor(AnchorType.BottomLeft).Position(80, 80).Size(300, 60)
.RichText(SpriteText("arrow_left") + " Back").FontSize(20).Notify(Close));

panel.Add(SBgButton
.Background(ButtonBg).Background("#796C4E").Ppu(3)
.Pivot(0, 0).Anchor(AnchorType.BottomLeft).Position(40, 40).Size(300, 60)
.RichText(SpriteText("arrow_left") + " Revert").FontSize(20).Notify(RevertSettings));
.Pivot(0, 0).Anchor(AnchorType.BottomLeft).Position(390, 80).Size(200, 60)
.RichText("Revert " + SpriteText("arrow_right")).FontSize(20).Notify(RevertSettings));
}

public static void Open(string id)
Expand Down
21 changes: 21 additions & 0 deletions SonsSdk/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public static void Destroy<T>(this GameObject go) where T : Component
Object.Destroy(component);
}
}

public static void TryDestroy(this GameObject go)
{
if (!go)
return;

Object.Destroy(go);
}

public static UnityEngine.Color ToUnityColor(this Color color)
{
Expand Down Expand Up @@ -151,4 +159,17 @@ public static T FirstContains<T>(this IEnumerable<T> iter, string name) where T
{
return iter.First(x => x.name.Contains(name));
}

public static void Finish(this CancellationTokenSource cts)
{
cts.Cancel();
cts.Dispose();
}

public static CancellationTokenSource ResetAndCreate(this CancellationTokenSource cts)
{
cts.Cancel();
cts.Dispose();
return new CancellationTokenSource();
}
}
92 changes: 92 additions & 0 deletions SonsSdk/ItemTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RedLoader.Utils;
using Sons.Items.Core;
using UnityEngine;
using UnityEngine.Localization.Settings;

namespace SonsSdk;

Expand Down Expand Up @@ -33,6 +34,97 @@ public static void AddItemComponent(int itemId, Type component, EPrefabType hook
{
ItemHookAdder.Add(new ItemHook(itemId, component, hookType));
}

public static Transform GetHeldPrefab(int itemId)
{
var item = ItemDatabaseManager.ItemById(itemId);
if (!item)
return null;

return item._heldPrefab;
}

public static Transform GetPickupPrefab(int itemId)
{
var item = ItemDatabaseManager.ItemById(itemId);
if (!item)
return null;

return item._pickupPrefab;
}

public static Transform GetPropPrefab(int itemId)
{
var item = ItemDatabaseManager.ItemById(itemId);
if (!item)
return null;

return item._propPrefab;
}

public static (Texture icon, Texture outline) GetIcon(int itemId)
{
var item = ItemDatabaseManager.ItemById(itemId);
if (!item)
return default;

return (item._uiData._icon, item._uiData._outlineIcon);
}

public static void RegisterItem(ItemData itemData, bool autoTranslationEntry = true)
{
if (ItemDatabaseManager._itemsCache.ContainsKey(itemData._id))
throw new Exception($"Item with id {itemData._id} already registered!");

ItemDatabaseManager._itemsCache.Add(itemData._id, itemData);
ItemDatabaseManager._instance._itemDataList.Add(itemData);

if (itemData._uiData != null && autoTranslationEntry)
{
SetupLocalizationForItem(itemData._uiData, itemData._uiData._title);
}
}

private static void SetupLocalizationForItem(ItemUiData itemUiData, string itemTitle)
{
var table = LocalizationSettings.StringDatabase.GetTable("Items");
if (string.IsNullOrEmpty(itemUiData._translationKey))
{
itemUiData._translationKey = $"I_{itemUiData._itemId}";
}

if (itemUiData._applyCustomActionText)
{
var leftActionText = itemUiData._leftActionCustomText;
var rightActionText = itemUiData._rightActionCustomText;

if (!string.IsNullOrEmpty(leftActionText))
{
var key = $"{itemUiData._translationKey}_LEFT_ACTION";
table.AddEntry(key, leftActionText);
itemUiData._leftActionCustomText = key;
}

if (!string.IsNullOrEmpty(rightActionText))
{
var key = $"{itemUiData._translationKey}_RIGHT_ACTION";
table.AddEntry(key, rightActionText);
itemUiData._rightActionCustomText = key;
}
}

var description = itemUiData._description;
if (!string.IsNullOrEmpty(description))
{
table.AddEntry($"{itemUiData._translationKey}_DESC", description);
}

if (!string.IsNullOrEmpty(itemTitle))
{
table.AddEntry(itemUiData._translationKey, itemTitle);
table.AddEntry($"{itemUiData._translationKey}_PLURAL", itemTitle + "s");
}
}

public struct ItemHook
{
Expand Down
Loading

0 comments on commit 0adf4b5

Please sign in to comment.