Skip to content

Commit a8714bb

Browse files
committed
Fix bugs, optimize interaction
1 parent db4bc97 commit a8714bb

13 files changed

+379
-310
lines changed

Editor/Scripts/Components/TreeView/CustomTreeView.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ internal class CustomTreeView : TreeView
3333

3434
public event Action<int> onItemDoubleClicked;
3535

36+
public event Action onContextClicked;
37+
38+
public event Action<int> onItemContextClicked;
39+
3640
public event Action<int, string, string> onItemRenamed;
3741

3842
public event Action<int[]> onItemDragged;
@@ -135,28 +139,30 @@ protected override void SelectionChanged(IList<int> selectedIds)
135139

136140
protected override void ContextClicked()
137141
{
138-
if (buildMenu == null)
142+
onContextClicked?.Invoke();
143+
144+
if (buildMenu != null)
139145
{
140-
return;
146+
GenericMenu menu = new GenericMenu();
147+
buildMenu.Invoke(menu);
148+
menu.ShowAsContext();
149+
// 确保立刻显示菜单
150+
Event.current.Use();
141151
}
142-
GenericMenu menu = new GenericMenu();
143-
buildMenu.Invoke(menu);
144-
menu.ShowAsContext();
145-
// 确保立刻显示菜单
146-
Event.current.Use();
147152
}
148153

149154
protected override void ContextClickedItem(int id)
150155
{
151-
if (buildItemMenu == null)
156+
onItemContextClicked?.Invoke(id);
157+
158+
if (buildItemMenu != null)
152159
{
153-
return;
160+
GenericMenu menu = new GenericMenu();
161+
buildItemMenu.Invoke(menu, id);
162+
menu.ShowAsContext();
163+
// 确保立刻显示菜单
164+
Event.current.Use();
154165
}
155-
GenericMenu menu = new GenericMenu();
156-
buildItemMenu.Invoke(menu, id);
157-
menu.ShowAsContext();
158-
// 确保立刻显示菜单
159-
Event.current.Use();
160166
}
161167

162168
#endregion
@@ -195,7 +201,8 @@ protected override bool CanStartDrag(CanStartDragArgs args)
195201
{
196202
foreach (int id in args.draggedItemIDs)
197203
{
198-
if (FindItem(id).isContainer)
204+
CustomTreeViewItem item = FindItem(id);
205+
if (item == null || item.isContainer)
199206
{
200207
return false;
201208
}

Editor/Scripts/Window/CodeExecutorWindow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void Init()
150150
// 应用设置
151151
ApplySettings();
152152
// 更新内容
153-
UpdateContent();
153+
RefreshData();
154154
}
155155

156156
private void Reload()
@@ -161,7 +161,7 @@ private void Reload()
161161
// 应用设置
162162
ApplySettings();
163163
// 更新内容
164-
UpdateContent();
164+
RefreshData();
165165
}
166166

167167
#region Data
@@ -224,7 +224,7 @@ private void ApplySettings_CodeEditor()
224224
{
225225
if (!IsContentReady()) return;
226226

227-
SetCodeEditorFontSize(CodeExecutorSettings.fontSize);
227+
SetCodeEditorFontSize(CodeExecutorSettings.fontSize, false);
228228
}
229229

230230
#endregion

Editor/Scripts/Window/CodeExecutorWindowContent.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ private void InitContent()
9090
InitSidebar();
9191
// 详情
9292
InitDetail();
93-
// 拖放区
94-
//InitDropArea();
9593
}
9694

9795
/// <summary>
@@ -116,7 +114,7 @@ private bool IsContentReady()
116114
/// <summary>
117115
/// 当前代码段信息引用
118116
/// </summary>
119-
private SnippetInfo m_CurrSnippetInfo = null;
117+
private SnippetInfo m_CurrSnippet = null;
120118

121119
/// <summary>
122120
/// 当前选中代码段
@@ -138,8 +136,18 @@ private void UpdateContent()
138136
// 加载代码段列表
139137
UpdateSnippetTreeView();
140138

141-
// 恢复选中的代码段
139+
// 恢复选中代码段
142140
Switch(m_SelectedSnippetGuid);
141+
142+
// 恢复选中类别
143+
if (!string.IsNullOrEmpty(m_SelectedCategory))
144+
{
145+
int itemID = GetSnippetTreeViewItemIdByCategory(m_SelectedCategory);
146+
if (itemID >= 0)
147+
{
148+
SetSnippetTreeViewSelection(itemID, false);
149+
}
150+
}
143151
}
144152

145153
/// <summary>
@@ -174,29 +182,21 @@ private void Switch(SnippetInfo snippet)
174182
bool isNew = IsNewSnippet(snippet);
175183

176184
// 保存引用
177-
m_CurrSnippetInfo = snippet;
185+
m_CurrSnippet = snippet;
178186
// 记录选择
179187
m_SelectedSnippetGuid = isNew ? null : snippet.guid;
180188

181189
// 更新界面状态
182190
UpdateNewCodeItemStyle(isNew);
183191

