Skip to content

Commit

Permalink
Merge pull request #126 from cgtuebingen/documentation.comments
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Irgendj0 authored Sep 25, 2024
2 parents 8c49831 + cd4172a commit 5eb6b4e
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 91 deletions.
8 changes: 7 additions & 1 deletion UnityProjekte/Base Project/Assets/Scripts/NewUIVisual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using TMPro;

//Controller for the UI
public class exampleUIText : MonoBehaviour
{
public WeatherGetter weatherGetter;
Expand All @@ -29,16 +29,19 @@ public class exampleUIText : MonoBehaviour
// Start is called before the first frame update
void Start()
{
//empty start
disableAllVisualizations();
}

// Update is called once per frame
void Update()
{
//get the weather
WeatherGetter.Result<WeatherResult>? weather = weatherGetter.getWeather();
if (weather is WeatherGetter.Result<WeatherResult> weatherRes) {
if(!weatherRes.isOk){
cityText.text = "Fehler ...";
//this means the date is outside of the allowed range
if (weatherGetter.errorString == "HTTP/1.1 400 Bad Request")
{
cityText.text = "invalid date";
Expand All @@ -47,6 +50,7 @@ void Update()
friendlyNameText.text = "";
activateVisualizationObject(null);
}
//activate the right visualization
else{
activateVisualizationObject(weatherRes.value.weatherType);
temperatureText.text = weatherRes.value.temp.ToString("0.0") + " °C";
Expand All @@ -62,6 +66,7 @@ void Update()

}

//now with english strings
string weatherToFriendlyString(WeatherResult.WeatherType weather){
switch(weather){
case WeatherResult.WeatherType.RAIN:
Expand Down Expand Up @@ -91,6 +96,7 @@ void disableAllVisualizations()
thunderstormVisualization.SetActive(false);
}

//bad code: emergency fix
void activateVisualizationObject(WeatherResult.WeatherType? weather)
{
if (weather is null)
Expand Down
2 changes: 1 addition & 1 deletion UnityProjekte/Base Project/Assets/Scripts/OWMResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using System;

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
//auto generated, for parsing JSON
public class Clouds
{
public int all ;
Expand Down
1 change: 1 addition & 0 deletions UnityProjekte/Base Project/Assets/Scripts/OWMWeather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class OWMWeather : WeatherResult
{
public OWMWeather(double temp, int id, string city):base(temp - 273.15,parseWeatherFromID(id),city) {
}
//parse the weather code
//Based on: https://openweathermap.org/weather-conditions
public static WeatherResult.WeatherType parseWeatherFromID(int id){
int firstDigit = id/100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using System;
[System.Serializable]
//auto generated, for parsing JSON
public class Hourly
{
public List<string> time;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Open Meteo specific weatherResult
public class OpenMeteoWeather : WeatherResult
{

public OpenMeteoWeather(double temp, int id, string city):base(temp,parseWeatherFromID(id),city) {
}
//parse weather ID
public static WeatherResult.WeatherType parseWeatherFromID(int id){
switch (id){
case 0:
Expand Down
12 changes: 0 additions & 12 deletions UnityProjekte/Base Project/Assets/Scripts/Rotation/RotateLeft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ public class RotateLeft : MonoBehaviour
//GameObject which shold be rotated (Drag&Drop Parameter)
public GameObject toRotate;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

/*
*Functions which rotates the specified Object while Collider is entered (left)
*/
Expand Down
12 changes: 0 additions & 12 deletions UnityProjekte/Base Project/Assets/Scripts/Rotation/RotateRight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@
public class RotateRight : MonoBehaviour
{
public GameObject toRotate;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

/*
*Functions which rotates the specified Object while Collider is entered (right)
*/
Expand Down
15 changes: 2 additions & 13 deletions UnityProjekte/Base Project/Assets/Scripts/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ public class Search : MonoBehaviour
{
public WeatherGetter weatherGetter;

// Start is called before the first frame update
void Start()
{


}

// Update is called once per frame
void Update()
{

}
//this gets run on "enter"
public void submit(){
weatherGetter.updateLocationFromString(GlobalNonNativeKeyboard.instance.keyboard.text);
Debug.Log(GlobalNonNativeKeyboard.instance.keyboard.text);
}
//show the keyboard on "click"
void OnTriggerEnter(){
GlobalNonNativeKeyboard.instance.ShowKeyboard();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

public class TouchPositionFinder : MonoBehaviour
{
//instead of rotating the globe
public float correction = 180.0f;
public WeatherGetter weatherGetter;
public OSMRequest zoomMap;
public float coolDown = 0.0f;
private void OnTriggerEnter(Collider other)
{
zoomMap.gameObject.SetActive(true);
if(coolDown + 1.0f > Time.time) return;
//show the map
zoomMap.gameObject.SetActive(true);
//reset cooldown
coolDown = Time.time;
Vector3 collisionPoint = other.ClosestPoint(transform.position);
Vector3 touchVector = (collisionPoint - gameObject.transform.position).normalized;
Expand Down
17 changes: 10 additions & 7 deletions UnityProjekte/Base Project/Assets/Scripts/WeatherGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public class WeatherGetter : MonoBehaviour
public string errorString = "";
void Start()
{
//request for now
requestTime = DateTime.Now;
requestTime = DateTime.Parse("2024-06-28", null, System.Globalization.DateTimeStyles.RoundtripKind);
}
//just a simple Get Request
IEnumerator GetRequest(string url)
{
//using (UnityWebRequest www = UnityWebRequest.Get(baseURL+"&appid=" + apiKey +"&lat=" + lat + "&lon=" + lon ))
Expand Down Expand Up @@ -71,6 +72,7 @@ IEnumerator GetRequestToOMByCity(string city)
} // The using block ensures www.Dispose() is called when this block is exited

}
//get Request to OWM by coordinates for geocoding. Continue by a request to Open Meteo
IEnumerator GetRequestToOMBCoords(float lat, float lon)
{
string url = baseURLOWM + apiKeyOWM + "&lat=" + lat.ToString("0.000000", CultureInfo.InvariantCulture) + "&lon=" + lon.ToString("0.000000", CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -98,11 +100,11 @@ IEnumerator GetRequestToOMBCoords(float lat, float lon)
// this will return the current weather if something was requested. Otherwise it will continue return null, unless something arrives
public Result<WeatherResult>? getWeather()
{
//not done
if (result == null) return null;
//Debug.Log(result.Value.value);
//Error
if (!result.Value.isOk) return Result<WeatherResult>.Error(null);


//parse using the right API
switch (api){
case API.OPEN_WEATHER_MAP:
OWMResult res = JsonUtility.FromJson<OWMResult>(result?.value);
Expand Down Expand Up @@ -137,12 +139,10 @@ public void updateLocation(float lat, float lon)
}
else
{

//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);
api = API.OPEN_METEO;
StartCoroutine(GetRequestToOMBCoords(lat,lon));
}
//Debug.Log(requestString);
}
//same as updateLocation, but with a string instead of coordinates, expects sanitized input
public void updateLocationFromString(string city)
Expand All @@ -161,13 +161,13 @@ public void updateLocationFromString(string city)
}
else
{
Debug.Log("forecast");
Coroutine rout = StartCoroutine(GetRequestToOMByCity(city));
}



}
//Redo the current request
public void update(){
if(coords != null) updateLocation(coords.lat,coords.lon);
}
Expand All @@ -176,10 +176,12 @@ public void setRequestTime(DateTime date)
requestTime = date;
update();
}
//we currently only use those two APIs
enum API
{
OPEN_WEATHER_MAP, OPEN_METEO
}
//C# does not have a result type. This why we do this
public struct Result<T>{
public static Result<T> Error(T value) => new Result<T>(value,false);
public static Result<T> Ok(T value) => new Result<T>(value,true);
Expand All @@ -193,6 +195,7 @@ public struct Result<T>{


}
//Make coordinates nicer by using a simple object
class Coords{
public float lat;
public float lon;
Expand Down
4 changes: 4 additions & 0 deletions UnityProjekte/Base Project/Assets/Scripts/WeatherMapGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
//this loads the requested tile from OpenStreetMap
public class WeatherMapGetter : MonoBehaviour
{
private Material material;
Expand All @@ -14,6 +15,7 @@ public class WeatherMapGetter : MonoBehaviour
private Texture2DArray textures;
void Start()
{
//initialize textures and material
textures = new Texture2DArray(256, 256, (int)Math.Pow(4, zoomLevel), TextureFormat.ARGB32, true);
material = GetComponent<Renderer>().material;
for (int x = 0; x < (int)Math.Pow(2, zoomLevel); x++)
Expand All @@ -23,9 +25,11 @@ void Start()
StartCoroutine(GetText(layer, x, y, zoomLevel));
}
}
//write attributes to shader
material.SetTexture("_TexList", textures);
material.SetInteger("_ZoomLevel",zoomLevel);
}
//request tile from OSM
IEnumerator GetText(string layer, int x, int y, int z)
{
string url = baseURl + layer + "/"+z+"/"+x+"/"+y+".png?appid=" + apiKey;
Expand Down
6 changes: 3 additions & 3 deletions UnityProjekte/Base Project/Assets/Scripts/WeatherResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections;
using System.Collections.Generic;
//using Unity.VisualScripting;
using UnityEngine;

using UnityEngine;
//abstact class for the weather result
public abstract class WeatherResult {

//Default Data
Expand All @@ -15,7 +15,7 @@ public WeatherResult(double temp, WeatherType id, string city) {
this.weatherType = id;
this.city = city;
}

//could be expanded, for adding new visualizations
public enum WeatherType{
SUN,RAIN,SNOW,CLOUDS,THUNDERSTORM
}
Expand Down
Loading

0 comments on commit 5eb6b4e

Please sign in to comment.