Files
AetherBags/KamiToolKit/Premade/Nodes/MultiStateButtonNode.cs
T
KnackAtNite 8db4ce6094
Debug Build and Test / Build against Latest Dalamud (push) Has been cancelled
Debug Build and Test / Build against Staging Dalamud (push) Has been cancelled
Initial commit: AetherBags + KamiToolKit for FC Gitea
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 14:46:31 -05:00

59 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using KamiToolKit.Nodes;
namespace KamiToolKit.Premade.Nodes;
/// <summary>
/// A TextButton that has a configurable set of states
/// </summary>
public class MultiStateButtonNode<T> : TextButtonNode where T : notnull {
public Action<T>? OnStateChanged { get; set; }
public MultiStateButtonNode()
=> OnClick = CycleState;
public required List<T> States {
get;
set {
field = value;
UpdateDisplay();
}
}
private int SelectedIndex {
get;
set {
field = value;
UpdateDisplay();
}
}
public T SelectedState {
get => States[SelectedIndex];
set => SelectedIndex = States.IndexOf(value);
}
private void CycleState() {
if (States.Count is 0) return;
SelectedIndex = (SelectedIndex + 1) % States.Count;
OnStateChanged?.Invoke(SelectedState);
}
private void UpdateDisplay() {
if (SelectedIndex < 0) return;
if (SelectedIndex > States.Count - 1) return;
String = GetStateText(States[SelectedIndex]);
}
protected virtual string GetStateText(T state) {
if (state is Enum enumState) {
return enumState.Description;
}
return state.ToString() ?? "Unable to Parse Type";
}
}