Skip to content

Commit 74a99b4

Browse files
committed
added scaling to resources
1 parent 18175de commit 74a99b4

File tree

10 files changed

+182
-109
lines changed

10 files changed

+182
-109
lines changed
2 KB
Binary file not shown.

GameData/MapResourceOverlay/MapResourceOverlay.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"URL":"https://raw.githubusercontent.com/Cyrik/MapResourceOverlay/master/GameData/MapResourceOverlay/MapResourceOverlay.version",
44
"DOWNLOAD":"https://github.com/Cyrik/MapResourceOverlay/releases",
55
"CHANGE_LOG_URL":"",
6-
"VERSION":{"MAJOR":0,"MINOR":2,"PATCH":3,"BUILD":0},
6+
"VERSION":{"MAJOR":0,"MINOR":2,"PATCH":4,"BUILD":0},
77
"KSP_VERSION":{"MAJOR":0,"MINOR":24,"PATCH":2}
88
}

GameData/MapResourceOverlay/PluginData/MapResourceOverlay/MapResourceOverlay.cfg

Lines changed: 0 additions & 56 deletions
This file was deleted.

Source/MapResourceOverlay/Extensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public static void Log(this Object obj, string msg)
1717
Debug.Log("[MRO]["+obj.GetType()+"][" + (new StackTrace()).GetFrame(1).GetMethod().Name + "] " + msg);
1818
}
1919

20+
public static bool IsAvailableWhileFixed(this ScienceExperiment experiment, ExperimentSituations situations, CelestialBody body)
21+
{
22+
return experiment.IsAvailableWhile(situations, body) &&
23+
!(experiment.experimentTitle == "Sample" &&
24+
!(situations == ExperimentSituations.SrfLanded || situations == ExperimentSituations.SrfSplashed));
25+
}
26+
2027
public static CelestialBody GetTargetBody(this MapObject target)
2128
{
2229
if (target.type == MapObject.MapObjectType.CELESTIALBODY)

Source/MapResourceOverlay/IOverlayProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IOverlayProvider :IConfigNode
1717
event EventHandler RedrawRequired;
1818
void DrawGui(MapOverlayGui gui);
1919
bool CanActivate();
20+
void BodyChanged(CelestialBody body);
2021
}
2122

2223
public abstract class OverlayProviderBase : IOverlayProvider
@@ -79,6 +80,11 @@ public virtual bool CanActivate()
7980
{
8081
return true;
8182
}
83+
84+
public virtual void BodyChanged(CelestialBody body)
85+
{
86+
_body = body;
87+
}
8288
}
8389

8490
public class OverlayTooltip

Source/MapResourceOverlay/MapOverlay.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public override void OnAwake()
131131
GameEvents.onHideUI.Add(MakeInvisible);
132132
GameEvents.onShowUI.Add(MakeVisible);
133133
_overlayProviders = new List<IOverlayProvider>();
134-
134+
_targetBody = MapView.MapCamera.target.GetTargetBody();
135135
}
136136

137137
public void ToggleGui()
@@ -241,6 +241,7 @@ private void UpdateMapView()
241241
{
242242
this.Log("Drawing at " + _targetBody.name + " because " +
243243
(_targetBody != _body ? "body changed." : "something else changed."));
244+
OverlayProvider.BodyChanged(_targetBody);
244245
_changed = false;
245246
var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
246247
var radii = System.IO.File.ReadAllLines(dir + "/Assets/Radii.cfg");
@@ -496,18 +497,16 @@ public override void OnLoad(ConfigNode node)
496497
{
497498
return Activator.CreateInstance(x) as IOverlayProvider;
498499
}
499-
catch (Exception)
500+
catch (Exception e)
500501
{
501-
this.Log("Couldnt instantiate: "+x.FullName);
502+
this.Log("Couldnt instantiate: "+x.FullName+"\n"+e);
502503
return null;
503504
}
504505
})
505506
.Where(x => x != null)
506507
.ToList();
507508

