Skip to content

Commit

Permalink
user content for mod settings pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Aug 4, 2024
1 parent 311335f commit 722c30e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- Added new events to `SdkEvents` (`OnWorldExited`, `OnPauseMenuOpened`, `OnPauseMenuClosed`)
-
- Added new events to `SdkEvents` (`OnWorldExited`, `OnPauseMenuOpened`, `OnPauseMenuClosed`).
- Auto assign the SettingsEntry of a config to the first field of type `SettingsEnry`.
- Set the valid scope of a InputAction if you only want a hotkey to trigger in spcific scenarios.
- Allow mods to add custom user content to the mods settings screen.
17 changes: 17 additions & 0 deletions SonsSdk/ModInputCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public static InputAction GetAction(this KeybindConfigEntry config)
return GetKeybind(config).Action;
}

/// <summary>
/// Set when the action can trigger. If <paramref name="needsPlayerControllable"/> is true, set the other two options to false as they are automatically checked.
/// </summary>
/// <param name="needsInGame">Does the player need to be in a world</param>
/// <param name="ignoreInConsole">Don't trigger the action in the console</param>
/// <param name="needsPlayerControllable">Don't trigger the action in the console, book, cutscene etc. (uses LocalPlayer.IsInWorld)</param>
public static void SetScope(this KeybindConfigEntry config, bool needsInGame = false, bool ignoreInConsole = false, bool needsPlayerControllable = false)
{
var keybind = GetKeybind(config);
if (keybind == null)
{
return;
}

keybind.SetScope(needsInGame, ignoreInConsole, needsPlayerControllable);
}

/// <summary>
/// Registers a callback for the "Performed" event of the input action.
/// </summary>
Expand Down
47 changes: 46 additions & 1 deletion SonsSdk/SUI/SettingsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,42 @@ public class SettingsRegistry

public static void CreateSettings<T>(ModBase mod, T settingsObject, bool changesNeedRestart = false, Action callback = null)
{
CreateSettings(mod, settingsObject, typeof(T));
CreateSettingsInternal(mod, settingsObject, typeof(T), changesNeedRestart, callback, null);
}

public static void CreateSettings<T>(ModBase mod, T settingsObject, SContainerOptions userContent, bool changesNeedRestart = false, Action callback = null)
{
CreateSettingsInternal(mod, settingsObject, typeof(T), changesNeedRestart, callback, userContent);
}

public static void CreateSettings(
ModBase mod,
object settingsObject,
Type settingsType,
bool changesNeedRestart = false,
Action callback = null)
{
CreateSettingsInternal(mod, settingsObject, settingsType, changesNeedRestart, callback, null);
}

public static void CreateSettings(
ModBase mod,
object settingsObject,
Type settingsType,
SContainerOptions userContent,
bool changesNeedRestart = false,
Action callback = null)
{
CreateSettingsInternal(mod, settingsObject, settingsType, changesNeedRestart, callback, userContent);
}

private static void CreateSettingsInternal(
ModBase mod,
object settingsObject,
Type settingsType,
bool changesNeedRestart,
Action callback,
SContainerOptions userContent)
{
var container = SContainer;
var configList = new List<SettingsConfigEntry>();
Expand All @@ -54,6 +81,13 @@ public static void CreateSettings(
{
settingsEntryField.SetValue(settingsObject, entry);
}

entry.UserContentContainer = userContent;

if (userContent != null)
{
userContent.RectTransform.SetParent(entry.Container.RectTransform, false);
}
}

private static void GenerateUi(ModBase mod,
Expand Down Expand Up @@ -463,6 +497,7 @@ private void CreateSpacing()
public class SettingsEntry
{
public SContainerOptions Container;
public SContainerOptions UserContentContainer;
internal Action Callback;
internal Action ConfigClassCallback;
public bool ChangesNeedRestart;
Expand Down Expand Up @@ -544,10 +579,20 @@ public void ParentTo(Transform parent)
cfg.RefreshVisibility();
}
}

if (UserContentContainer.Root)
{
UserContentContainer.RectTransform.SetParent(parent, false);
}
}

public void Unparent()
{
if (UserContentContainer.Root)
{
UserContentContainer.RectTransform.SetParent(Container.RectTransform, false);
}

foreach (var entry in ConfigEntries)
{
var element = entry.UiElement;
Expand Down

0 comments on commit 722c30e

Please sign in to comment.