Skip to content

Commit 5eb6b4e

Browse files
authored
Merge pull request #126 from cgtuebingen/documentation.comments
Cleanup
2 parents 8c49831 + cd4172a commit 5eb6b4e

15 files changed

+69
-91
lines changed

UnityProjekte/Base Project/Assets/Scripts/NewUIVisual.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44
using TMPro;
5-
5+
//Controller for the UI
66
public class exampleUIText : MonoBehaviour
77
{
88
public WeatherGetter weatherGetter;
@@ -29,16 +29,19 @@ public class exampleUIText : MonoBehaviour
2929
// Start is called before the first frame update
3030
void Start()
3131
{
32+
//empty start
3233
disableAllVisualizations();
3334
}
3435

3536
// Update is called once per frame
3637
void Update()
3738
{
39+
//get the weather
3840
WeatherGetter.Result<WeatherResult>? weather = weatherGetter.getWeather();
3941
if (weather is WeatherGetter.Result<WeatherResult> weatherRes) {
4042
if(!weatherRes.isOk){
4143
cityText.text = "Fehler ...";
44+
//this means the date is outside of the allowed range
4245
if (weatherGetter.errorString == "HTTP/1.1 400 Bad Request")
4346
{
4447
cityText.text = "invalid date";
@@ -47,6 +50,7 @@ void Update()
4750
friendlyNameText.text = "";
4851
activateVisualizationObject(null);
4952
}
53+
//activate the right visualization
5054
else{
5155
activateVisualizationObject(weatherRes.value.weatherType);
5256
temperatureText.text = weatherRes.value.temp.ToString("0.0") + " °C";
@@ -62,6 +66,7 @@ void Update()
6266

6367
}
6468

69+
//now with english strings
6570
string weatherToFriendlyString(WeatherResult.WeatherType weather){
6671
switch(weather){
6772
case WeatherResult.WeatherType.RAIN:
@@ -91,6 +96,7 @@ void disableAllVisualizations()
9196
thunderstormVisualization.SetActive(false);
9297
}
9398

99+
//bad code: emergency fix
94100
void activateVisualizationObject(WeatherResult.WeatherType? weather)
95101
{
96102
if (weather is null)

UnityProjekte/Base Project/Assets/Scripts/OWMResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using UnityEngine;
44
using System;
55

6-
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
6+
//auto generated, for parsing JSON
77
public class Clouds
88
{
99
public int all ;

UnityProjekte/Base Project/Assets/Scripts/OWMWeather.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class OWMWeather : WeatherResult
66
{
77
public OWMWeather(double temp, int id, string city):base(temp - 273.15,parseWeatherFromID(id),city) {
88
}
9+
//parse the weather code
910
//Based on: https://openweathermap.org/weather-conditions
1011
public static WeatherResult.WeatherType parseWeatherFromID(int id){
1112
int firstDigit = id/100;

UnityProjekte/Base Project/Assets/Scripts/OpenMeteoResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using System;
55
[System.Serializable]
6+
//auto generated, for parsing JSON
67
public class Hourly
78
{
89
public List<string> time;

UnityProjekte/Base Project/Assets/Scripts/OpenMeteoWeather.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
4-
4+
//Open Meteo specific weatherResult
55
public class OpenMeteoWeather : WeatherResult
66
{
77

88
public OpenMeteoWeather(double temp, int id, string city):base(temp,parseWeatherFromID(id),city) {
99
}
10+
//parse weather ID
1011
public static WeatherResult.WeatherType parseWeatherFromID(int id){
1112
switch (id){
1213
case 0:

UnityProjekte/Base Project/Assets/Scripts/Rotation/RotateLeft.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ public class RotateLeft : MonoBehaviour
77
//GameObject which shold be rotated (Drag&Drop Parameter)
88
public GameObject toRotate;
99

10-
// Start is called before the first frame update
11-
void Start()
12-
{
13-
14-
}
15-
16-
// Update is called once per frame
17-
void Update()
18-
{
19-
20-
}
21-
2210
/*
2311
*Functions which rotates the specified Object while Collider is entered (left)
2412
*/

UnityProjekte/Base Project/Assets/Scripts/Rotation/RotateRight.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@
66
public class RotateRight : MonoBehaviour
77
{
88
public GameObject toRotate;
9-
10-
// Start is called before the first frame update
11-
void Start()
12-
{
13-
14-
}
159

16-
// Update is called once per frame
17-
void Update()
18-
{
19-
20-
}
21-
2210
/*
2311
*Functions which rotates the specified Object while Collider is entered (right)
2412
*/

UnityProjekte/Base Project/Assets/Scripts/Search.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,11 @@ public class Search : MonoBehaviour
77
{
88
public WeatherGetter weatherGetter;
99

10-
// Start is called before the first frame update
11-
void Start()
12-
{
13-
14-
15-
}
16-
17-
// Update is called once per frame
18-
void Update()
19-
{
20-
21-
}
10+
//this gets run on "enter"
2211
public void submit(){
2312
weatherGetter.updateLocationFromString(GlobalNonNativeKeyboard.instance.keyboard.text);
24-
Debug.Log(GlobalNonNativeKeyboard.instance.keyboard.text);
2513
}
14+
//show the keyboard on "click"
2615
void OnTriggerEnter(){
2716
GlobalNonNativeKeyboard.instance.ShowKeyboard();
2817
}

UnityProjekte/Base Project/Assets/Scripts/TouchPositionFinder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
public class TouchPositionFinder : MonoBehaviour
44
{
5+
//instead of rotating the globe
56
public float correction = 180.0f;
67
public WeatherGetter weatherGetter;
78
public OSMRequest zoomMap;
89
public float coolDown = 0.0f;
910
private void OnTriggerEnter(Collider other)
1011
{
11-
zoomMap.gameObject.SetActive(true);
1212
if(coolDown + 1.0f > Time.time) return;
13+
//show the map
14+
zoomMap.gameObject.SetActive(true);
15+
//reset cooldown
1316
coolDown = Time.time;
1417
Vector3 collisionPoint = other.ClosestPoint(transform.position);
1518
Vector3 touchVector = (collisionPoint - gameObject.transform.position).normalized;

UnityProjekte/Base Project/Assets/Scripts/WeatherGetter.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ public class WeatherGetter : MonoBehaviour
2323
public string errorString = "";
2424
void Start()
2525
{
26+
//request for now
2627
requestTime = DateTime.Now;
27-
requestTime = DateTime.Parse("2024-06-28", null, System.Globalization.DateTimeStyles.RoundtripKind);
2828
}
29+
//just a simple Get Request
2930
IEnumerator GetRequest(string url)
3031
{
3132
//using (UnityWebRequest www = UnityWebRequest.Get(baseURL+"&appid=" + apiKey +"&lat=" + lat + "&lon=" + lon ))
@@ -71,6 +72,7 @@ IEnumerator GetRequestToOMByCity(string city)
7172
} // The using block ensures www.Dispose() is called when this block is exited
7273

7374
}
75+
//get Request to OWM by coordinates for geocoding. Continue by a request to Open Meteo
7476
IEnumerator GetRequestToOMBCoords(float lat, float lon)
7577
{
7678
string url = baseURLOWM + apiKeyOWM + "&lat=" + lat.ToString("0.000000", CultureInfo.InvariantCulture) + "&lon=" + lon.ToString("0.000000", CultureInfo.InvariantCulture);
@@ -98,11 +100,11 @@ IEnumerator GetRequestToOMBCoords(float lat, float lon)
98100
// this will return the current weather if something was requested. Otherwise it will continue return null, unless something arrives
99101
public Result<WeatherResult>? getWeather()
100102
{
103+
//not done
101104
if (result == null) return null;
102-
//Debug.Log(result.Value.value);
105+
//Error
103106
if (!result.Value.isOk) return Result<WeatherResult>.Error(null);
104-
105-
107+
//parse using the right API
106108
switch (api){
107109
case API.OPEN_WEATHER_MAP:
108110
OWMResult res = JsonUtility.FromJson<OWMResult>(result?.value);
@@ -137,12 +139,10 @@ public void updateLocation(float lat, float lon)
137139
}
138140
else
139141
{
140-
141142
//requestString = baseURLOPENM + "?latitude=" + lat + "&longitude=" + lon + "&hourly=temperature_2m,weather_code&start_date=" + requestTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) + "&end_date=" + requestTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
142143
api = API.OPEN_METEO;
143144
StartCoroutine(GetRequestToOMBCoords(lat,lon));
144145
}
145-
//Debug.Log(requestString);
146146
}
147147
//same as updateLocation, but with a string instead of coordinates, expects sanitized input
148148
public void updateLocationFromString(string city)
@@ -161,13 +161,13 @@ public void updateLocationFromString(string city)
161161
}
162162
else
163163
{
164-
Debug.Log("forecast");
165164
Coroutine rout = StartCoroutine(GetRequestToOMByCity(city));
166165
}
167166

168167

169168

170169
}
170+
//Redo the current request
171171
public void update(){
172172
if(coords != null) updateLocation(coords.lat,coords.lon);
173173
}
@@ -176,10 +176,12 @@ public void setRequestTime(DateTime date)
176176
requestTime = date;
177177
update();
178178
}
179+
//we currently only use those two APIs
179180
enum API
180181
{
181182
OPEN_WEATHER_MAP, OPEN_METEO
182183
}
184+
//C# does not have a result type. This why we do this
183185
public struct Result<T>{
184186
public static Result<T> Error(T value) => new Result<T>(value,false);
185187
public static Result<T> Ok(T value) => new Result<T>(value,true);
@@ -193,6 +195,7 @@ public struct Result<T>{
193195

194196

195197
}
198+
//Make coordinates nicer by using a simple object
196199
class Coords{
197200
public float lat;
198201
public float lon;

UnityProjekte/Base Project/Assets/Scripts/WeatherMapGetter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using UnityEngine;
55
using UnityEngine.UI;
66
using UnityEngine.Networking;
7+
//this loads the requested tile from OpenStreetMap
78
public class WeatherMapGetter : MonoBehaviour
89
{
910
private Material material;
@@ -14,6 +15,7 @@ public class WeatherMapGetter : MonoBehaviour
1415
private Texture2DArray textures;
1516
void Start()
1617
{
18+
//initialize textures and material
1719
textures = new Texture2DArray(256, 256, (int)Math.Pow(4, zoomLevel), TextureFormat.ARGB32, true);
1820
material = GetComponent<Renderer>().material;
1921
for (int x = 0; x < (int)Math.Pow(2, zoomLevel); x++)
@@ -23,9 +25,11 @@ void Start()
2325
StartCoroutine(GetText(layer, x, y, zoomLevel));
2426
}
2527
}
28+
//write attributes to shader
2629
material.SetTexture("_TexList", textures);
2730
material.SetInteger("_ZoomLevel",zoomLevel);
2831
}
32+
//request tile from OSM
2933
IEnumerator GetText(string layer, int x, int y, int z)
3034
{
3135
string url = baseURl + layer + "/"+z+"/"+x+"/"+y+".png?appid=" + apiKey;

UnityProjekte/Base Project/Assets/Scripts/WeatherResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections;
22
using System.Collections.Generic;
3-
//using Unity.VisualScripting;
4-
using UnityEngine;
53

4+
using UnityEngine;
5+
//abstact class for the weather result
66
public abstract class WeatherResult {
77

88
//Default Data
@@ -15,7 +15,7 @@ public WeatherResult(double temp, WeatherType id, string city) {
1515
this.weatherType = id;
1616
this.city = city;
1717
}
18-
18+
//could be expanded, for adding new visualizations
1919
public enum WeatherType{
2020
SUN,RAIN,SNOW,CLOUDS,THUNDERSTORM
2121
}

0 commit comments

Comments
 (0)