Skip to content

Commit 5fdb384

Browse files
Storing map webpage layer choice in the HTML Web Storage instead of the registry
1 parent 5e4989c commit 5fdb384

File tree

3 files changed

+36
-125
lines changed

3 files changed

+36
-125
lines changed

Source/ORTS.Common/InfoApiMap.cs

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121

2222
namespace Orts.Common
2323
{
24-
public class OverlayLayer
25-
{
26-
public string name;
27-
public bool show;
28-
}
29-
3024
public enum TypeOfPointOnApiMap {
3125
track,
3226
named,
@@ -50,8 +44,6 @@ public class LineOnApiMap
5044
public class InfoApiMap
5145
{
5246
public string typeOfLocomotive;
53-
public string baseLayer;
54-
public OverlayLayer[] overlayLayer;
5547

5648
public LinkedList<PointOnApiMap> pointOnApiMapList;
5749
public LinkedList<LineOnApiMap> lineOnApiMapList;
@@ -61,14 +53,9 @@ public class InfoApiMap
6153
public float lonMin;
6254
public float lonMax;
6355

64-
public const string RegistrySection = "ApiMap";
65-
public const string RegistryKeyBaselayer = "baseLayer";
66-
public const string RegistrySectionLayers = "ApiMap\\Layers";
67-
68-
public InfoApiMap(string powerSupplyName, string settingsFilePath, string registryKey)
56+
public InfoApiMap(string powerSupplyName)
6957
{
7058
initTypeOfLocomotive(powerSupplyName);
71-
initLayers(settingsFilePath, registryKey);
7259

7360
pointOnApiMapList = new LinkedList<PointOnApiMap>();
7461
lineOnApiMapList = new LinkedList<LineOnApiMap>();
@@ -99,54 +86,6 @@ private void initTypeOfLocomotive(string powerSupplyName)
9986
}
10087
}
10188

102-
private void initLayers(string settingsFilePath, string registryKey)
103-
{
104-
var store = SettingsStore.GetSettingStore(settingsFilePath, registryKey, RegistrySection);
105-
baseLayer = (string)store.GetUserValue(RegistryKeyBaselayer, typeof(string));
106-
107-
overlayLayer = null;
108-
store = SettingsStore.GetSettingStore(settingsFilePath, registryKey, RegistrySectionLayers);
109-
string[] names = store.GetUserNames();
110-
if (names.Length > 0)
111-
{
112-
overlayLayer = new OverlayLayer[names.Length];
113-
114-
int index = 0;
115-
foreach (string name in names)
116-
{
117-
overlayLayer[index] = new OverlayLayer
118-
{
119-
name = name,
120-
show = (bool)store.GetUserValue(name, typeof(bool))
121-
};
122-
index++;
123-
}
124-
}
125-
}
126-
127-
public static void storeLayerSetting(
128-
string settingsFilePath, string registryKey,
129-
string layerAction, string layerName)
130-
{
131-
if (layerAction == "baseLayerChange")
132-
{
133-
var store = SettingsStore.GetSettingStore(settingsFilePath, registryKey, InfoApiMap.RegistrySection);
134-
store.SetUserValue(InfoApiMap.RegistryKeyBaselayer, layerName);
135-
}
136-
else
137-
{
138-
var store = SettingsStore.GetSettingStore(settingsFilePath, registryKey, InfoApiMap.RegistrySectionLayers);
139-
if (layerAction == "overlayAdd")
140-
{
141-
store.SetUserValue(layerName, (bool)true);
142-
}
143-
if (layerAction == "overlayRemove")
144-
{
145-
store.SetUserValue(layerName, (bool)false);
146-
}
147-
}
148-
}
149-
15089
public static LatLon convertToLatLon(int TileX, int TileZ, float X, float Y, float Z)
15190
{
15291
double latitude = 1f;

Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const hr3 = new XMLHttpRequest;
2222
const httpCodeSuccess = 200;
2323
const xmlHttpRequestCodeDone = 4;
2424

25+
const storageBaseLayerKey = "OpenRails/apimap/baseLayer";
26+
const storageLayersKey = "OpenRails/apimap/layers/";
27+
2528
let map;
2629
let initToBeDone = true;
2730
let locomotiveMarker;
@@ -92,19 +95,6 @@ function ApiMapInit() {
9295

9396
layerControl = L.control.layers(baseLayers, null).addTo(map);
9497

95-
if ((apiMapInitInfo.baseLayer == null) || (apiMapInitInfo.baseLayer == "standard")) {
96-
orm.addTo(map);
97-
}
98-
if (apiMapInitInfo.baseLayer == "maximum speed") {
99-
ormMaxSpeed.addTo(map);
100-
}
101-
if (apiMapInitInfo.baseLayer == "gauge") {
102-
ormGauge.addTo(map);
103-
}
104-
if (apiMapInitInfo.baseLayer == "electrification") {
105-
ormElectrification.addTo(map);
106-
}
107-
10898
trackLayerGroup = L.layerGroup();
10999
namedLayerGroup = L.layerGroup();
110100
restLayerGroup = L.layerGroup();
@@ -165,28 +155,47 @@ function ApiMapInit() {
165155
layerControl.addOverlay(namedLayerGroup, 'named');
166156
layerControl.addOverlay(restLayerGroup, 'rest');
167157

168-
if (apiMapInitInfo.overlayLayer != null) {
169-
for (const layer of apiMapInitInfo.overlayLayer) {
170-
if ((layer.name == "track") && (layer.show == 1)) {
171-
trackLayerGroup.addTo(map);
158+
baseLayerFound = false;
159+
for (let i = 0; i < localStorage.length; i++) {
160+
key = localStorage.key(i);
161+
162+
if (key == storageBaseLayerKey) {
163+
if (value == "standard") {
164+
orm.addTo(map);
165+
}
166+
if (value == "maximum speed") {
167+
ormMaxSpeed.addTo(map);
172168
}
173-
if ((layer.name == "named") && (layer.show == 1)) {
174-
namedLayerGroup.addTo(map);
169+
if (value == "gauge") {
170+
ormGauge.addTo(map);
175171
}
176-
if ((layer.name == "rest") && (layer.show == 1)) {
177-
restLayerGroup.addTo(map);
172+
if (value == "electrification") {
173+
ormElectrification.addTo(map);
178174
}
179175
}
176+
177+
if ((key == storageLayersKey + "track") && (value == "true")) {
178+
trackLayerGroup.addTo(map);
179+
}
180+
if ((key == storageLayersKey + "named") && (value == "true")) {
181+
namedLayerGroup.addTo(map);
182+
}
183+
if ((key == storageLayersKey + "rest") && (value == "true")) {
184+
restLayerGroup.addTo(map);
185+
}
186+
}
187+
if (!baseLayerFound) {
188+
orm.addTo(map);
180189
}
181190

182-
map.on('baselayerchange', function(e) {
183-
ApiSendLayerChange('baseLayerChange', e.name);
191+
map.on('baselayerchange', function (e) {
192+
localStorage.setItem(storageBaseLayerKey, e.name);
184193
});
185194
map.on('overlayadd', function (e) {
186-
ApiSendLayerChange('overlayAdd', e.name);
195+
localStorage.setItem(storageLayersKey + e.name, "true");
187196
});
188197
map.on('overlayremove', function (e) {
189-
ApiSendLayerChange('overlayRemove', e.name);
198+
localStorage.setItem(storageLayersKey + e.name, "false");
190199
});
191200
}
192201

@@ -237,16 +246,3 @@ function ApiMap() {
237246
}
238247
hr1.send();
239248
}
240-
241-
function ApiSendLayerChange(action, name) {
242-
243-
hr3.open("POST", "/API/MAP", true);
244-
hr3.setRequestHeader("Content-Type", "application/json");
245-
hr3.send
246-
(JSON.stringify
247-
({
248-
"LayerAction": action,
249-
"LayerName": name
250-
})
251-
)
252-
}

Source/RunActivity/Viewer3D/WebServices/WebServer.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ private static async Task SerializationCallback(IHttpContext context, object dat
9090
}
9191
}
9292

93-
public static async Task<T> DeserializationCallback<T>(IHttpContext context)
94-
{
95-
using (var text = context.OpenRequestText())
96-
{
97-
return JsonConvert.DeserializeObject<T>(await text.ReadToEndAsync());
98-
}
99-
}
100-
10193
/// <summary>
10294
/// This contract resolver fixes JSON serialization for certain XNA classes.
10395
/// </summary>
@@ -278,8 +270,7 @@ public InfoApiMap apiMapInfo()
278270
public static InfoApiMap getApiMapInfo(Viewer viewer)
279271
{
280272
InfoApiMap infoApiMap = new InfoApiMap(
281-
viewer.PlayerLocomotive.PowerSupply.GetType().Name,
282-
UserSettings.SettingsFilePath, UserSettings.RegistryKey);
273+
viewer.PlayerLocomotive.PowerSupply.GetType().Name);
283274

284275
viewer.Simulator.TDB.TrackDB.addTrNodesToPointsOnApiMap(infoApiMap);
285276

@@ -292,20 +283,5 @@ public static InfoApiMap getApiMapInfo(Viewer viewer)
292283
[Route(HttpVerbs.Get, "/MAP")]
293284
public LatLonDirection LatLonDirection() => Viewer.Simulator.PlayerLocomotive.GetLatLonDirection();
294285
#endregion
295-
296-
#region /API/MAP
297-
[Route(HttpVerbs.Post, "/MAP")]
298-
public async Task StoreLayerChance()
299-
{
300-
var data = await HttpContext.GetRequestDataAsync<ApiMapLayerAction>(WebServer.DeserializationCallback<ApiMapLayerAction>);
301-
storeLayerSetting(UserSettings.SettingsFilePath, UserSettings.RegistryKey, data.LayerAction, data.LayerName);
302-
}
303-
#endregion
304-
}
305-
306-
public class ApiMapLayerAction
307-
{
308-
public string LayerAction;
309-
public string LayerName;
310286
}
311287
}

0 commit comments

Comments
 (0)