增加IconStyleautoHideWhenLabelEmpty参数设置当label为空时是否自动隐藏图标

This commit is contained in:
monitor1394
2021-06-21 07:08:59 +08:00
parent e9e7371213
commit a91d190d85
7 changed files with 29 additions and 14 deletions

View File

@@ -38,6 +38,8 @@
## master ## master
* (2021.06.21) Add `iconStyle`'s `AutoHideWhenLabelEmpty` to set whether the icon is automatically hidden when `label` is empty
# # v2.2.3 # # v2.2.3
* (2021.06.20) Release `v2.2.3` version * (2021.06.20) Release `v2.2.3` version

View File

@@ -38,6 +38,8 @@
## master ## master
* (2021.06.21) 增加`IconStyle``autoHideWhenLabelEmpty`参数设置当`label`为空时是否自动隐藏图标
## v2.2.3 ## v2.2.3
* (2021.06.20) 发布`v2.2.3`版本 * (2021.06.20) 发布`v2.2.3`版本

View File

@@ -28,6 +28,7 @@ namespace XCharts
PropertyField(prop, "m_Width"); PropertyField(prop, "m_Width");
PropertyField(prop, "m_Height"); PropertyField(prop, "m_Height");
PropertyField(prop, "m_Offset"); PropertyField(prop, "m_Offset");
PropertyField(prop, "m_AutoHideWhenLabelEmpty");
--EditorGUI.indentLevel; --EditorGUI.indentLevel;
} }
} }

View File

@@ -26,6 +26,7 @@ namespace XCharts
[SerializeField] private float m_Width = 20; [SerializeField] private float m_Width = 20;
[SerializeField] private float m_Height = 20; [SerializeField] private float m_Height = 20;
[SerializeField] private Vector3 m_Offset; [SerializeField] private Vector3 m_Offset;
[SerializeField] private bool m_AutoHideWhenLabelEmpty = false;
public void Reset() public void Reset()
{ {
@@ -36,6 +37,7 @@ namespace XCharts
m_Width = 20; m_Width = 20;
m_Height = 20; m_Height = 20;
m_Offset = Vector3.zero; m_Offset = Vector3.zero;
m_AutoHideWhenLabelEmpty = false;
} }
/// <summary> /// <summary>
/// Whether the data icon is show. /// Whether the data icon is show.
@@ -71,6 +73,10 @@ namespace XCharts
/// 水平方向对齐方式。 /// 水平方向对齐方式。
/// </summary> /// </summary>
public Align align { get { return m_Align; } set { m_Align = value; } } public Align align { get { return m_Align; } set { m_Align = value; } }
/// <summary>
/// 当label内容为空时是否自动隐藏图标
/// </summary>
public bool autoHideWhenLabelEmpty { get { return m_AutoHideWhenLabelEmpty; } set { m_AutoHideWhenLabelEmpty = value; } }
public IconStyle Clone() public IconStyle Clone()
{ {
var iconStyle = new IconStyle(); var iconStyle = new IconStyle();
@@ -82,6 +88,7 @@ namespace XCharts
iconStyle.height = height; iconStyle.height = height;
iconStyle.offset = offset; iconStyle.offset = offset;
iconStyle.align = align; iconStyle.align = align;
iconStyle.autoHideWhenLabelEmpty = autoHideWhenLabelEmpty;
return iconStyle; return iconStyle;
} }
@@ -95,6 +102,7 @@ namespace XCharts
height = iconStyle.height; height = iconStyle.height;
offset = iconStyle.offset; offset = iconStyle.offset;
align = iconStyle.align; align = iconStyle.align;
autoHideWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
} }
} }
} }

View File

@@ -576,14 +576,14 @@ namespace XCharts
{ {
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero, txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(0, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis, Vector2.zero, new Vector2(0, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis,
labelName, true); labelName);
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleLeft)); txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleLeft));
} }
else else
{ {
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero, txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis, Vector2.zero, new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis,
labelName, true); labelName);
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleRight)); txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleRight));
} }
var labelWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom); var labelWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom);
@@ -698,7 +698,7 @@ namespace XCharts
xAxis.runtimeMaxValue, dataZoom, isPercentStack); xAxis.runtimeMaxValue, dataZoom, isPercentStack);
var label = ChartHelper.AddAxisLabelObject(i, ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform, var label = ChartHelper.AddAxisLabelObject(i, ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform,
new Vector2(0, 1), new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), new Vector2(0, 1), new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(textWidth, textHeight),
xAxis, theme.axis,labelName, true); xAxis, theme.axis, labelName);
if (i == 0) xAxis.axisLabel.SetRelatedText(label.label, labelWidth); if (i == 0) xAxis.axisLabel.SetRelatedText(label.label, labelWidth);
label.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleCenter)); label.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleCenter));

