iFactory.Godot/BITKit/Scripts/UX/UXTabViewService.cs

83 lines
1.7 KiB
C#

using Godot;
using System;
using Godot.Collections;
namespace BITKit.UX
{
public partial class UXTabViewService : Control
{
[ExportCategory(Constant.Header.Settings)]
[Export(PropertyHint.Range, "0,100")]
private int currentIndex
{
get=>_currentIndex;
set => SetIndex(value);
}
[Export] private int initialIndex;
[ExportCategory(Constant.Header.Components)]
[Export] private Array<Button> tabs;
[Export] private TabContainer tabContainer;
private ButtonGroup _buttonGroup;
private int _currentIndex;
public override void _Ready()
{
if (_isReady is false) return;
_buttonGroup = new ButtonGroup();
for (var i = 0; i < tabs.Count; i++)
{
var tab = tabs[i];
var _i = i;
tab.Pressed += () => { SetIndex(_i); };
}
var index = (Engine.IsEditorHint(), currentIndex, initialIndex) switch
{
(true,_,_)=>currentIndex,
(false,_,>-1)=>initialIndex,
(_,_,-1)=>-1,
_ => -1
};
SetIndex(index);
}
private void SetIndex(int value)
{
if (_isReady is false) return;
if (value < 0) return;
EnsureConfiguration();
var max = tabContainer.GetChildCount() - 1;
value = Mathf.Clamp(value, 0, max < 0 ? 0 : max );
_currentIndex = value;
tabContainer.CurrentTab = value;
foreach (var tab in tabs)
{
tab.ButtonPressed = false;
}
if (tabs.TryGetElementAt(value, out var button))
{
button.ButtonPressed = true;
}
}
private void EnsureConfiguration()
{
if (_isReady is false) return;
tabContainer.TabsVisible = false;
foreach (var tab in tabs)
{
tab.ButtonGroup = _buttonGroup;
}
}
private bool _isReady => IsNodeReady() && tabContainer is not null & tabs is not null;
}
}