Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit ea7b91f

Browse files
committed
Implement Clipboard and Notifications, misc cleanups
1 parent c79223f commit ea7b91f

30 files changed

+510
-325
lines changed

src/CSConsole/ConsoleController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ private static void DisableConsole(Exception ex)
603603
}
604604
}
605605

606-
private static readonly Dictionary<string, string> helpDict = new Dictionary<string, string>();
606+
private static readonly Dictionary<string, string> helpDict = new();
607607

608608
public static void SetupHelpInteraction()
609609
{
@@ -658,15 +658,17 @@ public static void HelpSelected(int index)
658658
++x;
659659
660660
/* The following helpers are available in REPL mode:
661-
* GetUsing(); - prints the current using directives to the console log
662-
* GetVars(); - prints the names and values of the REPL variables you have defined
663-
* GetClasses(); - prints the names and members of the classes you have defined
664-
* Log(obj); - prints a message to the console log
665661
* CurrentTarget; - System.Object, the target of the active Inspector tab
666662
* AllTargets; - System.Object[], the targets of all Inspector tabs
663+
* Log(obj); - prints a message to the console log
667664
* Inspect(obj); - inspect the object with the Inspector
668665
* Inspect(someType); - inspect a Type with static reflection
669666
* Start(enumerator); - starts the IEnumerator as a Coroutine
667+
* Copy(obj); - copies the object to the UnityExplorer Clipboard
668+
* Paste(); - System.Object, the contents of the Clipboard.
669+
* GetUsing(); - prints the current using directives to the console log
670+
* GetVars(); - prints the names and values of the REPL variables you have defined
671+
* GetClasses(); - prints the names and members of the classes you have defined
670672
* help; - the default REPL help command, contains additional helpers.
671673
*/";
672674

src/CSConsole/ScriptInteraction.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,39 @@
66
using System.Text;
77
using UnityEngine;
88
using UnityExplorer.Runtime;
9+
using UnityExplorer.UI.Panels;
910
using UniverseLib;
1011

1112
namespace UnityExplorer.CSConsole
1213
{
1314
public class ScriptInteraction : InteractiveBase
1415
{
15-
public static void Log(object message)
16-
{
17-
ExplorerCore.Log(message);
18-
}
16+
public static object CurrentTarget
17+
=> InspectorManager.ActiveInspector?.Target;
1918

20-
public static object CurrentTarget => InspectorManager.ActiveInspector?.Target;
19+
public static object[] AllTargets
20+
=> InspectorManager.Inspectors.Select(it => it.Target).ToArray();
2121

22-
public static object[] AllTargets => InspectorManager.Inspectors.Select(it => it.Target).ToArray();
22+
public static void Log(object message)
23+
=> ExplorerCore.Log(message);
2324

2425
public static void Inspect(object obj)
25-
{
26-
InspectorManager.Inspect(obj);
27-
}
26+
=> InspectorManager.Inspect(obj);
2827

2928
public static void Inspect(Type type)
30-
{
31-
InspectorManager.Inspect(type);
32-
}
29+
=> InspectorManager.Inspect(type);
3330

34-
public static void Start(IEnumerator ienumerator)
35-
{
36-
RuntimeProvider.Instance.StartCoroutine(ienumerator);
37-
}
31+
public static void Start(IEnumerator ienumerator)
32+
=> RuntimeProvider.Instance.StartCoroutine(ienumerator);
33+
34+
public static void Copy(object obj)
35+
=> ClipboardPanel.Copy(obj);
36+
37+
public static object Paste()
38+
=> ClipboardPanel.Current;
3839

3940
public static void GetUsing()
40-
{
41-
Log(Evaluator.GetUsing());
42-
}
41+
=> Log(Evaluator.GetUsing());
4342

4443
public static void GetVars()
4544
{

src/CacheObject/CacheConfigEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public override void TrySetUserValue(object value)
4444
RefConfigElement.BoxedValue = value;
4545
}
4646

47-
protected override bool SetCellEvaluateState(CacheObjectCell cell) => false;
47+
protected override bool TryAutoEvaluateIfUnitialized(CacheObjectCell cell) => true;
4848
}
4949
}

src/CacheObject/CacheKeyValuePair.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ public override void TrySetUserValue(object value)
9292
}
9393

9494

