445 lines
15 KiB
C#
445 lines
15 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace AlicizaX.UI
|
|
{
|
|
#region GameObject States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("GameObject/Active")]
|
|
[ControlerStateAttachType(true)]
|
|
public class GameObjectActiveState : ControllerStateBase
|
|
{
|
|
[SerializeField] private bool _active = true;
|
|
[HideInInspector] [SerializeField] private bool _defaultActive = true;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.gameObject != null)
|
|
{
|
|
_defaultActive = recorder.gameObject.activeSelf;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.gameObject != null)
|
|
{
|
|
bool shouldBeActive = (entryIndex == selectionIndex) ? _active : _defaultActive;
|
|
recorder.gameObject.SetActive(shouldBeActive);
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.gameObject != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: {(_active ? "Show" : "Hide")}, Default: {(_defaultActive ? "Show" : "Hide")}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Transform States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Transform/Position")]
|
|
[ControlerStateAttachType(true)]
|
|
public class TransformPositionState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Vector2 _position;
|
|
[HideInInspector] [SerializeField] private Vector2 _defaultPosition;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<RectTransform>(out var rect))
|
|
{
|
|
_defaultPosition = rect.anchoredPosition;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<RectTransform>(out var rect))
|
|
{
|
|
rect.anchoredPosition = (entryIndex == selectionIndex) ? _position : _defaultPosition;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<RectTransform>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Position={_position}, Default={_defaultPosition}";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Transform/Scale")]
|
|
[ControlerStateAttachType(true)]
|
|
public class TransformScaleState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Vector3 _scale = Vector3.one;
|
|
[HideInInspector] [SerializeField] private Vector3 _defaultScale;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.transform != null)
|
|
{
|
|
_defaultScale = recorder.transform.localScale;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.transform != null)
|
|
{
|
|
recorder.transform.localScale = (entryIndex == selectionIndex) ? _scale : _defaultScale;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.transform != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Scale={_scale}, Default={_defaultScale}";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Transform/Rotation")]
|
|
[ControlerStateAttachType(true)]
|
|
public class TransformRotationState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Vector3 _rotation;
|
|
[HideInInspector] [SerializeField] private Vector3 _defaultRotation;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.transform != null)
|
|
{
|
|
_defaultRotation = recorder.transform.localEulerAngles;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.transform != null)
|
|
{
|
|
recorder.transform.localEulerAngles = (entryIndex == selectionIndex) ? _rotation : _defaultRotation;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.transform != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Rotation={_rotation}, Default={_defaultRotation}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Graphic States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Graphic/Color")]
|
|
[ControlerStateAttachType(true)]
|
|
public class GraphicColorState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Color _color = Color.white;
|
|
[HideInInspector] [SerializeField] private Color _defaultColor;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Graphic>(out var graphic))
|
|
{
|
|
_defaultColor = graphic.color;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Graphic>(out var graphic))
|
|
{
|
|
graphic.color = (entryIndex == selectionIndex) ? _color : _defaultColor;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<Graphic>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Color={_color}, Default={_defaultColor}";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Graphic/Material")]
|
|
[ControlerStateAttachType(true)]
|
|
public class GraphicMaterialState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Material _material;
|
|
[HideInInspector] [SerializeField] private Material _defaultMaterial;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Graphic>(out var graphic))
|
|
{
|
|
_defaultMaterial = graphic.material;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Graphic>(out var graphic))
|
|
{
|
|
graphic.material = (entryIndex == selectionIndex) ? _material : _defaultMaterial;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<Graphic>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
string matched = _material != null ? _material.name : "None";
|
|
string defaultVal = _defaultMaterial != null ? _defaultMaterial.name : "None";
|
|
return $"When Matched: {matched}, Default: {defaultVal}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Image States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Image/Sprite")]
|
|
[ControlerStateAttachType(true)]
|
|
public class ImageSpriteState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Sprite _sprite;
|
|
[HideInInspector] [SerializeField] private Sprite _defaultSprite;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Image>(out var image))
|
|
{
|
|
_defaultSprite = image.sprite;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<Image>(out var image))
|
|
{
|
|
image.sprite = (entryIndex == selectionIndex) ? _sprite : _defaultSprite;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<Image>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
string matched = _sprite != null ? _sprite.name : "None";
|
|
string defaultVal = _defaultSprite != null ? _defaultSprite.name : "None";
|
|
return $"When Matched: {matched}, Default: {defaultVal}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Text States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Text/Content")]
|
|
[ControlerStateAttachType(true)]
|
|
public class TextContentState : ControllerStateBase
|
|
{
|
|
[SerializeField] [TextArea(3, 10)] private string _text = "";
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder == null || entryIndex != selectionIndex) return;
|
|
|
|
if (recorder.TryGetComponent<Text>(out var text))
|
|
{
|
|
text.text = _text;
|
|
}
|
|
else if (recorder.TryGetComponent<TextMeshProUGUI>(out var tmp))
|
|
{
|
|
tmp.text = _text;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null &&
|
|
(recorder.GetComponent<Text>() != null || recorder.GetComponent<TextMeshProUGUI>() != null);
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
string preview = _text.Length > 30 ? _text.Substring(0, 30) + "..." : _text;
|
|
return $"When Matched: \"{preview}\"";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
[ControlerStateName("Text/Color")]
|
|
[ControlerStateAttachType(true)]
|
|
public class TextColorState : ControllerStateBase
|
|
{
|
|
[SerializeField] private Color _color = Color.white;
|
|
[HideInInspector] [SerializeField] private Color _defaultColor;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder == null) return;
|
|
|
|
if (recorder.TryGetComponent<Text>(out var text))
|
|
{
|
|
_defaultColor = text.color;
|
|
}
|
|
else if (recorder.TryGetComponent<TextMeshProUGUI>(out var tmp))
|
|
{
|
|
_defaultColor = tmp.color;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder == null) return;
|
|
|
|
Color targetColor = (entryIndex == selectionIndex) ? _color : _defaultColor;
|
|
|
|
if (recorder.TryGetComponent<Text>(out var text))
|
|
{
|
|
text.color = targetColor;
|
|
}
|
|
else if (recorder.TryGetComponent<TextMeshProUGUI>(out var tmp))
|
|
{
|
|
tmp.color = targetColor;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null &&
|
|
(recorder.GetComponent<Text>() != null || recorder.GetComponent<TextMeshProUGUI>() != null);
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Color={_color}, Default={_defaultColor}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region CanvasGroup States
|
|
|
|
[Serializable]
|
|
[ControlerStateName("CanvasGroup/Alpha")]
|
|
[ControlerStateAttachType(true)]
|
|
public class CanvasGroupAlphaState : ControllerStateBase
|
|
{
|
|
[SerializeField] [Range(0f, 1f)] private float _alpha = 1f;
|
|
[HideInInspector] [SerializeField] private float _defaultAlpha;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<CanvasGroup>(out var canvasGroup))
|
|
{
|
|
_defaultAlpha = canvasGroup.alpha;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<CanvasGroup>(out var canvasGroup))
|
|
{
|
|
canvasGroup.alpha = (entryIndex == selectionIndex) ? _alpha : _defaultAlpha;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<CanvasGroup>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Alpha={_alpha:F2}, Default={_defaultAlpha:F2}";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
[ControlerStateName("CanvasGroup/Interactable")]
|
|
[ControlerStateAttachType(true)]
|
|
public class CanvasGroupInteractableState : ControllerStateBase
|
|
{
|
|
[SerializeField] private bool _interactable = true;
|
|
[SerializeField] private bool _blocksRaycasts = true;
|
|
[HideInInspector] [SerializeField] private bool _defaultInteractable = true;
|
|
[HideInInspector] [SerializeField] private bool _defaultBlocksRaycasts = true;
|
|
|
|
public override void Init(UXControllerStateRecorder recorder)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<CanvasGroup>(out var canvasGroup))
|
|
{
|
|
_defaultInteractable = canvasGroup.interactable;
|
|
_defaultBlocksRaycasts = canvasGroup.blocksRaycasts;
|
|
}
|
|
}
|
|
|
|
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
|
|
{
|
|
if (recorder != null && recorder.TryGetComponent<CanvasGroup>(out var canvasGroup))
|
|
{
|
|
bool isActive = (entryIndex == selectionIndex);
|
|
canvasGroup.interactable = isActive ? _interactable : _defaultInteractable;
|
|
canvasGroup.blocksRaycasts = isActive ? _blocksRaycasts : _defaultBlocksRaycasts;
|
|
}
|
|
}
|
|
|
|
public override bool Valid(UXControllerStateRecorder recorder)
|
|
{
|
|
return recorder != null && recorder.GetComponent<CanvasGroup>() != null;
|
|
}
|
|
|
|
public override string GetDescription()
|
|
{
|
|
return $"When Matched: Interactable={_interactable}, Raycast={_blocksRaycasts}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|