Skip to content

Commit

Permalink
Zoom map resource legend; localized resource conc limits; hide unused…
Browse files Browse the repository at this point in the history
… resources
  • Loading branch information
DMagic1 committed Aug 15, 2020
1 parent c016710 commit 2543f75
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 18 deletions.
4 changes: 4 additions & 0 deletions SCANsat.Unity/Interfaces/ISCAN_ZoomMap.cs
Expand Up @@ -93,6 +93,10 @@ public interface ISCAN_ZoomMap

Texture2D LegendImage { get; }

Texture2D ResourceLegendImage { get; }

Vector2 ResourceLegendLabels { get; }

IList<string> MapTypes { get; }

IList<string> Resources { get; }
Expand Down
60 changes: 51 additions & 9 deletions SCANsat.Unity/Unity/SCAN_ZoomMap.cs
Expand Up @@ -78,6 +78,14 @@ public class SCAN_ZoomMap : CanvasFader, IDragHandler, IBeginDragHandler, IEndDr
[SerializeField]
private LayoutElement m_MapLayout = null;
[SerializeField]
private GameObject m_ResourceBar = null;
[SerializeField]
private RawImage m_ResourceLegendImage = null;
[SerializeField]
private TextHandler m_ResourceLegendLabelOne = null;
[SerializeField]
private TextHandler m_ResourceLegendLabelTwo = null;
[SerializeField]
private RawImage m_LegendImage = null;
[SerializeField]
private TextHandler m_LegendLabelOne = null;
Expand Down Expand Up @@ -292,6 +300,9 @@ public void setMap(ISCAN_ZoomMap map)
if (m_ResourceToggle != null)
m_ResourceToggle.isOn = map.ResourceToggle;

if (m_ResourceBar != null)
m_ResourceBar.SetActive(map.ResourceToggle);

if (!map.OrbitAvailable && m_OrbitObject != null)
m_OrbitObject.SetActive(false);

Expand All @@ -318,6 +329,9 @@ public void setMap(ISCAN_ZoomMap map)

SetLegend(map.LegendToggle);

if (map.ResourceToggle)
SetResourceLegend();

SetWindowState(map.WindowState);

SetRefreshState(map.MapRefresh);
Expand Down Expand Up @@ -549,7 +563,15 @@ private void ResetRefreshState(int i)
}
}

public void SetLegend(bool isOn)
public void SetLegends(bool isOn)
{
SetLegend(isOn);

if (zoomInterface.ResourceToggle)
SetResourceLegend();
}

private void SetLegend(bool isOn)
{
if (m_LegendBar == null)
return;
Expand Down Expand Up @@ -606,6 +628,20 @@ public void SetLegend(bool isOn)
}
}

public void SetResourceLegend()
{
if (m_ResourceLegendImage != null)
m_ResourceLegendImage.texture = zoomInterface.ResourceLegendImage;

Vector2 res = zoomInterface.ResourceLegendLabels;

if (m_ResourceLegendLabelOne != null)
m_ResourceLegendLabelOne.OnTextUpdate.Invoke(res.x.ToString(res.x < 0.1 ? "P1" : "P0"));

if (m_ResourceLegendLabelTwo != null)
m_ResourceLegendLabelTwo.OnTextUpdate.Invoke(res.y.ToString(res.y < 0.1 ? "P1" : "P0"));
}

private void SetFlagIcons(IList<MapLabelInfo> flags)
{
if (flags == null)
Expand Down Expand Up @@ -1134,7 +1170,7 @@ public void OnEndResize(BaseEventData eventData)
resizing = false;

zoomInterface.Size = new Vector2(m_MapLayout.preferredWidth - 8, m_MapLayout.preferredHeight - 8);

UpdateMapData(true);
}

Expand Down Expand Up @@ -1185,7 +1221,10 @@ public void UpdateMapData(bool text)

RefreshIcons();

SetLegend(zoomInterface.LegendToggle);
if (zoomInterface.ResourceToggle)
SetResourceLegend();

SetLegend(zoomInterface.LegendToggle);
}

public void ToggleWindowState()
Expand Down Expand Up @@ -1257,7 +1296,7 @@ private void SetType(string selection)
if (m_DropDownToggles != null)
m_DropDownToggles.SetAllTogglesOff();

UpdateMapData(false);
UpdateMapData(false);
}

public void ToggleResourceSelection(bool isOn)
Expand Down Expand Up @@ -1306,7 +1345,7 @@ private void SetResource(string selection)
if (m_DropDownToggles != null)
m_DropDownToggles.SetAllTogglesOff();

UpdateMapData(false);
UpdateMapData(false);
}

public void RefreshMap()
Expand All @@ -1316,7 +1355,7 @@ public void RefreshMap()

zoomInterface.RefreshMap();

UpdateMapData(false);
UpdateMapData(false);
}

public void ToggleColor(bool isOn)
Expand All @@ -1326,7 +1365,7 @@ public void ToggleColor(bool isOn)

zoomInterface.ColorToggle = isOn;

UpdateMapData(false);
UpdateMapData(false);
}

public void ToggleTerminator(bool isOn)
Expand All @@ -1336,7 +1375,7 @@ public void ToggleTerminator(bool isOn)

zoomInterface.TerminatorToggle = isOn;

UpdateMapData(false);
UpdateMapData(false);
}

public void ToggleOrbit(bool isOn)
Expand Down Expand Up @@ -1376,7 +1415,10 @@ public void ToggleResource(bool isOn)

zoomInterface.ResourceToggle = isOn;

UpdateMapData(false);
if (m_ResourceBar != null)
m_ResourceBar.SetActive(isOn);

UpdateMapData(false);
}

public void SyncVessel()
Expand Down
127 changes: 118 additions & 9 deletions SCANsat/SCAN_Unity/SCAN_UI_ZoomMap.cs
Expand Up @@ -53,6 +53,8 @@ public class SCAN_UI_ZoomMap : ISCAN_ZoomMap
private float terrainMin;
private float terrainMax;

private float resourceMin;
private float resourceMax;

private SCANresourceGlobal currentResource;
private List<SCANresourceGlobal> resources;
Expand All @@ -63,6 +65,9 @@ public class SCAN_UI_ZoomMap : ISCAN_ZoomMap

private SCAN_ZoomMap uiElement;

private Texture2D resourceLegend;
private const int RESOURCELEGENDWIDTH = 90;

private const int orbitSteps = 80;
private const float minZoom = 2;
private const float maxZoom = 1000;
Expand Down Expand Up @@ -381,9 +386,11 @@ protected void calcTerrainLimits()
int w = spotmap.MapWidth / 4;
int h = spotmap.MapHeight / 4;

resourceMax = 0;
resourceMin = 100;

terrainMax = -200000;
terrainMin = 100000;
float terrain = 0;

for (int i = 0; i < spotmap.MapHeight; i += 4)
{
Expand All @@ -395,10 +402,25 @@ protected void calcTerrainLimits()
lat = spotmap.unprojectLatitude(lo, la);
lon = spotmap.unprojectLongitude(lo, la);

if (lon < -180 || lon >= 180 || lat < -90 && lat >= 90 || double.IsNaN(lon) || double.IsNaN(lat))
if (double.IsNaN(lon) || double.IsNaN(lat) || lon < -180 || lon >= 180 || lat < -90 && lat >= 90)
continue;

terrain = (float)SCANUtil.getElevation(body, lon, lat);
float terrain = (float)SCANUtil.getElevation(body, lon, lat);

if (currentResource != null)
{
float resource = SCANUtil.ResourceOverlay(lat, lon, currentResource.Name, body, false) * 100f;

if (resource < resourceMin)
resourceMin = resource;
if (resource > resourceMax)
resourceMax = resource;
}
else
{
resourceMax = 100;
resourceMin = 0;
}

if (terrain < terrainMin)
terrainMin = terrain;
Expand All @@ -413,7 +435,22 @@ protected void calcTerrainLimits()
if (terrainMin == terrainMax)
terrainMin = terrainMax - 1f;

spotmap.setCustomRange(terrainMin, terrainMax);
if (currentResource != null && currentResource.CurrentBody != null && resourceMin < currentResource.CurrentBody.MinValue)
resourceMin = currentResource.CurrentBody.MinValue;

if (resourceMin >= resourceMax)
resourceMax = resourceMin + 1f;

if (resourceMin < 0)
resourceMin = 0;

if (currentResource != null && currentResource.CurrentBody != null && resourceMax > currentResource.CurrentBody.MaxValue)
resourceMax = currentResource.CurrentBody.MaxValue;

if (resourceMin >= resourceMax)
resourceMin = resourceMax - 1f;

spotmap.setCustomRange(terrainMin, terrainMax, resourceMin, resourceMax);
}

