Skip to content

Commit 9288995

Browse files
committed
trying to get saves to work
1 parent 070e2d5 commit 9288995

File tree

3 files changed

+102
-36
lines changed

3 files changed

+102
-36
lines changed

Source/MapResourceOverlay/MapOverlay.cs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,37 @@ public class MapOverlay : MonoBehaviour
3030
private bool _changed;
3131
private Resource _selectedResourceName;
3232
private Coordinates _mouseCoords;
33-
private CelestialBody targetBody;
34-
private double _lastRedraw;
33+
private CelestialBody _targetBody;
3534
private delegate bool IsCoveredDelegate(double lon, double lat, CelestialBody body, int mask);
3635

3736
private IsCoveredDelegate _scansatIsCoveredDelegate;
3837
private Type _scansatEnum;
39-
[KSPField(isPersistant = true)] public bool _show;
4038

41-
private Vector2 mouse;
42-
[KSPField(isPersistant = true)] public bool _useScansat;
39+
private Vector2 _mouse;
4340
private int _toolTipId;
4441
private int _currentLat;
42+
public int Cutoff
43+
{
44+
get { return MapOverlayController.Current.Cutoff; }
45+
set { MapOverlayController.Current.Cutoff = value; _changed = true; }
46+
}
4547

48+
public bool Bright
49+
{
50+
get { return MapOverlayController.Current.Bright; }
51+
set { MapOverlayController.Current.Bright = value; _changed = true; }
52+
}
4653

54+
public bool UseScansat
55+
{
56+
get { return MapOverlayController.Current.UseScansat; }
57+
set { MapOverlayController.Current.UseScansat = value; _changed = true; }
58+
}
59+
public bool Show
60+
{
61+
get { return MapOverlayController.Current.Show; }
62+
set { MapOverlayController.Current.Show = value; _changed = true; }
63+
}
4764
public MapOverlay()
4865
{
4966
_mapOverlayButton = ToolbarManager.Instance.add("MapResourceOverlay", "ResourceOverlay");
@@ -53,6 +70,9 @@ public MapOverlay()
5370
_mapOverlayButton.OnClick += e => ToggleGui();
5471
DontDestroyOnLoad(this);
5572
_toolTipId = new System.Random().Next(65536) + Assembly.GetExecutingAssembly().GetName().Name.GetHashCode() + "tooltip".GetHashCode();
73+
74+
GameEvents.onHideUI.Add(() => Show = false);
75+
GameEvents.onShowUI.Add(() => Show = true);
5676
}
5777

5878
public void ToggleGui()
@@ -110,17 +130,17 @@ private void updateMapView()
110130
else
111131
{
112132
gameObject.renderer.enabled = true;
113-
targetBody = GetTargetBody(MapView.MapCamera.target);
133+
_targetBody = GetTargetBody(MapView.MapCamera.target);
114134

115-
if (targetBody != null && (targetBody != _body || _changed))
135+
if (_targetBody != null && (_targetBody != _body || _changed))
116136
{
117137
_changed = false;
118-
Debug.Log("new target: " + targetBody.GetName());
138+
Debug.Log("new target: " + _targetBody.GetName());
119139
var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
120140
var radii = System.IO.File.ReadAllLines(dir + "/Assets/Radii.cfg");
121-
var radius = float.Parse(radii.First(x => x.StartsWith(targetBody.GetName())).Split('=')[1]);
122-
_body = targetBody;
123-
evilmesh(targetBody, SelectedResourceName);
141+
var radius = float.Parse(radii.First(x => x.StartsWith(_targetBody.GetName())).Split('=')[1]);
142+
_body = _targetBody;
143+
evilmesh(_targetBody, SelectedResourceName);
124144
gameObject.renderer.material = new Material(System.IO.File.ReadAllText(dir + "/Assets/MapOverlayShader.txt"));
125145
gameObject.renderer.enabled = true;
126146
gameObject.renderer.castShadows = false;
@@ -129,13 +149,10 @@ private void updateMapView()
129149
gameObject.transform.localScale = Vector3.one * 1000f * radius;
130150
gameObject.transform.localPosition = (Vector3.zero);
131151
gameObject.transform.localRotation = (Quaternion.identity);
132-
_lastRedraw = Planetarium.GetUniversalTime();
133152
}
134-
if (targetBody != null && UseScansat && _scansatIsCoveredDelegate != null &&
135-
_lastRedraw < Planetarium.GetUniversalTime())
153+
if (_targetBody != null && UseScansat && _scansatIsCoveredDelegate != null)
136154
{
137-
RecalculateColors(targetBody, SelectedResourceName);
138-
_lastRedraw = Planetarium.GetUniversalTime();
155+
RecalculateColors(_targetBody, SelectedResourceName);
139156
}
140157
}
141158
}
@@ -167,14 +184,18 @@ private void RecalculateColors(CelestialBody targetBody, Resource resource)
167184

