multi chip update
This commit is contained in:
parent
6c19b566d0
commit
9647327168
11
UiToolkit/MultiSelectChips/IMultiSelectChipItem.cs
Normal file
11
UiToolkit/MultiSelectChips/IMultiSelectChipItem.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace NEG.Utils.UiToolkits
|
||||||
|
{
|
||||||
|
public interface IMultiSelectChipItem
|
||||||
|
{
|
||||||
|
string Name { get; }
|
||||||
|
Color Color { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
UiToolkit/MultiSelectChips/IMultiSelectChipItem.cs.meta
Normal file
11
UiToolkit/MultiSelectChips/IMultiSelectChipItem.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f032e3d346d243d458c929d4046f0a1e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
24
UiToolkit/MultiSelectChips/MultiSelectChipItem.cs
Normal file
24
UiToolkit/MultiSelectChips/MultiSelectChipItem.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
|
namespace NEG.Utils.UiToolkits
|
||||||
|
{
|
||||||
|
public class MultiSelectChipItem
|
||||||
|
{
|
||||||
|
public VisualElement VisualElement { get; }
|
||||||
|
public IMultiSelectChipItem ChipItem { get; }
|
||||||
|
|
||||||
|
private readonly MultiSelectChips parent;
|
||||||
|
|
||||||
|
public MultiSelectChipItem(VisualElement visualElement, IMultiSelectChipItem element, MultiSelectChips multiSelectChips)
|
||||||
|
{
|
||||||
|
VisualElement = visualElement;
|
||||||
|
ChipItem = element;
|
||||||
|
parent = multiSelectChips;
|
||||||
|
visualElement.Q<Label>("Name").text = element.Name;
|
||||||
|
visualElement.Q<VisualElement>("Color").style.backgroundColor = element.Color;
|
||||||
|
visualElement.Q<Button>("RemoveBtn").clicked += () => parent.TryRemoveItem(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
UiToolkit/MultiSelectChips/MultiSelectChipItem.cs.meta
Normal file
11
UiToolkit/MultiSelectChips/MultiSelectChipItem.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1906fa5e31f79fe48a6edda94b9c1b4d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
7
UiToolkit/MultiSelectChips/MultiSelectChipItem.uxml
Normal file
7
UiToolkit/MultiSelectChips/MultiSelectChipItem.uxml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
|
||||||
|
<ui:VisualElement name="VisualElement" class="unity-button" style="min-height: 21px; flex-shrink: 1; flex-direction: row; flex-wrap: wrap; align-items: center;">
|
||||||
|
<ui:VisualElement name="Color" style="min-height: 15px; min-width: 15px; border-top-left-radius: 22px; border-bottom-left-radius: 22px; border-top-right-radius: 22px; border-bottom-right-radius: 22px; border-left-width: 0; border-right-width: 0; border-top-width: 0; border-bottom-width: 0; border-left-color: rgba(0, 0, 125, 255); border-right-color: rgba(0, 0, 125, 255); border-top-color: rgba(0, 0, 125, 255); border-bottom-color: rgba(0, 0, 125, 255); background-color: rgba(255, 0, 0, 255); max-height: 15px; justify-content: center; align-items: center;" />
|
||||||
|
<ui:Label text="Name" display-tooltip-when-elided="true" name="Name" style="padding-left: 7px; padding-right: 5px;" />
|
||||||
|
<ui:Button text="X" display-tooltip-when-elided="true" name="RemoveBtn" />
|
||||||
|
</ui:VisualElement>
|
||||||
|
</ui:UXML>
|
||||||
10
UiToolkit/MultiSelectChips/MultiSelectChipItem.uxml.meta
Normal file
10
UiToolkit/MultiSelectChips/MultiSelectChipItem.uxml.meta
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7e7a545b35ac90447adefa27e61dc82b
|
||||||
|
ScriptedImporter:
|
||||||
|
internalIDToNameTable: []
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
|
||||||
@ -1,126 +1,190 @@
|
|||||||
#if ADDRESSABLES && UI_ELEMENTS
|
#if ADDRESSABLES && UI_ELEMENTS
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using UnityEngine.AddressableAssets;
|
using UnityEngine.AddressableAssets;
|
||||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
using UnityEditor;
|
||||||
|
#endif
|
||||||
|
|
||||||
public class MultiSelectChips : VisualElement
|
namespace NEG.Utils.UiToolkits
|
||||||
{
|
{
|
||||||
public string LabelText
|
public class MultiSelectChips : VisualElement
|
||||||
{
|
{
|
||||||
get => label.text;
|
public event Action<IMultiSelectChipItem> OnTryingToRemoveItem;
|
||||||
set
|
public event Action<Rect> OnTryingToAddItem;
|
||||||
|
|
||||||
|
public string LabelText
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(value))
|
get => label.text;
|
||||||
|
set
|
||||||
{
|
{
|
||||||
if (label == null)
|
if (!string.IsNullOrEmpty(value))
|
||||||
{
|
{
|
||||||
InitLabel();
|
if (label == null)
|
||||||
|
{
|
||||||
|
InitLabel();
|
||||||
|
}
|
||||||
|
|
||||||
|
label.text = value;
|
||||||
|
}
|
||||||
|
else if (label != null)
|
||||||
|
{
|
||||||
|
label.RemoveFromHierarchy();
|
||||||
|
label = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
label.text = value;
|
|
||||||
}
|
|
||||||
else if (label != null)
|
|
||||||
{
|
|
||||||
label.RemoveFromHierarchy();
|
|
||||||
label = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public IList ItemsSource
|
public ICollection<IMultiSelectChipItem> ItemsSource
|
||||||
{
|
|
||||||
get => itemsSource;
|
|
||||||
|
|
||||||
set
|
|
||||||
{
|
{
|
||||||
itemsSource = value;
|
get => itemsSource;
|
||||||
UpdateItems();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Label label;
|
set
|
||||||
private AsyncOperationHandle<VisualTreeAsset> uxmlHandle, itemUxmlHandle;
|
|
||||||
|
|
||||||
private VisualTreeAsset itemPrefab;
|
|
||||||
|
|
||||||
private IList itemsSource;
|
|
||||||
|
|
||||||
public new class UxmlFactory : UxmlFactory<MultiSelectChips, UxmlTraits>
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public new class UxmlTraits : VisualElement.UxmlTraits
|
|
||||||
{
|
|
||||||
private readonly UxmlStringAttributeDescription label;
|
|
||||||
|
|
||||||
public UxmlTraits()
|
|
||||||
{
|
|
||||||
label = new()
|
|
||||||
{
|
{
|
||||||
name = "label"
|
itemsSource = value;
|
||||||
|
UpdateItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Label label;
|
||||||
|
private AsyncOperationHandle<VisualTreeAsset> uxmlHandle, itemUxmlHandle;
|
||||||
|
|
||||||
|
private VisualTreeAsset itemPrefab;
|
||||||
|
|
||||||
|
private ICollection<IMultiSelectChipItem> itemsSource;
|
||||||
|
|
||||||
|
private readonly List<MultiSelectChipItem> spawnedItems = new();
|
||||||
|
|
||||||
|
private VisualElement realItemsParent;
|
||||||
|
|
||||||
|
public new class UxmlFactory : UxmlFactory<MultiSelectChips, UxmlTraits>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public new class UxmlTraits : VisualElement.UxmlTraits
|
||||||
|
{
|
||||||
|
private readonly UxmlStringAttributeDescription label;
|
||||||
|
|
||||||
|
public UxmlTraits()
|
||||||
|
{
|
||||||
|
label = new()
|
||||||
|
{
|
||||||
|
name = "label"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||||
|
{
|
||||||
|
base.Init(ve, bag, cc);
|
||||||
|
((MultiSelectChips)ve).LabelText = label.GetValueFromBag(bag, cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MultiSelectChips() : base()
|
||||||
|
{
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
string path = AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets($"t:Script {nameof(MultiSelectChips)}")[0]);
|
||||||
|
path = path.Remove(path.LastIndexOf('/'));
|
||||||
|
|
||||||
|
SetVisuals(AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/MultiSelectChips.uxml"));
|
||||||
|
itemPrefab = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>($"{path}/MultiSelectChipItem.uxml");
|
||||||
|
#else
|
||||||
|
uxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChips.uxml");
|
||||||
|
itemUxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChipItem.uxml");
|
||||||
|
uxmlHandle.Completed += OnUxmlLoaded;
|
||||||
|
itemUxmlHandle.Completed += OnItemUxmlLoaded;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateItems()
|
||||||
|
{
|
||||||
|
if (itemPrefab == null || itemsSource == null || realItemsParent == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
List<MultiSelectChipItem> itemsToDestroy = new List<MultiSelectChipItem>(spawnedItems);
|
||||||
|
|
||||||
|
itemsToDestroy.RemoveAll((x) => itemsSource.Contains(x.ChipItem));
|
||||||
|
|
||||||
|
foreach (var item in itemsToDestroy)
|
||||||
|
{
|
||||||
|
realItemsParent.Remove(item.VisualElement);
|
||||||
|
spawnedItems.Remove(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<IMultiSelectChipItem> itemsToAdd = new(itemsSource);
|
||||||
|
|
||||||
|
foreach (var item in spawnedItems)
|
||||||
|
{
|
||||||
|
if (itemsToAdd.Contains(item.ChipItem))
|
||||||
|
itemsToAdd.Remove(item.ChipItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in itemsToAdd)
|
||||||
|
{
|
||||||
|
VisualElement chipItemVisuals = itemPrefab.Instantiate();
|
||||||
|
realItemsParent.Insert(0, chipItemVisuals);
|
||||||
|
spawnedItems.Add(new MultiSelectChipItem(chipItemVisuals, item, this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void TryRemoveItem(IMultiSelectChipItem item)
|
||||||
|
{
|
||||||
|
OnTryingToRemoveItem?.Invoke(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitLabel()
|
||||||
|
{
|
||||||
|
label = new Label()
|
||||||
|
{
|
||||||
|
pickingMode = PickingMode.Ignore
|
||||||
};
|
};
|
||||||
|
Insert(0, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
private void OnUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
||||||
{
|
{
|
||||||
base.Init(ve, bag, cc);
|
if (operation.Status == AsyncOperationStatus.Succeeded)
|
||||||
((MultiSelectChips)ve).LabelText = label.GetValueFromBag(bag, cc);
|
{
|
||||||
|
SetVisuals(operation.Result);
|
||||||
|
Addressables.Release(uxmlHandle);
|
||||||
|
UpdateItems();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError($"Asset failed to load.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public MultiSelectChips() : base()
|
private void OnItemUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
||||||
{
|
|
||||||
uxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChips.uxml");
|
|
||||||
itemUxmlHandle = Addressables.LoadAssetAsync<VisualTreeAsset>("NegUiToolkits/MultiSelectChipsItem.uxml");
|
|
||||||
uxmlHandle.Completed += OnUxmlLoaded;
|
|
||||||
itemUxmlHandle.Completed += OnItemUxmlLoaded;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateItems()
|
|
||||||
{
|
|
||||||
if (itemPrefab == null)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitLabel()
|
|
||||||
{
|
|
||||||
label = new Label()
|
|
||||||
{
|
{
|
||||||
pickingMode = PickingMode.Ignore
|
|
||||||
};
|
|
||||||
Add(label);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
if (operation.Status == AsyncOperationStatus.Succeeded)
|
||||||
{
|
{
|
||||||
if (operation.Status == AsyncOperationStatus.Succeeded)
|
itemPrefab = operation.Result;
|
||||||
{
|
Addressables.Release(itemUxmlHandle);
|
||||||
Add(operation.Result.Instantiate());
|
UpdateItems();
|
||||||
Addressables.Release(uxmlHandle);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError($"Asset failed to load.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError($"Asset failed to load.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnItemUxmlLoaded(AsyncOperationHandle<VisualTreeAsset> operation)
|
private void SetVisuals(VisualTreeAsset treeAsset)
|
||||||
{
|
{
|
||||||
|
realItemsParent = treeAsset.Instantiate();
|
||||||
|
Add(realItemsParent);
|
||||||
|
|
||||||
if (operation.Status == AsyncOperationStatus.Succeeded)
|
var button = realItemsParent.Q<Button>("AddItem");
|
||||||
{
|
|
||||||
itemPrefab = operation.Result;
|
button.clicked += () => OnTryingToAddItem?.Invoke(button.worldBound);
|
||||||
Addressables.Release(itemUxmlHandle);
|
|
||||||
UpdateItems();
|
realItemsParent = button.parent;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError($"Asset failed to load.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
|
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
|
||||||
<ui:ListView focusable="true" show-add-remove-footer="false" horizontal-scrolling="true" />
|
|
||||||
<ui:VisualElement style="flex-direction: row; flex-wrap: wrap;">
|
<ui:VisualElement style="flex-direction: row; flex-wrap: wrap;">
|
||||||
<ui:Button text="+" display-tooltip-when-elided="true" name="AddItem" />
|
<ui:Button text="+" display-tooltip-when-elided="true" name="AddItem" />
|
||||||
</ui:VisualElement>
|
</ui:VisualElement>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user