Skip to content

Commit

Permalink
add "Drop Link" button
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivashenko committed Aug 15, 2024
1 parent 2f7c7b1 commit 8a87944
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class ContainersInspector : ItemsReferenceInspector
protected override Type RoleType => typeof(ContainerRoleAttribute);
protected override string TabName => ContainersTabName;
protected override SerializedProperty Property { get; }
private readonly StatefulComponent _statefulComponent;

public ContainersInspector(SerializedObject serializedObject)
{
Property = serializedObject.FindProperty(ContainersTabName);
_serializedObject = serializedObject;
_statefulComponent = serializedObject.targetObject as StatefulComponent;
}

protected override void DrawTitle()
Expand Down Expand Up @@ -62,5 +64,13 @@ protected override string CreateItemAPI(string prefix, string name, SerializedPr
{
return $"{prefix}GetContainer(ContainerRole.{name});\n";
}

protected override void DrawExtraButtons()
{
if (GUILayout.Button("Update"))
{
_statefulComponent.OnValidate();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using StatefulUI.Runtime.Core;

namespace StatefulUI.Editor.Selector
{
Expand All @@ -9,9 +10,9 @@ public class ExtensionMethodWrapper
private readonly object[] _parameters;
private readonly MethodInfo _methodInfo;

public ExtensionMethodWrapper(object target, string name)
public ExtensionMethodWrapper(object target, StatefulComponent statefulComponent, string name)
{
_parameters = new []{target};
_parameters = new []{target, statefulComponent};
_methodInfo = GetExtensionMethods(target.GetType(), name);
}

Expand Down Expand Up @@ -47,4 +48,4 @@ private MethodInfo GetExtensionMethods(Type extendedType, string name)
return null;
}
}
}
}
7 changes: 4 additions & 3 deletions Assets/Plugins/StatefulUI/Editor/Selector/RoleDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
if (_extensionMethodWrapper == null)
{
var statefulComponent = property.serializedObject.targetObject as StatefulComponent;
var target = property.GetTargetObjectWithProperty();
_extensionMethodWrapper = new ExtensionMethodWrapper(target, roleAttribute.Action);
_extensionMethodWrapper = new ExtensionMethodWrapper(target, statefulComponent, roleAttribute.Action);
}

var context = new GenericMenu();
context.AddItem(new GUIContent(roleAttribute.Name), false, _extensionMethodWrapper.Invoke);
context.ShowAsContext ();
context.ShowAsContext();
}
}

Expand Down Expand Up @@ -71,4 +72,4 @@ protected static void SaveValue(SerializedProperty property, int value)
}
}
}
}
}
33 changes: 33 additions & 0 deletions Assets/Plugins/StatefulUI/Runtime/Core/StatefulComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private ObjectReference GetObject(int role)
return null;
}

[UsedImplicitly]
private Image GetImage(int role)
{
for (var i = 0; i < Images.Count; i++)
Expand Down Expand Up @@ -495,6 +496,38 @@ public bool HasItem<T>(int roleValue) where T : class
throw new Exception($"Type {type} is not supported");
}

public bool DropItem<T>(T reference) where T : class
{
#if UNITY_EDITOR
UnityEditor.Undo.RecordObject(this, "Drop item");
#endif

var type = typeof(T);
var manager = StatefulUiManager.Instance;
bool result;

if (type == manager.ButtonReferenceType) result = Buttons.Remove(reference as ButtonReference);
else if (type == manager.ImageReferenceType) result = Images.Remove(reference as ImageReference);
else if (type == manager.AnimatorReferenceType) result = Animators.Remove(reference as AnimatorReference);
else if (type == manager.ContainerReferenceType) result = Containers.Remove(reference as ContainerReference);
else if (type == manager.DropdownReferenceType) result = Dropdowns.Remove(reference as DropdownReference);
else if (type == manager.InnerComponentReferenceType) result = InnerComponents.Remove(reference as InnerComponentReference);
else if (type == manager.ObjectReferenceType) result = Objects.Remove(reference as ObjectReference);
else if (type == manager.SliderReferenceType) result = Sliders.Remove(reference as SliderReference);
else if (type == manager.TextInputReferenceType) result = TextsInputs.Remove(reference as TextInputReference);
else if (type == manager.TextReferenceType) result = Texts.Remove(reference as TextReference);
else if (type == manager.ToggleReferenceType) result = Toggles.Remove(reference as ToggleReference);
else if (type == manager.VideoPlayerReferenceType) result = VideoPlayers.Remove(reference as VideoPlayerReference);
else return false;

#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
OnValidate();
#endif

return result;
}

private bool HasVideoPlayer(int role)
{
foreach (var videoPlayerReference in VideoPlayers)
Expand Down
14 changes: 14 additions & 0 deletions Assets/Plugins/StatefulUI/Runtime/Core/StatefulUiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
using System.Globalization;
using System.Linq;
using System.Text;
using StatefulUI.Runtime.References;
using UnityEngine;
using UnityEngine.UI;
// ReSharper disable UnusedMember.Local

namespace StatefulUI.Runtime.Core
{
public static class StatefulUiUtils
{
private static void RemoveReference(this TextReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this ToggleReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this SliderReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this ButtonReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this ImageReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this TextInputReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this DropdownReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this ContainerReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this InnerComponentReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this VideoPlayerReference @ref, StatefulComponent obj) => obj.DropItem(@ref);
private static void RemoveReference(this ObjectReference @ref, StatefulComponent obj) => obj.DropItem(@ref);

public static int IndexOf<T>(this IList<T> list, T item)
{
for (var i = 0; i < list.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class ButtonReference : BaseReference
{
[Role(typeof(ButtonRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(ButtonRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class ContainerReference : BaseReference
{
[Role(typeof(ContainerRoleAttribute))]
[Role(typeof(ContainerRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

namespace StatefulUI.Runtime.References
{



[Serializable]
public class DropdownReference : BaseReference
{
[Role(typeof(DropdownRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(DropdownRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

public bool IsEmpty => DropdownFieldTMP == null && DropdownField == null;
public TMP_Dropdown DropdownFieldTMP;
public Dropdown DropdownField;
public bool IsTMP;


public int Value
{
get => IsTMP ? DropdownFieldTMP.value : DropdownField.value;
Expand All @@ -34,9 +34,6 @@ public int Value
}
}


public bool IsEmpty => DropdownFieldTMP == null && DropdownField == null;

public void SetValue(int value)
{
if (IsTMP) DropdownFieldTMP.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class ImageReference : BaseReference
{
[Role(typeof(ImageRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(ImageRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
public Image Image;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class InnerComponentReference : BaseReference
{
[Role(typeof(InnerComponentRoleAttribute))]
[Role(typeof(InnerComponentRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
public StatefulComponent InnerComponent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

namespace StatefulUI.Runtime.References
{
// ReSharper disable CheckNamespace


[Serializable]
public class ObjectReference : BaseReference
{
[Role(typeof(ObjectRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(ObjectRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;


[ChildOnly]
public GameObject Object;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class SliderReference : BaseReference
{
[Role(typeof(SliderRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(SliderRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

namespace StatefulUI.Runtime.References
{



[Serializable]
public class TextInputReference : BaseReference
{
[Role(typeof(TextInputRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(TextInputRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

public TMP_InputField InputFieldTMP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class TextReference : BaseReference
{
[Role(typeof(TextRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(TextRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

public string Identificator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class ToggleReference : BaseReference
{
[Role(typeof(ToggleRoleAttribute), "Copy Universal Link", "CopyUniversalLink")]
[Role(typeof(ToggleRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
public Toggle Toggle;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ namespace StatefulUI.Runtime.References
[Serializable]
public class VideoPlayerReference : BaseReference
{
[Role(typeof(VideoPlayerRoleAttribute))]
[Role(typeof(VideoPlayerRoleAttribute), "Drop Link", "RemoveReference")]
public int Role;

[ChildOnly]
public VideoPlayer VideoPlayer;
}
}

2 changes: 1 addition & 1 deletion Assets/Plugins/StatefulUI/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dmitry-ivashenko.statefului",
"displayName": "StatefulUI",
"version": "1.0.7",
"version": "1.0.8",
"unity": "2021.3",
"description": "Stateful UI - A library for structured state-based UI development in Unity",
"keywords": ["UI"],
Expand Down

0 comments on commit 8a87944

Please sign in to comment.