From 57a5ab482efd894c3b038babd2d5a557a3919e6b Mon Sep 17 00:00:00 2001 From: Sarbian Date: Fri, 20 Nov 2026 15:54:56 +0100 Subject: [PATCH 1/2] KodeKit: colour tints without uGUI's tween garbage uGUI's ColorTint transition runs Graphic.CrossFadeColor on every state change, which builds a tween callback and starts a coroutine: about 1 KB of garbage per pointer event (enter, down, up, exit) on every colour-tinted control. Most KodeKit controls are: segmented cells, stepper buttons, role buttons, list rows, title-bar icons, toggles and switches, and buttons in skins without hover sprites. InstantTint sets the Selectable's transition to None and paints the same ColorBlock colours into the target graphic's canvas renderer at once, following Unity's state order (disabled, pressed, selected, highlighted, normal). A change of interactable, by code or a CanvasGroup, is caught by one shared per-frame check over the live tints (a field read each). The 50 ms colour fade of segmented and painted controls is gone: the colour changes at once. Sprite-swap skins hand the Selectable back to its own transition. --- MechJeb2.UI/KodeKit/InstantTint.cs | 137 ++++++++++++++++++++++ MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs | 5 +- MechJeb2.UI/KodeKit/KodeWindow.cs | 3 +- MechJeb2.UI/KodeKit/Theme.cs | 4 +- 4 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 MechJeb2.UI/KodeKit/InstantTint.cs diff --git a/MechJeb2.UI/KodeKit/InstantTint.cs b/MechJeb2.UI/KodeKit/InstantTint.cs new file mode 100644 index 000000000..475900d61 --- /dev/null +++ b/MechJeb2.UI/KodeKit/InstantTint.cs @@ -0,0 +1,137 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace KodeKit +{ + /// + /// uGUI's colour-tint transition without its tween. Selectable.Transition.ColorTint runs + /// Graphic.CrossFadeColor 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 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 + /// interactable (code or a CanvasGroup) is caught by one shared check a frame over the live tints. + /// + [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 live = new List(); + static Watcher watcher; + + /// Tints with instead of its colour-tint transition. + 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() ?? s.gameObject.AddComponent(); + t.selectable = s; + t.colors = cb; + t.Paint(true); + } + + /// Hands the Selectable back to its own transition (a skin that swaps sprites instead). + public static void Off(Selectable s) + { + InstantTint t = s.GetComponent(); + if (t == null) return; + if (s.targetGraphic != null) s.targetGraphic.canvasRenderer.SetColor(Color.white); + Destroy(t); + } + + 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(); + } + 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(); + } + + /// Repaints the tints whose Selectable became (non-)interactable since their last paint: a field read each. + 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(); + } + } + } + } +} diff --git a/MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs b/MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs index e01e0f1ca..5bed8d39a 100644 --- a/MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs +++ b/MechJeb2.UI/KodeKit/KodeWindow.Layouts.cs @@ -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); } /// diff --git a/MechJeb2.UI/KodeKit/KodeWindow.cs b/MechJeb2.UI/KodeKit/KodeWindow.cs index fee14e75f..8bd652144 100644 --- a/MechJeb2.UI/KodeKit/KodeWindow.cs +++ b/MechJeb2.UI/KodeKit/KodeWindow.cs @@ -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); }); diff --git a/MechJeb2.UI/KodeKit/Theme.cs b/MechJeb2.UI/KodeKit/Theme.cs index 42189254b..de02b91e3 100644 --- a/MechJeb2.UI/KodeKit/Theme.cs +++ b/MechJeb2.UI/KodeKit/Theme.cs @@ -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) } } } From 7dac3f0e40f7db8b13d093aa3472a23f5e8a10d8 Mon Sep 17 00:00:00 2001 From: Sarbian Date: Fri, 20 Nov 2026 16:08:20 +0100 Subject: [PATCH 2/2] InstantTint: turn off rather than destroy when a skin swaps sprites 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. Destroy is deferred to the frame's end, so Use found the dying component and the control was left with no transition and no hover. --- MechJeb2.UI/KodeKit/InstantTint.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MechJeb2.UI/KodeKit/InstantTint.cs b/MechJeb2.UI/KodeKit/InstantTint.cs index 475900d61..8dc5939ec 100644 --- a/MechJeb2.UI/KodeKit/InstantTint.cs +++ b/MechJeb2.UI/KodeKit/InstantTint.cs @@ -34,6 +34,9 @@ public static void Use(Selectable s, ColorBlock cb) InstantTint t = s.GetComponent() ?? s.gameObject.AddComponent(); 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); } @@ -41,9 +44,9 @@ public static void Use(Selectable s, ColorBlock cb) public static void Off(Selectable s) { InstantTint t = s.GetComponent(); - if (t == null) return; + if (t == null || !t.enabled) return; + t.enabled = false; if (s.targetGraphic != null) s.targetGraphic.canvasRenderer.SetColor(Color.white); - Destroy(t); } void Paint(bool force = false)