Skip to content

Commit

Permalink
New syntax for vectors and description (#466)
Browse files Browse the repository at this point in the history
* Use Utils.ParseVectorX for pos/rot/size in generators; Use new syntax for dummy values in ReadFromJson

* Fix issue with spaces in given vectors

* description as a string

* Adapt GUIObjectInfos for new description format

* Simplify GUIObjectInfos.UpdateFields()

* Rename SMargin components; areas as vector4

* Fix areas command parsing

* Fix scrollbar issue in infos panel

* Simplify DisplayObjectData.SetLabel() & fix face text with rack's name
  • Loading branch information
Cedrok authored Feb 15, 2024
1 parent 2b47f91 commit 8d4cecc
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 178 deletions.
21 changes: 11 additions & 10 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -9252,9 +9252,9 @@ MonoBehaviour:
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: 'Build date: 2024-02-12
m_text: 'Build date: 2024-02-13

Commit #87a84205'
Commit #ca7c78e'
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2100000, guid: 79459efec17a4d00a321bdcc27bbc385, type: 2}
Expand Down Expand Up @@ -10197,6 +10197,7 @@ MonoBehaviour:
tmpDomainName: {fileID: 230592175}
tmpDomainAttrs: {fileID: 1441350295}
tmpAttributes: {fileID: 1036111023}
verticalScrollbar: {fileID: 2080815147}
multiPanel: {fileID: 107050141}
objList: {fileID: 336462793}
--- !u!95 &879283142
Expand Down Expand Up @@ -14233,7 +14234,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 217.10045}
m_AnchoredPosition: {x: 0, y: -0.000015258789}
m_SizeDelta: {x: 0, y: 500}
m_Pivot: {x: 0, y: 1}
--- !u!1 &1278892172
Expand Down Expand Up @@ -15160,7 +15161,7 @@ MonoBehaviour:
id:
parentId:
category: room
description: []
description:
domain:
tags: []
attributesKeys: []
Expand All @@ -15181,13 +15182,13 @@ MonoBehaviour:
reserved:
left: 0
right: 0
top: 0
bottom: 0
front: 0
back: 0
technical:
left: 0
right: 0
top: 0
bottom: 0
front: 0
back: 0
usableZone: {fileID: 1332629501}
reservedZone: {fileID: 0}
technicalZone: {fileID: 0}
Expand Down Expand Up @@ -18381,7 +18382,7 @@ MonoBehaviour:
id:
parentId:
category: rack
description: []
description:
domain:
tags: []
attributesKeys: []
Expand Down Expand Up @@ -26585,7 +26586,7 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 1513977461}
m_HandleRect: {fileID: 1513977460}
m_Direction: 2
m_Value: -0.0000021085382
m_Value: 1
m_Size: 0.5658
m_NumberOfSteps: 0
m_OnValueChanged:
Expand Down
58 changes: 8 additions & 50 deletions Assets/Scripts/DisplayObjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void PlaceTexts(string _labelPos)