184192
// 更新列表状态
185-
if (string.IsNullOrEmpty(m_SelectedCategory))
193+
if (isNew)
186194
{
187-
if (isNew)
188-
{
189-
ClearSnippetTreeViewSelection(false);
190-
}
191-
else
192-
{
193-
SetSnippetTreeViewSelection(m_SelectedSnippetGuid, false);
194-
}
195+
ClearSnippetTreeViewSelection(false);
195196
}
196197
else
197198
{
198-
int itemID = GetSnippetTreeViewItemIdByCategory(m_SelectedCategory);
199-
if (itemID >= 0) SetSnippetTreeViewSelection(itemID, false);
199+
SetSnippetTreeViewSelection(m_SelectedSnippetGuid, false);
200200
}
201201

202202
// 设置标题

Editor/Scripts/Window/CodeExecutorWindowMenu.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ public partial class CodeExecutorWindow : EditorWindow, IHasCustomMenu
1515
public void AddItemsToMenu(GenericMenu menu)
1616
{
1717
menu.AddItem(new GUIContent("Built-in Execution Mode/C#"), CodeExecutorManager.enableBuiltinExecModeCSharp, Menu_BuiltinExecutionModeCSharp);
18-
menu.AddItem(new GUIContent("Built-in Execution Mode/XLua (Standalone)"), CodeExecutorManager.enableBuiltinExecModeXLua, Menu_BuiltinExecutionModeXLua);
18+
menu.AddItem(new GUIContent("Built-in Execution Mode/xLua (Standalone)"), CodeExecutorManager.enableBuiltinExecModeXLua, Menu_BuiltinExecutionModeXLua);
19+
menu.AddItem(new GUIContent("Re-register Execution Modes"), false, Menu_ReRegisterExecutionModes);
1920
menu.AddItem(new GUIContent("Document: How to register execution modes?"), false, Menu_Document);
2021
menu.AddSeparator(string.Empty);
21-
menu.AddItem(new GUIContent("Reload"), false, Menu_Reload);
22+
menu.AddItem(new GUIContent("Reload Data & Settings"), false, Menu_Reload);
2223
menu.AddItem(new GUIContent("Show Serialized Data File"), false, Menu_ShowSerializedDataFile);
2324
menu.AddItem(new GUIContent("Show Serialized Settings File"), false, Menu_ShowSerializedSettingsFile);
2425
menu.AddSeparator(string.Empty);
26+
menu.AddDisabledItem(new GUIContent($"Code Editor Font Size/Current Font Size: {GetCodeEditorFontSize()}pt"));
27+
menu.AddItem(new GUIContent("Code Editor Font Size/> + 1pt (Ctrl+MouseUp)"), false, Menu_CodeEditorFontSizeUp);
28+
menu.AddItem(new GUIContent("Code Editor Font Size/> - 1pt (Ctrl+MouseDown)"), false, Menu_CodeEditorFontSizeDown);
29+
menu.AddSeparator(string.Empty);
2530
menu.AddItem(new GUIContent("Import From File"), false, Menu_ImportFromFile);
2631
menu.AddSeparator(string.Empty);
2732
menu.AddItem(new GUIContent("Clear Data ⚠️"), false, Menu_ClearData);
@@ -42,17 +47,19 @@ private void Menu_BuiltinExecutionModeXLua()
4247
CodeExecutorManager.enableBuiltinExecModeXLua = !CodeExecutorManager.enableBuiltinExecModeXLua;
4348
}
4449

50+
private void Menu_ReRegisterExecutionModes()
51+
{
52+
CodeExecutorManager.ReRegisterExecModes();
53+
}
54+
4555
private void Menu_Document()
4656
{
4757
Application.OpenURL("https://github.com/ichenpipi/unity-code-executor#readme");
4858
}
4959

5060
private void Menu_Reload()
5161
{
52-
// 重新加载数据和设置
5362
Reload();
54-
// 刷新注册模式
55-
CodeExecutorManager.ReRegisterExecModes();
5663
}
5764

5865
private void Menu_ShowSerializedDataFile()
@@ -83,9 +90,18 @@ private void Menu_ShowSerializedSettingsFile()
8390
EditorUtility.RevealInFinder(CodeExecutorSettings.SerializedFilePath);
8491
}
8592