508-
this.Log("from " + System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/Base.cfg");
509509
LoadConfig(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/Base.cfg");
510-
this.Log("from" + IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg"));
511510
var globalSavedConfigFilename = IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg");
512511
LoadConfig(globalSavedConfigFilename);
513512
_overlayProviders = _overlayProviders.Where(x => x.CanActivate()).ToList();
@@ -534,6 +533,7 @@ private void LoadConfig(string filename)
534533
{
535534
try
536535
{
536+
overlayProvider.BodyChanged(_targetBody);
537537
overlayProvider.Load(globalNode);
538538
}
539539
catch (Exception e)

Source/MapResourceOverlay/MapOverlayGui.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,7 @@ protected override void DrawWindowContents(int windowId)
9090
}
9191
}
9292
GUILayout.Box("Active Type Specific:");
93-
if (Model.OverlayProvider.GetType() == typeof(ResourceOverlayProvider))
94-
{
95-
var provider = (ResourceOverlayProvider)Model.OverlayProvider;
96-
97-
foreach (var res in provider.ColorConfigs)
98-
{
99-
if (GUILayout.Button(res.Resource.ResourceName))
100-
{
101-
provider.ActiveResource = res;
102-
Model.Reload();
103-
}
104-
}
105-
}
106-
else
107-
{
108-
Model.OverlayProvider.DrawGui(this);
109-
}
93+
Model.OverlayProvider.DrawGui(this);
11094
GUILayout.EndVertical();
11195
}
11296
}

Source/MapResourceOverlay/ResourceOverlayProvider.cs

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.Reflection.Emit;
7+
using LibNoise.Unity.Operator;
78
using UnityEngine;
89
using Object = System.Object;
910

@@ -12,24 +13,56 @@ namespace MapResourceOverlay
1213
public class ResourceOverlayProvider : OverlayProviderBase
1314
{
1415
private ResourceConfig _activeResource;
16+
private double _displayMax;
1517

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

1921
private Func<object,object> _getAmount;
2022

2123
private GetResourceAvailabilityByRealResourceNameDelegate _getResourceAvailabilityByRealResourceName;
24+
private bool _logaritmic;
25+
private bool _coloredScale;
2226

2327
public ResourceConfig ActiveResource
2428
{
2529
get { return _activeResource; }
2630
set
2731
{
2832
_activeResource = value;
33+
CalculateBase();
2934
RequiresRedraw();
3035
}
3136
}
3237

38+
public override void Activate(CelestialBody body)
39+
{
40+
base.Activate(body);
41+
CalculateBase();
42+
RequiresRedraw();
43+
}
44+
45+
private void CalculateBase()
46+
{
47+
_displayMax = 0;
48+
double avg = 0;
49+
for (int lat = 0; lat < 180; lat++)
50+
{
51+
for (int lon = 0; lon < 360; lon++)
52+
{
53+
var amount = (double)_getAmount(_getResourceAvailabilityByRealResourceName(_body.flightGlobalsIndex,
54+
ActiveResource.Resource.ResourceName, lon, lat));
55+
if (amount > _displayMax)
56+
{
57+
_displayMax = amount;
58+
}
59+
avg += amount;
60+
}
61+
}
62+
_displayMax *= 1000000;
63+
avg = avg/(180*360);
64+
}
65+
3366
public override Color32 CalculateColor32(double latitude, double longitude, CelestialBody body, bool useScansat,
3467
bool bright, double cutoff)
3568
{
@@ -40,10 +73,23 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
4073
}
4174
var avail = _getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex,
4275
ActiveResource.Resource.ResourceName, latitude, longitude);
43-
var amount = (double)_getAmount(avail);
44-
amount = amount*1000000;
76+
var amount = (double)_getAmount(avail) * 1000000;
4577
if (amount > cutoff)
4678
{
79+
if (Logaritmic)
80+
{
81+
amount = ((Math.Log(amount, 2) - (Math.Log(cutoff, 2))) * 255) / ((Math.Log(_displayMax, 2) - (Math.Log(cutoff, 2))));
82+
}
83+
else if (_coloredScale)
84+
{
85+
var color = ScanSatWrapper.heightToColor((float) amount, cutoff, (float) _displayMax);
86+
color.a = ActiveResource.HighColor.a;
87+
return color;
88+
}
89+
else
90+
{
91+
amount =((amount - cutoff) * 255) / (_displayMax - cutoff);
92+
}
4793
amount = Mathf.Clamp((float) amount, 0f, 255f);
4894
if (!bright)
4995
{
@@ -55,12 +101,25 @@ public override Color32 CalculateColor32(double latitude, double longitude, Cele
55101
}
56102
else
57103
{
58-
return new Color32(255, Convert.ToByte(amount), Convert.ToByte(amount), 150);
104+
return new Color32(155, Convert.ToByte(amount), Convert.ToByte(amount), 150);
59105
}
60106
}
61107
return ActiveResource.LowColor;
62108
}
63109