View File

@@ -12,6 +12,7 @@ namespace XCharts
{ {
public class ChartLabel : ChartObject public class ChartLabel : ChartObject
{ {
private bool m_AutoHideIconWhenLabelEmpty = false;
private bool m_LabelAutoSize = true; private bool m_LabelAutoSize = true;
private float m_LabelPaddingLeftRight = 3f; private float m_LabelPaddingLeftRight = 3f;
private float m_LabelPaddingTopBottom = 3f; private float m_LabelPaddingTopBottom = 3f;
@@ -48,6 +49,9 @@ namespace XCharts
} }
} }
public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
public bool isIconActive { get; private set; }
public ChartLabel() public ChartLabel()
{ {
} }
@@ -91,24 +95,21 @@ namespace XCharts
public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null) public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
{ {
if (m_IconImage == null) return; if (m_IconImage == null) return;
SetIconActive(iconStyle.show);
if (iconStyle.show) if (iconStyle.show)
{ {
ChartHelper.SetActive(m_IconImage.gameObject, true);
m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite; m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
m_IconImage.color = iconStyle.color; m_IconImage.color = iconStyle.color;
m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height); m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
m_IconOffest = iconStyle.offset; m_IconOffest = iconStyle.offset;
m_Align = iconStyle.align; m_Align = iconStyle.align;
m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
AdjustIconPos(); AdjustIconPos();
if (iconStyle.layer == IconStyle.Layer.UnderLabel) if (iconStyle.layer == IconStyle.Layer.UnderLabel)
m_IconRect.SetSiblingIndex(0); m_IconRect.SetSiblingIndex(0);
else else
m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1); m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1);
} }
else
{
ChartHelper.SetActive(m_IconImage.gameObject, false);
}
} }
public float GetLabelWidth() public float GetLabelWidth()
@@ -156,6 +157,7 @@ namespace XCharts
} }
public void SetIconActive(bool flag) public void SetIconActive(bool flag)
{ {
isIconActive = flag;
if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag); if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
} }
@@ -179,13 +181,17 @@ namespace XCharts
return sizeChange; return sizeChange;
} }
AdjustIconPos(); AdjustIconPos();
if (m_AutoHideIconWhenLabelEmpty && isIconActive)
{
ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
}
} }
return false; return false;
} }
private void AdjustIconPos() private void AdjustIconPos()
{ {
if (m_IconImage && m_IconImage.sprite != null && m_IconRect) if (m_IconImage && m_IconRect)
{ {
var iconX = 0f; var iconX = 0f;
switch (m_Align) switch (m_Align)

View File

@@ -400,7 +400,7 @@ namespace XCharts
} }
public static ChartLabel AddAxisLabelObject(int index, string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax, public static ChartLabel AddAxisLabelObject(int index, string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme, string content, bool autoHideWhenContentEmpty = false) Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme, string content)
{ {
var textStyle = axis.axisLabel.textStyle; var textStyle = axis.axisLabel.textStyle;
var iconStyle = axis.iconStyle; var iconStyle = axis.iconStyle;
@@ -414,10 +414,6 @@ namespace XCharts
GameObject.DestroyImmediate(oldText); GameObject.DestroyImmediate(oldText);
} }
var labelShow = axis.axisLabel.show && (axis.axisLabel.interval == 0 || index % (axis.axisLabel.interval + 1) == 0); var labelShow = axis.axisLabel.show && (axis.axisLabel.interval == 0 || index % (axis.axisLabel.interval + 1) == 0);
if (autoHideWhenContentEmpty && string.IsNullOrEmpty(content))
{
labelShow &= false;
}
label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme); label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme);
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, iconStyle.width, iconStyle.height); label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, iconStyle.width, iconStyle.height);
label.SetAutoSize(false); label.SetAutoSize(false);