Skip to content

Commit 0e2c41f

Browse files
committed
fixed tooltip while pause, added color config
1 parent 5bd3a9a commit 0e2c41f

File tree

3 files changed

+191
-32
lines changed

3 files changed

+191
-32
lines changed

Source/MapResourceOverlay/MapOverlay.cs

Lines changed: 171 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@ public class MapOverlay : ScenarioModule
1515
private CelestialBody _body;
1616
private Mesh _mesh;
1717

18-
private readonly List<Resource> _resources = new List<Resource>
19-
{
20-
new Resource("Karbonite"),
21-
new Resource("Minerals"),
22-
new Resource("Ore"),
23-
new Resource("Substrate"),
24-
new Resource("Water","Aquifer")
25-
};
18+
private List<ResourceConfig> _resources;
2619

2720
private IButton _mapOverlayButton;
2821
private MapOverlayGui _gui;
2922
private bool _changed;
30-
private Resource _selectedResourceName;
23+
private ResourceConfig _selectedResourceName;
3124
private Coordinates _mouseCoords;
3225
private CelestialBody _targetBody;
3326
private delegate bool IsCoveredDelegate(double lon, double lat, CelestialBody body, int mask);
@@ -48,7 +41,9 @@ public class MapOverlay : ScenarioModule
4841
[KSPField(isPersistant = true)]
4942
public bool show = true;
5043

51-
44+
private GlobalSettings _globalSettings;
45+
46+
5247
public int Cutoff
5348
{
5449
get { return cutoff; }
@@ -95,9 +90,10 @@ public bool Show
9590
}
9691
}
9792
}
98-
public MapOverlay()
93+
94+
public override void OnAwake()
9995
{
100-
this.Log("Instantiating");
96+
this.Log("Awaking");
10197
_origTransform = gameObject.transform.parent;
10298
var filter = gameObject.AddComponent<MeshFilter>();
10399
if (filter != null)
@@ -108,13 +104,9 @@ public MapOverlay()
108104
{
109105
_mesh = gameObject.GetComponent<MeshFilter>().mesh;
110106
}
111-
107+
_globalSettings = new GlobalSettings();
112108
gameObject.AddComponent<MeshRenderer>();
113-
}
114-
115-
public override void OnAwake()
116-
{
117-
this.Log("Awaking");
109+
118110
base.OnAwake();
119111
_mapOverlayButton = ToolbarManager.Instance.add("MapResourceOverlay", "ResourceOverlay");
120112
_mapOverlayButton.TexturePath = "MapResourceOverlay/Assets/MapOverlayIcon";
@@ -193,12 +185,12 @@ private static CelestialBody GetTargetBody(MapObject target)
193185
return null;
194186
}
195187

196-
public List<Resource> Resources
188+
public List<ResourceConfig> Resources
197189
{
198190
get { return _resources; }
199191
}
200192

201-
public Resource SelectedResourceName
193+
public ResourceConfig SelectedResourceName
202194
{
203195
get { return _selectedResourceName; }
204196
set
@@ -282,7 +274,7 @@ private void UpdateMapView()
282274
}
283275
}
284276

285-
private void RecalculateColors(CelestialBody targetBody, Resource resource)
277+
private void RecalculateColors(CelestialBody targetBody, ResourceConfig resource)
286278
{
287279
const int nbLong = 360;
288280
#region Vertices
@@ -308,7 +300,19 @@ private void RecalculateColors(CelestialBody targetBody, Resource resource)
308300

309301
public void OnGUI()
310302
{
311-
if (_targetBody != FlightGlobals.ActiveVessel.mainBody) //dont show tooltips on different bodys or ORS lags
303+
bool paused = false;
304+
if (HighLogic.LoadedSceneIsFlight)
305+
{
306+
try
307+
{
308+
paused = PauseMenu.isOpen || FlightResultsDialog.isDisplaying;
309+
}
310+
catch (Exception)
311+
{
312+
// ignore the error and assume the pause menu is not open
313+
}
314+
}
315+
if (_targetBody != FlightGlobals.ActiveVessel.mainBody || paused) //dont show tooltips on different bodys or ORS lags
312316
{
313317
return;
314318
}
@@ -321,7 +325,7 @@ public void OnGUI()
321325
_mouseCoords = GetMouseCoordinates(_targetBody);
322326
_mouse = Event.current.mousePosition;
323327
if (useScansat && _scansatIsCoveredDelegate != null && _mouseCoords != null &&
324-
!_scansatIsCoveredDelegate(_mouseCoords.Longitude, _mouseCoords.Latitude, _targetBody, GetScansatId(_selectedResourceName.ScansatName)))
328+
!_scansatIsCoveredDelegate(_mouseCoords.Longitude, _mouseCoords.Latitude, _targetBody, GetScansatId(_selectedResourceName.Resource.ScansatName)))
325329
{
326330
_mouseCoords = null;
327331
}
@@ -336,7 +340,7 @@ public void OnGUI()
336340

337341
_toolTipId = 0;
338342
var abundance = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(
339-
_targetBody.flightGlobalsIndex, _selectedResourceName.ResourceName, _mouseCoords.Latitude, _mouseCoords.Longitude)
343+
_targetBody.flightGlobalsIndex, _selectedResourceName.Resource.ResourceName, _mouseCoords.Latitude, _mouseCoords.Longitude)
340344
.getAmount();
341345
string abundanceString;
342346
if (abundance > 0.001)
@@ -353,7 +357,7 @@ public void OnGUI()
353357
GUI.Label(new Rect(5, 30, 190, 20), "Amount: " + abundanceString);
354358

