button and symbol events, PuzzleDetail log type
This commit is contained in:
@@ -1,53 +1,103 @@
|
||||
using NaughtyAttributes;
|
||||
using System;
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
||||
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
|
||||
|
||||
namespace EscapeRoomEngine.Desert.Runtime
|
||||
{
|
||||
public enum ButtonEventType
|
||||
{
|
||||
Pressed, Released, Activated, Deactivated
|
||||
}
|
||||
|
||||
public static class ButtonEventExtensions
|
||||
{
|
||||
public static string Description(this ButtonEventType type, Button button)
|
||||
{
|
||||
var action = type switch
|
||||
{
|
||||
ButtonEventType.Pressed => "pressed",
|
||||
ButtonEventType.Released => "released",
|
||||
ButtonEventType.Activated => "activated",
|
||||
ButtonEventType.Deactivated => "deactivated",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
||||
};
|
||||
|
||||
return $"{button} was {action}";
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void ButtonEventHandler(Button source, ButtonEventType e);
|
||||
|
||||
public class Button : MonoBehaviour
|
||||
{
|
||||
public bool Active
|
||||
public event ButtonEventHandler ButtonEvent;
|
||||
|
||||
[ShowNativeProperty]
|
||||
private bool Active
|
||||
{
|
||||
get => _active;
|
||||
private set
|
||||
set
|
||||
{
|
||||
var previous = _active;
|
||||
_active = value;
|
||||
Pressed = Pressed; // check whether is still pressed
|
||||
|
||||
if (previous != _active)
|
||||
{
|
||||
OnButtonEvent(_active ? ButtonEventType.Activated : ButtonEventType.Deactivated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool Pressed
|
||||
[ShowNativeProperty]
|
||||
private bool Pressed
|
||||
{
|
||||
get => _pressed;
|
||||
protected set
|
||||
set
|
||||
{
|
||||
var previous = _pressed;
|
||||
_pressed = Active && value;
|
||||
|
||||
if (previous != _pressed)
|
||||
{
|
||||
OnButtonEvent(_pressed ? ButtonEventType.Pressed : ButtonEventType.Released);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool _active, _pressed;
|
||||
private bool _active = true, _pressed;
|
||||
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||
public void Enable()
|
||||
protected virtual void Awake()
|
||||
{
|
||||
Active = true;
|
||||
ButtonEvent += (_, type) =>
|
||||
{
|
||||
if (type == ButtonEventType.Deactivated)
|
||||
{
|
||||
Pressed = false; // release button if it is deactivated
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||
public void Disable()
|
||||
private void OnButtonEvent(ButtonEventType type)
|
||||
{
|
||||
Active = false;
|
||||
Logger.Log(type.Description(this), LogType.PuzzleDetail);
|
||||
|
||||
ButtonEvent?.Invoke(this, type);
|
||||
}
|
||||
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||
public void Press()
|
||||
{
|
||||
Pressed = true;
|
||||
}
|
||||
#region Debug Buttons
|
||||
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)]
|
||||
public void Release()
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Enable() => Active = true;
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Disable() => Active = false;
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Press() => Pressed = true;
|
||||
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Release() => Pressed = false;
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
Pressed = false;
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user