initial commit
This commit is contained in:
parent
6715289efe
commit
788c3389af
37645 changed files with 2526849 additions and 80 deletions
|
@ -0,0 +1,128 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class TabButton : VisualElement
|
||||
{
|
||||
internal new class UxmlFactory : UxmlFactory<TabButton, UxmlTraits> { }
|
||||
|
||||
internal new class UxmlTraits : VisualElement.UxmlTraits
|
||||
{
|
||||
private readonly UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "text" };
|
||||
private readonly UxmlStringAttributeDescription m_Target = new UxmlStringAttributeDescription { name = "target" };
|
||||
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
TabButton item = ve as TabButton;
|
||||
|
||||
item.m_Label.text = m_Text.GetValueFromBag(bag, cc);
|
||||
item.TargetId = m_Target.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
|
||||
static readonly string styleName = "TabButtonStyles";
|
||||
static readonly string UxmlName = "TabButton";
|
||||
static readonly string s_UssClassName = "unity-tab-button";
|
||||
static readonly string s_UssActiveClassName = s_UssClassName + "--active";
|
||||
|
||||
private Label m_Label;
|
||||
|
||||
public bool IsCloseable { get; set; }
|
||||
public string TargetId { get; private set; }
|
||||
public VisualElement Target { get; set; }
|
||||
|
||||
public event Action<TabButton> OnSelect;
|
||||
public event Action<TabButton> OnClose;
|
||||
|
||||
public TabButton()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public TabButton(string text, VisualElement target)
|
||||
{
|
||||
Init();
|
||||
m_Label.text = text;
|
||||
Target = target;
|
||||
}
|
||||
|
||||
private void PopulateContextMenu(ContextualMenuPopulateEvent populateEvent)
|
||||
{
|
||||
DropdownMenu dropdownMenu = populateEvent.menu;
|
||||
|
||||
if (IsCloseable)
|
||||
{
|
||||
dropdownMenu.AppendAction("Close Tab", e => OnClose(this));
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateContextMenu(VisualElement visualElement)
|
||||
{
|
||||
ContextualMenuManipulator menuManipulator = new ContextualMenuManipulator(PopulateContextMenu);
|
||||
|
||||
visualElement.focusable = true;
|
||||
visualElement.pickingMode = PickingMode.Position;
|
||||
visualElement.AddManipulator(menuManipulator);
|
||||
|
||||
visualElement.AddManipulator(menuManipulator);
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
AddToClassList(s_UssClassName);
|
||||
styleSheets.Add(Resources.Load<StyleSheet>($"Styles/{styleName}"));
|
||||
|
||||
VisualTreeAsset visualTree = Resources.Load<VisualTreeAsset>($"UXML/{UxmlName}");
|
||||
visualTree.CloneTree(this);
|
||||
|
||||
m_Label = this.Q<Label>("Label");
|
||||
|
||||
CreateContextMenu(this);
|
||||
|
||||
RegisterCallback<MouseDownEvent>(OnMouseDownEvent);
|
||||
}
|
||||
|
||||
public void Select()
|
||||
{
|
||||
AddToClassList(s_UssActiveClassName);
|
||||
|
||||
if (Target != null)
|
||||
{
|
||||
Target.style.display = DisplayStyle.Flex;
|
||||
Target.style.flexGrow = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
RemoveFromClassList(s_UssActiveClassName);
|
||||
MarkDirtyRepaint();
|
||||
|
||||
if (Target != null)
|
||||
{
|
||||
Target.style.display = DisplayStyle.None;
|
||||
Target.style.flexGrow = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMouseDownEvent(MouseDownEvent e)
|
||||
{
|
||||
switch (e.button)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
OnSelect?.Invoke(this);
|
||||
break;
|
||||
}
|
||||
|
||||
case 2 when IsCloseable:
|
||||
{
|
||||
OnClose?.Invoke(this);
|
||||
break;
|
||||
}
|
||||
} // End of switch.
|
||||
|
||||
e.StopImmediatePropagation();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7f37167158a2e6a4793fa4641060bb9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,150 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class TabbedView : VisualElement
|
||||
{
|
||||
public new class UxmlFactory : UxmlFactory<TabbedView, UxmlTraits> { }
|
||||
|
||||
private const string k_styleName = "TabbedView";
|
||||
private const string s_UssClassName = "unity-tabbed-view";
|
||||
private const string s_ContentContainerClassName = "unity-tabbed-view__content-container";
|
||||
private const string s_TabsContainerClassName = "unity-tabbed-view__tabs-container";
|
||||
|
||||
private readonly VisualElement m_TabContent;
|
||||
private readonly VisualElement m_Content;
|
||||
|
||||
private readonly List<TabButton> m_Tabs = new List<TabButton>();
|
||||
private TabButton m_ActiveTab;
|
||||
|
||||
public override VisualElement contentContainer => m_Content;
|
||||
|
||||
public TabbedView()
|
||||
{
|
||||
AddToClassList(s_UssClassName);
|
||||
|
||||
styleSheets.Add(Resources.Load<StyleSheet>($"Styles/{k_styleName}"));
|
||||
|
||||
m_TabContent = new VisualElement();
|
||||
m_TabContent.name = "unity-tabs-container";
|
||||
m_TabContent.AddToClassList(s_TabsContainerClassName);
|
||||
hierarchy.Add(m_TabContent);
|
||||
|
||||
m_Content = new VisualElement();
|
||||
m_Content.name = "unity-content-container";
|
||||
m_Content.AddToClassList(s_ContentContainerClassName);
|
||||
hierarchy.Add(m_Content);
|
||||
|
||||
RegisterCallback<AttachToPanelEvent>(ProcessEvent);
|
||||
}
|
||||
|
||||
public void AddTab(TabButton tabButton, bool activate)
|
||||
{
|
||||
m_Tabs.Add(tabButton);
|
||||
m_TabContent.Add(tabButton);
|
||||
|
||||
tabButton.OnClose += RemoveTab;
|
||||
tabButton.OnSelect += Activate;
|
||||
|
||||
if (activate)
|
||||
{
|
||||
Activate(tabButton);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveTab(TabButton tabButton)
|
||||
{
|
||||
int index = m_Tabs.IndexOf(tabButton);
|
||||
|
||||
// If this tab is the active one make sure we deselect it first...
|
||||
if (m_ActiveTab == tabButton)
|
||||
{
|
||||
DeselectTab(tabButton);
|
||||
m_ActiveTab = null;
|
||||
}
|
||||
|
||||
m_Tabs.RemoveAt(index);
|
||||
m_TabContent.Remove(tabButton);
|
||||
|
||||
tabButton.OnClose -= RemoveTab;
|
||||
tabButton.OnSelect -= Activate;
|
||||
|
||||
// If we closed the active tab AND we have any tabs left - active the next valid one...
|
||||
if ((m_ActiveTab == null) && m_Tabs.Any())
|
||||
{
|
||||
int clampedIndex = Mathf.Clamp(index, 0, m_Tabs.Count - 1);
|
||||
TabButton tabToActivate = m_Tabs[clampedIndex];
|
||||
|
||||
Activate(tabToActivate);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessEvent(AttachToPanelEvent e)
|
||||
{
|
||||
// This code takes any existing tab buttons and hooks them into the system...
|
||||
for (int i = 0; i < m_Content.childCount; ++i)
|
||||
{
|
||||
VisualElement element = m_Content[i];
|
||||
|
||||
if (element is TabButton button)
|
||||
{
|
||||
m_Content.Remove(element);
|
||||
|
||||
if (button.Target == null)
|
||||
{
|
||||
string targetId = button.TargetId;
|
||||
|
||||
button.Target = this.Q(targetId);
|
||||
}
|
||||
AddTab(button, false);
|
||||
--i;
|
||||
}
|
||||
else
|
||||
{
|
||||
element.style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, if we need to, activate this tab...
|
||||
if (m_ActiveTab != null)
|
||||
{
|
||||
SelectTab(m_ActiveTab);
|
||||
}
|
||||
else if (m_TabContent.childCount > 0)
|
||||
{
|
||||
m_ActiveTab = (TabButton)m_TabContent[0];
|
||||
|
||||
SelectTab(m_ActiveTab);
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectTab(TabButton tabButton)
|
||||
{
|
||||
VisualElement target = tabButton.Target;
|
||||
|
||||
tabButton.Select();
|
||||
if (target != null)
|
||||
Add(target);
|
||||
}
|
||||
|
||||
private void DeselectTab(TabButton tabButton)
|
||||
{
|
||||
VisualElement target = tabButton.Target;
|
||||
|
||||
if (target != null)
|
||||
Remove(target);
|
||||
tabButton.Deselect();
|
||||
}
|
||||
|
||||
public void Activate(TabButton button)
|
||||
{
|
||||
if (m_ActiveTab != null)
|
||||
{
|
||||
DeselectTab(m_ActiveTab);
|
||||
}
|
||||
|
||||
m_ActiveTab = button;
|
||||
SelectTab(m_ActiveTab);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e8faaa69857c34a44900d1276314b4dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue