mirror of
https://github.com/GrahamKracker/UnityExplorer.git
synced 2025-07-16 00:07:52 +08:00
Implement Clipboard and Notifications, misc cleanups
This commit is contained in:
120
src/UI/Panels/ClipboardPanel.cs
Normal file
120
src/UI/Panels/ClipboardPanel.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.CacheObject;
|
||||
using UnityExplorer.CacheObject.Views;
|
||||
using UnityExplorer.Config;
|
||||
using UniverseLib;
|
||||
using UniverseLib.Input;
|
||||
using UniverseLib.UI;
|
||||
using UniverseLib.UI.Widgets;
|
||||
|
||||
namespace UnityExplorer.UI.Panels
|
||||
{
|
||||
public class ClipboardPanel : UIPanel
|
||||
{
|
||||
public static object Current { get; private set; }
|
||||
|
||||
public override UIManager.Panels PanelType => UIManager.Panels.Clipboard;
|
||||
public override string Name => "Clipboard";
|
||||
public override int MinWidth => 500;
|
||||
public override int MinHeight => 95;
|
||||
public override bool CanDragAndResize => true;
|
||||
public override bool NavButtonWanted => true;
|
||||
public override bool ShouldSaveActiveState => true;
|
||||
public override bool ShowByDefault => true;
|
||||
public override string GetSaveDataFromConfigManager() => ConfigManager.ClipboardData.Value;
|
||||
public override void DoSaveToConfigElement() => ConfigManager.ClipboardData.Value = this.ToSaveData();
|
||||
|
||||
private static Text CurrentPasteLabel;
|
||||
|
||||
public static void Copy(object obj)
|
||||
{
|
||||
Current = obj;
|
||||
Notification.ShowMessage("Copied!");
|
||||
UpdateCurrentPasteInfo();
|
||||
}
|
||||
|
||||
public static bool TryPaste(Type targetType, out object paste)
|
||||
{
|
||||
paste = Current;
|
||||
var pasteType = Current?.GetActualType();
|
||||
|
||||
if (Current != null && !targetType.IsAssignableFrom(pasteType))
|
||||
{
|
||||
Notification.ShowMessage($"Cannot assign '{pasteType.Name}' to '{targetType.Name}'!");
|
||||
return false;
|
||||
}
|
||||
|
||||
Notification.ShowMessage("Pasted!");
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ClearClipboard()
|
||||
{
|
||||
Current = null;
|
||||
UpdateCurrentPasteInfo();
|
||||
}
|
||||
|
||||
private static void UpdateCurrentPasteInfo()
|
||||
{
|
||||
CurrentPasteLabel.text = ToStringUtility.ToStringWithType(Current, typeof(object), false);
|
||||
}
|
||||
|
||||
private static void InspectClipboard()
|
||||
{
|
||||
if (Current.IsNullOrDestroyed())
|
||||
{
|
||||
Notification.ShowMessage("Cannot inspect a null or destroyed object!");
|
||||
return;
|
||||
}
|
||||
|
||||
InspectorManager.Inspect(Current);
|
||||
}
|
||||
|
||||
protected internal override void DoSetDefaultPosAndAnchors()
|
||||
{
|
||||
this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, MinWidth);
|
||||
this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, MinHeight);
|
||||
this.Rect.anchorMin = new Vector2(0.1f, 0.05f);
|
||||
this.Rect.anchorMax = new Vector2(0.4f, 0.15f);
|
||||
}
|
||||
|
||||
public override void ConstructPanelContent()
|
||||
{
|
||||
this.UIRoot.GetComponent<Image>().color = new(0.1f, 0.1f, 0.1f);
|
||||
|
||||
// Actual panel content
|
||||
|
||||
var firstRow = UIFactory.CreateHorizontalGroup(UIRoot, "FirstRow", false, false, true, true, 5, new(2,2,2,2), new(1,1,1,0));
|
||||
UIFactory.SetLayoutElement(firstRow, minHeight: 25, flexibleWidth: 999);
|
||||
|
||||
// Title for "Current Paste:"
|
||||
var currentPasteTitle = UIFactory.CreateLabel(firstRow, "CurrentPasteTitle", "Current paste:", TextAnchor.MiddleLeft, color: Color.grey);
|
||||
UIFactory.SetLayoutElement(currentPasteTitle.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 999);
|
||||
|
||||
// Clear clipboard button
|
||||
var clearButton = UIFactory.CreateButton(firstRow, "ClearPasteButton", "Clear Clipboard");
|
||||
UIFactory.SetLayoutElement(clearButton.Component.gameObject, minWidth: 120, minHeight: 25, flexibleWidth: 0);
|
||||
clearButton.OnClick += () => Copy(null);
|
||||
|
||||
// Current Paste info row
|
||||
var currentPasteHolder = UIFactory.CreateHorizontalGroup(UIRoot, "SecondRow", false, false, true, true, 0,
|
||||
new(2, 2, 2, 2), childAlignment: TextAnchor.UpperCenter);
|
||||
|
||||
// Actual current paste info label
|
||||
CurrentPasteLabel = UIFactory.CreateLabel(currentPasteHolder, "CurrentPasteInfo", "not set", TextAnchor.UpperLeft);
|
||||
UIFactory.SetLayoutElement(CurrentPasteLabel.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 999, flexibleHeight: 999);
|
||||
UpdateCurrentPasteInfo();
|
||||
|
||||
// Inspect button
|
||||
var inspectButton = UIFactory.CreateButton(currentPasteHolder, "InspectButton", "Inspect");
|
||||
UIFactory.SetLayoutElement(inspectButton.Component.gameObject, minHeight: 25, flexibleHeight: 0, minWidth: 80, flexibleWidth: 0);
|
||||
inspectButton.OnClick += InspectClipboard;
|
||||
}
|
||||
}
|
||||
}
|
@ -46,10 +46,7 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
public override string GetSaveDataFromConfigManager() => ConfigManager.InspectorData.Value;
|
||||
|
||||
public override void DoSaveToConfigElement()
|
||||
{
|
||||
ConfigManager.InspectorData.Value = this.ToSaveData();
|
||||
}
|
||||
public override void DoSaveToConfigElement() => ConfigManager.InspectorData.Value = this.ToSaveData();
|
||||
|
||||
protected internal override void DoSetDefaultPosAndAnchors()
|
||||
{
|
||||
|
@ -15,15 +15,33 @@ namespace UnityExplorer.UI.Panels
|
||||
{
|
||||
public class PanelDragger
|
||||
{
|
||||
private enum MouseState
|
||||
{
|
||||
Down,
|
||||
Held,
|
||||
NotPressed
|
||||
}
|
||||
|
||||
#region Static
|
||||
|
||||
public static bool Resizing { get; private set; }
|
||||
public static bool ResizePrompting => resizeCursorObj && resizeCursorObj.activeSelf;
|
||||
|
||||
public static bool ResizePrompting => s_resizeCursorObj && s_resizeCursorObj.activeSelf;
|
||||
public static GameObject resizeCursorObj;
|
||||
internal static bool wasAnyDragging;
|
||||
|
||||
internal static List<PanelDragger> Instances = new();
|
||||
|
||||
private static bool handledInstanceThisFrame;
|
||||
|
||||
static PanelDragger()
|
||||
{
|
||||
UIPanel.OnPanelsReordered += OnPanelsReordered;
|
||||
}
|
||||
|
||||
internal static void ForceEnd()
|
||||
{
|
||||
s_resizeCursorObj.SetActive(false);
|
||||
resizeCursorObj.SetActive(false);
|
||||
wasAnyDragging = false;
|
||||
Resizing = false;
|
||||
|
||||
@ -34,13 +52,6 @@ namespace UnityExplorer.UI.Panels
|
||||
}
|
||||
}
|
||||
|
||||
internal static List<PanelDragger> Instances = new List<PanelDragger>();
|
||||
|
||||
static PanelDragger()
|
||||
{
|
||||
UIPanel.OnPanelsReordered += OnPanelsReordered;
|
||||
}
|
||||
|
||||
public static void OnPanelsReordered()
|
||||
{
|
||||
Instances.Sort((a, b) => b.Panel.GetSiblingIndex().CompareTo(a.Panel.GetSiblingIndex()));
|
||||
@ -54,18 +65,9 @@ namespace UnityExplorer.UI.Panels
|
||||
}
|
||||
}
|
||||
|
||||
private enum MouseState
|
||||
{
|
||||
Down,
|
||||
Held,
|
||||
NotPressed
|
||||
}
|
||||
|
||||
private static bool handledInstanceThisFrame;
|
||||
|
||||
public static void UpdateInstances()
|
||||
{
|
||||
if (!s_resizeCursorObj)
|
||||
if (!resizeCursorObj)
|
||||
CreateCursorUI();
|
||||
|
||||
MouseState state;
|
||||
@ -99,10 +101,6 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
#endregion
|
||||
|
||||
public static GameObject s_resizeCursorObj;
|
||||
|
||||
internal static bool wasAnyDragging;
|
||||
|
||||
// Instance
|
||||
|
||||
public UIPanel UIPanel { get; private set; }
|
||||
@ -112,27 +110,23 @@ namespace UnityExplorer.UI.Panels
|
||||
public event Action<RectTransform> OnFinishResize;
|
||||
public event Action<RectTransform> OnFinishDrag;
|
||||
|
||||
private readonly RectTransform canvasTransform;
|
||||
|
||||
// Dragging
|
||||
public RectTransform DragableArea { get; set; }
|
||||
public bool WasDragging { get; set; }
|
||||
private Vector2 m_lastDragPosition;
|
||||
private Vector2 lastDragPosition;
|
||||
|
||||
// Resizing
|
||||
private const int RESIZE_THICKNESS = 10;
|
||||
|
||||
//internal readonly Vector2 minResize = new Vector2(200, 50);
|
||||
|
||||
private bool WasResizing { get; set; }
|
||||
private ResizeTypes m_currentResizeType = ResizeTypes.NONE;
|
||||
private Vector2 m_lastResizePos;
|
||||
private ResizeTypes currentResizeType = ResizeTypes.NONE;
|
||||
private Vector2 lastResizePos;
|
||||
|
||||
private bool WasHoveringResize => s_resizeCursorObj.activeInHierarchy;
|
||||
private bool WasHoveringResize => resizeCursorObj.activeInHierarchy;
|
||||
|
||||
private ResizeTypes m_lastResizeHoverType;
|
||||
private ResizeTypes lastResizeHoverType;
|
||||
|
||||
private Rect m_totalResizeRect;
|
||||
private Rect totalResizeRect;
|
||||
|
||||
public PanelDragger(RectTransform dragArea, RectTransform panelToDrag, UIPanel panel)
|
||||
{
|
||||
@ -141,16 +135,13 @@ namespace UnityExplorer.UI.Panels
|
||||
DragableArea = dragArea;
|
||||
Panel = panelToDrag;
|
||||
|
||||
if (!canvasTransform)
|
||||
canvasTransform = Panel.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
|
||||
|
||||
UpdateResizeCache();
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
if (s_resizeCursorObj)
|
||||
GameObject.Destroy(s_resizeCursorObj);
|
||||
if (resizeCursorObj)
|
||||
GameObject.Destroy(resizeCursorObj);
|
||||
|
||||
if (Instances.Contains(this))
|
||||
Instances.Remove(this);
|
||||
@ -166,7 +157,7 @@ namespace UnityExplorer.UI.Panels
|
||||
Vector3 dragPos = DragableArea.InverseTransformPoint(rawMousePos);
|
||||
bool inDragPos = DragableArea.rect.Contains(dragPos);
|
||||
|
||||
if (WasHoveringResize && s_resizeCursorObj)
|
||||
if (WasHoveringResize && resizeCursorObj)
|
||||
UpdateHoverImagePos();
|
||||
|
||||
switch (state)
|
||||
@ -243,15 +234,15 @@ namespace UnityExplorer.UI.Panels
|
||||
{
|
||||
wasAnyDragging = true;
|
||||
WasDragging = true;
|
||||
m_lastDragPosition = InputManager.MousePosition;
|
||||
lastDragPosition = InputManager.MousePosition;
|
||||
}
|
||||
|
||||
public void OnDrag()
|
||||
{
|
||||
var mousePos = InputManager.MousePosition;
|
||||
|
||||
Vector2 diff = (Vector2)mousePos - m_lastDragPosition;
|
||||
m_lastDragPosition = mousePos;
|
||||
Vector2 diff = (Vector2)mousePos - lastDragPosition;
|
||||
lastDragPosition = mousePos;
|
||||
|
||||
Panel.localPosition = Panel.localPosition + (Vector3)diff;
|
||||
|
||||
@ -296,7 +287,7 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
private void UpdateResizeCache()
|
||||
{
|
||||
m_totalResizeRect = new Rect(Panel.rect.x - RESIZE_THICKNESS + 1,
|
||||
totalResizeRect = new Rect(Panel.rect.x - RESIZE_THICKNESS + 1,
|
||||
Panel.rect.y - RESIZE_THICKNESS + 1,
|
||||
Panel.rect.width + DBL_THICKESS - 2,
|
||||
Panel.rect.height + DBL_THICKESS - 2);
|
||||
@ -305,34 +296,34 @@ namespace UnityExplorer.UI.Panels
|
||||
if (AllowDragAndResize)
|
||||
{
|
||||
m_resizeMask[ResizeTypes.Bottom] = new Rect(
|
||||
m_totalResizeRect.x,
|
||||
m_totalResizeRect.y,
|
||||
m_totalResizeRect.width,
|
||||
totalResizeRect.x,
|
||||
totalResizeRect.y,
|
||||
totalResizeRect.width,
|
||||
RESIZE_THICKNESS);
|
||||
|
||||
m_resizeMask[ResizeTypes.Left] = new Rect(
|
||||
m_totalResizeRect.x,
|
||||
m_totalResizeRect.y,
|
||||
totalResizeRect.x,
|
||||
totalResizeRect.y,
|
||||
RESIZE_THICKNESS,
|
||||
m_totalResizeRect.height);
|
||||
totalResizeRect.height);
|
||||
|
||||
m_resizeMask[ResizeTypes.Top] = new Rect(
|
||||
m_totalResizeRect.x,
|
||||
totalResizeRect.x,
|
||||
Panel.rect.y + Panel.rect.height - 2,
|
||||
m_totalResizeRect.width,
|
||||
totalResizeRect.width,
|
||||
RESIZE_THICKNESS);
|
||||
|
||||
m_resizeMask[ResizeTypes.Right] = new Rect(
|
||||
m_totalResizeRect.x + Panel.rect.width + RESIZE_THICKNESS - 2,
|
||||
m_totalResizeRect.y,
|
||||
totalResizeRect.x + Panel.rect.width + RESIZE_THICKNESS - 2,
|
||||
totalResizeRect.y,
|
||||
RESIZE_THICKNESS,
|
||||
m_totalResizeRect.height);
|
||||
totalResizeRect.height);
|
||||
}
|
||||
}
|
||||
|
||||
private bool MouseInResizeArea(Vector2 mousePos)
|
||||
{
|
||||
return m_totalResizeRect.Contains(mousePos);
|
||||
return totalResizeRect.Contains(mousePos);
|
||||
}
|
||||
|
||||
private ResizeTypes GetResizeType(Vector2 mousePos)
|
||||
@ -361,16 +352,16 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
public void OnHoverResize(ResizeTypes resizeType)
|
||||
{
|
||||
if (WasHoveringResize && m_lastResizeHoverType == resizeType)
|
||||
if (WasHoveringResize && lastResizeHoverType == resizeType)
|
||||
return;
|
||||
|
||||
// we are entering resize, or the resize type has changed.
|
||||
|
||||
//WasHoveringResize = true;
|
||||
m_lastResizeHoverType = resizeType;
|
||||
lastResizeHoverType = resizeType;
|
||||
|
||||
s_resizeCursorObj.SetActive(true);
|
||||
s_resizeCursorObj.transform.SetAsLastSibling();
|
||||
resizeCursorObj.SetActive(true);
|
||||
resizeCursorObj.transform.SetAsLastSibling();
|
||||
|
||||
// set the rotation for the resize icon
|
||||
float iconRotation = 0f;
|
||||
@ -387,9 +378,9 @@ namespace UnityExplorer.UI.Panels
|
||||
iconRotation = 135f; break;
|
||||
}
|
||||
|
||||
Quaternion rot = s_resizeCursorObj.transform.rotation;
|
||||
Quaternion rot = resizeCursorObj.transform.rotation;
|
||||
rot.eulerAngles = new Vector3(0, 0, iconRotation);
|
||||
s_resizeCursorObj.transform.rotation = rot;
|
||||
resizeCursorObj.transform.rotation = rot;
|
||||
|
||||
UpdateHoverImagePos();
|
||||
}
|
||||
@ -397,19 +388,19 @@ namespace UnityExplorer.UI.Panels
|
||||
// update the resize icon position to be above the mouse
|
||||
private void UpdateHoverImagePos()
|
||||
{
|
||||
s_resizeCursorObj.transform.localPosition = canvasTransform.InverseTransformPoint(InputManager.MousePosition);
|
||||
resizeCursorObj.transform.localPosition = UIManager.UIRootRect.InverseTransformPoint(InputManager.MousePosition);
|
||||
}
|
||||
|
||||
public void OnHoverResizeEnd()
|
||||
{
|
||||
//WasHoveringResize = false;
|
||||
s_resizeCursorObj.SetActive(false);
|
||||
resizeCursorObj.SetActive(false);
|
||||
}
|
||||
|
||||
public void OnBeginResize(ResizeTypes resizeType)
|
||||
{
|
||||
m_currentResizeType = resizeType;
|
||||
m_lastResizePos = InputManager.MousePosition;
|
||||
currentResizeType = resizeType;
|
||||
lastResizePos = InputManager.MousePosition;
|
||||
WasResizing = true;
|
||||
Resizing = true;
|
||||
}
|
||||
@ -417,15 +408,15 @@ namespace UnityExplorer.UI.Panels
|
||||
public void OnResize()
|
||||
{
|
||||
Vector3 mousePos = InputManager.MousePosition;
|
||||
Vector2 diff = m_lastResizePos - (Vector2)mousePos;
|
||||
Vector2 diff = lastResizePos - (Vector2)mousePos;
|
||||
|
||||
if ((Vector2)mousePos == m_lastResizePos)
|
||||
if ((Vector2)mousePos == lastResizePos)
|
||||
return;
|
||||
|
||||
if (mousePos.x < 0 || mousePos.y < 0 || mousePos.x > Screen.width || mousePos.y > Screen.height)
|
||||
return;
|
||||
|
||||
m_lastResizePos = mousePos;
|
||||
lastResizePos = mousePos;
|
||||
|
||||
float diffX = (float)((decimal)diff.x / Screen.width);
|
||||
float diffY = (float)((decimal)diff.y / Screen.height);
|
||||
@ -433,14 +424,14 @@ namespace UnityExplorer.UI.Panels
|
||||
Vector2 anchorMin = Panel.anchorMin;
|
||||
Vector2 anchorMax = Panel.anchorMax;
|
||||
|
||||
if (m_currentResizeType.HasFlag(ResizeTypes.Left))
|
||||
if (currentResizeType.HasFlag(ResizeTypes.Left))
|
||||
anchorMin.x -= diffX;
|
||||
else if (m_currentResizeType.HasFlag(ResizeTypes.Right))
|
||||
else if (currentResizeType.HasFlag(ResizeTypes.Right))
|
||||
anchorMax.x -= diffX;
|
||||
|
||||
if (m_currentResizeType.HasFlag(ResizeTypes.Top))
|
||||
if (currentResizeType.HasFlag(ResizeTypes.Top))
|
||||
anchorMax.y -= diffY;
|
||||
else if (m_currentResizeType.HasFlag(ResizeTypes.Bottom))
|
||||
else if (currentResizeType.HasFlag(ResizeTypes.Bottom))
|
||||
anchorMin.y -= diffY;
|
||||
|
||||
var prevMin = Panel.anchorMin;
|
||||
@ -475,13 +466,13 @@ namespace UnityExplorer.UI.Panels
|
||||
try
|
||||
{
|
||||
var text = UIFactory.CreateLabel(UIManager.UIRoot, "ResizeCursor", "↔", TextAnchor.MiddleCenter, Color.white, true, 35);
|
||||
s_resizeCursorObj = text.gameObject;
|
||||
resizeCursorObj = text.gameObject;
|
||||
|
||||
RectTransform rect = s_resizeCursorObj.GetComponent<RectTransform>();
|
||||
RectTransform rect = resizeCursorObj.GetComponent<RectTransform>();
|
||||
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 64);
|
||||
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 64);
|
||||
|
||||
s_resizeCursorObj.SetActive(false);
|
||||
resizeCursorObj.SetActive(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -23,8 +23,8 @@ namespace UnityExplorer.UI.Panels
|
||||
public static event Action OnPanelsReordered;
|
||||
public static event Action OnClickedOutsidePanels;
|
||||
|
||||
internal static readonly List<UIPanel> instances = new List<UIPanel>();
|
||||
internal static readonly Dictionary<int, UIPanel> transformToPanelDict = new Dictionary<int, UIPanel>();
|
||||
internal static readonly List<UIPanel> instances = new();
|
||||
internal static readonly Dictionary<int, UIPanel> transformToPanelDict = new();
|
||||
|
||||
public static void UpdateFocus()
|
||||
{
|
||||
|
@ -27,17 +27,6 @@ namespace UnityExplorer.UI.Panels
|
||||
private ButtonListHandler<GameObject, ButtonCell> dataHandler;
|
||||
private ScrollPool<ButtonCell> buttonScrollPool;
|
||||
|
||||
public override void ConstructPanelContent()
|
||||
{
|
||||
dataHandler = new ButtonListHandler<GameObject, ButtonCell>(buttonScrollPool, GetEntries, SetCell, ShouldDisplayCell, OnCellClicked);
|
||||
|
||||
buttonScrollPool = UIFactory.CreateScrollPool<ButtonCell>(this.content, "ResultsList", out GameObject scrollObj,
|
||||
out GameObject scrollContent);
|
||||
|
||||
buttonScrollPool.Initialize(dataHandler);
|
||||
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
||||
}
|
||||
|
||||
public void ShowResults()
|
||||
{
|
||||
dataHandler.RefreshData();
|
||||
@ -65,6 +54,17 @@ namespace UnityExplorer.UI.Panels
|
||||
cell.Button.ButtonText.text = $"<color=cyan>{obj.name}</color> ({obj.transform.GetTransformPath(true)})";
|
||||
}
|
||||
|
||||
public override void ConstructPanelContent()
|
||||
{
|
||||
dataHandler = new ButtonListHandler<GameObject, ButtonCell>(buttonScrollPool, GetEntries, SetCell, ShouldDisplayCell, OnCellClicked);
|
||||
|
||||
buttonScrollPool = UIFactory.CreateScrollPool<ButtonCell>(this.content, "ResultsList", out GameObject scrollObj,
|
||||
out GameObject scrollContent);
|
||||
|
||||
buttonScrollPool.Initialize(dataHandler);
|
||||
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
||||
}
|
||||
|
||||
protected internal override void DoSetDefaultPosAndAnchors()
|
||||
{
|
||||
this.Rect.anchorMin = new Vector2(0.5f, 0.5f);
|
||||
|
Reference in New Issue
Block a user