-
Notifications
You must be signed in to change notification settings - Fork 96
/
SCAN_MapLabel.cs
186 lines (148 loc) · 3.38 KB
/
SCAN_MapLabel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#region license
/*
* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* SCAN_MapLabel - Script for controlling map icon labels
*
* Copyright (c)2014 David Grandy <david.grandy@gmail.com>;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace SCANsat.Unity.Unity
{
public class SCAN_MapLabel : MonoBehaviour
{
[SerializeField]
private Image m_Image = null;
[SerializeField]
private TextHandler m_Label = null;
[SerializeField]
private LayoutElement m_Layout = null;
private Guid _guid;
private int _intID;
private string _stringID;
private RectTransform rect;
private MapLabelInfo label;
private int updateInterval = 60;
private int lastUpdate;
private bool flip;
public Guid ID
{
get { return _guid; }
}
public int IntID
{
get { return _intID; }
}
public string StringID
{
get { return _stringID; }
}
public MapLabelInfo Info
{
get { return label; }
}
private void Awake()
{
rect = GetComponent<RectTransform>();
}
private void Update()
{
if (!label.flash || m_Image == null || m_Label == null)
return;
lastUpdate++;
if (lastUpdate < updateInterval)
return;
lastUpdate = 0;
flip = !flip;
if (flip)
{
m_Image.color = label.flashColor;
m_Label.OnColorUpdate.Invoke(label.flashColor);
}
else
{
m_Image.color = label.baseColor;
m_Label.OnColorUpdate.Invoke(label.baseColor);
}
}
public void Setup(Guid id, MapLabelInfo info)
{
_guid = id;
Setup(info);
}
public void Setup(int id, MapLabelInfo info)
{
_intID = id;
Setup(info);
}
public void Setup(string id, MapLabelInfo info)
{
_stringID = id;
Setup(info);
}
public void Setup(MapLabelInfo info)
{
label = info;
if (m_Image != null)
m_Image.color = info.baseColor;
if (m_Label != null)
m_Label.OnColorUpdate.Invoke(info.baseColor);
if (rect != null)
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x - (info.width / 2), info.alignBottom ? rect.anchoredPosition.y + (info.width / 2) : rect.anchoredPosition.y);
UpdateLabel(info.label);
UpdateImage(info.image);
UpdatePosition(info.pos);
UpdateActive(info.show);
UpdateSize(info.width);
}
public void UpdateLabel(string l)
{
if (m_Label != null)
m_Label.OnTextUpdate.Invoke(l);
}
public void UpdateImage(Sprite s)
{
if (m_Image != null)
m_Image.sprite = s;
}
public void UpdateSize(int i)
{
if (m_Layout != null)
{
m_Layout.preferredHeight = i;
m_Layout.preferredWidth = i;
}
}
public void UpdateActive(bool show)
{
if (gameObject.activeSelf && !show)
{
gameObject.SetActive(false);
return;
}
else if (!gameObject.activeSelf && show)
gameObject.SetActive(true);
}
public void UpdatePosition(Vector2 p)
{
if (rect != null)
rect.anchoredPosition = new Vector2(p.x - (label.width / 2), label.alignBottom ? p.y + (label.width / 2) : p.y);
}
public void UpdatePositionActivation(MapLabelInfo info)
{
UpdateActive(info.show);
if (!info.show)
return;
UpdatePosition(info.pos);
if (label.label != info.label)
UpdateLabel(info.label);
}
}
}