Created
March 24, 2021 09:26
-
-
Save Raveler/c256dfc5851f961b9ed6355cd7d1a55b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
namespace Bombardel | |
{ | |
public class BetterToggle : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler | |
{ | |
[Header("General")] | |
public Image targetGraphic; | |
public BetterToggleGroup toggleGroup; | |
public bool disabled = false; | |
public bool isOn = false; | |
[Header("Sprite Swap")] | |
public bool spriteSwap; | |
[ConditionalField("spriteSwap")] | |
public Sprite normalSprite; | |
[ConditionalField("spriteSwap")] | |
public Sprite highlightedSprite; | |
[ConditionalField("spriteSwap")] | |
public Sprite pressedSprite; | |
[ConditionalField("spriteSwap")] | |
public Sprite selectedSprite; | |
[ConditionalField("spriteSwap")] | |
public Sprite disabledSprite; | |
[Header("Color Tint")] | |
public bool colorTint; | |
[ConditionalField("colorTint")] | |
public Color normalColor = Color.white; | |
[ConditionalField("colorTint")] | |
public Color highlightedColor = Color.white; | |
[ConditionalField("colorTint")] | |
public Color pressedColor = Color.white; | |
[ConditionalField("colorTint")] | |
public Color selectedColor = Color.white; | |
[ConditionalField("colorTint")] | |
public Color disabledColor = Color.white; | |
private bool pressed = false; | |
private bool highlighted = false; | |
public void Awake() | |
{ | |
UpdateState(); | |
if (toggleGroup != null) toggleGroup.RegisterToggle(this); | |
if (normalSprite == null) normalSprite = targetGraphic.sprite; | |
} | |
public void OnPointerEnter(PointerEventData eventData) | |
{ | |
highlighted = true; | |
UpdateState(); | |
} | |
public void OnPointerExit(PointerEventData eventData) | |
{ | |
highlighted = false; | |
UpdateState(); | |
} | |
public void OnPointerDown(PointerEventData eventData) | |
{ | |
pressed = true; | |
UpdateState(); | |
} | |
public void OnPointerUp(PointerEventData eventData) | |
{ | |
pressed = false; | |
UpdateState(); | |
} | |
public void OnPointerClick(PointerEventData eventData) | |
{ | |
if (toggleGroup != null) | |
{ | |
if (isOn) toggleGroup.ActivateToggle(null); | |
else toggleGroup.ActivateToggle(this); | |
} | |
else | |
{ | |
if (isOn) DisableToggle(); | |
else EnableToggle(); | |
} | |
} | |
private Sprite NullCoalesce(Sprite first, Sprite fallback) | |
{ | |
return first != null ? first : fallback; | |
} | |
private void UpdateState() | |
{ | |
// simplest use case | |
if (disabled) | |
{ | |
if (spriteSwap) targetGraphic.sprite = NullCoalesce(disabledSprite, normalSprite); | |
if (colorTint) targetGraphic.color = disabledColor; | |
return; | |
} | |
// selected | |
if (isOn) | |
{ | |
if (spriteSwap) targetGraphic.sprite = NullCoalesce(selectedSprite, normalSprite); | |
if (colorTint) targetGraphic.color = selectedColor; | |
return; | |
} | |
// pressed | |
if (pressed) | |
{ | |
if (spriteSwap) targetGraphic.sprite = NullCoalesce(pressedSprite, NullCoalesce(selectedSprite, normalSprite)); | |
if (colorTint) targetGraphic.color = pressedColor; | |
return; | |
} | |
// we are highlighted | |
if (highlighted) | |
{ | |
if (spriteSwap) targetGraphic.sprite = NullCoalesce(highlightedSprite, normalSprite); | |
if (colorTint) targetGraphic.color = highlightedColor; | |
return; | |
} | |
if (spriteSwap) targetGraphic.sprite = normalSprite; | |
if (colorTint) targetGraphic.color = normalColor; | |
} | |
public void EnableToggle() | |
{ | |
isOn = true; | |
UpdateState(); | |
} | |
public void DisableToggle() | |
{ | |
isOn = false; | |
UpdateState(); | |
} | |
public void OnDestroy() | |
{ | |
if (toggleGroup != null) toggleGroup.UnregisterToggle(this); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Bombardel | |
{ | |
public class BetterToggleGroup : MonoBehaviour | |
{ | |
public bool allowSwitchOff = false; | |
[ConditionalField("allowSwitchOff", true)] | |
public BetterToggle defaultToggleAllOff; | |
private List<BetterToggle> toggles = new List<BetterToggle>(); | |
private BetterToggle activeToggle = null; | |
public void RegisterToggle(BetterToggle toggle) | |
{ | |
toggles.Add(toggle); | |
} | |
public void UnregisterToggle(BetterToggle toggle) | |
{ | |
toggles.Remove(toggle); | |
} | |
public void Update() | |
{ | |
if (activeToggle == null && !allowSwitchOff) | |
{ | |
if (defaultToggleAllOff != null) ActivateToggle(defaultToggleAllOff); | |
else if (toggles.Count > 0) ActivateToggle(toggles[0]); | |
} | |
} | |
public void ActivateToggle(BetterToggle toggle) | |
{ | |
if (activeToggle == toggle && !allowSwitchOff) return; // can't disable yourself if you can't switch everything off | |
if (activeToggle != null) activeToggle.DisableToggle(); | |
activeToggle = toggle; | |
if (activeToggle != null) activeToggle.EnableToggle(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment