Skip to content

Commit

Permalink
science map alpha and overlay api alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrik committed Aug 31, 2014
1 parent 5b4863f commit 7d44033
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 18 deletions.
35 changes: 31 additions & 4 deletions Source/MapResourceOverlay/IOverlayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,48 @@ public interface IOverlayProvider
{
Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, ResourceConfig config, bool useScansat, bool bright, double cutoff);
OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body, ResourceConfig config);
bool IsCoveredAt(double latitude, double longitude, CelestialBody body, ResourceConfig config);
string GuiName { get; }
}

public class OverlayTooltip
{
public OverlayTooltip(string title, GUIContent content)
public OverlayTooltip(string title, GUIContent content, Vector2 size = new Vector2())
{
Title = title;
Content = content;
Size = size;
}

public string Title { get; set; }
public GUIContent Content { get; set; }
public Vector2 Size { get; set; }
}

class HightmapProvider : IOverlayProvider
{
public Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, ResourceConfig config, bool useScansat,
bool bright, double cutoff)
{
if (useScansat && ScanSatWrapper.Instance.Active() && !IsCoveredAt(latitude, longitude, body, config))
{
return new Color32(0, 0, 0, 0);
}
var scanSat = ScanSatWrapper.Instance;
var dict = new Dictionary<string, int>();
return scanSat.GetElevationColor32(body, longitude, latitude);
}

public OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
var scanSat = ScanSatWrapper.Instance;
return new OverlayTooltip("",new GUIContent("Height: "+ScanSatWrapper.GetElevation(body, longitude, latitude)+"m"));
}

public bool IsCoveredAt(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
return ScanSatWrapper.Instance.IsCovered(longitude, latitude, body, "AltimetryHiRes");
}

public string GuiName { get { return "Hight Map"; } }
}

Expand All @@ -50,15 +61,19 @@ class BiomeOverlayProvider : IOverlayProvider
public Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, ResourceConfig config, bool useScansat,
bool bright, double cutoff)
{
if (useScansat && ScanSatWrapper.Instance.Active() && !IsCoveredAt(latitude, longitude, body, config))
{
return new Color32(0,0,0,0);
}
var scanSat = ScanSatWrapper.Instance;
var biome = scanSat.GetBiome(longitude, latitude, body);
if (biome != null)
{
var color = biome.mapColor;
color.a = 0.20f;
color.a = 0.30f;
return color;
}
return new Color32();
return new Color32(0, 0, 0, 0);
}

public OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body, ResourceConfig config)
Expand All @@ -67,6 +82,12 @@ public OverlayTooltip TooltipContent(double latitude, double longitude, Celestia
var biome = scanSat.GetBiome(longitude, latitude, body);
return new OverlayTooltip("",new GUIContent("Biome: "+biome.name));
}

public bool IsCoveredAt(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
return ScanSatWrapper.Instance.IsCovered(longitude, latitude, body, "Biome");
}

public string GuiName { get { return "Biome Map"; } }
}

Expand Down Expand Up @@ -107,6 +128,12 @@ public OverlayTooltip TooltipContent(double latitude, double longitude, Celestia
.getAmount();
return new OverlayTooltip(config.Resource.ResourceName,new GUIContent("Amount: "+(abundance * 1000000.0).ToString("0.0") + "ppm"));
}

public bool IsCoveredAt(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
return ScanSatWrapper.Instance.IsCovered(longitude, latitude, body, config.Resource);
}

public string GuiName { get { return "Resource Map"; } }

}
Expand Down
28 changes: 16 additions & 12 deletions Source/MapResourceOverlay/MapOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MapOverlay : ScenarioModule
private IButton _mapOverlayButton;
private MapOverlayGui _gui;
private bool _changed;
private ResourceConfig _selectedResourceName;
private ResourceConfig _selectedResource;
private Coordinates _mouseCoords;
private CelestialBody _targetBody;

Expand Down Expand Up @@ -162,7 +162,7 @@ public void Start()
{
this.Log("MapResourceOverlay starting");
gameObject.layer = 10;
SelectedResourceName = Resources[0];
SelectedResource = Resources[0];
var type = typeof(IOverlayProvider);
_overlayProviders = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetLoadableTypes().Where(x => type.IsAssignableFrom(x) && !x.IsInterface))
Expand All @@ -182,12 +182,12 @@ public List<ResourceConfig> Resources
get { return _resources; }
}

public ResourceConfig SelectedResourceName
public ResourceConfig SelectedResource
{
get { return _selectedResourceName; }
get { return _selectedResource; }
set
{
_selectedResourceName = value;
_selectedResource = value;
_changed = true;
}
}
Expand Down Expand Up @@ -237,7 +237,7 @@ private void UpdateMapView()
var radii = System.IO.File.ReadAllLines(dir + "/Assets/Radii.cfg");
var radius = float.Parse(radii.First(x => x.StartsWith(_targetBody.GetName())).Split('=')[1]);
_body = _targetBody;
CreateMesh(_targetBody, SelectedResourceName);
CreateMesh(_targetBody, SelectedResource);
gameObject.renderer.material = new Material(System.IO.File.ReadAllText(dir + "/Assets/MapOverlayShader.txt"));
gameObject.renderer.enabled = true;
gameObject.renderer.castShadows = false;
Expand All @@ -249,7 +249,7 @@ private void UpdateMapView()
}
if (_targetBody != null && useScansat && _scanSat.Active())
{
RecalculateColors(_targetBody, SelectedResourceName);
RecalculateColors(_targetBody, SelectedResource);
}
}
}
Expand Down Expand Up @@ -305,7 +305,7 @@ public void OnGUI()
_mouseCoords = _targetBody.GetMouseCoordinates();
_mouse = Event.current.mousePosition;
if (useScansat && _scanSat.Active() && _mouseCoords != null &&
!_scanSat.IsCovered(_mouseCoords.Longitude, _mouseCoords.Latitude, _targetBody,_selectedResourceName.Resource))
!OverlayProvider.IsCoveredAt(_mouseCoords.Longitude, _mouseCoords.Latitude, _targetBody,_selectedResource))
{
_mouseCoords = null;
}
Expand All @@ -320,12 +320,16 @@ public void OnGUI()

_toolTipId = 0;
var overlayTooltip = OverlayProvider.TooltipContent(_mouseCoords.Latitude, _mouseCoords.Longitude,
_targetBody, SelectedResourceName);

GUI.Window(_toolTipId, new Rect(_mouse.x + 10, _mouse.y + 10, 200f, 55f), i =>
_targetBody, SelectedResource);
if (Math.Abs(overlayTooltip.Size.x) < 0.01 && Math.Abs(overlayTooltip.Size.y) < 0.01)
{
overlayTooltip.Size = new Vector2(200f,55f);
}
var style = new GUIStyle(GUI.skin.label) {wordWrap = true};
GUI.Window(_toolTipId, new Rect(_mouse.x + 10, _mouse.y + 10, overlayTooltip.Size.x, overlayTooltip.Size.y), i =>
{
GUI.Label(new Rect(5, 10, 190, 20), "Long: " + _mouseCoords.Longitude.ToString("###.##") + " Lat: " + _mouseCoords.Latitude.ToString("####.##"));
GUI.Label(new Rect(5, 30, 190, 20), overlayTooltip.Content);
GUI.Label(new Rect(5, 30, overlayTooltip.Size.x - 10, overlayTooltip.Size.y - 35), overlayTooltip.Content,style);

},
overlayTooltip.Title);
Expand Down
2 changes: 1 addition & 1 deletion Source/MapResourceOverlay/MapOverlayGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected override void DrawWindowContents(int windowId)
{
if (GUILayout.Button(res.Resource.ResourceName))
{
Model.SelectedResourceName = res;
Model.SelectedResource = res;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Source/MapResourceOverlay/MapResourceOverlay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="ResourceConfig.cs" />
<Compile Include="ScanSatWrapper.cs" />
<Compile Include="ScenarioAdder.cs" />
<Compile Include="ScienceOverlayProvider.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="Window.cs" />
</ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion Source/MapResourceOverlay/ScanSatWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ public static Color heightToColor(float val, int scheme)
// (these are collected here for the same reason)



public bool IsCovered(double longitude, double latitude, CelestialBody body,string str)
{
if (!Active())
{
return false;
}
return _scansatIsCoveredDelegate(longitude, latitude, body, GetScansatId(str));
}
}
}
71 changes: 71 additions & 0 deletions Source/MapResourceOverlay/ScienceOverlayProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Linq;
using UnityEngine;

