109 lines
2.6 KiB
C#
109 lines
2.6 KiB
C#
using Plane.FormationCreator.Formation;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Plane.FormationCreator
|
|
{
|
|
class AppConfig
|
|
{
|
|
public AppConfig(MapManager mapMannager)
|
|
{
|
|
_mapManager = mapMannager;
|
|
|
|
_dirPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"Plane",
|
|
AppDomain.CurrentDomain.FriendlyName,
|
|
"Config"
|
|
);
|
|
_filePath = Path.Combine(
|
|
_dirPath,
|
|
"Config.json"
|
|
);
|
|
|
|
string json;
|
|
try
|
|
{
|
|
json = File.ReadAllText(_filePath);
|
|
}
|
|
catch
|
|
{
|
|
json = "{}";
|
|
}
|
|
_config = JsonConvert.DeserializeObject(json);
|
|
|
|
App.Current.Exit += (sender, e) =>
|
|
{
|
|
this.Center = _mapManager.Center;
|
|
this.ZoomLevel = _mapManager.MapView.map.ZoomLevel;
|
|
this.Save();
|
|
};
|
|
}
|
|
|
|
string _dirPath;
|
|
string _filePath;
|
|
|
|
dynamic _config;
|
|
|
|
MapManager _mapManager;
|
|
|
|
public LatLng Center
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
var text = (string)_config.Center;
|
|
var fields = text.Split(',');
|
|
return new LatLng(double.Parse(fields[0]), double.Parse(fields[1]));
|
|
}
|
|
catch
|
|
{
|
|
return new LatLng(40.160491667, 116.23426389);
|
|
}
|
|
}
|
|
set
|
|
{
|
|
if (_config!=null)
|
|
_config.Center = $"{value.Lat},{value.Lng}";
|
|
}
|
|
}
|
|
|
|
public double ZoomLevel
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
var text = (string)_config.ZoomLevel;
|
|
return double.Parse(text);
|
|
}
|
|
catch
|
|
{
|
|
return 19;
|
|
}
|
|
}
|
|
set
|
|
{
|
|
if (_config != null)
|
|
_config.ZoomLevel = value;
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
if (!Directory.Exists(_dirPath))
|
|
{
|
|
Directory.CreateDirectory(_dirPath);
|
|
}
|
|
if (_config != null)
|
|
File.WriteAllText(_filePath, JsonConvert.SerializeObject(_config));
|
|
}
|
|
}
|
|
}
|