Skip to content

Commit

Permalink
Map labels
Browse files Browse the repository at this point in the history
  • Loading branch information
DMagic1 committed Dec 30, 2016
1 parent 9b3fc7d commit 7623758
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 19 deletions.
8 changes: 7 additions & 1 deletion SCANsat.Unity/Interfaces/ISCAN_MainMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public interface ISCAN_MainMap

bool Minimized { get; set; }

float Scale { get; }

Vector2 Position { get; set; }

Dictionary<Guid, string> VesselInfoList { get; }
Dictionary<Guid, MapLabelInfo> VesselInfoList { get; }

void ClampToScreen(RectTransform rect);

Expand All @@ -34,6 +36,10 @@ public interface ISCAN_MainMap

string VesselInfo(Guid id);

Sprite VesselType(Guid id);

Vector2 VesselPosition(Guid id);

void Update();
}
}
14 changes: 14 additions & 0 deletions SCANsat.Unity/MapLabelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace SCANsat.Unity
{
public struct MapLabelInfo
{
public string label;
public Sprite image;
public Vector2 pos;
}
}
76 changes: 68 additions & 8 deletions SCANsat.Unity/Unity/SCAN_MainMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public class SCAN_MainMap : MonoBehaviour, IDragHandler, IBeginDragHandler, IEnd
private GameObject m_VesselPrefab = null;
[SerializeField]
private Transform m_VesselTransform = null;
[SerializeField]
private GameObject m_MapPrefab = null;

private ISCAN_MainMap mapInterface;
private bool loaded;
private RectTransform rect;
private Vector2 mouseStart;
private Vector3 windowStart;
private List<SCAN_VesselInfo> vessels = new List<SCAN_VesselInfo>();
private List<SCAN_MapLabel> mapLabels = new List<SCAN_MapLabel>();

private void Awake()
{
Expand All @@ -60,11 +63,21 @@ private void Update()

mapInterface.Update();

for (int i = vessels.Count - 1; i >= 0; i--)
if (!mapInterface.Minimized)
{
SCAN_VesselInfo vessel = vessels[i];
for (int i = vessels.Count - 1; i >= 0; i--)
{
SCAN_VesselInfo vessel = vessels[i];

vessel.UpdateText(i, mapInterface.VesselInfo(vessel.ID));
vessel.UpdateText(mapInterface.VesselInfo(vessel.ID));
}
}

for (int i = mapLabels.Count - 1; i >= 0; i--)
{
SCAN_MapLabel mapLabel = mapLabels[i];

mapLabel.UpdatePosition(mapInterface.VesselPosition(mapLabel.ID));
}
}

Expand All @@ -87,6 +100,8 @@ public void setMap(ISCAN_MainMap map)

CreateVessels(map.VesselInfoList);

SetScale(map.Scale);

SetPosition(map.Position);

if (m_VesselTransform != null)
Expand All @@ -95,6 +110,11 @@ public void setMap(ISCAN_MainMap map)
loaded = true;
}

public void SetScale(float scale)
{
rect.localScale = Vector3.one * scale;
}

public void SetPosition(Vector2 pos)
{
if (rect == null)
Expand All @@ -103,6 +123,19 @@ public void SetPosition(Vector2 pos)
rect.anchoredPosition = new Vector3(pos.x, pos.y, 0);
}

public void RefreshVesselTypes()
{
if (mapInterface == null)
return;

for (int i = mapLabels.Count - 1; i >= 0; i--)
{
SCAN_MapLabel mapLabel = mapLabels[i];

mapLabel.UpdateImage(mapInterface.VesselType(mapLabel.ID));
}
}

public void RefreshVessels()
{
for (int i = vessels.Count - 1; i >= 0; i--)
Expand All @@ -113,13 +146,22 @@ public void RefreshVessels()
Destroy(v.gameObject);
}

for (int i = mapLabels.Count - 1; i >= 0; i--)
{
SCAN_MapLabel m = mapLabels[i];

m.gameObject.SetActive(false);
Destroy(m.gameObject);
}

vessels.Clear();
mapLabels.Clear();

if (mapInterface != null)
CreateVessels(mapInterface.VesselInfoList);
}

private void CreateVessels(Dictionary<Guid, string> vessels)
private void CreateVessels(Dictionary<Guid, MapLabelInfo> vessels)
{
if (vessels == null)
return;
Expand All @@ -131,13 +173,15 @@ private void CreateVessels(Dictionary<Guid, string> vessels)
{
Guid id = vessels.ElementAt(i).Key;

string name = vessels[id];
MapLabelInfo label = vessels[id];

CreateVessel(id, name);
CreateVessel(id, i + 1, label.label);

CreateMapLabel(id, i + 1, label);
}
}

private void CreateVessel(Guid id, string vessel)
private void CreateVessel(Guid id, int i, string vessel)
{
SCAN_VesselInfo vInfo = Instantiate(m_VesselPrefab).GetComponent<SCAN_VesselInfo>();

Expand All @@ -146,11 +190,27 @@ private void CreateVessel(Guid id, string vessel)

vInfo.transform.SetParent(m_VesselTransform, false);

vInfo.SetVessel(id, vessel, mapInterface);
vInfo.SetVessel(id, i, vessel, mapInterface);

vessels.Add(vInfo);
}

private void CreateMapLabel(Guid id, int i, MapLabelInfo info)
{
SCAN_MapLabel mapLabel = Instantiate(m_MapPrefab).GetComponent<SCAN_MapLabel>();

if (mapLabel == null)
return;

mapLabel.transform.SetParent(m_MainMap.transform, false);

info.label = string.Format("[{0}]", i);

mapLabel.Setup(id, info.label, info.image, info.pos);

mapLabels.Add(mapLabel);
}