namespace MapResourceOverlay
{
class ScienceOverlayProvider : IOverlayProvider
{
public Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, ResourceConfig config, bool useScansat,
bool bright, double cutoff)
{
var situation = ExperimentSituations.InSpaceLow;
var biome = ScanSatWrapper.Instance.GetBiome(longitude, latitude, body);
var experiments = ResearchAndDevelopment.GetExperimentIDs().Select(ResearchAndDevelopment.GetExperiment)
.Where(x => x.biomeMask != 0 && x.situationMask != 0)
.Where(x => x.IsAvailableWhile(situation,body))
.GroupBy(x => x.BiomeIsRelevantWhile(situation))
.SelectMany(x => x.Select(y => new{exp = y, subj = ResearchAndDevelopment.GetExperimentSubject(y,situation,body,x.Key ? biome.name:"")}))
.Select(x => ResearchAndDevelopment.GetScienceValue(x.exp.dataScale * x.exp.baseValue,x.subj))
.Sum();
return new Color32(Convert.ToByte(Mathf.Clamp(experiments,0,255)),0,0,150);

}

public OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
var biome = ScanSatWrapper.Instance.GetBiome(longitude, latitude, body);
var test = Enum.GetValues(typeof(ExperimentSituations)).Cast<ExperimentSituations>()
.Select(situation => ResearchAndDevelopment.GetExperimentIDs().Select(ResearchAndDevelopment.GetExperiment)
.Where(x => x.biomeMask != 0 && x.situationMask != 0)
.Where(x => x.IsAvailableWhile(situation, body))
.GroupBy(x => x.BiomeIsRelevantWhile(situation))
.SelectMany(x => x.Select(y => new { exp = y, subj = ResearchAndDevelopment.GetExperimentSubject(y, situation, body, x.Key ? biome.name : "") }))
.Select(x => new {value =ResearchAndDevelopment.GetScienceValue(x.exp.dataScale * x.exp.baseValue, x.subj), exp = x})
.Aggregate(new {str = "", total = 0f},
(a,x) => new {str = a.str+" n: "+x.exp.exp.experimentTitle+" v: "+x.value, total = a.total + x.value},
result => "Situation "+Enum.GetName(typeof(ExperimentSituations),situation)+"total: "+result.total+result.str))
.Aggregate("", (str,x) => str+x+"\n");

return new OverlayTooltip(biome.name,new GUIContent(test), new Vector2(500,500));
}

public bool IsCoveredAt(double latitude, double longitude, CelestialBody body, ResourceConfig config)
{
return ScanSatWrapper.Instance.IsCovered(longitude, latitude, body, "AltimetryHiRes");
}

public string GuiName { get { return "Science Map"; } }
private float GetBodyScienceValueMultipler(ExperimentSituations situations,CelestialBody body)
{
switch (situations)
{
case ExperimentSituations.FlyingHigh:
return body.scienceValues.FlyingHighDataValue;
case ExperimentSituations.FlyingLow:
return body.scienceValues.FlyingLowDataValue;
case ExperimentSituations.InSpaceHigh:
return body.scienceValues.InSpaceHighDataValue;
case ExperimentSituations.InSpaceLow:
return body.scienceValues.InSpaceLowDataValue;
case ExperimentSituations.SrfLanded:
return body.scienceValues.LandedDataValue;
case ExperimentSituations.SrfSplashed:
return body.scienceValues.SplashedDataValue;
default:
return 0f;
}

}
}
}

0 comments on commit 7d44033

Please sign in to comment.