Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions MechJeb2.UI/KodeKit/InstantTint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace KodeKit
{
/// <summary>
/// uGUI's colour-tint transition without its tween. <c>Selectable.Transition.ColorTint</c> runs
/// <c>Graphic.CrossFadeColor</c> on every state change, which builds a tween callback and starts a coroutine: about 1 KB of
/// garbage per pointer event on every button, toggle and segment (FINDINGS rule 52). This sets the Selectable's transition
/// to None and paints the same <see cref="ColorBlock"/> colours itself, at once, into the target graphic's canvas
/// renderer, as the transition does at the end of its fade. The states follow Unity's order: disabled, pressed, selected,
/// highlighted, normal. Pointer and selection events arrive here as they do at the Selectable; a change of
/// <c>interactable</c> (code or a CanvasGroup) is caught by one shared check a frame over the live tints.
/// </summary>
[DisallowMultipleComponent]
public sealed class InstantTint : UIBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler,
ISelectHandler, IDeselectHandler
{
Selectable selectable;
ColorBlock colors;
bool inside, down, selected, paintedInteractable;
Color painted;

static readonly List<InstantTint> live = new List<InstantTint>();
static Watcher watcher;

/// <summary>Tints <paramref name="s"/> with <paramref name="cb"/> instead of its colour-tint transition.</summary>
public static void Use(Selectable s, ColorBlock cb)
{
s.colors = cb; // kept for whoever reads it
s.transition = Selectable.Transition.None;
InstantTint t = s.GetComponent<InstantTint>() ?? s.gameObject.AddComponent<InstantTint>();
t.selectable = s;
t.colors = cb;
// turned off, not destroyed, by Off: a restyle switches a widget's look back and forth in one frame (the skin's
// sprite-swap button look, then the widget's own colour tint), and a destroyed component lingers to the frame's end
t.enabled = true;
t.Paint(true);
}

/// <summary>Hands the Selectable back to its own transition (a skin that swaps sprites instead).</summary>
public static void Off(Selectable s)
{
InstantTint t = s.GetComponent<InstantTint>();
if (t == null || !t.enabled) return;
t.enabled = false;
if (s.targetGraphic != null) s.targetGraphic.canvasRenderer.SetColor(Color.white);
}

void Paint(bool force = false)
{
if (selectable == null) return;
Graphic g = selectable.targetGraphic;
if (g == null) return;
paintedInteractable = selectable.IsInteractable();
Color c = !paintedInteractable ? colors.disabledColor
: down ? colors.pressedColor
: selected ? colors.selectedColor
: inside ? colors.highlightedColor
: colors.normalColor;
c *= colors.colorMultiplier;
if (!force && c == painted) return;
painted = c;
g.canvasRenderer.SetColor(c);
}

protected override void OnEnable()
{
base.OnEnable();
inside = down = false;
live.Add(this);
if (watcher == null)
{
var go = new GameObject("KodeKit InstantTint");
DontDestroyOnLoad(go);
watcher = go.AddComponent<Watcher>();
}
Paint(true);
}

protected override void OnDisable()
{
live.Remove(this);
base.OnDisable();
}

public void OnPointerEnter(PointerEventData e)
{
inside = true;
Paint();
}

public void OnPointerExit(PointerEventData e)
{
inside = false;
Paint();
}

public void OnPointerDown(PointerEventData e)
{
if (e.button != PointerEventData.InputButton.Left) return;
down = true;
Paint();
}

public void OnPointerUp(PointerEventData e)
{
if (e.button != PointerEventData.InputButton.Left) return;
down = false;
Paint();
}

public void OnSelect(BaseEventData e)
{
selected = true;
Paint();
}

public void OnDeselect(BaseEventData e)
{
selected = false;
Paint();
}

/// <summary>Repaints the tints whose Selectable became (non-)interactable since their last paint: a field read each.</summary>
sealed class Watcher : MonoBehaviour
{
void LateUpdate()
{
for (int i = 0; i < live.Count; i++)
{
InstantTint t = live[i];
if (t.selectable != null && t.selectable.IsInteractable() != t.paintedInteractable) t.Paint();
}
}
}
}
}
5 changes: 2 additions & 3 deletions MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ static void Paint(Selectable s, Image img, Sprite sprite, Color color, Color nor
img.type = sprite != null && sprite.border != Vector4.zero ? Image.Type.Sliced : Image.Type.Simple;
img.color = color;
s.targetGraphic = img;
s.transition = Selectable.Transition.ColorTint;
ColorBlock cb = ColorBlock.defaultColorBlock;
cb.normalColor = cb.selectedColor = normal;
cb.highlightedColor = hover;
cb.pressedColor = press;
cb.disabledColor = DisabledTint(normal);
cb.fadeDuration = 0.05f;
s.colors = cb;
// the colours of uGUI's colour-tint transition, without its tween's garbage (FINDINGS rule 52)
InstantTint.Use(s, cb);
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions MechJeb2.UI/KodeKit/KodeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,13 @@ public static UIButton ListRow(UIObject parent, string text, Action onClick)
{
img.sprite = null;
img.color = Color.white;
button.transition = Selectable.Transition.ColorTint;
ColorBlock cb = ColorBlock.defaultColorBlock;
cb.normalColor = new Color(1, 1, 1, 0);
cb.highlightedColor = new Color(1, 1, 1, 0.12f);
cb.pressedColor = new Color(1, 1, 1, 0.2f);
cb.selectedColor = cb.normalColor;
cb.disabledColor = DisabledTint(cb.normalColor);
button.colors = cb;
InstantTint.Use(button, cb); // uGUI's colour tint without its tween's garbage (FINDINGS rule 52)
th.ApplyText(label, bar.gameObject.activeSelf ? bar.color : th.Text);
line.color = new Color(1, 1, 1, 0.07f);
});
Expand Down
4 changes: 2 additions & 2 deletions MechJeb2.UI/KodeKit/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public void ApplySelectable(Selectable s, Image img)
s.targetGraphic = img;
if (Highlight != null || Active != null)
{
InstantTint.Off(s);
s.transition = Selectable.Transition.SpriteSwap;
s.spriteState = new SpriteState { highlightedSprite = Highlight, pressedSprite = Active, disabledSprite = Disabled };
}
else
{
s.transition = Selectable.Transition.ColorTint;
ColorBlock cb = ColorBlock.defaultColorBlock;
cb.highlightedColor = new Color(1f, 1f, 0.75f);
cb.pressedColor = new Color(0.7f, 0.9f, 0.7f);
s.colors = cb;
InstantTint.Use(s, cb); // uGUI's colour tint without its tween's garbage (FINDINGS rule 52)
}
}
}
Expand Down
Loading