public void OnBeginDrag(PointerEventData eventData)
{
if (rect == null)
Expand Down
58 changes: 58 additions & 0 deletions SCANsat.Unity/Unity/SCAN_MapLabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace SCANsat.Unity.Unity
{
public class SCAN_MapLabel : MonoBehaviour
{
[SerializeField]
private Image m_Image = null;
[SerializeField]
private TextHandler m_Label = null;

private Guid _id;
private RectTransform rect;

public Guid ID
{
get { return _id; }
}

private void Awake()
{
rect = GetComponent<RectTransform>();
}

public void Setup(Guid id, string label, Sprite image, Vector2 pos)
{
_id = id;

UpdateLabel(label);

UpdateImage(image);

UpdatePosition(pos);
}

public void UpdateLabel(string l)
{
if (!string.IsNullOrEmpty(l) && m_Label != null)
m_Label.OnTextUpdate.Invoke(l);
}

public void UpdateImage(Sprite s)
{
if (m_Image != null)
m_Image.sprite = s;
}

public void UpdatePosition(Vector2 p)
{
if (rect != null)
rect.anchoredPosition = p;
}

}
}
8 changes: 5 additions & 3 deletions SCANsat.Unity/Unity/SCAN_VesselInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@ public class SCAN_VesselInfo : MonoBehaviour
private Guid _id;
private string vesselName;
private ISCAN_MainMap mapInterface;
private int index;

public Guid ID
{
get { return _id; }
}

public void SetVessel(Guid id, string name, ISCAN_MainMap map)
public void SetVessel(Guid id, int i, string name, ISCAN_MainMap map)
{
if (m_VesselText == null || map == null)
return;

_id = id;
index = i;
vesselName = !string.IsNullOrEmpty(name) && name.Length > 26 ? name.Substring(0, 26) : name;
mapInterface = map;

m_VesselText.OnTextUpdate.Invoke(name);
}

public void UpdateText(int i, string value)
public void UpdateText(string value)
{
if (m_VesselText == null)
return;

m_VesselText.OnTextUpdate.Invoke(string.Format("[{0}] {1}: {2}", i, vesselName, value));
m_VesselText.OnTextUpdate.Invoke(string.Format("[{0}] {1}: {2}", index, vesselName, value));
}

}
Expand Down
60 changes: 53 additions & 7 deletions SCANsat/SCAN_Unity/SCAN_UI_MainMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using UnityEngine;
using SCANsat.Unity.Interfaces;
using SCANsat.Unity;
using SCANsat.Unity.Unity;
using SCANsat.SCAN_Data;
using SCANsat.SCAN_UI.UI_Framework;
Expand Down Expand Up @@ -107,6 +108,12 @@ private void vesselChange(Vessel V)
uiElement.RefreshVessels();
}

public void SetScale(float scale)
{
if (uiElement != null)
uiElement.SetScale(scale);
}

public void Update()
{
if (!_isVisible || data == null)
Expand Down Expand Up @@ -277,21 +284,57 @@ public bool Minimized
set { SCANcontroller.controller.mainMapMinimized = value; }
}

public float Scale
{
get { return SCAN_Settings_Config.Instance.UIScale; }
}

public Vector2 Position
{
get { return SCAN_Settings_Config.Instance.MainMapPosition; }
set { SCAN_Settings_Config.Instance.MainMapPosition = value; }
}

public Dictionary<Guid, string> VesselInfoList
public Sprite VesselType(Guid id)
{
if (!SCANcontroller.controller.knownVessels.Contains(id))
return null;

Vessel sv = SCANcontroller.controller.knownVessels[id].vessel;

return null;
}

public Vector2 VesselPosition(Guid id)
{
if (!SCANcontroller.controller.knownVessels.Contains(id))
return new Vector2();

Vessel sv = SCANcontroller.controller.knownVessels[id].vessel;

double lon = SCANUtil.fixLon(sv.longitude);
double lat = SCANUtil.fixLat(sv.latitude);

lon = lon * ((360 * Scale * GameSettings.UI_SCALE) / 360f);
lat = (180 * Scale * GameSettings.UI_SCALE) - lat * ((180 * Scale * GameSettings.UI_SCALE) / 180f);

return new Vector2((float)lon, (float)lat);
}

public Dictionary<Guid, MapLabelInfo> VesselInfoList
{
get
{
Dictionary<Guid, string> vessels = new Dictionary<Guid, string>();
Dictionary<Guid, MapLabelInfo> vessels = new Dictionary<Guid, MapLabelInfo>();

vessels.Add(v.id, v.vesselName);
vessels.Add(v.id, new MapLabelInfo()
{
label = "",
image = VesselType(v.id),
pos = new Vector2()
});

for (int i = SCANcontroller.controller.Known_Vessels.Count - 1; i >= 0 ; i--)
for (int i = SCANcontroller.controller.Known_Vessels.Count - 1; i >= 0; i--)
{
SCANcontroller.SCANvessel sv = SCANcontroller.controller.Known_Vessels[0];

Expand All @@ -301,11 +344,14 @@ public Vector2 Position
if (sv.vessel.mainBody != v.mainBody)
continue;

vessels.Add(sv.vessel.id, sv.vessel.vesselName);
vessels.Add(sv.vessel.id, new MapLabelInfo()
{
label = "",
image = VesselType(sv.vessel.id),
pos = new Vector2()
});
}

SCANUtil.SCANlog("Returning {0} vessels...", vessels.Count);

return vessels;
}
}
Expand Down

0 comments on commit 7623758

Please sign in to comment.