|
| 1 | +// COPYRIGHT 2013 by the Open Rails project. |
| 2 | +// |
| 3 | +// This file is part of Open Rails. |
| 4 | +// |
| 5 | +// Open Rails is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Open Rails is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Open Rails. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +using System.Collections.Generic; |
| 19 | +using Microsoft.Xna.Framework; |
| 20 | +using ORTS.Common; |
| 21 | + |
| 22 | +namespace Orts.Common |
| 23 | +{ |
| 24 | + public class OverlayLayer |
| 25 | + { |
| 26 | + public string name; |
| 27 | + public bool show; |
| 28 | + } |
| 29 | + |
| 30 | + public enum TypeOfPointOnApiMap { |
| 31 | + track, |
| 32 | + named, |
| 33 | + rest |
| 34 | + } |
| 35 | + |
| 36 | + public class PointOnApiMap |
| 37 | + { |
| 38 | + public LatLon latLon; |
| 39 | + public string color; |
| 40 | + public TypeOfPointOnApiMap typeOfPointOnApiMap; |
| 41 | + public string name; |
| 42 | + } |
| 43 | + |
| 44 | + public class LineOnApiMap |
| 45 | + { |
| 46 | + public LatLon latLonFrom; |
| 47 | + public LatLon latLonTo; |
| 48 | + } |
| 49 | + |
| 50 | + public class InfoApiMap |
| 51 | + { |
| 52 | + public string typeOfLocomotive; |
| 53 | + public string baseLayer; |
| 54 | + public OverlayLayer[] overlayLayer; |
| 55 | + |
| 56 | + public LinkedList<PointOnApiMap> pointOnApiMapList; |
| 57 | + public LinkedList<LineOnApiMap> lineOnApiMapList; |
| 58 | + |
| 59 | + public float latMin; |
| 60 | + public float latMax; |
| 61 | + public float lonMin; |
| 62 | + public float lonMax; |
| 63 | + |
| 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) |
| 69 | + { |
| 70 | + initTypeOfLocomotive(powerSupplyName); |
| 71 | + initLayers(settingsFilePath, registryKey); |
| 72 | + |
| 73 | + pointOnApiMapList = new LinkedList<PointOnApiMap>(); |
| 74 | + lineOnApiMapList = new LinkedList<LineOnApiMap>(); |
| 75 | + |
| 76 | + latMax = -999999f; |
| 77 | + latMin = +999999f; |
| 78 | + lonMax = -999999f; |
| 79 | + lonMin = +999999f; |
| 80 | + } |
| 81 | + |
| 82 | + private void initTypeOfLocomotive(string powerSupplyName) |
| 83 | + { |
| 84 | + string powerSupplyNameToLower = powerSupplyName.ToLower(); |
| 85 | + if (powerSupplyNameToLower.Contains("steam")) |
| 86 | + { |
| 87 | + typeOfLocomotive = "steam"; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + if (powerSupplyNameToLower.Contains("diesel")) |
| 92 | + { |
| 93 | + typeOfLocomotive = "diesel"; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + typeOfLocomotive = "electric"; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 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 | + |
| 150 | + public static LatLon convertToLatLon(int TileX, int TileZ, float X, float Y, float Z) |
| 151 | + { |
| 152 | + double latitude = 1f; |
| 153 | + double longitude = 1f; |
| 154 | + |
| 155 | + WorldLocation mstsLocation = new WorldLocation(TileX, TileZ, X, Y, Z); |
| 156 | + new WorldLatLon().ConvertWTC(TileX, TileZ, mstsLocation.Location, ref latitude, ref longitude); |
| 157 | + LatLon latLon = new LatLon(MathHelper.ToDegrees((float)latitude), MathHelper.ToDegrees((float)longitude)); |
| 158 | + |
| 159 | + return latLon; |
| 160 | + } |
| 161 | + |
| 162 | + public void addToPointOnApiMap( |
| 163 | + int TileX, int TileZ, float X, float Y, float Z, |
| 164 | + string color, TypeOfPointOnApiMap typeOfPointOnApiMap, string name) |
| 165 | + { |
| 166 | + LatLon latLon = InfoApiMap.convertToLatLon(TileX, TileZ, X, Y, Z); |
| 167 | + |
| 168 | + addToPointOnApiMap(latLon, |
| 169 | + color, typeOfPointOnApiMap, name); |
| 170 | + } |
| 171 | + |
| 172 | + public void addToPointOnApiMap( |
| 173 | + LatLon latLon, |
| 174 | + string color, TypeOfPointOnApiMap typeOfPointOnApiMap, string name) |
| 175 | + { |
| 176 | + PointOnApiMap pointOnApiMap = new PointOnApiMap |
| 177 | + { |
| 178 | + latLon = latLon, |
| 179 | + color = color, |
| 180 | + typeOfPointOnApiMap = typeOfPointOnApiMap, |
| 181 | + name = name |
| 182 | + }; |
| 183 | + |
| 184 | + if (pointOnApiMap.typeOfPointOnApiMap == TypeOfPointOnApiMap.named) |
| 185 | + { |
| 186 | + // named last is the list so that they get displayed on top |
| 187 | + pointOnApiMapList.AddLast(pointOnApiMap); |
| 188 | + } |
| 189 | + else |
| 190 | + { |
| 191 | + pointOnApiMapList.AddFirst(pointOnApiMap); |
| 192 | + } |
| 193 | + |
| 194 | + if (pointOnApiMap.latLon.Lat > latMax) |
| 195 | + { |
| 196 | + latMax = pointOnApiMap.latLon.Lat; |
| 197 | + } |
| 198 | + if (pointOnApiMap.latLon.Lat < latMin) |
| 199 | + { |
| 200 | + latMin = pointOnApiMap.latLon.Lat; |
| 201 | + } |
| 202 | + if (pointOnApiMap.latLon.Lon > lonMax) |
| 203 | + { |
| 204 | + lonMax = pointOnApiMap.latLon.Lon; |
| 205 | + } |
| 206 | + if (pointOnApiMap.latLon.Lon < lonMin) |
| 207 | + { |
| 208 | + lonMin = pointOnApiMap.latLon.Lon; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + public void addToLineOnApiMap(LatLon latLonFrom, LatLon latLongTo) |
| 213 | + { |
| 214 | + LineOnApiMap lineOnApiMap = new LineOnApiMap |
| 215 | + { |
| 216 | + latLonFrom = latLonFrom, |
| 217 | + latLonTo = latLongTo |
| 218 | + }; |
| 219 | + lineOnApiMapList.AddLast(lineOnApiMap); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments