From cd5a1ca10b96c2a6753263e42020f0c16d17241f Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sun, 30 Jun 2019 18:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SplitArea=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=9D=90=E6=A0=87=E8=BD=B4=E5=88=86=E5=89=B2=E5=8C=BA=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scripts/Editor/PropertyDrawers/AxisDrawer.cs | 8 ++- .../PropertyDrawers/AxisSplitAreaDrawer.cs | 54 +++++++++++++++++++ .../AxisSplitAreaDrawer.cs.meta | 11 ++++ Scripts/UI/Internal/Axis.cs | 45 ++++++++++++++++ Scripts/UI/Internal/CoordinateChart.cs | 33 +++++++++--- 5 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs create mode 100644 Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs.meta diff --git a/Scripts/Editor/PropertyDrawers/AxisDrawer.cs b/Scripts/Editor/PropertyDrawers/AxisDrawer.cs index b67ae208..838fbb8a 100644 --- a/Scripts/Editor/PropertyDrawers/AxisDrawer.cs +++ b/Scripts/Editor/PropertyDrawers/AxisDrawer.cs @@ -48,6 +48,7 @@ namespace XCharts SerializedProperty m_Data = prop.FindPropertyRelative("m_Data"); SerializedProperty m_AxisName = prop.FindPropertyRelative("m_AxisName"); SerializedProperty m_AxisTick = prop.FindPropertyRelative("m_AxisTick"); + SerializedProperty m_SplitArea = prop.FindPropertyRelative("m_SplitArea"); SerializedProperty m_MinMaxType = prop.FindPropertyRelative("m_MinMaxType"); SerializedProperty m_Min = prop.FindPropertyRelative("m_Min"); SerializedProperty m_Max = prop.FindPropertyRelative("m_Max"); @@ -110,6 +111,9 @@ namespace XCharts EditorGUI.PropertyField(drawRect, m_AxisTick); drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUI.GetPropertyHeight(m_AxisTick); + EditorGUI.PropertyField(drawRect, m_SplitArea); + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + drawRect.y += EditorGUI.GetPropertyHeight(m_SplitArea); if (type == Axis.AxisType.Category) { @@ -137,8 +141,9 @@ namespace XCharts SerializedProperty m_Type = prop.FindPropertyRelative("m_Type"); SerializedProperty m_AxisTick = prop.FindPropertyRelative("m_AxisTick"); SerializedProperty m_AxisName = prop.FindPropertyRelative("m_AxisName"); + SerializedProperty m_SplitArea = prop.FindPropertyRelative("m_SplitArea"); float height = 0; - height += 8 * EditorGUIUtility.singleLineHeight + 7 * EditorGUIUtility.standardVerticalSpacing; + height += 9 * EditorGUIUtility.singleLineHeight + 8 * EditorGUIUtility.standardVerticalSpacing; Axis.AxisType type = (Axis.AxisType)m_Type.enumValueIndex; if (type == Axis.AxisType.Category) { @@ -170,6 +175,7 @@ namespace XCharts } height += EditorGUI.GetPropertyHeight(m_AxisName); height += EditorGUI.GetPropertyHeight(m_AxisTick); + height += EditorGUI.GetPropertyHeight(m_SplitArea); return height; } diff --git a/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs b/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs new file mode 100644 index 00000000..8fdfcfa6 --- /dev/null +++ b/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs @@ -0,0 +1,54 @@ +using UnityEditor; +using UnityEngine; + +namespace XCharts +{ + [CustomPropertyDrawer(typeof(Axis.SplitArea), true)] + public class AxisSplitAreaDrawer : PropertyDrawer + { + private bool m_ColorFoldout = false; + private int m_ColorSize = 0; + private bool m_SplitAreaToggle = false; + + + public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) + { + Rect drawRect = pos; + drawRect.height = EditorGUIUtility.singleLineHeight; + SerializedProperty show = prop.FindPropertyRelative("m_Show"); + SerializedProperty m_Color = prop.FindPropertyRelative("m_Color"); + + ChartEditorHelper.MakeFoldout(ref drawRect, ref m_SplitAreaToggle, "Split Area", show, false); + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + if (m_SplitAreaToggle) + { + ++EditorGUI.indentLevel; + m_ColorFoldout = EditorGUI.Foldout(drawRect, m_ColorFoldout, "Color"); + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + drawRect.width = pos.width; + if (m_ColorFoldout) + { + ChartEditorHelper.MakeList(ref drawRect, ref m_ColorSize, m_Color); + } + --EditorGUI.indentLevel; + } + } + + public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) + { + float height = 0; + if (m_SplitAreaToggle) + { + height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + if (m_ColorFoldout) + { + SerializedProperty m_Data = prop.FindPropertyRelative("m_Color"); + int num = m_Data.arraySize + 1; + height += num * EditorGUIUtility.singleLineHeight + (num - 1) * EditorGUIUtility.standardVerticalSpacing; + height += EditorGUIUtility.standardVerticalSpacing; + } + } + return height; + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs.meta b/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs.meta new file mode 100644 index 00000000..1490325c --- /dev/null +++ b/Scripts/Editor/PropertyDrawers/AxisSplitAreaDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 988570bd6485942da9879649ed4adb5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/UI/Internal/Axis.cs b/Scripts/UI/Internal/Axis.cs index c72f371a..d9b69342 100644 --- a/Scripts/UI/Internal/Axis.cs +++ b/Scripts/UI/Internal/Axis.cs @@ -135,6 +135,49 @@ namespace XCharts } } + /// + /// Split area of axis in grid area, not shown by default. + /// + [Serializable] + public class SplitArea + { + [SerializeField] private bool m_Show; + [SerializeField] private List m_Color; + + /// + /// Set this to true to show the splitArea. + /// + /// false + public bool show { get { return m_Show; } set { m_Show = value; } } + /// + /// Color of split area. SplitArea color could also be set in color array, + /// which the split lines would take as their colors in turns. + /// Dark and light colors in turns are used by default. + /// + /// ['rgba(250,250,250,0.3)','rgba(200,200,200,0.3)'] + public List color { get { return m_Color; } set { m_Color = value; } } + + public static SplitArea defaultSplitArea + { + get + { + return new SplitArea() + { + m_Show = false, + m_Color = new List(){ + new Color32(250,250,250,77), + new Color32(200,200,200,77) + } + }; + } + } + + public Color getColor(int index){ + var i = index % color.Count; + return color[i]; + } + } + [SerializeField] protected bool m_Show = true; [SerializeField] protected AxisType m_Type; [SerializeField] protected AxisMinMaxType m_MinMaxType; @@ -148,6 +191,7 @@ namespace XCharts [SerializeField] protected List m_Data = new List(); [SerializeField] protected AxisName m_AxisName = AxisName.defaultAxisName; [SerializeField] protected AxisTick m_AxisTick = AxisTick.defaultTick; + [SerializeField] protected SplitArea m_SplitArea = SplitArea.defaultSplitArea; public bool show { get { return m_Show; } set { m_Show = value; } } public AxisType type { get { return m_Type; } set { m_Type = value; } } @@ -163,6 +207,7 @@ namespace XCharts public AxisName axisName { get { return m_AxisName; } set { m_AxisName = value; } } public AxisTick axisTick { get { return m_AxisTick; } set { m_AxisTick = value; } } + public SplitArea splitArea { get { return m_SplitArea; } set { m_SplitArea = value; } } public int filterStart { get; set; } public int filterEnd { get; set; } diff --git a/Scripts/UI/Internal/CoordinateChart.cs b/Scripts/UI/Internal/CoordinateChart.cs index 19c9d91d..2b643d00 100644 --- a/Scripts/UI/Internal/CoordinateChart.cs +++ b/Scripts/UI/Internal/CoordinateChart.cs @@ -547,15 +547,24 @@ namespace XCharts #region draw tick and splitline if (m_YAxis.show) { - for (int i = 0; i < m_YAxis.GetScaleNumber(m_DataZoom); i++) + var scaleWidth = m_YAxis.GetScaleWidth(coordinateHig, m_DataZoom); + var size = m_YAxis.GetScaleNumber(m_DataZoom); + for (int i = 0; i < size; i++) { float pX = 0; - float pY = coordinateY + i * m_YAxis.GetScaleWidth(coordinateHig, m_DataZoom); + float pY = coordinateY + i * scaleWidth; if (m_YAxis.boundaryGap && m_YAxis.axisTick.alignWithLabel) { - pY -= m_YAxis.GetScaleWidth(coordinateHig, m_DataZoom) / 2; + pY -= scaleWidth / 2; + } + if (m_YAxis.splitArea.show && i < size - 1) + { + ChartHelper.DrawPolygon(vh, new Vector2(coordinateX, pY), + new Vector2(coordinateX + coordinateWid, pY), + new Vector2(coordinateX + coordinateWid, pY + scaleWidth), + new Vector2(coordinateX, pY + scaleWidth), + m_YAxis.splitArea.getColor(i)); } - if (m_YAxis.axisTick.show) { pX += zeroX - m_YAxis.axisTick.length - 2; @@ -571,13 +580,23 @@ namespace XCharts } if (m_XAxis.show) { - for (int i = 0; i < m_XAxis.GetScaleNumber(m_DataZoom); i++) + var scaleWidth = m_XAxis.GetScaleWidth(coordinateWid, m_DataZoom); + var size = m_XAxis.GetScaleNumber(m_DataZoom); + for (int i = 0; i < size; i++) { - float pX = coordinateX + i * m_XAxis.GetScaleWidth(coordinateWid, m_DataZoom); + float pX = coordinateX + i * scaleWidth; float pY = 0; if (m_XAxis.boundaryGap && m_XAxis.axisTick.alignWithLabel) { - pX -= m_XAxis.GetScaleWidth(coordinateWid, m_DataZoom) / 2; + pX -= scaleWidth / 2; + } + if (m_XAxis.splitArea.show && i < size - 1) + { + ChartHelper.DrawPolygon(vh, new Vector2(pX, coordinateY), + new Vector2(pX, coordinateY + coordinateHig), + new Vector2(pX + scaleWidth, coordinateY + coordinateHig), + new Vector2(pX + scaleWidth, coordinateY), + m_XAxis.splitArea.getColor(i)); } if (m_XAxis.axisTick.show) {