95-
protected override bool SetCellEvaluateState(CacheObjectCell cell)
96-
{
97-
// not needed
98-
return false;
99-
}
95+
protected override bool TryAutoEvaluateIfUnitialized(CacheObjectCell cell) => true;
10096
}
10197
}

src/CacheObject/CacheListEntry.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CacheListEntry : CacheObjectBase
1313

1414
public override bool ShouldAutoEvaluate => true;
1515
public override bool HasArguments => false;
16-
public override bool CanWrite => Owner.CanWrite;
16+
public override bool CanWrite => Owner?.CanWrite ?? false;
1717

1818
public void SetListOwner(InteractiveList list, int listIndex)
1919
{
@@ -37,11 +37,6 @@ public override void TrySetUserValue(object value)
3737
(Owner as InteractiveList).TrySetValueToIndex(value, this.ListIndex);
3838
}
3939

40-
41-
protected override bool SetCellEvaluateState(CacheObjectCell cell)
42-
{
43-
// not needed
44-
return false;
45-
}
40+
protected override bool TryAutoEvaluateIfUnitialized(CacheObjectCell cell) => true;
4641
}
4742
}

src/CacheObject/CacheMember.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected override void SetValueState(CacheObjectCell cell, ValueStateArgs args)
9595
private static readonly Color evalEnabledColor = new Color(0.15f, 0.25f, 0.15f);
9696
private static readonly Color evalDisabledColor = new Color(0.15f, 0.15f, 0.15f);
9797

98-
protected override bool SetCellEvaluateState(CacheObjectCell objectcell)
98+
protected override bool TryAutoEvaluateIfUnitialized(CacheObjectCell objectcell)
9999
{
100100
var cell = objectcell as CacheMemberCell;
101101

@@ -126,13 +126,13 @@ protected override bool SetCellEvaluateState(CacheObjectCell objectcell)
126126
SetValueState(cell, ValueStateArgs.Default);
127127
cell.RefreshSubcontentButton();
128128

129-
return true;
129+
return false;
130130
}
131131

132132
if (State == ValueState.NotEvaluated)
133133
Evaluate();
134134

135-
return false;
135+
return true;
136136
}
137137

138138
public void OnEvaluateClicked()
@@ -148,7 +148,7 @@ public void OnEvaluateClicked()
148148
this.Evaluator = Pool<EvaluateWidget>.Borrow();
149149
Evaluator.OnBorrowedFromPool(this);
150150
Evaluator.UIRoot.transform.SetParent((CellView as CacheMemberCell).EvaluateHolder.transform, false);
151-
SetCellEvaluateState(CellView);
151+
TryAutoEvaluateIfUnitialized(CellView);
152152
}
153153
else
154154
{
@@ -157,7 +157,7 @@ public void OnEvaluateClicked()
157157
else
158158
Evaluator.UIRoot.SetActive(true);
159159

160-
SetCellEvaluateState(CellView);
160+
TryAutoEvaluateIfUnitialized(CellView);
161161
}
162162
}
163163
}

src/CacheObject/CacheObjectBase.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ protected string GetValueLabel()
254254

255255
// Setting cell state from our model
256256

257-
/// <summary>Return true if SetCell should abort, false if it should continue.</summary>
258-
protected abstract bool SetCellEvaluateState(CacheObjectCell cell);
257+
/// <summary>Return false if SetCell should abort, true if it should continue.</summary>
258+
protected abstract bool TryAutoEvaluateIfUnitialized(CacheObjectCell cell);
259259

260260
public virtual void SetDataToCell(CacheObjectCell cell)
261261
{
@@ -271,9 +271,21 @@ public virtual void SetDataToCell(CacheObjectCell cell)
271271
IValue.SetLayout();
272272
}
273273

274-
if (SetCellEvaluateState(cell))
274+
bool evaluated = TryAutoEvaluateIfUnitialized(cell);
275+
276+
if (cell.CopyButton != null)
277+
{
278+
bool hasEvaluated = State != ValueState.NotEvaluated && State != ValueState.Exception;
279+
cell.CopyButton.Component.gameObject.SetActive(hasEvaluated);
280+
cell.PasteButton.Component.gameObject.SetActive(hasEvaluated && this.CanWrite);
281+
}
282+
283+
if (!evaluated)
275284
return;
276285