if (item && item.attributes.ContainsKey("template") && !string.IsNullOrEmpty(item.attributes["template"]))
{
Vector2 size = JsonUtility.FromJson<Vector2>(item.attributes["size"]);
Vector2 size = Utils.ParseVector2(item.attributes["size"]);
if (item.attributes["sizeUnit"] == LengthUnit.Millimeter)
size /= 1000;
else if (item.attributes["sizeUnit"] == LengthUnit.Centimeter)
Expand Down Expand Up @@ -190,67 +190,25 @@ public void UpdateLabels()
///<param name="_str">The attribute to set</param>
public void SetLabel(string _str)
{
OgreeObject obj = GetComponent<OgreeObject>();
if (obj)
{
if (_str[0] == '#')
{
string attr = _str.Substring(1);
if (attr == "name")
WriteLabels(obj.name, true);
else if (attr.Contains("description"))
{
if (attr == "description")
WriteLabels(string.Join("\n", obj.description));
else if (int.TryParse(attr.Substring(11), out int i) && i > 0 && obj.description.Count >= i)
WriteLabels(obj.description[i - 1]);
else
GameManager.instance.AppendLogLine("Wrong description index", ELogTarget.both, ELogtype.warning);
}
else if (obj.attributes.ContainsKey(attr))
WriteLabels(obj.attributes[attr]);
else
{
GameManager.instance.AppendLogLine($"{name} doesn't contain {attr} attribute.", ELogTarget.both, ELogtype.warning);
return;
}
}
else
WriteLabels(_str);
}
Slot s = GetComponent<Slot>();
if (s)
WriteLabels(name);
WriteLabels(_str);
attrToDisplay = _str;
Sensor sensor = GetComponent<Sensor>();
if (sensor)
{
if (_str == "#temperature")
WriteLabels($"{Utils.FloatToRefinedStr(sensor.temperature)} {sensor.temperatureUnit}");
else
GameManager.instance.AppendLogLine($"Sensor can only show temperature (for now)", ELogTarget.both, ELogtype.warning);
}
}

///<summary>
/// Set displayed texts with given string.
///</summary>
///<param name="_str">The string to display</param>
///<param name="_face">If set to true, add referential to front and rear labels</param>
private void WriteLabels(string _str, bool _face = false)
private void WriteLabels(string _str)
{
foreach (TextMeshPro tmp in usedLabels)
{
tmp.text = _str;
if (_face)
if (TryGetComponent(out Rack rack) && rack.name == _str)
{
if (TryGetComponent(out Rack _))
{
if (tmp == labelFront)
tmp.text += " (F)";
if (tmp == labelRear)
tmp.text += " (R)";
}
if (tmp == labelFront)
tmp.text += " (F)";
if (tmp == labelRear)
tmp.text += " (R)";
}
tmp.text = $"<color=#{color}>{tmp.text}</color>";

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Enums_Strucs/SDataFromJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct SApiObject
public string id;
public string parentId;
public string category;
public List<string> description;
public string description;
public string domain;
public List<string> tags;
public Dictionary<string, string> attributes;
Expand Down
19 changes: 13 additions & 6 deletions Assets/Scripts/Enums_Strucs/SMargin.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public struct SMargin
{
public float left;
public float right;
public float top;
public float bottom;
public float front;
public float back;

public SMargin(SMargin _data)
{
top = _data.top;
bottom = _data.bottom;
front = _data.front;
back = _data.back;
right = _data.right;
left = _data.left;
}

public SMargin(Vector4 _data)
{
front = _data.x;
back = _data.y;
right = _data.z;
left = _data.z;
}
}
88 changes: 17 additions & 71 deletions Assets/Scripts/GUIObjectInfos.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class GUIObjectInfos : MonoBehaviour
{
Expand All @@ -12,6 +13,7 @@ public class GUIObjectInfos : MonoBehaviour
[SerializeField] private TMP_Text tmpDomainName = null;
[SerializeField] private TMP_Text tmpDomainAttrs = null;
[SerializeField] private TMP_Text tmpAttributes = null;
[SerializeField] private Scrollbar verticalScrollbar = null;

[Header("Multi objects")]
[SerializeField] private GameObject multiPanel = null;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void UpdateMultiFields(List<GameObject> _objects)
///<param name="_obj">The object whose information are displayed</param>
private void UpdateFields(OgreeObject _obj)
{
int i = 1;
int textHeight = 1;
tmpName.text = _obj.id.Replace(".", "/");
if (!string.IsNullOrEmpty(_obj.domain) && GameManager.instance.allItems.Contains(_obj.domain))
{
Expand All @@ -99,99 +101,43 @@ private void UpdateFields(OgreeObject _obj)
tmpAttributes.text += $"<b>standard deviation:</b> {Utils.FloatToRefinedStr(tempInfos.std)} {tempInfos.unit}\n";
tmpAttributes.text += $"<b>minimum:</b> {Utils.FloatToRefinedStr(tempInfos.min)} {tempInfos.unit}\n";
tmpAttributes.text += $"<b>maximum:</b> {Utils.FloatToRefinedStr(tempInfos.max)} {tempInfos.unit}\n";
i += 4;
textHeight += 4;
if (!string.IsNullOrEmpty(tempInfos.hottestChild))
{
tmpAttributes.text += $"<b>hottest child:</b> {tempInfos.hottestChild}\n";
i++;
textHeight++;
}
}
else
{
// Display posXY if available
if (_obj.attributes.ContainsKey("posXY") && _obj.attributes.ContainsKey("posXYUnit")
&& !string.IsNullOrEmpty(_obj.attributes["posXY"]) && !string.IsNullOrEmpty(_obj.attributes["posXYUnit"]))
// Display description with multiple lines
if (!string.IsNullOrEmpty(_obj.description))
{
Vector2 posXY = JsonUtility.FromJson<Vector2>(_obj.attributes["posXY"]);
tmpAttributes.text += $"<b>posXY:</b> {posXY.x:0.##}/{posXY.y:0.##} ({_obj.attributes["posXYUnit"]})\n";
i++;

// If rack, display pos by tile name if available
if (_obj is Rack && _obj.transform.parent)
{
Room room = _obj.transform.parent.GetComponent<Room>();
if (room.attributes.ContainsKey("tiles"))
{
List<STile> tiles = JsonConvert.DeserializeObject<List<STile>>(room.attributes["tiles"]);
STile tileData = new();
foreach (STile t in tiles)
{
if (t.location == $"{posXY.x:0}/{posXY.y:0}")
tileData = t;
}
if (!string.IsNullOrEmpty(tileData.location) && !string.IsNullOrEmpty(tileData.label))
{
tmpAttributes.text += $"<b>tile's label:</b> {tileData.label}\n";
i++;
}
}
}
}

// Display orientation if available
if (_obj.attributes.ContainsKey("orientation"))
{
tmpAttributes.text += $"<b>orientation:</b> {_obj.attributes["orientation"]}\n";
i++;
}

// Display size if available
if (_obj.attributes.ContainsKey("size") && _obj.attributes.ContainsKey("sizeUnit"))
{
Vector2 size = JsonUtility.FromJson<Vector2>(_obj.attributes["size"]);
tmpAttributes.text += $"<b>size:</b> {size.x}{_obj.attributes["sizeUnit"]} x {size.y}{_obj.attributes["sizeUnit"]} x {_obj.attributes["height"]}{_obj.attributes["heightUnit"]}\n";
i++;
}

// Display template if available
if (_obj.attributes.ContainsKey("template") && !string.IsNullOrEmpty(_obj.attributes["template"]))
{
tmpAttributes.text += $"<b>template:</b> {_obj.attributes["template"]}\n";
i++;
tmpAttributes.text += "<b>description:</b>\n";
tmpAttributes.text += $"{_obj.description}\n";
textHeight += _obj.description.Count(c => c == '\n') + 1;
}

// Display all other attributes
List<string> excludeAttr = new List<string> { "posXY", "posXYUnit", "orientation", "size", "sizeUnit", "template", "separators", "tiles", "colors" };
foreach (KeyValuePair<string, string> kvp in _obj.attributes)
{
if (!string.IsNullOrEmpty(kvp.Value) && !excludeAttr.Contains(kvp.Key))
{
tmpAttributes.text += $"<b>{kvp.Key}:</b> {kvp.Value}\n";
i++;
}
tmpAttributes.text += $"<b>{kvp.Key}:</b> {kvp.Value}\n";
textHeight++;
}

// Display all descriptions
if (_obj.description.Count != 0)
{
tmpAttributes.text += "<b>description:</b>\n";
for (int j = 0; j < _obj.description.Count; j++)
{
tmpAttributes.text += $"<b>{j + 1}:</b> {_obj.description[j]}\n";
i++;
}
}
// Display tags using their color
if (_obj.tags.Count > 0)
{
tmpAttributes.text += "<b>tags: </b>";
i++;
textHeight++;
foreach (string tagName in _obj.tags)
tmpAttributes.text += $"<b><color=#{GameManager.instance.GetTag(tagName).colorCode}>{tagName}</b></color> ";
}
}
// Set correct height for scroll view
RectTransform rt = tmpAttributes.transform.parent.GetComponent<RectTransform>();
rt.sizeDelta = new(0, i * 30);
rt.sizeDelta = new(0, textHeight * 30);
verticalScrollbar.value = 1;
}

///<summary>
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Network/ApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class ApiManager : MonoBehaviour
{
#region Structures from API
public struct SRequest
{
public string type;
Expand Down Expand Up @@ -79,6 +80,7 @@ private struct SLayerContentResp
public string message;
public List<SApiObject> data;
}
#endregion

public static ApiManager instance;

Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Network/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ private async void ModifyObject(string _input)
if ((room.attributes.ContainsKey("reserved") && room.attributes["reserved"] != newData.attributes["reserved"])
|| !room.attributes.ContainsKey("reserved"))
{
SMargin reserved = JsonUtility.FromJson<SMargin>(newData.attributes["reserved"]);
SMargin technical = JsonUtility.FromJson<SMargin>(newData.attributes["technical"]);
SMargin reserved = new(Utils.ParseVector4(newData.attributes["reserved"]));
SMargin technical = new(Utils.ParseVector4(newData.attributes["technical"]));
room.SetAreas(reserved, technical);
}
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/OgreeClasses/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ public void SetTemperature(string _value, string _sensorName)
{
SApiObject se = new()
{
description = new(),
attributes = new(),

name = "sensor", // ?
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/OgreeClasses/OgreeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class OgreeObject : MonoBehaviour, ISerializationCallbackReceiver, ICompa
public string id;
public string parentId;
public string category;
public List<string> description = new();
public string description;
public string domain;
public List<string> tags = new();

Expand Down Expand Up @@ -101,7 +101,7 @@ public virtual void UpdateFromSApiObject(SApiObject _src)
parentId = _src.parentId;
category = _src.category;
domain = _src.domain;
description = _src.description;
description = _src.description.Replace("\\n", "\n");
attributes = _src.attributes;

foreach (string newTag in _src.tags)
Expand Down
Loading

0 comments on commit 8d4cecc

Please sign in to comment.