AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
TextLimit.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System;
9 using UnityEngine;
10 using UnityEngine.UI;
11 
12 namespace XCharts
13 {
20  [Serializable]
21  public class TextLimit : SubComponent
22  {
23  [SerializeField] private bool m_Enable = false;
24  [SerializeField] private float m_MaxWidth = 0;
25  [SerializeField] private float m_Gap = 1;
26  [SerializeField] private string m_Suffix = "...";
27 
33  public bool enable
34  {
35  get { return m_Enable; }
36  set { if (PropertyUtil.SetStruct(ref m_Enable, value)) SetComponentDirty(); }
37  }
43  public float maxWidth
44  {
45  get { return m_MaxWidth; }
46  set { if (PropertyUtil.SetStruct(ref m_MaxWidth, value)) SetComponentDirty(); }
47  }
53  public float gap
54  {
55  get { return m_Gap; }
56  set { if (PropertyUtil.SetStruct(ref m_Gap, value)) SetComponentDirty(); }
57  }
63  public string suffix
64  {
65  get { return m_Suffix; }
66  set { if (PropertyUtil.SetClass(ref m_Suffix, value)) SetComponentDirty(); }
67  }
68 
69  private ChartText m_RelatedText;
70  private float m_RelatedTextWidth = 0;
71 
72  public TextLimit Clone()
73  {
74  var textLimit = new TextLimit();
75  textLimit.enable = enable;
76  textLimit.maxWidth = maxWidth;
77  textLimit.gap = gap;
78  textLimit.suffix = suffix;
79  return textLimit;
80  }
81 
82  public void Copy(TextLimit textLimit)
83  {
84  enable = textLimit.enable;
85  maxWidth = textLimit.maxWidth;
86  gap = textLimit.gap;
87  suffix = textLimit.suffix;
88  }
89 
90  public void SetRelatedText(ChartText txt, float labelWidth)
91  {
92  m_RelatedText = txt;
93  m_RelatedTextWidth = labelWidth;
94  }
95 
96 
97  public string GetLimitContent(string content)
98  {
99  float checkWidth = m_MaxWidth > 0 ? m_MaxWidth : m_RelatedTextWidth;
100  if (m_RelatedText == null || checkWidth <= 0) return content;
101  else
102  {
103  if (m_Enable)
104  {
105  float len = m_RelatedText.GetPreferredWidth(content);
106  float suffixLen = m_RelatedText.GetPreferredWidth(suffix);
107  if (len >= checkWidth - m_Gap * 2)
108  {
109  return content.Substring(0, GetAdaptLength(content, suffixLen)) + suffix;
110  }
111  else
112  {
113  return content;
114  }
115  }
116  else
117  {
118  return content;
119  }
120  }
121  }
122 
123  private int GetAdaptLength(string content, float suffixLen)
124  {
125  int start = 0;
126  int middle = content.Length / 2;
127  int end = content.Length;
128  float checkWidth = m_MaxWidth > 0 ? m_MaxWidth : m_RelatedTextWidth;
129  float limit = checkWidth - m_Gap * 2 - suffixLen;
130  if (limit < 0) return 0;
131  float len = 0;
132  while (len != limit && middle != start)
133  {
134  len = m_RelatedText.GetPreferredWidth(content.Substring(0, middle));
135  if (len < limit)
136  {
137  start = middle;
138  }
139  else if (len > limit)
140  {
141  end = middle;
142  }
143  else
144  {
145  break;
146  }
147  middle = (start + end) / 2;
148  }
149  return middle;
150  }
151  }
152 }
XCharts.ChartText
Definition: ChartText.cs:16
XCharts.TextLimit.maxWidth
float maxWidth
Set the maximum width. A default of 0 indicates automatic fetch; otherwise, custom....
Definition: TextLimit.cs:44
XCharts.SubComponent
Definition: ChartComponent.cs:71
XCharts
Definition: RewardChart.cs:14
XCharts.TextLimit
Text character limitation and adaptation component. When the length of the text exceeds the set lengt...
Definition: TextLimit.cs:21
XCharts.TextLimit.gap
float gap
White pixel distance at both ends. 两边留白像素距离。 [default:10f]
Definition: TextLimit.cs:54
XCharts.TextLimit.enable
bool enable
Whether to enable text limit. 是否启用文本自适应。 [default:true]
Definition: TextLimit.cs:34
XCharts.TextLimit.suffix
string suffix
Suffixes when the length exceeds. 长度超出时的后缀。 [default: "..."]
Definition: TextLimit.cs:64