Skip to content

Commit

Permalink
added scaling to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrik committed Sep 2, 2014
1 parent 18175de commit 74a99b4
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 109 deletions.
Binary file modified GameData/MapResourceOverlay/MapResourceOverlay.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion GameData/MapResourceOverlay/MapResourceOverlay.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"URL":"https://raw.githubusercontent.com/Cyrik/MapResourceOverlay/master/GameData/MapResourceOverlay/MapResourceOverlay.version",
"DOWNLOAD":"https://github.com/Cyrik/MapResourceOverlay/releases",
"CHANGE_LOG_URL":"",
"VERSION":{"MAJOR":0,"MINOR":2,"PATCH":3,"BUILD":0},
"VERSION":{"MAJOR":0,"MINOR":2,"PATCH":4,"BUILD":0},
"KSP_VERSION":{"MAJOR":0,"MINOR":24,"PATCH":2}
}

This file was deleted.

7 changes: 7 additions & 0 deletions Source/MapResourceOverlay/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public static void Log(this Object obj, string msg)
Debug.Log("[MRO]["+obj.GetType()+"][" + (new StackTrace()).GetFrame(1).GetMethod().Name + "] " + msg);
}

public static bool IsAvailableWhileFixed(this ScienceExperiment experiment, ExperimentSituations situations, CelestialBody body)
{
return experiment.IsAvailableWhile(situations, body) &&
!(experiment.experimentTitle == "Sample" &&
!(situations == ExperimentSituations.SrfLanded || situations == ExperimentSituations.SrfSplashed));
}

public static CelestialBody GetTargetBody(this MapObject target)
{
if (target.type == MapObject.MapObjectType.CELESTIALBODY)
Expand Down
6 changes: 6 additions & 0 deletions Source/MapResourceOverlay/IOverlayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IOverlayProvider :IConfigNode
event EventHandler RedrawRequired;
void DrawGui(MapOverlayGui gui);
bool CanActivate();
void BodyChanged(CelestialBody body);
}

public abstract class OverlayProviderBase : IOverlayProvider
Expand Down Expand Up @@ -79,6 +80,11 @@ public virtual bool CanActivate()
{
return true;
}

public virtual void BodyChanged(CelestialBody body)
{
_body = body;
}
}

public class OverlayTooltip
Expand Down
10 changes: 5 additions & 5 deletions Source/MapResourceOverlay/MapOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override void OnAwake()
GameEvents.onHideUI.Add(MakeInvisible);
GameEvents.onShowUI.Add(MakeVisible);
_overlayProviders = new List<IOverlayProvider>();

_targetBody = MapView.MapCamera.target.GetTargetBody();
}

public void ToggleGui()
Expand Down Expand Up @@ -241,6 +241,7 @@ private void UpdateMapView()
{
this.Log("Drawing at " + _targetBody.name + " because " +
(_targetBody != _body ? "body changed." : "something else changed."));
OverlayProvider.BodyChanged(_targetBody);
_changed = false;
var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var radii = System.IO.File.ReadAllLines(dir + "/Assets/Radii.cfg");
Expand Down Expand Up @@ -496,18 +497,16 @@ public override void OnLoad(ConfigNode node)
{
return Activator.CreateInstance(x) as IOverlayProvider;
}
catch (Exception)
catch (Exception e)
{
this.Log("Couldnt instantiate: "+x.FullName);
this.Log("Couldnt instantiate: "+x.FullName+"\n"+e);
return null;
}
})
.Where(x => x != null)
.ToList();

this.Log("from " + System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/Base.cfg");
LoadConfig(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/Base.cfg");
this.Log("from" + IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg"));
var globalSavedConfigFilename = IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg");
LoadConfig(globalSavedConfigFilename);
_overlayProviders = _overlayProviders.Where(x => x.CanActivate()).ToList();
Expand All @@ -534,6 +533,7 @@ private void LoadConfig(string filename)
{
try
{
overlayProvider.BodyChanged(_targetBody);
overlayProvider.Load(globalNode);
}
catch (Exception e)
Expand Down
18 changes: 1 addition & 17 deletions Source/MapResourceOverlay/MapOverlayGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,7 @@ protected override void DrawWindowContents(int windowId)
}
}
GUILayout.Box("Active Type Specific:");
if (Model.OverlayProvider.GetType() == typeof(ResourceOverlayProvider))
{
var provider = (ResourceOverlayProvider)Model.OverlayProvider;

foreach (var res in provider.ColorConfigs)
{
if (GUILayout.Button(res.Resource.ResourceName))
{
provider.ActiveResource = res;
Model.Reload();
}
}
}
else
{
Model.OverlayProvider.DrawGui(this);
}
Model.OverlayProvider.DrawGui(this);
GUILayout.EndVertical();
}
}
Expand Down
120 changes: 106 additions & 14 deletions Source/MapResourceOverlay/ResourceOverlayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using LibNoise.Unity.Operator;
using UnityEngine;
using Object = System.Object;

