Revize 54fb8008
Přidáno uživatelem Zuzana Káčereková před více než 3 roky(ů)
Client/Client/Assets/Scripts/UI/WebGLMapController.cs | ||
---|---|---|
5 | 5 |
using UnityEngine; |
6 | 6 |
using UnityEngine.EventSystems; |
7 | 7 |
|
8 |
/// <summary> |
|
9 |
/// Controls the map in WebGL app |
|
10 |
/// |
|
11 |
/// Author: kacerekz |
|
12 |
/// |
|
13 |
/// </summary> |
|
8 | 14 |
public class WebGLMapController : MonoBehaviour |
9 | 15 |
{ |
16 |
/// <summary> |
|
17 |
/// The map to control |
|
18 |
/// </summary> |
|
10 | 19 |
[SerializeField] |
11 | 20 |
private MapRenderer map; |
12 | 21 |
|
22 |
/// <summary> |
|
23 |
/// Zoom delta |
|
24 |
/// </summary> |
|
13 | 25 |
[SerializeField] |
14 | 26 |
private float zoomSpeed; |
15 | 27 |
|
28 |
/// <summary> |
|
29 |
/// Min zoom |
|
30 |
/// </summary> |
|
16 | 31 |
[SerializeField] |
17 | 32 |
private float minScale = 0.5f; |
18 | 33 |
|
34 |
/// <summary> |
|
35 |
/// Max zoom |
|
36 |
/// </summary> |
|
19 | 37 |
[SerializeField] |
20 | 38 |
private float maxScale = 1f; |
21 | 39 |
|
40 |
/// <summary> |
|
41 |
/// Original position |
|
42 |
/// </summary> |
|
22 | 43 |
private Vector3 originalPos; |
44 |
|
|
45 |
/// <summary> |
|
46 |
/// Point clicked in screen space |
|
47 |
/// </summary> |
|
23 | 48 |
private Vector3 screenPoint; |
49 |
|
|
50 |
/// <summary> |
|
51 |
/// Offset from map position |
|
52 |
/// </summary> |
|
24 | 53 |
private Vector3 offset; |
25 | 54 |
|
55 |
/// <summary> |
|
56 |
/// Block input internally |
|
57 |
/// </summary> |
|
26 | 58 |
public bool block; |
59 |
|
|
60 |
/// <summary> |
|
61 |
/// Block input externally |
|
62 |
/// </summary> |
|
27 | 63 |
public bool blockExternal; |
28 | 64 |
|
65 |
// Start is called before the first frame update |
|
29 | 66 |
void Start() |
30 | 67 |
{ |
31 | 68 |
originalPos = map.transform.position; |
32 | 69 |
} |
33 | 70 |
|
71 |
// Update is called every frame |
|
34 | 72 |
void Update() |
35 | 73 |
{ |
36 | 74 |
ClampPan(); |
37 | 75 |
} |
38 | 76 |
|
77 |
/// <summary> |
|
78 |
/// Mouse click handler |
|
79 |
/// </summary> |
|
39 | 80 |
void OnMouseDown() |
40 | 81 |
{ |
41 | 82 |
if (!map.interactable || block || blockExternal) return; |
... | ... | |
49 | 90 |
offset = map.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); |
50 | 91 |
} |
51 | 92 |
|
93 |
/// <summary> |
|
94 |
/// Mouse drag handler |
|
95 |
/// </summary> |
|
52 | 96 |
void OnMouseDrag() |
53 | 97 |
{ |
54 | 98 |
if (!map.interactable || block || blockExternal) return; |
... | ... | |
59 | 103 |
ClampPan(); |
60 | 104 |
} |
61 | 105 |
|
106 |
/// <summary> |
|
107 |
/// Mouse over handler |
|
108 |
/// </summary> |
|
62 | 109 |
private void OnMouseOver() |
63 | 110 |
{ |
64 | 111 |
if (!map.interactable || block || blockExternal) return; |
... | ... | |
84 | 131 |
ClampPan(); |
85 | 132 |
} |
86 | 133 |
|
134 |
/// <summary> |
|
135 |
/// Mouse up handler |
|
136 |
/// </summary> |
|
87 | 137 |
public void OnMouseUp() |
88 | 138 |
{ |
89 | 139 |
block = false; |
90 | 140 |
} |
91 | 141 |
|
142 |
/// <summary> |
|
143 |
/// Clamp pan to allowed values (map size) |
|
144 |
/// </summary> |
|
92 | 145 |
private void ClampPan() |
93 | 146 |
{ |
94 | 147 |
var bounds = map.transform.Find("Underlay").GetComponent<SpriteRenderer>().sprite.bounds; |
... | ... | |
157 | 210 |
|
158 | 211 |
} |
159 | 212 |
|
213 |
/// <summary> |
|
214 |
/// Clamp scale to allowed values |
|
215 |
/// </summary> |
|
216 |
/// <param name="scale">Scale to clamp</param> |
|
217 |
/// <returns>Vlamped scale</returns> |
|
160 | 218 |
private float ClampScale(float scale) |
161 | 219 |
{ |
162 | 220 |
return Mathf.Min(Mathf.Max(scale, minScale), maxScale); |
163 | 221 |
} |
164 | 222 |
|
223 |
/// <summary> |
|
224 |
/// Adjust bounds as per zoom level |
|
225 |
/// </summary> |
|
226 |
/// <param name="bounds">Bounds</param> |
|
227 |
/// <param name="scale">Zoom level</param> |
|
228 |
/// <returns>Adjusted bounds</returns> |
|
165 | 229 |
private Bounds TransformBounds(Bounds bounds, Vector3 scale) |
166 | 230 |
{ |
167 | 231 |
var size = bounds.size; |
... | ... | |
173 | 237 |
return new Bounds(bounds.center, size); |
174 | 238 |
} |
175 | 239 |
|
240 |
/// <summary> |
|
241 |
/// Focus on a given sprite |
|
242 |
/// </summary> |
|
243 |
/// <param name="sprite">Sprite to focus on</param> |
|
176 | 244 |
public void ZoomOn(SpriteRenderer sprite) |
177 | 245 |
{ |
178 | 246 |
if (sprite == null) |
Také k dispozici: Unified diff
Last edits