/
characterLoader.cs
576 lines (439 loc) · 15.5 KB
/
characterLoader.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.XR.iOS;
using UnityEngine.Networking;
using System.Linq;
using SimpleJSON;
public class characterLoader : MonoBehaviour {
public string test_text = "H"; //string of the location name
private JSONNode allglyphs; //default coords of character or letter glyphs
private JSONNode database; //translation amount for each coord in glyphs
public GameObject spawnee;
public float scale = 0.0025f;
public LineRenderer lRend;
public float x_dist = 5.0f;
public float y_dist = 5.0f;
private bool isGlyphDone = false;
private bool isLocDone = false;
private bool hasSaved = false; //has this location been saved before?
private bool wasHidden = false;
private List<GameObject> sphereList = new List<GameObject> ();
private List<Vector3> positions = new List<Vector3>();
private float lat;
private float lng;
private string loc_type;
private string loc_name;
//View Mode variables
private bool isAnimLineInitialized = false;
List<GameObject> animLines = new List<GameObject>();
List<float> animLineDists = new List<float> ();
List<float> animLineCounters = new List<float> ();
List<Vector3> animLineDestinations = new List<Vector3> ();
List<Vector3> animLineOrigins = new List<Vector3> ();
public float LineDrawSpeed = 1.0f;
public GameObject animLineObj;
private bool isEditMode = true;
// Use this for initialization
void Start () {
StartCoroutine (GETGlyphs ());
StartCoroutine (GETLocation ());
}
void OnGUI()
{
string modeString = isEditMode == false ? "VIEW" : "EDIT";
if (GUI.Button(new Rect(Screen.width -150.0f, 0.0f, 150.0f, 100.0f), modeString))
{
isEditMode = !isEditMode; //toggle the state
Debug.Log ("NOW ON EDIT: " + isEditMode);
}
}
// Update is called once per frame
void Update () {
if (isGlyphDone && isLocDone) {
if (isEditMode) { //if app is in edit mode
//Debug.Log ("APP IN EDIT MODE");
if (wasHidden) {
//Destroy previous animLines
GameObject[] goArray2 = GameObject.FindGameObjectsWithTag("animlineobj");
if (goArray2.Length > 0)
{
Debug.Log ("DESTROYING PREVIOUS ANIMLINES");
for (int i = 0; i < goArray2.Length; i++)
{
GameObject go2 = goArray2[i];
Destroy (go2);
}
}
isAnimLineInitialized = false; //!IMPORTANT
Debug.Log ("UNHIDE SPHERES & WHITE LINES");
//UNHide all spheres
if (sphereList.Count > 0)
{
Debug.Log ("UNHIDING SPHERES");
for (int i = 0; i < sphereList.Count; i++)
{
GameObject go = sphereList[i];
go.SetActive (true);
}
}
//UNHide white line
lRend.gameObject.SetActive (true);
Debug.Log ("UNHIDING WHITELINE");
wasHidden = false;
}
lRend.positionCount = positions.Count;
Vector3[] posArray = positions.ToArray ();
lRend.SetPositions (posArray);
} else {
//Debug.Log ("APP IN VIEW MODE");
if (!isAnimLineInitialized) { //if anim lines has not been initialized yet
init_AnimLine ();
} else {
update_AnimLine (); //if anim lines have been previously been initialized, update/animate the lines
}
}
}
}
//Load DEFAULT glyphs coordinates from a server
IEnumerator GETGlyphs(){
// using some web API that uses REST and spits out JSON
UnityWebRequest request = UnityWebRequest.Get("http://68.183.20.22:8080/api/glyphs");
yield return request.SendWebRequest ();
if (request.isNetworkError) {
Debug.Log (request.error);
} else {
Debug.Log ("GETTING RESPONSE FOR GETGLYPH()");
//var resultTxt = request.downloadHandler.text;
// now let's parse this
allglyphs = JSON.Parse(request.downloadHandler.text);
}
}
//Check if location has been saved in DB. If yes, get the saved coord translations
IEnumerator GET_DBCoords (string location){
Debug.Log ("CHECKING LOCATION NAME: " + location + " IN DB");
string URL = "http://68.183.20.22:8080/api/coords?loc=" + location;
// using some web API that uses REST and spits out JSON
UnityWebRequest request = UnityWebRequest.Get(URL);
yield return request.SendWebRequest ();
if (request.isNetworkError) {
Debug.Log (request.error);
} else {
var resultTxt = request.downloadHandler.text;
if (resultTxt == "NONE") {
hasSaved = false;
} else {
Debug.Log ("DB DB DB");
Debug.Log (request.downloadHandler.text);
hasSaved = true;
database = JSON.Parse(request.downloadHandler.text);
Debug.Log ("DB COUNT: "+ database [0]["translations"].Count);
}
isLocDone = true;
}
}
//Get Lat & Lng of current device
IEnumerator GETLocation(){
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
yield break;
// Start service before querying location
Input.location.Start();
Debug.Log ("GETTING LOCATION!");
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
// Access granted and location value could be retrieved
//print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
Debug.Log ("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
lat = Input.location.lastData.latitude;
lng = Input.location.lastData.longitude;
//Get location name using GMaps Reverse Geocoding
StartCoroutine (GETLocName (lat, lng));
}
// Stop service if there is no need to query location updates continuously
Input.location.Stop();
}
//Get location name using GMaps Reverse Geocoding
IEnumerator GETLocName(float lat, float lng){
var s_lat = lat.ToString ();
var s_lng = lng.ToString ();
string URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + s_lat + "," + s_lng + "&key=AIzaSyAkME1xnJuC3gQIH-8sTaE3ysWv6LgzIoU";
// using some web API that uses REST and spits out JSON
UnityWebRequest request = UnityWebRequest.Get(URL);
yield return request.SendWebRequest ();
if (request.isNetworkError) {
Debug.Log (request.error);
} else {
Debug.Log ("GETTING RESPONSE FOR GET LOCNAME!!");
// we got something so lets show the results
var resultTxt = request.downloadHandler.text;
//Debug.Log(resultTxt);
// now let's parse this
var results = JSON.Parse(request.downloadHandler.text);
Debug.Log(results["status"]);
loc_type = results ["results"] [0] ["address_components"] [0] ["types"] [0].Value;
loc_name = results ["results"] [0] ["address_components"] [0] ["short_name"].Value;
string name0 = results ["results"] [0] ["address_components"] [0] ["short_name"].Value.ToString ();
string name1 = results ["results"] [0] ["address_components"] [1] ["short_name"].Value.ToString ();
string name2 = results ["results"] [0] ["address_components"] [2] ["short_name"].Value.ToString ();
//Debug.Log (loc_name);
//Debug.Log (loc_type);
//test_text = loc_name.ToString () + " " + loc_type.ToString ();
test_text = name0 + " " + name1 + " " + name2;
//test_text = "hello world";
StartCoroutine (GET_DBCoords (test_text));
}
}
//find the index that has the drawing command of specified character
int findIndexOfGlyph(string Char){
//spell out numbers
switch (Char)
{
case "0":
Char = "zero";
break;
case "1":
Char = "one";
break;
case "2":
Char = "two";
break;
case "3":
Char = "three";
break;
case "4":
Char = "four";
break;
case "5":
Char = "five";
break;
case "6":
Char = "six";
break;
case "7":
Char = "seven";
break;
case "8":
Char = "eight";
break;
case "9":
Char = "nine";
break;
}
for (var i = 0; i < allglyphs.Count; i++) {
var currentChar = allglyphs [i] ["name"].Value;
if (currentChar == Char) {
return i;
}
}
return 1000; //return impossible index if character is not found
}
//Initialize coordinates to draw
void drawLetters(Vector3 plane_pos){
Debug.Log ("DRAWING LETTERS: "+test_text);
int index = 0;
int line = 4;
foreach (char c in test_text) {
var ind = findIndexOfGlyph (c.ToString ());
if (c.ToString () == " ") { //if character is a space
line--;
index = 0;
}
var commands = allglyphs [ind] ["commands"];
lRend.positionCount = commands.Count;
for (var i = 0; i < commands.Count; i++) {
//Debug.Log (commands [i] ["type"].Value);
string type = commands [i] ["type"].Value;
// if (type == "Z")
// break;
if (type == "C") {
// var x1 = float.Parse (commands [i] ["x1"].Value) * scale;
// var y1 = float.Parse (commands [i] ["y1"].Value) * scale;
//Instantiate(spawnee, new Vector3(x1, y1, 0), Quaternion.identity);
// var x2 = float.Parse (commands [i] ["x2"].Value) * scale;
// var y2 = float.Parse (commands [i] ["y2"].Value) * scale;
//Instantiate(spawnee, new Vector3(x2, y2, 0), Quaternion.identity);
}
if (type != "Z") {
//Debug.Log (commands [i] ["x"].Value);
//Debug.Log (commands [i] ["y"].Value);
var x = float.Parse (commands [i] ["x"].Value) * scale * 0.1f + (index * x_dist * 0.2f) - 0.5f;
var y = float.Parse (commands [i] ["y"].Value) * scale * 0.1f + (line * y_dist * 0.2f);
var z = 0.0f;
var coord_index = positions.Count; //positions.Count is the index of the current coordinate
Vector3 ori_pos = new Vector3 (x, y, z);
if (hasSaved) { //if DB has location, translate the coordinates according to DB data. if not move ahead.
ori_pos = transCoord (ori_pos, coord_index);
}
//Debug.Log (y);
var adj_x = ori_pos.x + plane_pos.x;
//Debug.Log ("ori_pos.x = " + ori_pos.x);
var adj_y = ori_pos.y + plane_pos.y;
var adj_z = ori_pos.z + plane_pos.z;
//draw sphere
GameObject go = Instantiate(spawnee, new Vector3(adj_x, adj_y, adj_z), Quaternion.identity);
//send message to prefab to assign index in array
//send & save default coord and initialization data to sphere instance
string[] tempStorage = new string[8];
tempStorage[0] = coord_index.ToString();
tempStorage[1] = x.ToString();
tempStorage[2] = y.ToString();
tempStorage[3] = z.ToString();
tempStorage[4] = plane_pos.x.ToString();
tempStorage[5] = plane_pos.y.ToString();
tempStorage[6] = plane_pos.z.ToString();
tempStorage[7] = test_text;
//go.SendMessage("Indexing", coord_index);
go.SendMessage("Initializing", tempStorage);
Vector3 pos = new Vector3 (adj_x, adj_y, adj_z);
positions.Add (pos);
}
}
index++;
}
isGlyphDone = true;
}
//Translate coordinate using found data in database
Vector3 transCoord (Vector3 pos, int index){
var x = pos.x;
var y = pos.y;
var z = pos.z;
for (var i = 0; i < database [0]["translations"].Count; i++) {
//Debug.Log ("HERE");
//Debug.Log (index);
var db_index = int.Parse(database [0]["translations"] [i] ["index"].Value);
//Debug.Log (db_index);
if (index == db_index) {
Debug.Log ("TRANSLATING COORDINATE INDEX: " + index);
Debug.Log (x);
Debug.Log (float.Parse(database [0]["translations"] [i] ["x"].Value));
x = pos.x + float.Parse(database [0]["translations"] [i] ["x"].Value);
Debug.Log ("db adjusted: "+x);
y = pos.y + float.Parse(database [0]["translations"] [i] ["y"].Value);
z = pos.z + float.Parse(database [0]["translations"] [i] ["z"].Value);
//break;
}
}
Vector3 final_pos = new Vector3 (x, y, z);
return final_pos;
}
void moveLine (string[] Str){
//Debug.Log ("moving line?");
int id = int.Parse (Str [0]);
//Debug.Log (id);
float new_x = float.Parse (Str [1]);
float new_y = float.Parse (Str [2]);
float new_z = float.Parse (Str [3]);
//Debug.Log (new_x);
//Debug.Log (new_y);
//Debug.Log (new_z);
Vector3 new_pos = new Vector3 (new_x, new_y, new_z);
positions [id] = new_pos;
}
void init_AnimLine (){
//Hide all spheres
GameObject[] goArray = GameObject.FindGameObjectsWithTag("sphere");
if (goArray.Length > 0)
{
Debug.Log ("HIDING SPHERES");
sphereList.Clear ();
for (int i = 0; i < goArray.Length; i++)
{
GameObject go = goArray[i];
sphereList.Add (go);
go.SetActive (false);
}
}
//Hide white line
lRend.gameObject.SetActive (false);
Debug.Log ("HIDING WHITE LINE");
wasHidden = true;
//Destroy previous animLines
GameObject[] goArray2 = GameObject.FindGameObjectsWithTag("animlineobj");
if (goArray2.Length > 0)
{
Debug.Log ("DESTROYING PREVIOUS ANIMLINES");
for (int i = 0; i < goArray2.Length; i++)
{
GameObject go2 = goArray2[i];
Destroy (go2);
}
}
//Reset all animLines Lists, because the code below use .Add() – maybe use indexing instead of add?
animLines.Clear();
animLineDists.Clear ();
animLineCounters.Clear ();
animLineDestinations.Clear ();
animLineOrigins.Clear ();
Debug.Log ("CLEARING ANIMLINES LISTS");
//Initialize the actual coords & lines
for (var i = 0; i < positions.Count; i++) {
Debug.Log ("INITIALIZING ANIM LINE: " + i);
GameObject animLine = Instantiate(animLineObj, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
animLines.Add (animLine);
LineRenderer lRend = animLines[i].AddComponent<LineRenderer>();
var thisLine = animLines [i].GetComponent<LineRenderer> ();
thisLine.SetColors (Color.red,Color.blue);
thisLine.material = new Material(Shader.Find("Particles/Additive"));
AnimationCurve curve = new AnimationCurve();
curve.AddKey(0, 0.004f);
curve.AddKey(1, 0.004f);
thisLine.widthCurve = curve;
thisLine.SetPosition(0,positions[i]);
animLineOrigins.Add (positions [i]);
if (i == positions.Count - 1) {
animLineDists.Add (Vector3.Distance (positions [i], positions [0]));
animLineDestinations.Add (positions [0]);
thisLine.SetPosition (1, positions [0]);
} else {
animLineDists.Add (Vector3.Distance (positions [i], positions [i+1]));
animLineDestinations.Add (positions [i+1]);
thisLine.SetPosition(1,positions[i+1]);
}
//initialize counter for each line
animLineCounters.Add (0.0f);
}
isAnimLineInitialized = true; //anim lines initializing is complete, set its bool to true
}
void update_AnimLine(){
for (var i = 0; i < animLines.Count; i++) {
//if (animLineCounters [i] < animLineDists [i]) {
animLineCounters [i] += .005f / LineDrawSpeed;
float d = Mathf.Lerp (0, animLineDists [i], animLineCounters [i]);
Vector3 pointA = animLineOrigins [i];
Vector3 pointB = animLineDestinations [i];
Vector3 pointBetween = d * Vector3.Normalize (pointB - pointA) + pointA;
var thisLine = animLines [i].GetComponent<LineRenderer> ();
thisLine.SetPosition (1, pointBetween);
float distOri = Mathf.Abs(Vector3.Distance (pointB, pointA));
float distNow = Mathf.Abs(Vector3.Distance (pointBetween, pointA));
if (distOri == distNow) {
animLineCounters [i] = 0.0f;
}
//} else {
// animLineCounters [i] = 0.0f;
//}
}
isAnimLineInitialized = true; //anim lines initializing is complete, set its bool to true
}
}