Skip to content

Commit

Permalink
Fix inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Dec 16, 2023
1 parent bc153af commit 4f0cfc5
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
- Added more events
- Added more events.
- Allow binding keybind config to panel visibility.
- Added `clearpickups` command (removes all pickups in a radius).
- Fixed some inputs not working sometimes.
- Various bug fixes.
- SUI improvements.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</p>

<p align="center">
<a href="https://github.com/ToniMacaroni/RedLoader/releases/latest"><img src="https://img.shields.io/github/downloads/ToniMacaroni/SaberFactory/total?label=downloads&style=for-the-badge"></a>
<a href="https://github.com/ToniMacaroni/RedLoader/releases/latest"><img src="https://img.shields.io/github/v/release/ToniMacaroni/SonsModLoader?label=latest&style=for-the-badge"></a>
</p>

Expand Down
2 changes: 1 addition & 1 deletion RedLoader/Utils/Observable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public T Value
get => _value;
set
{
if (_value.Equals(value))
if ((_value == null && value == null) || (_value != null && _value.Equals(value)))
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions Resources/README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</p>

<p align="center">
<a href="https://github.com/ToniMacaroni/RedLoader/releases/latest"><img src="https://img.shields.io/github/downloads/ToniMacaroni/SaberFactory/total?label=downloads&style=for-the-badge"></a>
<a href="https://github.com/ToniMacaroni/RedLoader/releases/latest"><img src="https://img.shields.io/github/v/release/ToniMacaroni/SonsModLoader?label=latest&style=for-the-badge"></a>
</p>

Expand Down
2 changes: 1 addition & 1 deletion SonsGameManager/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected override void OnGameStart()
Log("======= GAME STARTED ========");

// -- Enable debug console --
DebugConsole.Instance.enabled = true;
//DebugConsole.Instance.enabled = true;
DebugConsole.SetCheatsAllowed(true);
DebugConsole.Instance.SetBlockConsole(false);

Expand Down
23 changes: 23 additions & 0 deletions SonsGameManager/DebugCommands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Text;
using AdvancedTerrainGrass;
using Endnight.Utilities;
using RedLoader;
using RedLoader.Utils;
using Sons.Ai.Vail.Inventory;
using Sons.Characters;
using Sons.Construction.GRABS;
using Sons.Gameplay;
Expand Down Expand Up @@ -104,6 +106,27 @@ private void NoForestCommand(string args)
PathologicalGames.PoolManager.Pools["SmallTree"].gameObject.SetActive(!isActive);
}

/// <summary>
/// Clears all pickups in a radius
/// </summary>
/// <param name="args"></param>
/// <command>clearpickups</command>
[DebugCommand("clearpickups")]
private void ClearPickupsCommand(string args)
{
if(!float.TryParse(args, out var radius))
radius = 5f;

var pickups = Resources.FindObjectsOfTypeAll<VailPickup>();
foreach (var pickup in pickups)
{
if (Vector3.Distance(ActiveWorldLocation.Position, pickup.transform.position) > radius)
continue;

UnityEngine.Object.Destroy(pickup.gameObject);
}
}

/// <summary>
/// Go to a pickup by name (picks the first one that contains the name). Useful for finding story items.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions SonsGameManager/SonsGameManager.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions SonsSdk/CustomState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using RedLoader;
using UnityEngine.InputSystem;

namespace SonsSdk;

public abstract class CustomState
{
public bool IsActive { get; private set; }

protected InputAction[] ActionsToDisable;

protected CustomState() {}

protected CustomState(InputAction[] actionsToDisable)
{
ActionsToDisable = actionsToDisable;
}

public void Start()
{
if(ActionsToDisable != null)
foreach (var action in ActionsToDisable)
action.Disable();

OnStart();
GlobalEvents.OnUpdate.Subscribe(OnUpdateInternal);
IsActive = true;
}

public void End()
{
IsActive = false;
GlobalEvents.OnUpdate.Subscribe(OnUpdateInternal);
OnEnd();

if(ActionsToDisable != null)
foreach (var action in ActionsToDisable)
action.Enable();
}

private void OnUpdateInternal()
{
if(!IsActive)
return;

OnUpdate();
}

protected abstract void OnStart();

protected abstract void OnEnd();

protected abstract void OnUpdate();
}
64 changes: 64 additions & 0 deletions SonsSdk/PreviewBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Shapes;
using UnityEngine;

namespace SonsSdk;

public class PreviewBox : IDisposable
{
public GameObject Container;
public Transform Transform;
public Cuboid Cube;

public bool IsActive { get; private set; }

public PreviewBox()
{
Container = new GameObject("PreviewBox");
Transform = Container.transform;
Cube = Container.AddComponent<Cuboid>();

IsActive = true;
}

public PreviewBox(Color color) : this()
{
SetColor(color);
}

public PreviewBox SetColor(Color color)
{
Cube.Color = color;
return this;
}

public void Set(Vector3 pos, Vector3 size)
{
Transform.SetPositionAndRotation(pos, Quaternion.identity);
Transform.localScale = size;
}

public void Set(Vector3 pos, Vector3 size, Quaternion rot)
{
Transform.SetPositionAndRotation(pos, rot);
Transform.localScale = size;
}

public void SetActive(bool active)
{
if (active && !IsActive)
{
Container.SetActive(true);
IsActive = true;
}
else if (!active && IsActive)
{
Container.SetActive(false);
IsActive = false;
}
}

public void Dispose()
{
Container.TryDestroy();
}
}
22 changes: 22 additions & 0 deletions SonsSdk/SUI/SContainerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public SContainerOptions(GameObject root) : base(root)

