/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
#nullable enable
using System;
using System.Collections.Generic;
using com.IvanMurzak.Unity.MCP.Editor.Utils;
using UnityEngine.UIElements;
namespace com.IvanMurzak.Unity.MCP.Editor.UI.Controls
{
///
/// A segmented control with N segments and a sliding highlight.
/// Implements for UI Toolkit integration.
/// All visual styling is defined in _segmented-control.uss.
///
#if UNITY_2023_2_OR_NEWER
[UxmlElement]
public partial class SegmentedControl : VisualElement, INotifyValueChanged
#else
public class SegmentedControl : VisualElement, INotifyValueChanged
#endif
{
private static readonly string[] UssPaths =
EditorAssetLoader.GetEditorAssetPaths("Editor/UI/uss/common/_segmented-control.uss");
#if UNITY_2023_2_OR_NEWER
[UxmlAttribute("choices")]
public string Choices
{
get => string.Join(",", _segmentLabels);
set
{
if (!string.IsNullOrEmpty(value))
{
var labels = value.Split(',');
for (var i = 0; i < labels.Length; i++)
labels[i] = labels[i].Trim();
_segmentLabels = labels;
BuildSegments(labels);
}
}
}
private string[] _segmentLabels = Array.Empty();
[UxmlAttribute("default-index")]
public int DefaultIndex
{
get => _selectedIndex;
set => SetValueWithoutNotify(value);
}
#else
public new class UxmlFactory : UxmlFactory { }
public new class UxmlTraits : VisualElement.UxmlTraits
{
private readonly UxmlStringAttributeDescription _choices = new() { name = "choices", defaultValue = "" };
private readonly UxmlIntAttributeDescription _defaultIndex = new() { name = "default-index", defaultValue = 0 };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var control = (SegmentedControl)ve;
var choicesValue = _choices.GetValueFromBag(bag, cc);
var defaultIndex = _defaultIndex.GetValueFromBag(bag, cc);
if (!string.IsNullOrEmpty(choicesValue))
{
var labels = choicesValue.Split(',');
for (var i = 0; i < labels.Length; i++)
labels[i] = labels[i].Trim();
control.BuildSegments(labels);
control.SetValueWithoutNotify(defaultIndex);
}
}
}
#endif
private readonly VisualElement _track;
private readonly VisualElement _highlight;
private readonly List