private void checkForScanners()
Expand Down Expand Up @@ -544,6 +581,12 @@ public void OnDestroy()
uiElement.gameObject.SetActive(false);
MonoBehaviour.Destroy(uiElement.gameObject);
}

if (resourceLegend != null)
{
GameObject.Destroy(resourceLegend);
resourceLegend = null;
}
}

public void SetScale(float scale)
Expand Down Expand Up @@ -1463,7 +1506,40 @@ public IList<string> MapTypes

public IList<string> Resources
{
get { return new List<string>(resources.Select(r => r.DisplayName)); }
get
{
List<string> rList = new List<string>();

bool threshold;

if (!SCAN_Settings_Config.Instance.HideZeroResources)
threshold = SCANUtil.getCoveragePercentage(data, SCANtype.ResourceLoRes) > (SCAN_Settings_Config.Instance.StockTreshold * 100) || SCANUtil.getCoveragePercentage(data, SCANtype.ResourceHiRes) > (SCAN_Settings_Config.Instance.StockTreshold * 100);
else
threshold = true;

for (int i = 0; i < resources.Count; i++)
{
SCANresourceGlobal res = resources[i];

if (threshold)
{
SCANresourceBody resBody = res.getBodyConfig(body.bodyName);

if (resBody != null)
{
if (resBody.DefaultZero)
continue;
}
else if (res.DefaultZero)
continue;
}

rList.Add(res.DisplayName);
}

return rList;
}
//get { return new List<string>(resources.Select(r => r.DisplayName)); }
}

public IList<string> LegendLabels
Expand Down Expand Up @@ -1558,6 +1634,36 @@ public Vector2 MapPosition(double lat, double lon)

return new Vector2((float)Lon, (float)Lat);
}

public Texture2D ResourceLegendImage
{
get
{
if (resourceLegend == null)
resourceLegend = new Texture2D(RESOURCELEGENDWIDTH, 1, TextureFormat.RGB24, false);

if (currentResource == null)
return null;

Color32[] pix = new Color32[RESOURCELEGENDWIDTH];

for (int i = 0; i < RESOURCELEGENDWIDTH; i++)
{
float val = (i * 1f) / (RESOURCELEGENDWIDTH * 1f);
pix[i] = palette.lerp(currentResource.MinColor32, currentResource.MaxColor32, val);
}

resourceLegend.SetPixels32(pix);
resourceLegend.Apply();

return resourceLegend;
}
}

public Vector2 ResourceLegendLabels
{
get { return new Vector2(resourceMin / 100f, resourceMax / 100f); }
}

public Dictionary<string, MapLabelInfo> OrbitLabelList
{
Expand Down Expand Up @@ -1789,7 +1895,7 @@ public void ClampToScreen(RectTransform rect)

public string MapInfo(Vector2 mapPos)
{
Vector2d pos = MousePosition(mapPos);
Vector2d pos = MousePosition(mapPos);

double mlon = pos.x;
double mlat = pos.y;
Expand Down Expand Up @@ -1827,8 +1933,11 @@ private string mouseOverInfo(double lon, double lat)

if (SCANUtil.isCovered(lon, lat, data, SCANtype.Biome))
{
SCANUtil.getBiomeDisplayName(infoString, body, lon, lat);
infoString.Append(" ");
if (body.BiomeMap != null)
{
SCANUtil.getBiomeDisplayName(infoString, body, lon, lat);
infoString.Append(" ");
}
}

if (SCANconfigLoader.GlobalResource && spotmap.Resource != null)
Expand Down Expand Up @@ -1982,7 +2091,7 @@ public void RefreshMap()
{
resetMap();

uiElement.SetLegend(LegendToggle);
uiElement.SetLegends(LegendToggle);
}

public void VesselSync()
Expand Down

0 comments on commit 2543f75

Please sign in to comment.