168185
public void OnGUI()
169186
{
170-
if (Show && targetBody != null)
187+
if (_targetBody != FlightGlobals.ActiveVessel.mainBody) //dont show tooltips on different bodys or ORS lags
188+
{
189+
return;
190+
}
191+
if (Show && _targetBody != null)
171192
{
172193
if (Event.current.type == EventType.Layout)
173194
{
174-
_mouseCoords = GetMouseCoordinates(targetBody);
175-
mouse = Event.current.mousePosition;
195+
_mouseCoords = GetMouseCoordinates(_targetBody);
196+
_mouse = Event.current.mousePosition;
176197
if (UseScansat && _scansatIsCoveredDelegate != null &&_mouseCoords != null &&
177-
!_scansatIsCoveredDelegate(_mouseCoords.Longitude, _mouseCoords.Latitude, targetBody, GetScansatId(_selectedResourceName.ScansatName)))
198+
!_scansatIsCoveredDelegate(_mouseCoords.Longitude, _mouseCoords.Latitude, _targetBody, GetScansatId(_selectedResourceName.ScansatName)))
178199
{
179200
_mouseCoords = null;
180201
}
@@ -184,7 +205,7 @@ public void OnGUI()
184205

185206
_toolTipId = 0;
186207
var abundance = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(
187-
targetBody.flightGlobalsIndex, _selectedResourceName.ResourceName, _mouseCoords.Latitude, _mouseCoords.Longitude)
208+
_targetBody.flightGlobalsIndex, _selectedResourceName.ResourceName, _mouseCoords.Latitude, _mouseCoords.Longitude)
188209
.getAmount();
189210
string abundanceString;
190211
if (abundance > 0.001)
@@ -195,7 +216,7 @@ public void OnGUI()
195216
{
196217
abundanceString = (abundance * 1000000.0).ToString("0.0") + "ppm";
197218
}
198-
GUI.Window(_toolTipId, new Rect(mouse.x + 10, mouse.y + 10, 200f, 55f), i =>
219+
GUI.Window(_toolTipId, new Rect(_mouse.x + 10, _mouse.y + 10, 200f, 55f), i =>
199220
{
200221
GUI.Label(new Rect(5, 10, 190, 20), "Long: " + _mouseCoords.Longitude.ToString("###.##") + " Lat: " + _mouseCoords.Latitude.ToString("####.##"));
201222
GUI.Label(new Rect(5, 30, 190, 20), "Amount: " + abundanceString);
@@ -367,14 +388,21 @@ private Color32 CalculateColor32At(CelestialBody body, Resource resource, double
367388
var avail = ORSPlanetaryResourceMapData.getResourceAvailabilityByRealResourceName(body.flightGlobalsIndex, resource.ResourceName, latitude, longitude);
368389
var amount = avail.getAmount();
369390
amount = Mathf.Clamp((float)amount * 500000f, 0f, 255f);
370-
if (amount > 0.0)
391+
if (!Bright)
371392
{
372-
return new Color32(Convert.ToByte(amount), byte.MinValue, byte.MinValue, 70);
393+
if (amount > 0.0)
394+
{
395+
return new Color32(Convert.ToByte(amount), 0, 0, 150);
396+
}
373397
}
374398
else
375399
{
376-
return new Color32(byte.MinValue, byte.MinValue, byte.MinValue, 0);
400+
if (amount > 0.0)
401+
{
402+
return new Color32(255, Convert.ToByte(amount), Convert.ToByte(amount), 150);
403+
}
377404
}
405+
return new Color32(byte.MinValue, byte.MinValue, byte.MinValue, 0);
378406
}
379407

380408
private static CelestialBody GetTargetBody(MapObject target)
@@ -388,12 +416,6 @@ private static CelestialBody GetTargetBody(MapObject target)
388416
return null;
389417
}
390418

391-
public bool Show
392-
{
393-
get { return _show; }
394-
set { _show = value; _changed = true; }
395-
}
396-
397419
public List<Resource> Resources
398420
{
399421
get { return _resources; }
@@ -409,11 +431,7 @@ public Resource SelectedResourceName
409431
}
410432
}
411433

412-
public bool UseScansat
413-
{
414-
get { return _useScansat; }
415-
set { _useScansat = value; _changed = true; }
416-
}
434+
417435

418436
private int GetScansatId(string resourceName)
419437
{
@@ -489,6 +507,8 @@ protected override void DrawWindowContents(int windowId)
489507
}
490508
}
491509

510+
Model.Bright = GUILayout.Toggle(Model.Bright, "bright");
511+
492512

493513

494514
GUILayout.Box("");
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MapResourceOverlay
7+
{
8+
public class MapOverlayController : ScenarioModule
9+
{
10+
private static MapOverlayController _current;
11+
public static MapOverlayController Current
12+
{
13+
get
14+
{
15+
if (_current)
16+
{
17+
return _current;
18+
}
19+
var game = HighLogic.CurrentGame;
20+
if (game == null) { return null; }
21+
var scenario = game.scenarios.Select(s => s.moduleRef).OfType<MapOverlayController>().SingleOrDefault();
22+
if (scenario != null)
23+
{
24+
return scenario;
25+
}
26+
var proto = game.AddProtoScenarioModule(typeof(MapOverlayController), GameScenes.FLIGHT, GameScenes.TRACKSTATION);
27+
if (proto.targetScenes.Contains(HighLogic.LoadedScene))
28+
{
29+
proto.Load(ScenarioRunner.fetch);
30+
}
31+
32+
return game.scenarios.Select(s => s.moduleRef).OfType<MapOverlayController>().SingleOrDefault();
33+
}
34+
}
35+
[KSPField(isPersistant = true)] public int Cutoff;
36+
[KSPField(isPersistant = true)] public bool Bright = true;
37+
[KSPField(isPersistant = true)] public bool UseScansat = true;
38+
[KSPField(isPersistant = true)] public bool Show;
39+
40+
public void OnDestroy()
41+
{
42+
_current = null;
43+
}
44+
}
45+
}

Source/MapResourceOverlay/MapResourceOverlay.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
</ItemGroup>
5353
<ItemGroup>
5454
<Compile Include="MapOverlay.cs" />
55+
<Compile Include="MapOverlayController.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
5657
<Compile Include="Utilities.cs" />
5758
<Compile Include="Window.cs" />

0 commit comments

Comments
 (0)