355359
},
356-
_selectedResourceName.ResourceName);
360+
_selectedResourceName.Resource.ResourceName);
357361
}
358362
}
359363
}
@@ -402,7 +406,7 @@ public static Coordinates GetMouseCoordinates(CelestialBody targetBody)
402406
}
403407

404408

405-
private void evilmesh(CelestialBody targetBody, Resource resource)
409+
private void evilmesh(CelestialBody targetBody, ResourceConfig resource)
406410
{
407411

408412
_mesh.Clear();
@@ -507,22 +511,25 @@ private void evilmesh(CelestialBody targetBody, Resource resource)
507511
_mesh.Optimize();
508512
}
509513

510-
private Color32 CalculateColor32At(CelestialBody body, Resource resource, double latitude,
514+
private Color32 CalculateColor32At(CelestialBody body, ResourceConfig resource, double latitude,
511515
double longitude)
512516
{
513517
if (useScansat && _scansatIsCoveredDelegate != null &&
514-
!_scansatIsCoveredDelegate(longitude, latitude, body, GetScansatId(resource.ScansatName)))
518+
!_scansatIsCoveredDelegate(longitude, latitude, body, GetScansatId(resource.Resource.ScansatName)))
515519
{
516520
return new Color32(0, 0, 0, 0);
517521
}
518-
var avail = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex, resource.ResourceName, latitude, longitude);
522+
var avail = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex, resource.Resource.ResourceName, latitude, longitude);
519523
var amount = avail.getAmount();
520524
amount = Mathf.Clamp((float)amount * 1000000f, 0f, 255f);
521525
if (!bright)
522526
{
523527
if (amount > cutoff)
524528
{
525-
return new Color32(Convert.ToByte(amount), 0, 0, 150);
529+
var r = amount * (resource.HighColor.r / 255.0);
530+
var g = amount * (resource.HighColor.g / 255.0);
531+
var b = amount * (resource.HighColor.b / 255.0);
532+
return new Color32(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b), resource.HighColor.a);
526533
}
527534
}
528535
else
@@ -532,9 +539,142 @@ private Color32 CalculateColor32At(CelestialBody body, Resource resource, double
532539
return new Color32(255, Convert.ToByte(amount), Convert.ToByte(amount), 150);
533540
}
534541
}
535-
return new Color32(byte.MinValue, byte.MinValue, byte.MinValue, 0);
542+
return resource.LowColor;
543+
}
544+
545+
public override void OnLoad(ConfigNode node)
546+
{
547+
this.Log("loading");
548+
base.OnLoad(node);
549+
var globalConfigFilename = IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg");
550+
if (File.Exists<MapOverlay>(globalConfigFilename))
551+
{
552+
var globalNode = ConfigNode.Load(globalConfigFilename);
553+
_globalSettings.Load(globalNode);
554+
}
555+
_resources = _globalSettings.ColorConfigs;
556+
}
557+
558+
public override void OnSave(ConfigNode node)
559+
{
560+
this.Log("saving");
561+
base.OnSave(node);
562+
var gloablNode = new ConfigNode();
563+
_globalSettings.Save(gloablNode);
564+
gloablNode.Save(IOUtils.GetFilePathFor(GetType(), "MapResourceOverlay.cfg"));
565+
566+
}
567+
}
568+
569+
public class GlobalSettings : IConfigNode
570+
{
571+
public List<ResourceConfig> ColorConfigs { get; set; }
572+
573+
public GlobalSettings()
574+
{
575+
var config = new ResourceConfig
576+
{
577+
Resource = new Resource("Karbonite"),
578+
LowColor = new Color32(0, 0, 0, 0),
579+
HighColor = new Color32(255, 0, 0, 200)
580+
};
581+
var config2 = new ResourceConfig
582+
{
583+
Resource = new Resource("Ore"),
584+
LowColor = new Color32(0, 0, 0, 0),
585+
HighColor = new Color32(0, 255, 0, 200)
586+
};
587+
var config3 = new ResourceConfig
588+
{
589+
Resource = new Resource("Water", "Aquifer"),
590+
LowColor = new Color32(0, 0, 0, 0),
591+
HighColor = new Color32(0, 0, 255, 200)
592+
};
593+
var config4 = new ResourceConfig
594+
{
595+
Resource = new Resource("Minerals"),
596+
LowColor = new Color32(0, 0, 0, 0),
597+
HighColor = new Color32(0, 255, 255, 200)
598+
};
599+
var config5 = new ResourceConfig
600+
{
601+
Resource = new Resource("Substrate"),
602+
LowColor = new Color32(0, 0, 0, 0),
603+
HighColor = new Color32(255, 0, 255, 200)
604+
};
605+
ColorConfigs = new List<ResourceConfig>{config, config2,config3,config4,config5};
606+
}
607+
608+
public void Load(ConfigNode node)
609+
{
610+
611+
try
612+
{
613+
var globalSettingsNode = node.GetNode("globalSettings");
614+
var colorConfigsNode = globalSettingsNode.GetNode("colorConfigs");
615+
ColorConfigs = new List<ResourceConfig>();
616+
foreach (ConfigNode value in colorConfigsNode.nodes)
617+
{
618+
ColorConfigs.Add(ResourceConfig.Load(value));
619+
}
620+
}
621+
catch (Exception e)
622+
{
623+
this.Log("Globalconfig broken"+e);
624+
}
625+
}
626+
627+
public void Save(ConfigNode node)
628+
{
629+
630+
var globalSettingsNode = node.AddNode("globalSettings");
631+
var colorConfigsNode = globalSettingsNode.AddNode("colorConfigs");
632+
foreach (var colorConfig in ColorConfigs)
633+
{
634+
colorConfig.Save(colorConfigsNode);
635+
}
636+
637+
}
638+
639+
}
640+
public class ResourceConfig
641+
{
642+
public Resource Resource { get; set; }
643+
public Color32 LowColor { get; set; }
644+
public Color32 HighColor { get; set; }
645+
public static ResourceConfig Load(ConfigNode configNode)
646+
{
647+
var res = new ResourceConfig
648+
{
649+
Resource = Resource.DeserializeResource(configNode.GetValue("Resource")),
650+
LowColor = StringToColor(configNode.GetValue("LowColor")),
651+
HighColor = StringToColor(configNode.GetValue("HighColor"))
652+
};
653+
return res;
654+
}
655+
656+
private static Color StringToColor(string str)
657+
{
658+
var strArr = str.TrimStart('(').TrimEnd(')').Split(',');
659+
byte r, g, b, a;
660+
if (strArr.Count() == 4 && Byte.TryParse(strArr[0], out r) && Byte.TryParse(strArr[1], out g) && Byte.TryParse(strArr[2], out b) && Byte.TryParse(strArr[3], out a))
661+
{
662+
return new Color32(r, g, b, a);
663+
}
664+
return new Color32();
536665
}
537666

667+
public void Save(ConfigNode node)
668+
{
669+
var colorConfigNode = node.AddNode(Resource.ResourceName);
670+
colorConfigNode.AddValue("Resource", Resource.Serialize());
671+
colorConfigNode.AddValue("LowColor", ColorToString(LowColor));
672+
colorConfigNode.AddValue("HighColor", ColorToString(HighColor));
673+
}
538674

675+
private string ColorToString(Color32 color)
676+
{
677+
return "(" + color.r + "," + color.g + "," + color.b + "," + color.a + ")";
678+
}
539679
}
540680
}