286+
// The following only executes if the object has evaluated.
287+
// For members and properties with args, they will return by default now.
288+
277289
switch (State)
278290
{
279291
case ValueState.Exception:

src/CacheObject/IValues/InteractiveList.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public class InteractiveList : InteractiveValue, ICellPoolDataSource<CacheListEn
3232
private PropertyInfo genericIndexer;
3333

3434
public int ItemCount => cachedEntries.Count;
35-
private readonly List<CacheListEntry> cachedEntries = new List<CacheListEntry>();
35+
private readonly List<CacheListEntry> cachedEntries = new();
3636

3737
public ScrollPool<CacheListEntryCell> ListScrollPool { get; private set; }
3838

3939
public Text TopLabel;
40+
private LayoutElement scrollLayout;
41+
private Text NotSupportedLabel;
4042

4143
public override void OnBorrowed(CacheObjectBase owner)
4244
{
@@ -65,6 +67,28 @@ private void ClearAndRelease()
6567
cachedEntries.Clear();
6668
}
6769

70+
// List entry scroll pool
71+
72+
public override void SetLayout()
73+
{
74+
var minHeight = 5f;
75+
76+
foreach (var cell in ListScrollPool.CellPool)
77+
{
78+
if (cell.Enabled)
79+
minHeight += cell.Rect.rect.height;
80+
}
81+
82+
this.scrollLayout.minHeight = Math.Min(InspectorPanel.CurrentPanelHeight - 400f, minHeight);
83+
}
84+
85+
public void OnCellBorrowed(CacheListEntryCell cell) { } // not needed
86+
87+
public void SetCell(CacheListEntryCell cell, int index)
88+
{
89+
CacheObjectControllerHelper.SetCell(cell, index, cachedEntries, null);
90+
}
91+
6892
// Setting the List value itself to this model
6993
public override void SetValue(object value)
7094
{
@@ -212,32 +236,6 @@ public void TrySetValueToIndex(object value, int index)
212236
}
213237
}
214238

