diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index c321c61d..13fddf8f 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -35,6 +35,7 @@ ## master +* (2021.06.12) Add `iconStyle` `align` parameter to set the horizontal alignment of the icon * (2021.06.12) Optimized import of `Theme` and automatic font refresh (#148) * (2021.06.10) Fixed compatibility issues with `Unity` version (#154) * (2021.06.05) Improved Candlestickchart support for inverse (#152) diff --git a/CHANGELOG.md b/CHANGELOG.md index fee7dac4..0b89580d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ ## master +* (2021.06.12) 增加`IconStyle`的`align`参数设置图标的水平对齐 * (2021.06.12) 优化主题`Theme`的导入和字体自动刷新 (#148) * (2021.06.10) 修复`Unity`版本兼容问题 (#154) * (2021.06.05) 完善`CandlestickChart`对`inverse`的支持 (#152) diff --git a/Editor/PropertyDrawers/IconStyleDrawer.cs b/Editor/PropertyDrawers/IconStyleDrawer.cs index e1e5dd1b..36a3bfb8 100644 --- a/Editor/PropertyDrawers/IconStyleDrawer.cs +++ b/Editor/PropertyDrawers/IconStyleDrawer.cs @@ -22,6 +22,7 @@ namespace XCharts { ++EditorGUI.indentLevel; PropertyField(prop, "m_Layer"); + PropertyField(prop, "m_Align"); PropertyField(prop, "m_Sprite"); PropertyField(prop, "m_Color"); PropertyField(prop, "m_Width"); diff --git a/Runtime/Component/Main/Serie.cs b/Runtime/Component/Main/Serie.cs index f9e59649..6879292b 100644 --- a/Runtime/Component/Main/Serie.cs +++ b/Runtime/Component/Main/Serie.cs @@ -247,7 +247,7 @@ namespace XCharts /// /// 对齐方式 /// - public enum SerieAlign + public enum Align { Center, Left, @@ -337,7 +337,7 @@ namespace XCharts [SerializeField] private SerieDataSortType m_DataSortType = SerieDataSortType.Descending; [SerializeField] private Orient m_Orient = Orient.Vertical; - [SerializeField] private SerieAlign m_Align = SerieAlign.Center; + [SerializeField] private Align m_Align = Align.Center; [SerializeField] private float m_Left; [SerializeField] private float m_Right; [SerializeField] private float m_Top; @@ -1045,7 +1045,7 @@ namespace XCharts /// /// 组件水平方向对齐方式。 /// - public SerieAlign align + public Align align { get { return m_Align; } set { if (PropertyUtil.SetStruct(ref m_Align, value)) SetVerticesDirty(); } diff --git a/Runtime/Component/Sub/IconStyle.cs b/Runtime/Component/Sub/IconStyle.cs index 1addc799..4e0fc70d 100644 --- a/Runtime/Component/Sub/IconStyle.cs +++ b/Runtime/Component/Sub/IconStyle.cs @@ -20,6 +20,7 @@ namespace XCharts } [SerializeField] private bool m_Show; [SerializeField] private Layer m_Layer; + [SerializeField] private Align m_Align = Align.Left; [SerializeField] private Sprite m_Sprite; [SerializeField] private Color m_Color = Color.white; [SerializeField] private float m_Width = 20; @@ -66,7 +67,10 @@ namespace XCharts /// 图标偏移。 /// public Vector3 offset { get { return m_Offset; } set { m_Offset = value; } } - + /// + /// 水平方向对齐方式。 + /// + public Align align { get { return m_Align; } set { m_Align = value; } } public IconStyle Clone() { var iconStyle = new IconStyle(); @@ -77,6 +81,7 @@ namespace XCharts iconStyle.width = width; iconStyle.height = height; iconStyle.offset = offset; + iconStyle.align = align; return iconStyle; } @@ -89,6 +94,7 @@ namespace XCharts width = iconStyle.width; height = iconStyle.height; offset = iconStyle.offset; + align = iconStyle.align; } } } diff --git a/Runtime/Internal/Object/ChartLabel.cs b/Runtime/Internal/Object/ChartLabel.cs index 0d5aef6d..341059f8 100644 --- a/Runtime/Internal/Object/ChartLabel.cs +++ b/Runtime/Internal/Object/ChartLabel.cs @@ -20,6 +20,7 @@ namespace XCharts private RectTransform m_IconRect; private RectTransform m_ObjectRect; private Vector3 m_IconOffest; + private Align m_Align = Align.Left; private Image m_IconImage; @@ -60,6 +61,7 @@ namespace XCharts m_LabelText = new ChartText(labelObj); m_LabelRect = m_LabelText.gameObject.GetComponent(); m_ObjectRect = labelObj.GetComponent(); + m_Align = Align.Left; } public void SetAutoSize(bool flag) @@ -96,6 +98,7 @@ namespace XCharts m_IconImage.color = iconStyle.color; m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height); m_IconOffest = iconStyle.offset; + m_Align = iconStyle.align; AdjustIconPos(); if (iconStyle.layer == IconStyle.Layer.UnderLabel) m_IconRect.SetSiblingIndex(0); @@ -185,22 +188,47 @@ namespace XCharts if (m_IconImage && m_IconImage.sprite != null && m_IconRect) { var iconX = 0f; - switch (m_LabelText.text.alignment) + switch (m_Align) { - case TextAnchor.LowerLeft: - case TextAnchor.UpperLeft: - case TextAnchor.MiddleLeft: - iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2; + case Align.Left: + switch (m_LabelText.text.alignment) + { + case TextAnchor.LowerLeft: + case TextAnchor.UpperLeft: + case TextAnchor.MiddleLeft: + iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2; + break; + case TextAnchor.LowerRight: + case TextAnchor.UpperRight: + case TextAnchor.MiddleRight: + iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2; + break; + case TextAnchor.LowerCenter: + case TextAnchor.UpperCenter: + case TextAnchor.MiddleCenter: + iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2; + break; + } break; - case TextAnchor.LowerRight: - case TextAnchor.UpperRight: - case TextAnchor.MiddleRight: - iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2; - break; - case TextAnchor.LowerCenter: - case TextAnchor.UpperCenter: - case TextAnchor.MiddleCenter: - iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2; + case Align.Right: + switch (m_LabelText.text.alignment) + { + case TextAnchor.LowerLeft: + case TextAnchor.UpperLeft: + case TextAnchor.MiddleLeft: + iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2; + break; + case TextAnchor.LowerRight: + case TextAnchor.UpperRight: + case TextAnchor.MiddleRight: + iconX = m_IconRect.sizeDelta.x / 2; + break; + case TextAnchor.LowerCenter: + case TextAnchor.UpperCenter: + case TextAnchor.MiddleCenter: + iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2; + break; + } break; } m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);