Source/MapResourceOverlay/MapOverlayGui.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override void DrawWindowContents(int windowId)
6868

6969
foreach (var res in Model.Resources)
7070
{
71-
if (GUILayout.Button(res.ResourceName))
71+
if (GUILayout.Button(res.Resource.ResourceName))
7272
{
7373
Model.SelectedResourceName = res;
7474
}

Source/MapResourceOverlay/Resource.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Serialization;
2+
13
namespace MapResourceOverlay
24
{
35
public class Resource
@@ -10,5 +12,22 @@ public Resource(string resourceName, string scansatName = null)
1012
ResourceName = resourceName;
1113
ScansatName = scansatName ?? resourceName;
1214
}
15+
16+
public string Serialize()
17+
{
18+
return "ResourceName=" + ResourceName + ",ScansatName=" + ScansatName;
19+
}
20+
21+
public static Resource DeserializeResource(string str)
22+
{
23+
var arr = str.Split(',');
24+
var resourceName = arr[0].Split('=')[1];
25+
var scansatName = resourceName;
26+
if (arr.Length == 2)
27+
{
28+
scansatName = arr[1].Split('=')[1];
29+
}
30+
return new Resource(resourceName,scansatName);
31+
}
1332
}
1433
}

0 commit comments

Comments
 (0)