From e3fb5685bfce8936fbea246cdcbca7fa53880bca Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sat, 15 Jun 2024 13:21:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`Editor`=E4=B8=8B`Data`?= =?UTF-8?q?=E7=9A=84=E6=B7=BB=E5=8A=A0=E3=80=81=E5=88=A0=E9=99=A4=E3=80=81?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E7=A7=BB=E5=8A=A8=E6=93=8D=E4=BD=9C=E6=8C=89?= =?UTF-8?q?=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation~/zh/changelog.md | 1 + Editor/Series/SerieEditor.cs | 30 +++++++--- Editor/Utilities/ChartEditorHelper.cs | 84 ++++++++++++++------------- 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index c53f7981..acc046fa 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -73,6 +73,7 @@ slug: /changelog ## master +* (2024.06.15) 增加`Editor`下`Data`的添加、删除、上下移动操作按钮 * (2024.06.11) 修复`Axis`的`IndicatorLabel`可能会遮挡住`Tooltip`的问题 * (2024.06.11) 修复`Tooltip`在`Cross`时`Axis`的`IndicatorLabel`可能不显示的问题 (#315) * (2024.06.10) 调整`Tooltip`的`Corss`重命名为`Cross` diff --git a/Editor/Series/SerieEditor.cs b/Editor/Series/SerieEditor.cs index 5fa47e9a..3eee17fe 100644 --- a/Editor/Series/SerieEditor.cs +++ b/Editor/Series/SerieEditor.cs @@ -162,7 +162,7 @@ namespace XCharts.Editor } } - private void DrawSerieDataHeader(Rect drawRect,HeaderCallbackContext context) + private void DrawSerieDataHeader(Rect drawRect, HeaderCallbackContext context) { var serieData = context.serieData; var fieldCount = context.fieldCount; @@ -180,12 +180,18 @@ namespace XCharts.Editor var sereName = serieData.FindPropertyRelative("m_Name"); var data = serieData.FindPropertyRelative("m_Data"); #if UNITY_2019_3_OR_NEWER - var gap = 2; - var namegap = 3; + var gap = 2; + var namegap = 3; + var buttomLength = 30; #else var gap = 0; var namegap = 0; + var buttomLength = 30; #endif + if (showName) + { + buttomLength += 12; + } if (fieldCount <= 1) { while (2 > data.arraySize) @@ -197,13 +203,13 @@ namespace XCharts.Editor SerializedProperty element = data.GetArrayElementAtIndex(1); var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * 15 + gap; drawRect.x = startX; - drawRect.xMax = maxX; + drawRect.xMax = maxX - buttomLength; EditorGUI.PropertyField(drawRect, element, GUIContent.none); } else { var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * 15 + gap; - var dataWidTotal = (currentWidth - (startX + 20.5f + 1)); + var dataWidTotal = currentWidth - (startX + 20.5f + 1) - buttomLength; var dataWid = dataWidTotal / fieldCount; var xWid = dataWid - 0; for (int i = 0; i < dimension; i++) @@ -226,6 +232,9 @@ namespace XCharts.Editor drawRect.width = dataWid + 40 + dimension * namegap - 2.5f; EditorGUI.PropertyField(drawRect, sereName, GUIContent.none); } + drawRect.x = lastX; + drawRect.width = lastWid; + ChartEditorHelper.UpDownAddDeleteButton(drawRect, context.listProp, index); EditorGUIUtility.fieldWidth = lastFieldWid; EditorGUIUtility.labelWidth = lastLabelWid; } @@ -242,12 +251,14 @@ namespace XCharts.Editor var fieldCount = dimension + (showName ? 1 : 0); var serieData = m_Datas.GetArrayElementAtIndex(index); var dataIndex = serieData.FindPropertyRelative("m_Index").intValue; - var callbackContext = new HeaderCallbackContext(){ + var callbackContext = new HeaderCallbackContext() + { serieData = serieData, fieldCount = fieldCount, showName = showName, index = index, - dimension = dimension + dimension = dimension, + listProp = m_Datas }; m_DataElementFoldout[index] = ChartEditorHelper.DrawSerieDataHeader("SerieData " + dataIndex, flag, false, null, callbackContext, DrawSerieDataHeader); if (m_DataElementFoldout[index]) @@ -327,7 +338,10 @@ namespace XCharts.Editor var sourceIndex = dataLink.FindPropertyRelative("m_Source"); var targetIndex = dataLink.FindPropertyRelative("m_Target"); var value = dataLink.FindPropertyRelative("m_Value"); - ChartEditorHelper.MakeThreeField(ref drawRect, drawRect.width, sourceIndex, targetIndex, value, ""); + var hig = ChartEditorHelper.MakeThreeField(ref drawRect, drawRect.width, sourceIndex, targetIndex, value, ""); + var btnRect = drawRect; + btnRect.y -= hig; + ChartEditorHelper.UpDownAddDeleteButton(btnRect, m_Datas, index); }); if (m_LinksElementFoldout[index]) { diff --git a/Editor/Utilities/ChartEditorHelper.cs b/Editor/Utilities/ChartEditorHelper.cs index 4737241f..afe48c66 100644 --- a/Editor/Utilities/ChartEditorHelper.cs +++ b/Editor/Utilities/ChartEditorHelper.cs @@ -13,6 +13,7 @@ namespace XCharts.Editor public bool showName; public int index; public int dimension; + public SerializedProperty listProp; } public class HeaderMenuInfo @@ -52,6 +53,8 @@ namespace XCharts.Editor public const float GAP_WIDTH = 0; public const float DIFF_WIDTH = 1; #endif + public const float ICON_WIDHT = 10; + public const float ICON_GAP = 0; static Dictionary s_GUIContentCache; static ChartEditorHelper() @@ -115,21 +118,23 @@ namespace XCharts.Editor drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; } - public static void MakeThreeField(ref Rect drawRect, float rectWidth, SerializedProperty prop1, - SerializedProperty prop2, SerializedProperty prop3, string name) + public static float MakeThreeField(ref Rect drawRect, float rectWidth, SerializedProperty prop1, + SerializedProperty prop2, SerializedProperty prop3, string name, bool btnSpacing = true) { EditorGUI.LabelField(drawRect, name); var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + GAP_WIDTH; - var diff = 13 + EditorGUI.indentLevel * 14; + var diff = 13f + EditorGUI.indentLevel * 14; var offset = diff - INDENT_WIDTH; - var tempWidth = (rectWidth - startX + diff) / 3; + var tempWidth = (rectWidth - startX + diff - (btnSpacing ? (ICON_WIDHT + ICON_GAP) * 4 : 0)) / 3 + 8.5f; var centerXRect = new Rect(startX, drawRect.y, tempWidth, drawRect.height - 1); var centerYRect = new Rect(centerXRect.x + tempWidth - offset, drawRect.y, tempWidth - 1, drawRect.height - 1); var centerZRect = new Rect(centerYRect.x + tempWidth - offset, drawRect.y, tempWidth - 1, drawRect.height - 1); EditorGUI.PropertyField(centerXRect, prop1, GUIContent.none); EditorGUI.PropertyField(centerYRect, prop2, GUIContent.none); EditorGUI.PropertyField(centerZRect, prop3, GUIContent.none); - drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + var hig = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + drawRect.y += hig; + return hig; } public static void MakeVector2(ref Rect drawRect, float rectWidth, SerializedProperty prop, string name) @@ -344,15 +349,12 @@ namespace XCharts.Editor { EditorGUI.indentLevel++; var listSize = listProp.arraySize; - var iconWidth = 10; - var iconGap = 0f; - if (showSize) { var headerHeight = DrawSplitterAndBackground(drawRect); if (showOrder) { - var elementRect = new Rect(drawRect.x, drawRect.y, drawRect.width - iconWidth + 2, drawRect.height); + var elementRect = new Rect(drawRect.x, drawRect.y, drawRect.width - ICON_WIDHT + 2, drawRect.height); var oldColor = GUI.contentColor; GUI.contentColor = Color.black; GUI.contentColor = oldColor; @@ -405,40 +407,14 @@ namespace XCharts.Editor DrawSplitterAndBackground(drawRect); if (showOrder) { - var temp = INDENT_WIDTH + GAP_WIDTH + iconGap; var isSerie = "Serie".Equals(element.type); var elementRect = isSerie ? - new Rect(drawRect.x, drawRect.y, drawRect.width + INDENT_WIDTH - 2 * iconGap, drawRect.height) : - new Rect(drawRect.x, drawRect.y, drawRect.width - 4 * iconWidth, drawRect.height); + new Rect(drawRect.x, drawRect.y, drawRect.width + INDENT_WIDTH - 2 * ICON_GAP, drawRect.height) : + new Rect(drawRect.x, drawRect.y, drawRect.width - 4 * ICON_WIDHT, drawRect.height); EditorGUI.PropertyField(elementRect, element, new GUIContent("Element " + i)); - var iconRect = new Rect(drawRect.width - 4 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height); - var oldColor = GUI.contentColor; - GUI.contentColor = Color.black; - if (GUI.Button(iconRect, EditorCustomStyles.iconUp, EditorCustomStyles.invisibleButton)) - { - if (i > 0) listProp.MoveArrayElement(i, i - 1); - } - iconRect = new Rect(drawRect.width - 3 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height); - if (GUI.Button(iconRect, EditorCustomStyles.iconDown, EditorCustomStyles.invisibleButton)) - { - if (i < listProp.arraySize - 1) listProp.MoveArrayElement(i, i + 1); - } - iconRect = new Rect(drawRect.width - 2 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height); - if (GUI.Button(iconRect, EditorCustomStyles.iconAdd, EditorCustomStyles.invisibleButton)) - { - if (i < listProp.arraySize && i >= 0) listProp.InsertArrayElementAtIndex(i); - } - iconRect = new Rect(drawRect.width - iconWidth + temp, drawRect.y, iconWidth, drawRect.height); - if (GUI.Button(iconRect, EditorCustomStyles.iconRemove, EditorCustomStyles.invisibleButton)) - { - if (i < listProp.arraySize && i >= 0) listProp.DeleteArrayElementAtIndex(i); - } - else - { - drawRect.y += EditorGUI.GetPropertyHeight(element); - height += EditorGUI.GetPropertyHeight(element); - } - GUI.contentColor = oldColor; + UpDownAddDeleteButton(drawRect, listProp, i); + drawRect.y += EditorGUI.GetPropertyHeight(element); + height += EditorGUI.GetPropertyHeight(element); } else { @@ -451,6 +427,34 @@ namespace XCharts.Editor EditorGUI.indentLevel--; } + public static void UpDownAddDeleteButton(Rect drawRect, SerializedProperty listProp, int i) + { + var temp = INDENT_WIDTH + GAP_WIDTH + ICON_GAP; + var iconRect = new Rect(drawRect.width - 4 * ICON_WIDHT + temp, drawRect.y, ICON_WIDHT, drawRect.height); + var oldColor = GUI.contentColor; + GUI.contentColor = Color.black; + if (GUI.Button(iconRect, EditorCustomStyles.iconUp, EditorCustomStyles.invisibleButton)) + { + if (i > 0) listProp.MoveArrayElement(i, i - 1); + } + iconRect = new Rect(drawRect.width - 3 * ICON_WIDHT + temp, drawRect.y, ICON_WIDHT, drawRect.height); + if (GUI.Button(iconRect, EditorCustomStyles.iconDown, EditorCustomStyles.invisibleButton)) + { + if (i < listProp.arraySize - 1) listProp.MoveArrayElement(i, i + 1); + } + iconRect = new Rect(drawRect.width - 2 * ICON_WIDHT + temp, drawRect.y, ICON_WIDHT, drawRect.height); + if (GUI.Button(iconRect, EditorCustomStyles.iconAdd, EditorCustomStyles.invisibleButton)) + { + if (i < listProp.arraySize && i >= 0) listProp.InsertArrayElementAtIndex(i); + } + iconRect = new Rect(drawRect.width - ICON_WIDHT + temp, drawRect.y, ICON_WIDHT, drawRect.height); + if (GUI.Button(iconRect, EditorCustomStyles.iconRemove, EditorCustomStyles.invisibleButton)) + { + if (i < listProp.arraySize && i >= 0) listProp.DeleteArrayElementAtIndex(i); + } + GUI.contentColor = oldColor; + } + public static bool PropertyField(ref Rect drawRect, Dictionary heights, string key, SerializedProperty prop) {