diff --git a/Editor/ChildComponents/BackgroundDrawer.cs b/Editor/ChildComponents/BackgroundDrawer.cs new file mode 100644 index 00000000..726249a2 --- /dev/null +++ b/Editor/ChildComponents/BackgroundDrawer.cs @@ -0,0 +1,28 @@ +using UnityEditor; +using UnityEngine; +using XCharts.Runtime; + +namespace XCharts.Editor +{ + [CustomPropertyDrawer(typeof(Background), true)] + public class BackgroundDrawer : BasePropertyDrawer + { + public override string ClassName { get { return "Background"; } } + public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) + { + base.OnGUI(pos, prop, label); + if (MakeComponentFoldout(prop, "m_Show", true)) + { + ++EditorGUI.indentLevel; + PropertyField(prop, "m_Image"); + PropertyField(prop, "m_ImageType"); + PropertyField(prop, "m_ImageColor"); + PropertyField(prop, "m_ImageWidth"); + PropertyField(prop, "m_ImageHeight"); + PropertyField(prop, "m_AutoColor"); + PropertyField(prop, "m_BorderStyle"); + --EditorGUI.indentLevel; + } + } + } +} \ No newline at end of file diff --git a/Runtime/Widgets/SVGImage.cs.meta b/Editor/ChildComponents/BackgroundDrawer.cs.meta similarity index 83% rename from Runtime/Widgets/SVGImage.cs.meta rename to Editor/ChildComponents/BackgroundDrawer.cs.meta index cf55b08e..110fd622 100644 --- a/Runtime/Widgets/SVGImage.cs.meta +++ b/Editor/ChildComponents/BackgroundDrawer.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: fe10ee35a038646b6aedfffb30f84024 +guid: 88c83fad35bc544cab4106096d171189 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Editor/MainComponents/BaseGraphEditor.cs b/Editor/MainComponents/BaseGraphEditor.cs new file mode 100644 index 00000000..ef129ed8 --- /dev/null +++ b/Editor/MainComponents/BaseGraphEditor.cs @@ -0,0 +1,112 @@ +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; +using UnityEngine.Assertions; +using XCharts.Runtime; + +namespace XCharts.Editor +{ + public class BaseGraphEditor : UnityEditor.Editor + { + class Styles + { + public static readonly GUIContent btnAddComponent = new GUIContent("Add Main Component", ""); + public static readonly GUIContent btnRebuildChartObject = new GUIContent("Rebuild Object", ""); + public static readonly GUIContent btnSaveAsImage = new GUIContent("Save As Image", ""); + public static readonly GUIContent btnCheckWarning = new GUIContent("Check Warning", ""); + public static readonly GUIContent btnHideWarning = new GUIContent("Hide Warning", ""); + } + public BaseGraph m_BaseGraph; + + public static T AddUIComponent(string chartName) where T : BaseGraph + { + return XChartsEditor.AddGraph(chartName); + } + + protected Dictionary m_Properties = new Dictionary(); + + protected virtual void OnEnable() + { + m_Properties.Clear(); + m_BaseGraph = (BaseGraph)target; + } + + public override void OnInspectorGUI() + { + serializedObject.Update(); + PropertyField("m_Script"); + + OnStartInspectorGUI(); + OnDebugInspectorGUI(); + serializedObject.ApplyModifiedProperties(); + } + + protected virtual void OnStartInspectorGUI() { } + + protected virtual void OnDebugInspectorGUI() + { + EditorGUILayout.Space(); + OnDebugStartInspectorGUI(); + OnDebugEndInspectorGUI(); + } + + protected virtual void OnDebugStartInspectorGUI() { } + protected virtual void OnDebugEndInspectorGUI() { } + + protected void PropertyField(string name) + { + if (!m_Properties.ContainsKey(name)) + { + var prop = serializedObject.FindProperty(name); + if (prop == null) + { + Debug.LogError("Property " + name + " not found!"); + return; + } + m_Properties.Add(name, prop); + } + EditorGUILayout.PropertyField(m_Properties[name]); + } + + protected void PropertyField(SerializedProperty property) + { + Assert.IsNotNull(property); + var title = ChartEditorHelper.GetContent(property.displayName); + PropertyField(property, title); + } + + protected void PropertyField(SerializedProperty property, GUIContent title) + { + EditorGUILayout.PropertyField(property, title); + } + + protected void PropertyListField(string relativePropName, bool showOrder = true, params HeaderMenuInfo[] menus) + { + var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f); + var height = 0f; + var prop = FindProperty(relativePropName); + prop.isExpanded = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height, + prop, prop.isExpanded, showOrder, true, menus); + if (prop.isExpanded) + { + GUILayoutUtility.GetRect(1f, height - 17); + } + } + + protected void PropertyTwoFiled(string relativePropName) + { + var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f); + var prop = FindProperty(relativePropName); + ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName); + } + + protected SerializedProperty FindProperty(string path) + { + if (!m_Properties.ContainsKey(path)) + { + m_Properties.Add(path, serializedObject.FindProperty(path)); + } + return m_Properties[path]; + } + } +} \ No newline at end of file diff --git a/Editor/MainComponents/BaseGraphEditor.cs.meta b/Editor/MainComponents/BaseGraphEditor.cs.meta new file mode 100644 index 00000000..cc9f93ef --- /dev/null +++ b/Editor/MainComponents/BaseGraphEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 88786092000154d359c1aa954ce664f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Windows/XChartsEditor.cs b/Editor/Windows/XChartsEditor.cs index bc64266f..949bc3eb 100644 --- a/Editor/Windows/XChartsEditor.cs +++ b/Editor/Windows/XChartsEditor.cs @@ -73,7 +73,7 @@ namespace XCharts.Editor return chart; } - public static T AddGraph(string graphName) where T : BaseGraph + public static T AddGraph(string graphName) where T : Graphic { var parent = GetParent(); if (parent == null) return null; diff --git a/Runtime/Component/Background/Background.cs b/Runtime/Component/Background/Background.cs index 2d000def..bce23b29 100644 --- a/Runtime/Component/Background/Background.cs +++ b/Runtime/Component/Background/Background.cs @@ -17,6 +17,8 @@ namespace XCharts.Runtime [SerializeField] private Sprite m_Image; [SerializeField] private Image.Type m_ImageType; [SerializeField] private Color m_ImageColor = Color.white; + [SerializeField][Since("v3.10.0")] private float m_ImageWidth = 0; + [SerializeField][Since("v3.10.0")] private float m_ImageHeight = 0; [SerializeField] private bool m_AutoColor = true; [SerializeField][Since("v3.10.0")] private BorderStyle m_BorderStyle = new BorderStyle(); @@ -58,6 +60,26 @@ namespace XCharts.Runtime set { if (PropertyUtil.SetColor(ref m_ImageColor, value)) SetComponentDirty(); } } + /// + /// the width of background image. + /// ||背景图宽度。 + /// + public float imageWidth + { + get { return m_ImageWidth; } + set { if (PropertyUtil.SetStruct(ref m_ImageWidth, value)) SetComponentDirty(); } + } + + /// + /// the height of background image. + /// ||背景图高度。 + /// + public float imageHeight + { + get { return m_ImageHeight; } + set { if (PropertyUtil.SetStruct(ref m_ImageHeight, value)) SetComponentDirty(); } + } + /// /// Whether to use theme background color for component color when the background component is on. /// ||当background组件开启时,是否自动使用主题背景色作为backgrounnd组件的颜色。当设置为false时,用imageColor作为颜色。 diff --git a/Runtime/Internal/UIComponent.cs b/Runtime/Internal/UIComponent.cs index c0fb9f87..ab3ab1dc 100644 --- a/Runtime/Internal/UIComponent.cs +++ b/Runtime/Internal/UIComponent.cs @@ -14,7 +14,7 @@ namespace XCharts.Runtime { [SerializeField] private bool m_DebugModel = false; [SerializeField] protected UIComponentTheme m_Theme = new UIComponentTheme(); - [SerializeField] private ImageStyle m_Background = new ImageStyle() { show = false }; + [SerializeField] private Background m_Background = new Background() { show = true }; protected bool m_DataDirty; private ThemeType m_CheckTheme = 0; @@ -24,7 +24,7 @@ namespace XCharts.Runtime /// /// 背景样式。 /// - public ImageStyle background { get { return m_Background; } set { m_Background = value; color = Color.white; } } + public Background background { get { return m_Background; } set { m_Background = value; color = Color.white; } } /// /// Update chart theme. /// ||切换内置主题。 diff --git a/Runtime/Internal/UIComponentTheme.cs b/Runtime/Internal/UIComponentTheme.cs index df0f94b3..7d2d2e0b 100644 --- a/Runtime/Internal/UIComponentTheme.cs +++ b/Runtime/Internal/UIComponentTheme.cs @@ -50,5 +50,13 @@ namespace XCharts.Runtime else return ColorUtil.clearColor32; } } + + public Color32 GetBackgroundColor(Background background) + { + if (background != null && background.show && !background.autoColor) + return background.imageColor; + else + return backgroundColor; + } } } \ No newline at end of file diff --git a/Runtime/Internal/Utilities/ChartHelper.cs b/Runtime/Internal/Utilities/ChartHelper.cs index 2b21fd59..44298b7c 100644 --- a/Runtime/Internal/Utilities/ChartHelper.cs +++ b/Runtime/Internal/Utilities/ChartHelper.cs @@ -368,6 +368,28 @@ namespace XCharts.Runtime } } + public static void SetBackground(Image background, Background imageStyle) + { + if (background == null) return; + if (imageStyle.show) + { + background.gameObject.SetActive(true); + background.sprite = imageStyle.image; + background.color = imageStyle.imageColor; + background.type = imageStyle.imageType; + if (imageStyle.imageWidth > 0 && imageStyle.imageHeight > 0) + { + background.rectTransform.sizeDelta = new Vector2(imageStyle.imageWidth, imageStyle.imageHeight); + } + } + else + { + background.sprite = null; + background.color = Color.clear; + background.gameObject.SetActive(false); + } + } + public static ChartLabel AddAxisLabelObject(int total, int index, string name, Transform parent, Vector2 sizeDelta, Axis axis, ComponentTheme theme, string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter, Color32 iconDefaultColor = default(Color32)) diff --git a/Runtime/Internal/Utilities/UIHelper.cs b/Runtime/Internal/Utilities/UIHelper.cs index a5598f30..54f9ff25 100644 --- a/Runtime/Internal/Utilities/UIHelper.cs +++ b/Runtime/Internal/Utilities/UIHelper.cs @@ -13,27 +13,30 @@ namespace XCharts.Runtime { internal static void DrawBackground(VertexHelper vh, UIComponent component) { - if (component.background.show == false || - (component.background.sprite == null && ChartHelper.IsClearColor(component.background.color))) - { - var p1 = new Vector3(component.graphX, component.graphY); - var p2 = new Vector3(component.graphX + component.graphWidth, component.graphY); - var p3 = new Vector3(component.graphX + component.graphWidth, component.graphY + component.graphHeight); - var p4 = new Vector3(component.graphX, component.graphY + component.graphHeight); - UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, GetBackgroundColor(component)); - } + var background = component.background; + if (!background.show) + return; + if (background.image != null) + return; + + var backgroundColor = component.theme.GetBackgroundColor(background); + var borderWidth = background.borderStyle.GetRuntimeBorderWidth(); + var borderColor = background.borderStyle.GetRuntimeBorderColor(); + var cornerRadius = background.borderStyle.GetRuntimeCornerRadius(); + UGL.DrawRoundRectangleWithBorder(vh, component.graphRect, backgroundColor, backgroundColor, cornerRadius, + borderWidth, borderColor); } internal static void InitBackground(UIComponent table) { if (table.background.show == false || - (table.background.sprite == null && ChartHelper.IsClearColor(table.background.color))) + (table.background.image == null && ChartHelper.IsClearColor(table.background.imageColor))) { ChartHelper.DestoryGameObject(table.transform, "Background"); return; } - var sizeDelta = table.background.width > 0 && table.background.height > 0 ? - new Vector2(table.background.width, table.background.height) : + var sizeDelta = table.background.imageWidth > 0 && table.background.imageHeight > 0 ? + new Vector2(table.background.imageWidth, table.background.imageHeight) : table.graphSizeDelta; var backgroundObj = ChartHelper.AddObject("Background", table.transform, table.graphMinAnchor, table.graphMaxAnchor, table.graphPivot, sizeDelta); @@ -44,14 +47,7 @@ namespace XCharts.Runtime table.graphMaxAnchor, table.graphPivot, sizeDelta); ChartHelper.SetBackground(backgroundImage, table.background); backgroundObj.transform.SetSiblingIndex(0); - } - - public static Color32 GetBackgroundColor(UIComponent component) - { - if (component.background.show && !ChartHelper.IsClearColor(component.background.color)) - return component.background.color; - else - return component.theme.backgroundColor; + backgroundObj.SetActive(table.background.show && table.background.image != null); } } } \ No newline at end of file diff --git a/Runtime/Widgets.meta b/Runtime/Widgets.meta deleted file mode 100644 index acbde32f..00000000 --- a/Runtime/Widgets.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 5e8b7c21c2d5a45f09bfd4028bbe5f63 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/Widgets/SVGImage.cs b/Runtime/Widgets/SVGImage.cs deleted file mode 100644 index 1e12c979..00000000 --- a/Runtime/Widgets/SVGImage.cs +++ /dev/null @@ -1,33 +0,0 @@ -using UnityEngine; -using UnityEngine.UI; -using XUGL; - -namespace XCharts.Runtime -{ - [ExecuteInEditMode] - public class SVGImage : MaskableGraphic - { - [SerializeField] private bool m_MirrorY; - [SerializeField] private string m_SVGPath; - - private SVGPath m_Path; - - public string svgPath { set { m_SVGPath = value; } get { return m_SVGPath; } } - public bool mirrorY { set { m_MirrorY = value; } get { return m_MirrorY; } } - - protected override void Awake() - { - base.Awake(); - m_Path = SVGPath.Parse(m_SVGPath); - m_Path.mirrorY = m_MirrorY; - } - - protected override void OnPopulateMesh(VertexHelper vh) - { - vh.Clear(); - if (m_Path != null) - m_Path.Draw(vh); - } - - } -} \ No newline at end of file