Skip to content

Commit

Permalink
fix: fix using missing and refactor
Browse files Browse the repository at this point in the history
1.启动界面修改为GetWindows 只需要一个
2.修改因为代码修改编辑器重载 导致m_encoding 为null的问题
3.增加Ctrl+S 保存数据表功能 避免每次修改都需要关闭界面。
4.添加缺失命名空间
5.修改创建编辑器窗口错误
6.修改数据表每行显示10条数据 可用滑动条拖动 防止列数太多导致无法看清
  • Loading branch information
WhiteCaster authored and Shaun-Fong committed Feb 9, 2023
1 parent f1fc9a9 commit 5d9f14c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
101 changes: 88 additions & 13 deletions Editor/DataTableEditor/DataTableEditingWindow.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using static DataTableEditor.Utility;

namespace DataTableEditor
{

public class DataTableEditingWindowInstance
{
public DataTableEditingWindow Instance { get; private set; }
Expand All @@ -30,7 +31,6 @@ public void SetData(string path, Encoding encoding)

public class DataTableEditingWindow : EditorWindow
{

public List<DataTableRowData> RowDatas { get; private set; }

private List<DataTableRowData> RowDatasTemp;
Expand All @@ -48,10 +48,11 @@ public class DataTableEditingWindow : EditorWindow
private Vector2 m_scrollViewPos;

private Encoding m_encoding;

private int m_codePage;
public void OpenWindow(string path, Encoding encoding)
{
m_encoding = encoding;
m_codePage = encoding.CodePage;
FilePath = path;
RowDatas = DataTableUtility.LoadDataTableFile(FilePath, m_encoding);

Expand Down Expand Up @@ -81,7 +82,6 @@ public void OpenWindow(string path, Encoding encoding)
private void OnGUI()
{
m_scrollViewPos = GUILayout.BeginScrollView(m_scrollViewPos);

if (RowDatas == null || RowDatas.Count == 0)
{
Close();
Expand All @@ -108,7 +108,7 @@ private void OnGUI()
true);

#else
reorderableList =
reorderableList =
new ReorderableList(RowDatas, typeof(List<DataTableRowData>), true, false, true, true);

#endif
Expand All @@ -117,12 +117,20 @@ private void OnGUI()
{
for (int i = 0; i < RowDatas[index].Data.Count; i++)
{
rect.width =
(this.position.width - 20) /
RowDatas[index].Data.Count;
if (RowDatas[index].Data.Count > 10)
{
rect.width =
(this.position.width - 20) /
10;
}
else
{
rect.width =
(this.position.width - 20) /
RowDatas[index].Data.Count;
}

rect.x = rect.width * i + 20;

RowDatas[index].Data[i] =
EditorGUI.TextField(rect, "", RowDatas[index].Data[i],
this.Theme);
Expand All @@ -140,7 +148,7 @@ private void OnGUI()
{
RowDatas.Add(new DataTableRowData()
{
Data = new List<string>() { "", "", "", "" }
Data = new List<string>() {"", "", "", ""}
});
}
else
Expand All @@ -160,6 +168,7 @@ private void OnGUI()
for (int i = 0; i < RowDatas.Count; i++)
RowDatas[i].Data.Add("");
}
Focus();
};

reorderableList.onRemoveCallback = list =>
Expand All @@ -178,6 +187,7 @@ private void OnGUI()
RowDatas[i].Data.RemoveAt(RowDatas[i].Data.Count - 1);
}
}
Focus();
};

reorderableList.drawHeaderCallback = (Rect rect) =>
Expand All @@ -200,7 +210,72 @@ private void OnGUI()

reorderableList.DoLayoutList();

if (RowDatas != null && RowDatas.Count > 0)
{
if (RowDatas[0].Data.Count > 10)
{
float listItemWidth = 0f;
float listX = 0f;
listItemWidth = (position.width - 20) / 10;
listX = listItemWidth * (RowDatas[0].Data.Count - 1) + 20;
GUILayout.Label("", new GUIStyle() {fixedWidth = listX});
}
}

GUILayout.EndScrollView();
if (IsCombinationKey(EventModifiers.Control, KeyCode.S, EventType.KeyDown))
{
SaveDataTable();
}
}

private void SaveDataTable()
{
if (!CheckDirty())
return;

RowDatasTemp = new List<DataTableRowData>();
for (int i = 0; i < RowDatas.Count; i++)
{
DataTableRowData data = new DataTableRowData();

for (int j = 0; j < RowDatas[i].Data.Count; j++)
{
data.Data.Add(RowDatas[i].Data[j]);
}

RowDatasTemp.Add(data);
}

if (m_encoding == null)
{
m_encoding = Encoding.GetEncoding(m_codePage);
}

DataTableUtility.SaveDataTableFile(FilePath, RowDatas, m_encoding);
}

private bool IsCombinationKey(EventModifiers preKey, KeyCode postKey, EventType postKeyEvent)
{
if (preKey != EventModifiers.None)
{
bool eventDown = (Event.current.modifiers & preKey) != 0;
if (eventDown && Event.current.rawType == postKeyEvent && Event.current.keyCode == postKey)
{
Event.current.Use();
return true;
}
}
else
{
if (Event.current.rawType == postKeyEvent && Event.current.keyCode == postKey)
{
Event.current.Use();
return true;
}
}

return false;
}

private void OnDisable()
Expand All @@ -209,11 +284,11 @@ private void OnDisable()
return;

bool result = EditorUtility.DisplayDialog("提示", "你已经对表格进行了修改,是否需要保存?", "是", "否");

if (result)
{
DataTableUtility.SaveDataTableFile(FilePath, RowDatas, m_encoding);
SaveDataTable();
}
Focus();
}

/// <summary>
Expand Down Expand Up @@ -268,4 +343,4 @@ private bool CheckDirty()
return false;
}
}
}
}
16 changes: 2 additions & 14 deletions Editor/DataTableEditor/LauncherEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace DataTableEditor
public class LauncherEditorWindow : EditorWindow
{
public static float ButtonHeight = 50;
public static LauncherEditorWindow Instance;
private Encoding m_CurrentEncoding;
private bool m_IsValidEncode;

Expand All @@ -23,19 +22,8 @@ public class LauncherEditorWindow : EditorWindow
[MenuItem("表格编辑器/打开 &1", priority = 2)]
public static void OpenWindow()
{
if (Instance != null)
{
Instance.Close();
return;
}

#if UNITY_2019_1_OR_NEWER
Instance = LauncherEditorWindow.CreateWindow<LauncherEditorWindow>("数据表编辑器");
#else
Instance = EditorWindowUtility.CreateWindow<DataTableEditor>("数据表编辑器");
#endif

Instance.Show();
LauncherEditorWindow window = GetWindow<LauncherEditorWindow>();
window.Show();
}

private void OnEnable()
Expand Down

0 comments on commit 5d9f14c

Please sign in to comment.