protected override void VisibilityObservalbleChanged(bool value)
{
if(InvertVisibility)
value = !value;

if (_observableTogglesGameObject)
{
if(value == Root.activeSelf)
Expand All @@ -34,9 +37,28 @@ protected override void VisibilityObservalbleChanged(bool value)
/// <param name="observable">The observable boolean value to bind to.</param>
/// <param name="toggleGameObject">Whether to toggle the GameObject's active state based on the observable value or the canvasgroup alpha.</param>
public SContainerOptions BindVisibility(Observable<bool> observable, bool toggleGameObject)
{
UnbindVisibility();

InvertVisibility = false;
VisibilityObservable = observable;
_observableTogglesGameObject = toggleGameObject;
observable.OnValueChanged += VisibilityObservalbleChanged;
VisibilityObservalbleChanged(observable.Value);

return this;
}

/// <summary>
/// Binds the visibility of the container to an observable boolean value.
/// </summary>
/// <param name="observable">The observable boolean value to bind to.</param>
/// <param name="toggleGameObject">Whether to toggle the GameObject's active state based on the observable value or the canvasgroup alpha.</param>
public SContainerOptions BindVisibilityInverted(Observable<bool> observable, bool toggleGameObject)
{
UnbindVisibility();

InvertVisibility = true;
VisibilityObservable = observable;
_observableTogglesGameObject = toggleGameObject;
observable.OnValueChanged += VisibilityObservalbleChanged;
Expand Down
31 changes: 30 additions & 1 deletion SonsSdk/SUI/SPanelOptions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
using UnityEngine;
using RedLoader;
using SonsSdk;
using UnityEngine;

namespace SUI;

public class SPanelOptions : SContainerOptions
{
public string Id { get; internal set; }

private bool _closeOnKeyRelease;

public SPanelOptions(GameObject root) : base(root)
{
}

public SPanelOptions BindKeyConfig(KeybindConfigEntry keybind, bool closeOnKeyRelease = false)
{
_closeOnKeyRelease = closeOnKeyRelease;
keybind.Notify(KeyPressed, KeyReleased);
return this;
}

private void KeyPressed()
{
if (_closeOnKeyRelease)
{
Active(true);
return;
}

Toggle();
}

private void KeyReleased()
{
if(_closeOnKeyRelease)
Active(false);
}
}
47 changes: 47 additions & 0 deletions SonsSdk/SUI/SUiElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ public class SUiElement<T> : SUiElement
protected TooltipInfo TooltipInfo;

protected Observable<bool> VisibilityObservable;
protected bool InvertVisibility;

public SUiElement(GameObject root) : base(root)
{ }

protected virtual void VisibilityObservalbleChanged(bool value)
{
if (InvertVisibility)
value = !value;

if(value == Root.activeSelf)
return;

Expand All @@ -93,7 +97,24 @@ protected virtual void VisibilityObservalbleChanged(bool value)
public T BindVisibility(Observable<bool> observable)
{
UnbindVisibility();

InvertVisibility = false;
VisibilityObservable = observable;
observable.OnValueChanged += VisibilityObservalbleChanged;
VisibilityObservalbleChanged(observable.Value);

return (T)(object)this;
}

/// <summary>
/// Binds the visibility of the container to an observable boolean value.
/// </summary>
/// <param name="observable">The observable boolean value to bind to.</param>
public T BindVisibilityInverted(Observable<bool> observable)
{
UnbindVisibility();

InvertVisibility = true;
VisibilityObservable = observable;
observable.OnValueChanged += VisibilityObservalbleChanged;
VisibilityObservalbleChanged(observable.Value);
Expand Down Expand Up @@ -543,9 +564,35 @@ public T Pivot(float? x = null, float? y = null)
RectTransform.pivot = pivot;
return (T)(object)this;
}

/// <summary>
/// A shortcut to set the pivot to (0,1) and anchor to top left.
/// </summary>
/// <returns></returns>
public T TopLeft()
{
RectTransform.anchorMin = new Vector2(0, 1);
RectTransform.anchorMax = new Vector2(0, 1);
RectTransform.pivot = new Vector2(0, 1);
return (T)(object)this;
}

/// <summary>
/// A shortcut to set the pivot to (0,1) and anchor to top left.
/// Additionally sets the position (with the y value negated).
/// This might be easier for users coming from web dev or frameworks like Imgui.
/// </summary>
/// <returns></returns>
public T TopLeft(float x, float y)
{
TopLeft();
Position(x, -y);
return (T)(object)this;
}

/// <summary>
/// Sets the aspect ratio mode for the objects's aspect ratio fitter.
/// Adds an aspect ratio fitter if none is present.
/// </summary>
/// <param name="mode">The aspect ratio mode to apply.</param>
public T AspectRatio(AspectRatioFitter.AspectMode mode)
Expand Down
4 changes: 3 additions & 1 deletion SonsSdk/SonsSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<Reference Include="Newtonsoft.Json">
<HintPath>F:\SteamLibrary\steamapps\common\Sons Of The Forest\_RedLoader\Game\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ShapesRuntime">
<HintPath>F:\SteamLibrary\steamapps\common\Sons Of The Forest\_RedLoader\Game\ShapesRuntime.dll</HintPath>
</Reference>
<Reference Include="Sons">
<HintPath>$(GamePath)\$(ProjectAlias)\Game\Sons.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -197,7 +200,6 @@

<ItemGroup>
<Folder Include="AssetMaps\" />
<Folder Include="ModFramework\" />
</ItemGroup>

</Project>
Loading

0 comments on commit 4f0cfc5

Please sign in to comment.