110+
public bool Logaritmic
111+
{
112+
get { return _logaritmic; }
113+
set
114+
{
115+
if (_logaritmic!= value)
116+
{
117+
_logaritmic = value;
118+
RequiresRedraw();
119+
}
120+
}
121+
}
122+
64123
public override OverlayTooltip TooltipContent(double latitude, double longitude, CelestialBody body)
65124
{
66125
var abundance = (double)_getAmount(_getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex,
@@ -81,7 +140,7 @@ public override string GuiName
81140

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

84-
public ResourceOverlayProvider()
143+
private void LoadFailsafe()
85144
{
86145
var config = new ResourceConfig
87146
{
@@ -113,12 +172,20 @@ public ResourceOverlayProvider()
113172
LowColor = new Color32(0, 0, 0, 0),
114173
HighColor = new Color32(255, 0, 255, 200)
115174
};
116-
ColorConfigs = new List<ResourceConfig> {config, config2, config3, config4, config5};
175+
ColorConfigs = new List<ResourceConfig> { config, config2, config3, config4, config5 };
117176
ActiveResource = config;
118177
}
119178

120179
public override void Load(ConfigNode node)
121180
{
181+
try
182+
{
183+
InitiateOrs();
184+
}
185+
catch (Exception e)
186+
{
187+
this.Log("Couldnt find ORS" + e);
188+
}
122189
try
123190
{
124191
if (node.HasNode("ResourceOverlay"))
@@ -135,15 +202,8 @@ public override void Load(ConfigNode node)
135202
}
136203
catch (Exception e)
137204
{
138-
this.Log("Could not load config, using default" + e);
139-
}
140-
try
141-
{
142-
InitiateOrs();
143-
}
144-
catch (Exception e)
145-
{
146-
this.Log("Couldnt find ORS" + e);
205+
this.Log("Could not load config, using default, because " + e);
206+
LoadFailsafe();
147207
}
148208
}
149209

@@ -200,5 +260,37 @@ static Func<object, object> GenerateFunc(MethodInfo method)
200260
instance
201261
).Compile();
202262
}
263+
264+
public override void DrawGui(MapOverlayGui gui)
265+
{
266+
base.DrawGui(gui);
267+
GUILayout.BeginVertical();
268+
Logaritmic = GUILayout.Toggle(Logaritmic, "Logarithmic Scale");
269+
ColoredScale = GUILayout.Toggle(ColoredScale, "Colored Scale");
270+
GUILayout.Space(15);
271+
foreach (var res in ColorConfigs)
272+
{
273+
if (GUILayout.Button(res.Resource.ResourceName))
274+
{
275+
ActiveResource = res;
276+
}
277+
}
278+
GUILayout.EndVertical();
279+
}
280+
281+
public bool ColoredScale
282+
{
283+
get { return _coloredScale; }
284+
set
285+
{
286+
if (_coloredScale != value)
287+
{
288+
_coloredScale = value;
289+
RequiresRedraw();
290+
}
291+
292+
293+
}
294+
}
203295
}
204296
}

0 commit comments

Comments
 (0)