215-
// List entry scroll pool
216-
217-
public override void SetLayout()
218-
{
219-
var minHeight = 5f;
220-
221-
foreach (var cell in ListScrollPool.CellPool)
222-
{
223-
if (cell.Enabled)
224-
minHeight += cell.Rect.rect.height;
225-
}
226-
227-
this.scrollLayout.minHeight = Math.Min(InspectorPanel.CurrentPanelHeight - 400f, minHeight);
228-
}
229-
230-
public void OnCellBorrowed(CacheListEntryCell cell) { } // not needed
231-
232-
public void SetCell(CacheListEntryCell cell, int index)
233-
{
234-
CacheObjectControllerHelper.SetCell(cell, index, cachedEntries, null);
235-
}
236-
237-
private LayoutElement scrollLayout;
238-
239-
private Text NotSupportedLabel;
240-
241239
public override GameObject CreateContent(GameObject parent)
242240
{
243241
UIRoot = UIFactory.CreateVerticalGroup(parent, "InteractiveList", true, true, true, true, 6, new Vector4(10, 3, 15, 4),

src/CacheObject/Views/CacheObjectCell.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using UnityExplorer.CacheObject.IValues;
88
using UnityExplorer.Inspectors;
99
using UnityExplorer.UI;
10+
using UnityExplorer.UI.Panels;
1011
using UnityExplorer.UI.Widgets;
1112
using UniverseLib;
1213
using UniverseLib.UI;
@@ -47,9 +48,10 @@ public void Enable()
4748
public LayoutElement NameLayout;
4849
public GameObject RightGroupContent;
4950
public LayoutElement RightGroupLayout;
51+
public GameObject SubContentHolder;
5052

5153
public Text NameLabel;
52-
public InputFieldRef HiddenNameLabel;
54+
public InputFieldRef HiddenNameLabel; // for selecting the name label
5355
public Text TypeLabel;
5456
public Text ValueLabel;
5557
public Toggle Toggle;
@@ -60,7 +62,11 @@ public void Enable()
6062
public ButtonRef SubContentButton;
6163
public ButtonRef ApplyButton;
6264

63-
public GameObject SubContentHolder;
65+
public ButtonRef CopyButton;
66+
public ButtonRef PasteButton;
67+
68+
public readonly Color subInactiveColor = new(0.23f, 0.23f, 0.23f);
69+
public readonly Color subActiveColor = new(0.23f, 0.33f, 0.23f);
6470

6571
protected virtual void ApplyClicked()
6672
{
@@ -82,26 +88,26 @@ protected virtual void SubContentClicked()
8288
this.Occupant.OnCellSubContentToggle();
8389
}
8490

85-
public readonly Color subInactiveColor = new Color(0.23f, 0.23f, 0.23f);
86-
public readonly Color subActiveColor = new Color(0.23f, 0.33f, 0.23f);
91+
protected virtual void OnCopyClicked()
92+
{
93+
ClipboardPanel.Copy(this.Occupant.Value);
94+
}
95+
96+
protected virtual void OnPasteClicked()
97+
{
98+
if (ClipboardPanel.TryPaste(this.Occupant.FallbackType, out object paste))
99+
this.Occupant.SetUserValue(paste);
100+
}
87101

88102
public void RefreshSubcontentButton()
89103
{
90-
if (!this.SubContentHolder.activeSelf)
91-
{
92-
this.SubContentButton.ButtonText.text = "▲";
93-
RuntimeProvider.Instance.SetColorBlock(SubContentButton.Component, subInactiveColor, subInactiveColor * 1.3f);
94-
}
95-
else
96-
{
97-
this.SubContentButton.ButtonText.text = "▼";
98-
RuntimeProvider.Instance.SetColorBlock(SubContentButton.Component, subActiveColor, subActiveColor * 1.3f);
99-
}
104+
this.SubContentButton.ButtonText.text = SubContentHolder.activeSelf ? "▼" : "▲";
105+
Color color = SubContentHolder.activeSelf ? subActiveColor : subInactiveColor;
106+
RuntimeProvider.Instance.SetColorBlock(SubContentButton.Component, color, color * 1.3f);
100107
}
101108

102109
protected abstract void ConstructEvaluateHolder(GameObject parent);
103110

104-
105111
public virtual GameObject CreateContent(GameObject parent)
106112
{
107113
// Main layout
@@ -158,7 +164,7 @@ public virtual GameObject CreateContent(GameObject parent)
158164

159165
TypeLabel = UIFactory.CreateLabel(rightHoriGroup, "ReturnLabel", "<notset>", TextAnchor.MiddleLeft);
160166
TypeLabel.horizontalOverflow = HorizontalWrapMode.Wrap;
161-
UIFactory.SetLayoutElement(TypeLabel.gameObject, minHeight: 25, flexibleHeight: 150, minWidth: 60, flexibleWidth: 0);
167+
UIFactory.SetLayoutElement(TypeLabel.gameObject, minHeight: 25, flexibleHeight: 150, minWidth: 45, flexibleWidth: 0);
162168

163169
// Bool and number value interaction
164170

@@ -188,6 +194,24 @@ public virtual GameObject CreateContent(GameObject parent)
188194
ValueLabel.horizontalOverflow = HorizontalWrapMode.Wrap;
189195
UIFactory.SetLayoutElement(ValueLabel.gameObject, minHeight: 25, flexibleHeight: 150, flexibleWidth: 9999);
190196

197+
// Copy and Paste buttons
198+
199+
var buttonHolder = UIFactory.CreateHorizontalGroup(rightHoriGroup, "CopyPasteButtons", false, false, true, true, 4,
200+
bgColor: new(1,1,1,0), childAlignment: TextAnchor.MiddleLeft);
201+
UIFactory.SetLayoutElement(buttonHolder, minWidth: 60, flexibleWidth: 0);
202+
203+
CopyButton = UIFactory.CreateButton(buttonHolder, "CopyButton", "Copy", new Color(0.13f, 0.13f, 0.13f, 1f));
204+
UIFactory.SetLayoutElement(CopyButton.Component.gameObject, minHeight: 25, minWidth: 28, flexibleWidth: 0);
205+
CopyButton.ButtonText.color = Color.yellow;
206+
CopyButton.ButtonText.fontSize = 10;
207+
CopyButton.OnClick += OnCopyClicked;
208+
209+
PasteButton = UIFactory.CreateButton(buttonHolder, "PasteButton", "Paste", new Color(0.13f, 0.13f, 0.13f, 1f));
210+
UIFactory.SetLayoutElement(PasteButton.Component.gameObject, minHeight: 25, minWidth: 28, flexibleWidth: 0);
211+
PasteButton.ButtonText.color = Color.green;
212+
PasteButton.ButtonText.fontSize = 10;
213+
PasteButton.OnClick += OnPasteClicked;
214+
191215
// Subcontent
192216

193217
SubContentHolder = UIFactory.CreateUIObject("SubContent", UIRoot);

0 commit comments

Comments
 (0)