Skip to content

Commit

Permalink
Mostly just more comments
Browse files Browse the repository at this point in the history
There is a small fix in here though.
  • Loading branch information
SabreML committed Sep 30, 2023
1 parent 5bf3aa8 commit fc428a9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
56 changes: 35 additions & 21 deletions src/JollyMenuKeybinds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ namespace JollyRebind
{
public static class JollyMenuKeybinds
{
// Set to `TRUE` if `JollySetupDialog`'s constructor has finished.
// Used by `OpKeyBinder_set_value()`.
public static bool MenuReady = false;

// Array of `UIelementWrapper`s containing each input rebind button.
public static UIelementWrapper[] KeybindWrappers = new UIelementWrapper[4];


Expand All @@ -26,6 +29,7 @@ public static void SetUpHooks()
On.JollyCoop.JollyMenu.JollyPlayerSelector.Update += JollyPlayerSelector_UpdateHK;
On.JollyCoop.JollyMenu.JollyPlayerSelector.AddColorButton += JollyPlayerSelector_AddColorButtonHK;

// Manual hook for the `set_value()` method of the `OpKeyBinder.value` property.
new ILHook(
typeof(OpKeyBinder).GetProperty("value", BindingFlags.Public | BindingFlags.Instance).GetSetMethod(),
new ILContext.Manipulator(OpKeyBinder_set_value)
Expand All @@ -38,11 +42,13 @@ private static void JollySetupDialogHK(On.JollyCoop.JollyMenu.JollySetupDialog.o
InitBoundKey();
orig(self, name, manager, closeButtonPos);

// Place all of the rebind buttons.
CreateKeyBinders(self.slidingMenu);

// Skip this step if the player count is modified to avoid any mod compatibility issues.
if (KeybindWrappers.Length == 4)
{
// Edit the order in which buttons are selected by the movement keys to account for the new keybinders.
// (Skip this step if the player count is modified to avoid any mod compatibility issues.)
FixSelectableOrder(self.slidingMenu);
}

Expand All @@ -57,6 +63,7 @@ private static void JollySetupDialog_ShutDownProcessHK(On.JollyCoop.JollyMenu.Jo
{
if (KeybindWrappers[i] != null)
{
// Save each keybinder to file and remove them.
KeybindWrappers[i].SaveConfig();
KeybindWrappers[i] = null;
}
Expand All @@ -72,28 +79,34 @@ private static void CreateKeyBinders(JollySlidingMenu self)
{
JollyPlayerSelector playerSelector = self.playerSelector[i];

if (KeybindWrappers[i] == null)
if (KeybindWrappers[i] != null)
{
OpKeyBinder keyBinder = new OpKeyBinder(
JollyRebindConfig.PlayerPointInputs[i],
playerSelector.pos - new Vector2(10f, 73f),
new Vector2(120f, 35f),
false
);
keyBinder.description = $"Click to change Player {i + 1}'s point button";
keyBinder.OnValueUpdate += KeybindValueUpdated;

// If a mod (specifically 'Myriad of Slugcats') has increased the number of players above a certain amount, then the keybinders need to be smaller.
if (KeybindWrappers.Length >= 12)
{
keyBinder.size *= 0.56f;
keyBinder._label.scale *= 0.5f;
keyBinder._sprite.scale *= 0.5f;
}
// If there's already a keybinder for this index, continue.
// (AFAIK this should be impossible, but presumably I added this here for a reason and it's not hurting anything so :shrug:.)
continue;
}

KeybindWrappers[i] = new UIelementWrapper(self.menu.tabWrapper, keyBinder);
Debug.Log($"(JollyRebind) Keybind UI added for player {i + 1}");
OpKeyBinder keyBinder = new OpKeyBinder(
JollyRebindConfig.PlayerPointInputs[i],
playerSelector.pos - new Vector2(10f, 73f),
new Vector2(120f, 35f),
false
);
keyBinder.description = $"Click to change Player {i + 1}'s point button";
keyBinder.OnValueUpdate += KeybindValueUpdated;

// If a mod (specifically 'Myriad of Slugcats') has increased the number of players above a certain amount, then the keybinders need to be smaller.
if (KeybindWrappers.Length >= 12)
{
keyBinder.size *= 0.56f;
keyBinder._label.scale *= 0.5f;
keyBinder._sprite.scale *= 0.5f;
// Also shift them over a bit.
keyBinder.pos += new Vector2(18f, 5f);
}

KeybindWrappers[i] = new UIelementWrapper(self.menu.tabWrapper, keyBinder);
Debug.Log($"(JollyRebind) Keybind UI added for player {i + 1}");
}
}

Expand All @@ -111,6 +124,7 @@ private static void FixSelectableOrder(JollySlidingMenu self)
KeybindWrappers[KeybindWrappers.Length - 1].nextSelectable[2] = KeybindWrappers[0];
}


// Called by the `OpKeyBinder.OnValueUpdate` event.
// This is used to block setting the button to 'none' when the escape key is pressed.
private static void KeybindValueUpdated(UIconfig config, string newValue, string oldValue)
Expand Down Expand Up @@ -140,7 +154,7 @@ private static void JollyPlayerSelector_AddColorButtonHK(On.JollyCoop.JollyMenu.


// Adds a check to the setter of `OpKeyBinder.value` to stop it from playing a sound before the menu is fully loaded.
// This is needed because otherwise all four keybind buttons will try to play a sound at the same time when the menu opens, resulting in a loud noise.
// This is needed because otherwise all of the keybind buttons will try to play a sound at the same time when the menu opens, resulting in a loud noise.
private static void OpKeyBinder_set_value(ILContext il)
{
ILCursor cursor = new ILCursor(il);
Expand Down
2 changes: 1 addition & 1 deletion src/JollyRebindConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JollyRebindConfig : OptionInterface
private static ConfigHolder configHolder;


// Cache `this.config` as a static variable so that I can be a bit lazy with it in `CreateInputConfigs()`.
// Cache `this.config` in a static field so that I can be a bit lazy with it in `CreateInputConfigs()`.
public JollyRebindConfig()
{
configHolder = config;
Expand Down
9 changes: 7 additions & 2 deletions src/JollyRebindMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ namespace JollyRebind
[BepInPlugin("sabreml.jollyrebind", "JollyRebind", "1.2.2")]
public class JollyRebindMod : BaseUnityPlugin
{
// The number of co-op players
// The maximum number of co-op players. (Default: 4)
// The 'Myriad of Slugcats' mod can increase this up to 16.
public static int PlayerCount;


// A `HashSet` of previously logged controller element exceptions.
// These are tracked because `JollyInputUpdate` happens once every frame, and this could very quickly spam the log file otherwise.
// (`HashSet`s don't allow duplicate entries.)
private static readonly HashSet<string> exceptionLogLog = new HashSet<string>();


Expand All @@ -42,17 +44,20 @@ private void Init(On.RainWorld.orig_OnModsInit orig, RainWorld self)
// 'Myriad of Slugcats' compatibility.
private void PostInit(On.RainWorld.orig_PostModsInit orig, RainWorld self)
{
// Always 4 by default, but Myriad can increase it up to 16.
// The `PlayerObjectBodyColors` field seems to be the most reliable way to get a max player count.
PlayerCount = RainWorld.PlayerObjectBodyColors.Length;
if (PlayerCount > 4)
{
// Remake these arrays with a bigger size.
JollyMenuKeybinds.KeybindWrappers = new Menu.Remix.UIelementWrapper[PlayerCount];
JollyRebindConfig.PlayerPointInputs = new Configurable<KeyCode>[PlayerCount];
}

// Whether it's modified or not, finish setting up the input configs.
JollyRebindConfig.CreateInputConfigs();
}


// If the player has a custom keybind set (AKA: Not the map key), this method sets `jollyButtonDown` to true if the key is being held down.
private void JollyInputUpdateHK(On.Player.orig_JollyInputUpdate orig, Player self)
{
Expand Down

0 comments on commit fc428a9

Please sign in to comment.