Expand All @@ -12,24 +13,56 @@ namespace MapResourceOverlay
public class ResourceOverlayProvider : OverlayProviderBase
{
private ResourceConfig _activeResource;
private double _displayMax;

private delegate Object GetResourceAvailabilityByRealResourceNameDelegate(
int bodyIndex, string resourceName, double lon, double lat);

private Func<object,object> _getAmount;

private GetResourceAvailabilityByRealResourceNameDelegate _getResourceAvailabilityByRealResourceName;
private bool _logaritmic;
private bool _coloredScale;

public ResourceConfig ActiveResource
{
get { return _activeResource; }
set
{
_activeResource = value;
CalculateBase();
RequiresRedraw();
}
}

public override void Activate(CelestialBody body)
{
base.Activate(body);
CalculateBase();
RequiresRedraw();
}

private void CalculateBase()
{
_displayMax = 0;
double avg = 0;
for (int lat = 0; lat < 180; lat++)
{
for (int lon = 0; lon < 360; lon++)
{
var amount = (double)_getAmount(_getResourceAvailabilityByRealResourceName(_body.flightGlobalsIndex,
ActiveResource.Resource.ResourceName, lon, lat));
if (amount > _displayMax)
{
_displayMax = amount;
}
avg += amount;
}
}
_displayMax *= 1000000;
avg = avg/(180*360);
}

public override Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, bool useScansat,
bool bright, double cutoff)
{
Expand All @@ -40,10 +73,23 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
}
var avail = _getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex,
ActiveResource.Resource.ResourceName, latitude, longitude);
var amount = (double)_getAmount(avail);
amount = amount*1000000;
var amount = (double)_getAmount(avail) * 1000000;
if (amount > cutoff)
{
if (Logaritmic)
{
amount = ((Math.Log(amount, 2) - (Math.Log(cutoff, 2))) * 255) / ((Math.Log(_displayMax, 2) - (Math.Log(cutoff, 2))));
}
else if (_coloredScale)
{
var color = ScanSatWrapper.heightToColor((float) amount, cutoff, (float) _displayMax);
color.a = ActiveResource.HighColor.a;
return color;
}
else
{
amount =((amount - cutoff) * 255) / (_displayMax - cutoff);
}
amount = Mathf.Clamp((float) amount, 0f, 255f);
if (!bright)
{
Expand All @@ -55,12 +101,25 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
}
else
{
return new Color32(255, Convert.ToByte(amount), Convert.ToByte(amount), 150);
return new Color32(155, Convert.ToByte(amount), Convert.ToByte(amount), 150);
}
}
return ActiveResource.LowColor;
}

public bool Logaritmic
{
get { return _logaritmic; }
set
{
if (_logaritmic!= value)
{
_logaritmic = value;
RequiresRedraw();
}
}
}

public override OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body)
{
var abundance = (double)_getAmount(_getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex,
Expand All @@ -81,7 +140,7 @@ public override string GuiName

public List<ResourceConfig> ColorConfigs { get; set; }

public ResourceOverlayProvider()
private void LoadFailsafe()
{
var config = new ResourceConfig
{
Expand Down Expand Up @@ -113,12 +172,20 @@ public ResourceOverlayProvider()
LowColor = new Color32(0, 0, 0, 0),
HighColor = new Color32(255, 0, 255, 200)
};
ColorConfigs = new List<ResourceConfig> {config, config2, config3, config4, config5};
ColorConfigs = new List<ResourceConfig> { config, config2, config3, config4, config5 };
ActiveResource = config;
}

public override void Load(ConfigNode node)
{
try
{
InitiateOrs();
}
catch (Exception e)
{
this.Log("Couldnt find ORS" + e);
}
try
{
if (node.HasNode("ResourceOverlay"))
Expand All @@ -135,15 +202,8 @@ public override void Load(ConfigNode node)
}
catch (Exception e)
{
this.Log("Could not load config, using default" + e);
}
try
{
InitiateOrs();
}
catch (Exception e)
{
this.Log("Couldnt find ORS" + e);
this.Log("Could not load config, using default, because " + e);
LoadFailsafe();
}
}

Expand Down Expand Up @@ -200,5 +260,37 @@ static Func<object, object> GenerateFunc(MethodInfo method)
instance
).Compile();
}

public override void DrawGui(MapOverlayGui gui)
{
base.DrawGui(gui);
GUILayout.BeginVertical();
Logaritmic = GUILayout.Toggle(Logaritmic, "Logarithmic Scale");
ColoredScale = GUILayout.Toggle(ColoredScale, "Colored Scale");
GUILayout.Space(15);
foreach (var res in ColorConfigs)
{
if (GUILayout.Button(res.Resource.ResourceName))
{
ActiveResource = res;
}
}
GUILayout.EndVertical();
}

public bool ColoredScale
{
get { return _coloredScale; }
set
{
if (_coloredScale != value)
{
_coloredScale = value;
RequiresRedraw();
}


}
}
}
}
Loading

0 comments on commit 74a99b4

Please sign in to comment.