AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
Tooltip.cs
1 /******************************************/
2 /* */
3 /* Copyright (c) 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /******************************************/
7 
8 using System.Collections.Generic;
9 using UnityEngine;
10 using UnityEngine.UI;
11 
12 namespace XCharts
13 {
18  [System.Serializable]
19  public class Tooltip : MainComponent
20  {
25  public enum Type
26  {
31  Line,
36  Shadow,
41  None,
46  Corss
47  }
48 
49  [SerializeField] private bool m_Show;
50  [SerializeField] private Type m_Type;
51  [SerializeField] private string m_Formatter;
52  [SerializeField] private string m_ItemFormatter;
53  [SerializeField] private string m_TitleFormatter;
54  [SerializeField] private float m_FixedWidth = 0;
55  [SerializeField] private float m_FixedHeight = 0;
56  [SerializeField] private float m_MinWidth = 0;
57  [SerializeField] private float m_MinHeight = 0;
58  [SerializeField] private string m_NumericFormatter = "";
59  [SerializeField] private float m_PaddingLeftRight = 5f;
60  [SerializeField] private float m_PaddingTopBottom = 5f;
61  [SerializeField] private bool m_IgnoreDataShow = false;
62  [SerializeField] private string m_IgnoreDataDefaultContent = "-";
63  [SerializeField] private bool m_AlwayShow = false;
64  [SerializeField] private Vector2 m_Offset = new Vector2(18f, -25f);
65  [SerializeField] private Sprite m_BackgroundImage;
66  [SerializeField] private TextStyle m_TextStyle = new TextStyle();
67  [SerializeField] private LineStyle m_LineStyle = new LineStyle(LineStyle.Type.None);
68  private DelegateTooltipPosition m_PositionFunction;
69 
70  private GameObject m_GameObject;
71  private GameObject m_Content;
72  private ChartText m_ContentText;
73  private Image m_ContentImage;
74  private RectTransform m_ContentRect;
75  private RectTransform m_ContentTextRect;
76  private List<int> lastDataIndex = new List<int>();
77 
82  public bool show
83  {
84  get { return m_Show; }
85  set { if (PropertyUtil.SetStruct(ref m_Show, value)) { SetAllDirty(); SetActive(value); } }
86  }
91  public Type type
92  {
93  get { return m_Type; }
94  set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetAllDirty(); }
95  }
129  public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
136  public string titleFormatter { get { return m_TitleFormatter; } set { m_TitleFormatter = value; } }
142  public string itemFormatter { get { return m_ItemFormatter; } set { m_ItemFormatter = value; } }
143 
148  public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } }
153  public float fixedHeight { get { return m_FixedHeight; } set { m_FixedHeight = value; } }
158  public float minWidth { get { return m_MinWidth; } set { m_MinWidth = value; } }
163  public float minHeight { get { return m_MinHeight; } set { m_MinHeight = value; } }
174  public string numericFormatter
175  {
176  get { return m_NumericFormatter; }
177  set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
178  }
183  public float paddingLeftRight { get { return m_PaddingLeftRight; } set { m_PaddingLeftRight = value; } }
188  public float paddingTopBottom { get { return m_PaddingTopBottom; } set { m_PaddingTopBottom = value; } }
193  public bool ignoreDataShow { get { return m_IgnoreDataShow; } set { m_IgnoreDataShow = value; } }
198  public string ignoreDataDefaultContent { get { return m_IgnoreDataDefaultContent; } set { m_IgnoreDataDefaultContent = value; } }
203  public Sprite backgroundImage { get { return m_BackgroundImage; } set { m_BackgroundImage = value; SetBackground(m_BackgroundImage); } }
208  public bool alwayShow { get { return m_AlwayShow; } set { m_AlwayShow = value; } }
213  public Vector2 offset { get { return m_Offset; } set { m_Offset = value; } }
218  public TextStyle textStyle
219  {
220  get { return m_TextStyle; }
221  set { if (value != null) { m_TextStyle = value; SetComponentDirty(); } }
222  }
227  public LineStyle lineStyle
228  {
229  get { return m_LineStyle; }
230  set { if (value != null) m_LineStyle = value; SetComponentDirty(); }
231  }
232 
236  public override bool componentDirty
237  {
238  get { return m_ComponentDirty || lineStyle.componentDirty || textStyle.componentDirty; }
239  }
240 
241  public override void ClearComponentDirty()
242  {
243  base.ClearComponentDirty();
244  lineStyle.ClearComponentDirty();
245  textStyle.ClearComponentDirty();
246  }
250  public Dictionary<int, List<int>> runtimeSerieIndex = new Dictionary<int, List<int>>();
255  public List<int> runtimeDataIndex { get { return m_RuntimeDateIndex; } internal set { m_RuntimeDateIndex = value; } }
256  private List<int> m_RuntimeDateIndex = new List<int>() { -1, -1 };
261  public double[] runtimeXValues { get { return m_RuntimeXValue; } internal set { m_RuntimeXValue = value; } }
262  private double[] m_RuntimeXValue = new double[2] { -1, -1 };
267  public double[] runtimeYValues { get { return m_RuntimeYValue; } internal set { m_RuntimeYValue = value; } }
268  private double[] m_RuntimeYValue = new double[2] { -1, -1 };
273  public Vector2 runtimePointerPos { get; internal set; }
278  public float runtimeWidth { get { return m_ContentRect.sizeDelta.x; } }
283  public float runtimeHeight { get { return m_ContentRect.sizeDelta.y; } }
288  public bool runtimeInited { get { return m_GameObject != null; } }
293  public GameObject runtimeGameObject { get { return m_GameObject; } }
297  public float runtimeAngle { get; internal set; }
301  public int runtimeGridIndex { get; internal set; }
302  public int runtimePolarIndex { get; internal set; }
303 
304  public DelegateTooltipPosition positionFunction
305  {
306  get { return m_PositionFunction; }
307  set { m_PositionFunction = value; }
308  }
309 
310  public static Tooltip defaultTooltip
311  {
312  get
313  {
314  var tooltip = new Tooltip
315  {
316  m_Show = true
317  };
318  return tooltip;
319  }
320  }
321 
326  public void SetObj(GameObject obj)
327  {
328  m_GameObject = obj;
329  m_GameObject.SetActive(false);
330  }
331 
336  public void SetContentObj(GameObject content)
337  {
338  m_Content = content;
339  m_ContentRect = m_Content.GetComponent<RectTransform>();
340  m_ContentImage = m_Content.GetComponent<Image>();
341  m_ContentImage.raycastTarget = false;
342  m_ContentText = new ChartText(m_Content);
343  if (m_ContentText != null)
344  {
345  m_ContentTextRect = m_ContentText.gameObject.GetComponentInChildren<RectTransform>();
346  }
348  }
349 
354  public void UpdateToTop()
355  {
356  if (m_GameObject == null) return;
357  int count = m_GameObject.transform.parent.childCount;
358  m_GameObject.GetComponent<RectTransform>().SetSiblingIndex(count - 1);
359  }
360 
365  public void SetContentBackgroundColor(Color color)
366  {
367  if (m_ContentImage != null)
368  m_ContentImage.color = color;
369  }
370 
375  public void SetBackground(Sprite sprite)
376  {
377  if (m_ContentImage != null)
378  {
379  m_ContentImage.type = Image.Type.Sliced;
380  m_ContentImage.sprite = sprite;
381  }
382  }
383 
388  public void SetContentTextColor(Color color)
389  {
390  if (m_ContentText != null)
391  {
392  m_ContentText.SetColor(color);
393  }
394  }
395 
400  public void UpdateContentText(string txt)
401  {
402  if (m_ContentText != null)
403  {
404  m_ContentText.SetText(txt);
405  float wid, hig;
406  if (m_FixedWidth > 0) wid = m_FixedWidth;
407  else if (m_MinWidth > 0 && m_ContentText.GetPreferredWidth() < m_MinWidth) wid = m_MinWidth;
408  else wid = m_ContentText.GetPreferredWidth() + m_PaddingLeftRight * 2;
409  if (m_FixedHeight > 0) hig = m_FixedHeight;
410  else if (m_MinHeight > 0 && m_ContentText.GetPreferredHeight() < m_MinHeight) hig = m_MinHeight;
411  else hig = m_ContentText.GetPreferredHeight() + m_PaddingTopBottom * 2;
412  if (m_ContentRect != null) m_ContentRect.sizeDelta = new Vector2(wid, hig);
413  if (m_ContentTextRect != null)
414  {
415  m_ContentTextRect.anchoredPosition = new Vector3(m_PaddingLeftRight, -m_PaddingTopBottom);
416  }
417  }
418  }
419 
423  internal void ClearValue()
424  {
425  for (int i = 0; i < runtimeDataIndex.Count; i++) runtimeDataIndex[i] = -1;
426  for (int i = 0; i < runtimeXValues.Length; i++) runtimeXValues[i] = -1;
427  for (int i = 0; i < runtimeYValues.Length; i++) runtimeYValues[i] = -1;
428  }
429 
434  public bool IsActive()
435  {
436  return m_GameObject != null && m_GameObject.activeInHierarchy;
437  }
438 
443  public void SetActive(bool flag)
444  {
445  if (!flag && m_AlwayShow) return;
446  if (lastDataIndex.Count >= 2)
447  lastDataIndex[0] = lastDataIndex[1] = -1;
448  if (m_GameObject && m_GameObject.activeInHierarchy != flag)
449  m_GameObject.SetActive(flag);
450  }
451 
456  public void UpdateContentPos(Vector2 pos)
457  {
458  if (m_Content)
459  {
460  if (m_PositionFunction != null)
461  m_Content.transform.localPosition = m_PositionFunction(pos);
462  else
463  m_Content.transform.localPosition = pos;
464  }
465  }
466 
471  public Vector3 GetContentPos()
472  {
473  if (m_Content)
474  return m_Content.transform.localPosition;
475  else
476  return Vector3.zero;
477  }
478 
484  public bool IsDataIndexChanged()
485  {
486  if (runtimeDataIndex.Count < 2 || lastDataIndex.Count < 2) return false;
487  return runtimeDataIndex[0] != lastDataIndex[0] ||
488  runtimeDataIndex[1] != lastDataIndex[1];
489  }
490 
494  internal void UpdateLastDataIndex()
495  {
496  if (lastDataIndex.Count > 0 && runtimeDataIndex.Count > 0) lastDataIndex[0] = runtimeDataIndex[0];
497  if (lastDataIndex.Count > 0 && runtimeDataIndex.Count > 1) lastDataIndex[1] = runtimeDataIndex[1];
498  }
499 
504  public bool IsSelected()
505  {
506  foreach (var index in runtimeDataIndex)
507  if (index >= 0) return true;
508  return false;
509  }
510 
516  public bool IsSelected(int index)
517  {
518  foreach (var temp in runtimeDataIndex)
519  if (temp == index) return true;
520  return false;
521  }
522 
523  public void ClearSerieDataIndex()
524  {
525  foreach (var kv in runtimeSerieIndex)
526  {
527  kv.Value.Clear();
528  }
529  }
530 
531  public void AddSerieDataIndex(int serieIndex, int dataIndex)
532  {
533  if (!runtimeSerieIndex.ContainsKey(serieIndex))
534  {
535  runtimeSerieIndex[serieIndex] = new List<int>();
536  }
537  runtimeSerieIndex[serieIndex].Add(dataIndex);
538  }
539 
540  public bool isAnySerieDataIndex()
541  {
542  foreach (var kv in runtimeSerieIndex)
543  {
544  if (kv.Value.Count > 0) return true;
545  }
546  return false;
547  }
548  }
549 }
XCharts.Tooltip.Type
Type
Indicator type. 指示器类型。
Definition: Tooltip.cs:25
XCharts.Tooltip
Tooltip component. 提示框组件。
Definition: Tooltip.cs:19
XCharts.Tooltip.alwayShow
bool alwayShow
Whether to trigger after always display. 是否触发后一直显示。
Definition: Tooltip.cs:208
XCharts.Tooltip.IsDataIndexChanged
bool IsDataIndexChanged()
Whether the data item indicated by tooltip has changed. 提示框所指示的数据项是否发生变化。
Definition: Tooltip.cs:484
XCharts.Tooltip.runtimeWidth
float runtimeWidth
the width of tooltip. 提示框宽。
Definition: Tooltip.cs:278
XCharts.Tooltip.paddingLeftRight
float paddingLeftRight
the text padding of left and right. defaut:5. 左右边距。
Definition: Tooltip.cs:183
XCharts.Tooltip.IsSelected
bool IsSelected()
当前提示框是否选中数据项
Definition: Tooltip.cs:504
XCharts.ChartText
Definition: ChartText.cs:16
XCharts.MainComponent
Definition: ChartComponent.cs:67
XCharts.Tooltip.paddingTopBottom
float paddingTopBottom
the text padding of top and bottom. defaut:5. 上下边距。
Definition: Tooltip.cs:188
XCharts.TextStyle
Settings related to text. 文本的相关设置。
Definition: TextStyle.cs:21
XCharts.Tooltip.UpdateToTop
void UpdateToTop()
Keep Tooltiop displayed at the top. 保持Tooltiop显示在最顶上
Definition: Tooltip.cs:354
XCharts.LineStyle.Type
Type
线的类型。
Definition: LineStyle.cs:25
XCharts.Tooltip.runtimeXValues
double[] runtimeXValues
the value for x indicator label. 指示器X轴上要显示的值。
Definition: Tooltip.cs:261
XCharts.Tooltip.SetContentTextColor
void SetContentTextColor(Color color)
设置提示框文本字体颜色
Definition: Tooltip.cs:388
XCharts.Tooltip.SetContentObj
void SetContentObj(GameObject content)
绑定提示框的文本框gameObject
Definition: Tooltip.cs:336
XCharts.RoseType.None
@ None
Don't show as Nightingale chart.不展示成南丁格尔玫瑰图
XCharts.Tooltip.minWidth
float minWidth
Minimum width. If fixedWidth has a value, get fixedWidth first. 最小宽度。如若 fixedWidth 设有值,优先取 fixedWidth...
Definition: Tooltip.cs:158
XCharts.Tooltip.show
bool show
Whether to show the tooltip component. 是否显示提示框组件。
Definition: Tooltip.cs:83
XCharts.ChartComponent.componentDirty
virtual bool componentDirty
组件重新初始化标记。
Definition: ChartComponent.cs:25
XCharts.Tooltip.runtimeYValues
double[] runtimeYValues
the value for y indicator label. 指示器Y轴上要显示的值。
Definition: Tooltip.cs:267
XCharts.Tooltip.runtimeSerieIndex
Dictionary< int, List< int > > runtimeSerieIndex
当前提示框所指示的Serie索引(目前只对散点图有效)。
Definition: Tooltip.cs:250
XCharts.Tooltip.GetContentPos
Vector3 GetContentPos()
获得当前提示框的位置
Definition: Tooltip.cs:471
XCharts
Definition: RewardChart.cs:14
XCharts.Tooltip.backgroundImage
Sprite backgroundImage
The image of icon. 图标的图片。
Definition: Tooltip.cs:203
XCharts.Tooltip.runtimePointerPos
Vector2 runtimePointerPos
the current pointer position. 当前鼠标位置。
Definition: Tooltip.cs:273
XCharts.Tooltip.formatter
string formatter
A string template formatter for the total content of the prompt box. Support for wrapping lines with ...
Definition: Tooltip.cs:129
XCharts.Tooltip.SetContentBackgroundColor
void SetContentBackgroundColor(Color color)
设置提示框文本背景色
Definition: Tooltip.cs:365
XCharts.Tooltip.SetBackground
void SetBackground(Sprite sprite)
设置提示框文本背景图片
Definition: Tooltip.cs:375
XCharts.LineStyle
The style of line. 线条样式。 注: 修改 lineStyle 中的颜色不会影响图例颜色,如果需要图例颜色和折线图颜色一致,需修改 itemStyle....
Definition: LineStyle.cs:20
XCharts.Tooltip.lineStyle
LineStyle lineStyle
the line style of indicator line. 指示线样式。
Definition: Tooltip.cs:228
XCharts.Tooltip.ignoreDataDefaultContent
string ignoreDataDefaultContent
The default display character information for ignored data. 被忽略数据的默认显示字符信息。
Definition: Tooltip.cs:198
XCharts.Tooltip.minHeight
float minHeight
Minimum height. If fixedHeight has a value, take priority over fixedHeight. 最小高度。如若 fixedHeight 设有值,优...
Definition: Tooltip.cs:163
XCharts.Tooltip.runtimeInited
bool runtimeInited
Whether the tooltip has been initialized. 提示框是否已初始化。
Definition: Tooltip.cs:288
XCharts.Tooltip.fixedWidth
float fixedWidth
Fixed width. Higher priority than minWidth. 固定宽度。比 minWidth 优先。
Definition: Tooltip.cs:148
XCharts.Tooltip.SetObj
void SetObj(GameObject obj)
绑定提示框gameObject
Definition: Tooltip.cs:326
XCharts.Tooltip.ignoreDataShow
bool ignoreDataShow
Whether to show ignored data on tooltip. 是否显示忽略数据在tooltip上。
Definition: Tooltip.cs:193
XCharts.Tooltip.SetActive
void SetActive(bool flag)
设置提示框是否显示
Definition: Tooltip.cs:443
XCharts.Tooltip.offset
Vector2 offset
The position offset of tooltip relative to the mouse position. 提示框相对于鼠标位置的偏移。
Definition: Tooltip.cs:213
XCharts.Tooltip.componentDirty
override bool componentDirty
组件是否需要刷新
Definition: Tooltip.cs:237
XCharts.Tooltip.textStyle
TextStyle textStyle
the text style of content. 提示框内容文本样式。
Definition: Tooltip.cs:219
XCharts.Tooltip.UpdateContentPos
void UpdateContentPos(Vector2 pos)
更新文本框位置
Definition: Tooltip.cs:456
XCharts.Tooltip.runtimeGameObject
GameObject runtimeGameObject
the gameObject of tooltip. 提示框的gameObject。
Definition: Tooltip.cs:293
XCharts.Tooltip.numericFormatter
string numericFormatter
Standard numeric format string. Used to format numeric values to display as strings....
Definition: Tooltip.cs:175
XCharts.SerieType.Line
@ Line
折线图。折线图是用折线将各个数据点标志连接起来的图表,用于展现数据的变化趋势。可用于直角坐标系和极坐标系上。
XCharts.Tooltip.UpdateContentText
void UpdateContentText(string txt)
设置提示框文本内容
Definition: Tooltip.cs:400
XCharts.Tooltip.runtimeHeight
float runtimeHeight
the height of tooltip. 提示框高。
Definition: Tooltip.cs:283
XCharts.Tooltip.type
Type type
Indicator type. 提示框指示器类型。
Definition: Tooltip.cs:92
XCharts.Tooltip.runtimeGridIndex
int runtimeGridIndex
当前指示的Grid索引。
Definition: Tooltip.cs:301
XCharts.Tooltip.runtimeDataIndex
List< int > runtimeDataIndex
The data index currently indicated by Tooltip. 当前提示框所指示的数据项索引。
Definition: Tooltip.cs:255
XCharts.Tooltip.titleFormatter
string titleFormatter
The string template formatter for the tooltip title content. Support for wrapping lines with ....
Definition: Tooltip.cs:136
XCharts.Tooltip.itemFormatter
string itemFormatter
a string template formatter for a single Serie or data item content. Support for wrapping lines with ...
Definition: Tooltip.cs:142
XCharts.Tooltip.fixedHeight
float fixedHeight
Fixed height. Higher priority than minHeight. 固定高度。比 minHeight 优先。
Definition: Tooltip.cs:153
XCharts.Tooltip.runtimeAngle
float runtimeAngle
当前指示的角度。
Definition: Tooltip.cs:297
XCharts.DelegateTooltipPosition
delegate Vector3 DelegateTooltipPosition(Vector3 pos)
Tooltip的position自定义委托函数。
XCharts.Tooltip.IsActive
bool IsActive()
提示框是否显示
Definition: Tooltip.cs:434
XCharts.Tooltip.IsSelected
bool IsSelected(int index)
指定索引的数据项是否被提示框选中
Definition: Tooltip.cs:516