86-
/// <summary>
87-
/// 导入
88-
/// </summary>
93+
private void Menu_CodeEditorFontSizeUp()
94+
{
95+
int newSize = GetCodeEditorFontSize() + 1;
96+
SetCodeEditorFontSize(newSize, true);
97+
}
98+
99+
private void Menu_CodeEditorFontSizeDown()
100+
{
101+
int newSize = GetCodeEditorFontSize() - 1;
102+
SetCodeEditorFontSize(newSize, true);
103+
}
104+
89105
private void Menu_ImportFromFile()
90106
{
91107
const string title = "Import code from file";

Editor/Scripts/Window/Content/CodeExecutorWindowDetail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void InitDetail()
8787
/// </summary>
8888
private void OnExecuteButtonClick()
8989
{
90-
ExecuteSnippet(m_CurrSnippetInfo.name, m_CurrSnippetInfo.code, m_CurrSnippetInfo.mode);
90+
ExecuteSnippet(m_CurrSnippet.name, m_CurrSnippet.code, m_CurrSnippet.mode);
9191
}
9292

9393
/// <summary>

Editor/Scripts/Window/Content/Detail/CodeExecutorWindowCodeEditor.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,18 @@ private void UpdateCodeTextFieldHeight()
184184
/// <param name="evt"></param>
185185
private void OnCodeTextFieldValueChanged(ChangeEvent<string> evt)
186186
{
187-
if (m_CurrSnippetInfo == null) return;
187+
if (m_CurrSnippet == null) return;
188188
// 更新数据
189189
string code = m_CodeTextField.value;
190-
m_CurrSnippetInfo.code = code;
190+
m_CurrSnippet.code = code;
191191
// 序列化数据
192-
if (IsNewSnippet(m_CurrSnippetInfo))
192+
if (IsNewSnippet(m_CurrSnippet))
193193
{
194194
CodeExecutorManager.SetNewSnippetCode(code);
195195
}
196196
else
197197
{
198-
CodeExecutorManager.SetSnippetCode(m_CurrSnippetInfo.guid, code);
198+
CodeExecutorManager.SetSnippetCode(m_CurrSnippet.guid, code);
199199
}
200200
}
201201

@@ -259,11 +259,9 @@ private void OnCodeTextFieldMouseWheel(WheelEvent evt)
259259
{
260260
// 改变字体大小
261261
const int step = 1;
262-
float oldSize = m_CodeTextField.style.fontSize.value.value;
262+
float oldSize = GetCodeEditorFontSize();
263263
int newSize = (int)(evt.delta.y < 0 ? oldSize + step : oldSize - step);
264-
newSize = SetCodeEditorFontSize(newSize);
265-
CodeExecutorSettings.fontSize = newSize;
266-
ShowNotification($"Font size: {newSize}");
264+
SetCodeEditorFontSize(newSize, true);
267265
// 阻止事件的默认行为,停止事件传播
268266
evt.PreventDefault();
269267
evt.StopImmediatePropagation();
@@ -275,7 +273,7 @@ private void OnCodeTextFieldMouseWheel(WheelEvent evt)
275273
/// </summary>
276274
private void OnCodeEditorClipboardButtonClick()
277275
{
278-
PipiUtility.SaveToClipboard(m_CurrSnippetInfo.code);
276+
PipiUtility.SaveToClipboard(m_CurrSnippet.code);
279277
ShowNotification("Copied to clipboard", 1f);
280278
}
281279

@@ -313,11 +311,19 @@ private void SetCodeEditorText(string text, bool notify = false)
313311
}
314312
}
315313

316-
private int SetCodeEditorFontSize(int size)
314+
private int GetCodeEditorFontSize()
315+
{
316+
if (m_CodeTextField == null) return 0;
317+
return (int)m_CodeTextField.style.fontSize.value.value;
318+
}
319+
320+
private void SetCodeEditorFontSize(int size, bool showNotification)
317321
{
318322
size = Mathf.Clamp(size, 8, 40);
319323
m_CodeTextField.style.fontSize = size;
320-
return size;
324+
CodeExecutorSettings.fontSize = size;
325+
// 提示
326+
if (showNotification) ShowNotification($"Font size: {size}pt");
321327
}
322328

323329
private void SetCodeEditorEditable(bool isEditable, bool focus = false)

Editor/Scripts/Window/Content/Detail/CodeExecutorWindowHeader.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ private void OnHeaderGeometryChangedEventChanged(GeometryChangedEvent evt)
189189
private void OnSaveButtonClick()
190190
{
191191
// 添加新的代码段
192-
string code = m_CurrSnippetInfo.code;
193-
string mode = m_CurrSnippetInfo.mode;
192+
string code = m_CurrSnippet.code;
193+
string mode = m_CurrSnippet.mode;
194194
SaveAsNewSnippet(code, "Unnamed", mode);
195195
}
196196

@@ -199,15 +199,21 @@ private void OnSaveButtonClick()
199199
/// </summary>
200200
private void OnDuplicateButtonClick()
201201
{
202-
DuplicateSnippet(m_CurrSnippetInfo);
202+
DuplicateSnippet(m_CurrSnippet);
203203
}
204204

205205
/// <summary>
206206
/// 编辑按钮点击回调
207207
/// </summary>
208208
private void OnEditButtonClick()
209209
{
210+
// 切换为编辑状态
210211
SetCodeEditorEditable(true, true);
212+
// 列表选中代码段
213+
if (m_CurrSnippet != null)
214+
{
215+
SetSnippetTreeViewSelection(m_CurrSnippet.guid, false);
216+
}
211217
}
212218

213219
/// <summary>

0 commit comments

Comments
 (0)