Newer
Older
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[System.Serializable]
public class PiPiece : MonoBehaviour
{
private bool isOver;
private bool lerp;
private Image thisImg;
[HideInInspector]
[SerializeField]
private float innerRadius;
[HideInInspector]
[SerializeField]
private float outerRadius;
[HideInInspector]
[SerializeField]
private Color normalColor;
[HideInInspector]
[SerializeField]
private Color highlightColor;
[HideInInspector]
[SerializeField]
private Color disabledColor;
[HideInInspector]
[SerializeField]
private PiUI.SliceEvent clickEvent;
[HideInInspector]
[SerializeField]
private UnityEvent onHoverEnter;
[HideInInspector]
[SerializeField]
private UnityEvent onHoverExit;
[SerializeField]
private bool onHoverEvents;
[SerializeField]
[HideInInspector]
PiUI parent;
float scaledOR;
public ToolMode ToolMode;
private float maxAngle;
private float minAngle;
private Text sliceLabel;
private Image sliceIcon;
private bool isInteractable;
private bool lastFrameIsOver;
void OnEnable()
{
thisImg = GetComponent<Image>( );
sliceIcon = transform.GetChild(0).GetComponent<Image>( );
sliceLabel = GetComponentInChildren<Text>( );
}
private void Start()
{
thisImg.color= normalColor;
}
public void ManualUpdate()
{
Vector2 inputAxis = parent.joystickInput;
sliceIcon.transform.position = Center( );
sliceLabel.transform.position = Center( ) - new Vector2(0, sliceIcon.rectTransform.sizeDelta.y + parent.textVerticalOffset) * parent.scaleModifier * transform.lossyScale.magnitude;
if (isInteractable)
{
if (isOver && transform.localScale.sqrMagnitude < (Vector2.one * parent.hoverScale).sqrMagnitude)
{
transform.localScale = Vector2.Lerp(transform.localScale, Vector2.one * parent.hoverScale, Time.deltaTime * 10f);
}
else if (transform.localScale.sqrMagnitude > 1 && !isOver)
{
transform.localScale = Vector2.Lerp(transform.localScale, Vector2.one, Time.deltaTime * 10f);
}
Vector2 mousePos = Input.mousePosition;
Vector2 temp = mousePos - (Vector2)transform.position;
float angle = (Mathf.Atan2(temp.y, temp.x) * Mathf.Rad2Deg);
angle = (angle + 360) % 360;
scaledOR = outerRadius;
if (angle < maxAngle && angle > minAngle && temp.magnitude >= innerRadius && temp.magnitude <= scaledOR)
{
isOver = true;
}
else if (parent.useController && isInteractable)
{
temp = inputAxis;
angle = (Mathf.Atan2(temp.y, temp.x) * Mathf.Rad2Deg);
angle = (angle + 360) % 360;
if (angle == 0)
{
angle += 1;
}
if (angle < maxAngle && angle >= minAngle && inputAxis != Vector2.zero)
{
isOver = true;
}
else
{
isOver = false;
thisImg.color= Color.Lerp(thisImg.color, normalColor, Time.deltaTime * 10f);
}
}
else
{
isOver = false;
thisImg.color= Color.Lerp(thisImg.color, normalColor, Time.deltaTime * 10f);
}
if (!parent.interactable)
{
isOver = false;
if (parent.fade)
{
thisImg.color= Color.Lerp(thisImg.color, Color.clear, Time.deltaTime * 10f);
}
}
if (isOver && parent.interactable)
{
scaledOR *= parent.hoverScale;
transform.SetAsLastSibling( );
thisImg.color= Color.Lerp(thisImg.color, highlightColor, Time.deltaTime * 10f);
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0) || parent.useController && parent.joystickButton)
{
clickEvent.Invoke(ToolMode);
}
}
}
else
{
thisImg.color = disabledColor;
transform.localScale = Vector2.Lerp(transform.localScale, Vector2.one, Time.deltaTime * 10f);
}
if (transform.rotation.eulerAngles.z == 359f || transform.rotation.eulerAngles.z == 0)
{
transform.rotation = Quaternion.identity;
}
if (transform.rotation.eulerAngles.z == 359f || transform.rotation.eulerAngles.z == 0 && parent.openedMenu)
{
transform.rotation = Quaternion.identity;
maxAngle = 359f;
minAngle = 359f - (thisImg.fillAmount * 360);
}
else if (parent.interactable)
{
maxAngle = transform.rotation.eulerAngles.z;
minAngle = transform.rotation.eulerAngles.z - (thisImg.fillAmount * 360);
}
sliceLabel.transform.rotation = Quaternion.identity;
sliceIcon.transform.rotation = Quaternion.identity;
if (lastFrameIsOver != isOver && isInteractable && parent.interactable && onHoverEvents)
{
if (isOver && onHoverEnter.GetPersistentEventCount() >= 0)
{
OnHoverEnter( );
}
else if (!isOver && onHoverEnter.GetPersistentEventCount( ) >= 0)
{
OnHoverExit( );
}
}
if (isOver)
{
parent.overMenu = true;
}
lastFrameIsOver = isOver;
}
public Vector2 Center()
{
if (!thisImg)
{
thisImg = GetComponent<Image>( );
}
float temp = (innerRadius * parent.iconDistance / parent.scaleModifier + outerRadius / parent.scaleModifier) / 3f;
temp *= transform.lossyScale.magnitude;
float angleOfFill = thisImg.fillAmount * 360;
Vector2 center = Quaternion.AngleAxis(transform.rotation.eulerAngles.z - angleOfFill / 2f, Vector3.forward) * new Vector2(temp, 0);
center += (Vector2)parent.transform.position;
return center;
}
public void SetData(PiUI.PiData piData, float iR, float oR, PiUI creator)
{
parent = creator;
if (!thisImg || !sliceIcon || !sliceLabel)
{
thisImg = GetComponent<Image>( );
sliceIcon = transform.GetChild(0).GetComponent<Image>( );
sliceLabel = GetComponentInChildren<Text>( );
}
innerRadius = iR;
outerRadius = oR;
normalColor = piData.nonHighlightedColor;
highlightColor = piData.highlightedColor;
disabledColor = piData.disabledColor;
clickEvent = piData.onSlicePressed;
if (parent.fade)
{
thisImg.color= Color.clear;
}
maxAngle = transform.rotation.eulerAngles.z;
minAngle = transform.rotation.eulerAngles.z - (thisImg.fillAmount * 360);
if (transform.rotation.eulerAngles.z == 359f || transform.rotation.eulerAngles.z == 0)
{
transform.rotation = Quaternion.identity;
maxAngle = 359f;
minAngle = 359f - (thisImg.fillAmount * 360);
}
sliceIcon.rectTransform.sizeDelta = new Vector2(piData.iconSize, piData.iconSize);
sliceLabel.text = piData.sliceLabel;
sliceIcon.sprite = piData.icon;
sliceIcon.transform.position = Center( );
sliceLabel.transform.position = Center( ) - new Vector2(0, sliceIcon.rectTransform.sizeDelta.y + parent.textVerticalOffset) * parent.scaleModifier * transform.localScale.magnitude;
isInteractable = piData.isInteractable;
onHoverEvents = piData.hoverFunctions;
if (onHoverEvents)
{
onHoverEnter = piData.onHoverEnter;
onHoverExit = piData.onHoverExit;
}
}
private void OnHoverEnter()
{
onHoverEnter.Invoke( );
}
private void OnHoverExit()
{
onHoverExit.Invoke( );
}
}