forked from nathan-zaloum/BucketCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadialSlider.cs
124 lines (87 loc) · 4.4 KB
/
RadialSlider.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class RadialSlider : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler {
//----------------------------------------------------------------------------------------------------------------------------------------------//
[SerializeField]
private TopUpManager topUpManager;
[SerializeField]
private Text balance;
public bool topUp = true;
public Image fill;
public Text amount;
public Text target;
public bool overThis = false;
public bool beingHeld = false;
//private int step = 5;
public float rotationValue = 72;
private float tempAmount = 20;
//----------------------------------------------------------------------------------------------------------------------------------------------//
private void Start () {
//target.text = "Your new bucket balance will be $" + (topUpManager.target + tempAmount).ToString ();
}
//----------------------------------------------------------------------------------------------------------------------------------------------//
private void Update () {
if (topUp) {
target.text = "Your new bucket balance will be $" + (int.Parse (balance.text.Substring (balance.text.IndexOf ('$') + 1)) + int.Parse (amount.text.Substring (amount.text.IndexOf ('$') + 1))).ToString ();
} else {
target.text = "Your new bucket balance will be $" + Mathf.Clamp ((int.Parse (balance.text.Substring (balance.text.IndexOf ('$') + 1)) - int.Parse (amount.text.Substring (amount.text.IndexOf ('$') + 1))), 0, (int.Parse (balance.text.Substring (balance.text.IndexOf ('$') + 1)) - int.Parse (amount.text.Substring (amount.text.IndexOf ('$') + 1)))).ToString ();
}
CheckForInput ();
if (beingHeld) {
Vector3 touch;
if (Input.touchCount > 0) {
touch = Input.GetTouch (0).position;
} else {
touch = Input.mousePosition;
}
Vector3 targetDir = touch - transform.parent.transform.position;
float angle = Mathf.Atan2 (targetDir.y, targetDir.x) * Mathf.Rad2Deg;
if (angle <= 90 && angle >= 0) {
rotationValue = -(angle - 90);
} else if (angle >= -180 && angle < 0) {
rotationValue = -angle + 90;
} else if (angle <= 180 && angle > 90) {
rotationValue = 360 - (angle - 90);
}
transform.parent.transform.rotation = Quaternion.AngleAxis (Mathf.Floor ((-rotationValue + 90) / 18) * 18, Vector3.forward);
fill.fillAmount = (Mathf.Ceil (rotationValue / 18) * 18) / 360;
tempAmount = Mathf.RoundToInt (fill.fillAmount * 100);
topUpManager.MatchSliders (tempAmount);
amount.text = "$" + tempAmount.ToString ();
//if (topUp) {
// target.text = "Your new bucket balance will be $" + (topUpManager.target + tempAmount).ToString ();
//} else {
// target.text = "Your new bucket balance will be $" + Mathf.Clamp ((topUpManager.target - tempAmount), 0, (topUpManager.target - tempAmount)).ToString ();
//}
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------//
#region Input
private void CheckForInput () {
if (Input.GetMouseButtonDown (0) || (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)) {
if (overThis) {
beingHeld = true;
}
}
if (Input.GetMouseButtonUp (0) && Input.touchCount == 0) {
beingHeld = false;
}
}
public void OnPointerEnter (PointerEventData eventData) {
//overThis = true;
}
public void OnPointerExit (PointerEventData eventData) {
//overThis = false;
}
public void OnPointerDown (PointerEventData eventData) {
beingHeld = true;
}
public void OnPointerUp (PointerEventData eventData) {
beingHeld = false;
}
#endregion
//----------------------------------------------------------------------------------------------------------------------------------------------//
}