Added Null Checked IToggleState Extension and Added Some Documentation

This commit is contained in:
Syntriax 2022-11-14 13:58:44 +03:00
parent 7574769637
commit 636056b82e
5 changed files with 41 additions and 11 deletions

View File

@ -4,7 +4,12 @@ namespace Syntriax.Modules.ToggleState
{
public interface IToggleState
{
bool Toggled { get; set; }
bool IsToggled { get; set; }
/// <summary>
/// Called everytime the IsToggled field is changed
/// </summary>
/// <value>The new value of IsToggled</value>
Action<bool> OnToggleStateChanged { get; set; }
}
}

14
IToggleStateExtensions.cs Normal file
View File

@ -0,0 +1,14 @@
namespace Syntriax.Modules.ToggleState
{
public static class IToggleStateExtensions
{
/// <summary>
/// Checks if the provided parameter IToggleState is toggled, if the parameter is null returns the nullValue parameter
/// </summary>
/// <param name="toggleState">IToggleState to be checked if toggled or not</param>
/// <param name="nullValue">The value that will be returned if toggleState is null. Default value: true</param>
/// <returns>IToggleState's toggle value, or if null return nullValue parameter</returns>
public static bool IsToggledNullChecked(this IToggleState toggleState, bool nullValue = true)
=> toggleState == null ? nullValue : toggleState.IsToggled;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d321123fb56053e49b20da45bfda3a65
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,15 +4,15 @@ namespace Syntriax.Modules.ToggleState
{
public class ToggleStateMember : IToggleState
{
private bool _toggled = true;
public bool Toggled
private bool _isToggled = true;
public bool IsToggled
{
get => _toggled;
get => _isToggled;
set
{
bool oldValue = _toggled;
bool oldValue = _isToggled;
_toggled = value;
_isToggled = value;
if (oldValue = !value)
OnToggleStateChanged?.Invoke(value);

View File

@ -5,15 +5,15 @@ namespace Syntriax.Modules.ToggleState
{
public class ToggleStateMonobehaviour : MonoBehaviour, IToggleState
{
private bool _toggled = true;
public bool Toggled
private bool _isToggled = true;
public bool IsToggled
{
get => _toggled;
get => _isToggled;
set
{
bool oldValue = _toggled;
bool oldValue = _isToggled;
_toggled = value;
_isToggled = value;
if (oldValue = !value)
OnToggleStateChanged?.Invoke(value);