OSDN Git Service

Added assists aiding Aspiring Apiarists
[automap/automap.git] / Automap / AutomapMod.cs
1 using System.Collections.ObjectModel;
2 using System.Linq;
3
4 using Vintagestory.API.Client;
5 using Vintagestory.API.Common;
6
7
8 namespace Automap
9 {
10     public class AutomapMod : ModSystem
11     {
12                 public const string _configFilename = @"automap.json";
13
14         private ICoreAPI API { get; set; }
15         private ICoreClientAPI ClientAPI { get; set; }
16         private ILogger Logger { get; set; }
17         private AutomapSystem _localAutomap;
18         private AutomapGUIDialog _automapDialog;
19
20
21         public override bool ShouldLoad(EnumAppSide forSide)
22         {
23             return forSide.IsClient();
24         }
25
26                 public override void StartClientSide(ICoreClientAPI api)
27                 {
28                         this.API = api;
29
30                         if (api.Side == EnumAppSide.Client)
31                         {
32                                 this.ClientAPI = api as ICoreClientAPI;
33                                 this.Logger = Mod.Logger;
34
35                                 ClientAPI.Logger.VerboseDebug("Automap Present!");
36                                 PrepareClientsideConfig( );
37                                 _localAutomap = new AutomapSystem(this.ClientAPI, this.Logger,  this.CachedConfiguration);
38                                 _automapDialog = new AutomapGUIDialog(ClientAPI, _localAutomap, this.CachedConfiguration);
39
40                                 ClientAPI.Input.RegisterHotKey(AutomapGUIDialog._automapControlPanelKey, "Automap control panel", GlKeys.M, HotkeyType.GUIOrOtherControls, shiftPressed: true);
41                                 ClientAPI.Input.SetHotKeyHandler(AutomapGUIDialog._automapControlPanelKey, ToggleAM_Dialog);
42
43                                 ClientAPI.Event.LeaveWorld += PersistParameterChange;
44                         }
45
46             base.StartClientSide(api);
47         }
48
49                 public override double ExecuteOrder()
50                 {
51                         return 1.2;
52                 }
53
54                 private bool ToggleAM_Dialog(KeyCombination comb)
55                 {
56                         if (_automapDialog.IsOpened()) _automapDialog.TryClose();
57                         else _automapDialog.TryOpen();
58
59                         return true;
60                 }
61
62                 internal PersistedConfiguration CachedConfiguration {
63                         get
64                         {
65                         return ( PersistedConfiguration )ClientAPI.ObjectCache[_configFilename];
66                         }
67                         set
68                         {
69                         ClientAPI.ObjectCache.Add(_configFilename, value);
70                         }
71                 }
72
73                 private void PrepareClientsideConfig( )
74                 {
75                 PersistedConfiguration config = ClientAPI.LoadModConfig<PersistedConfiguration>(_configFilename);
76
77                 if (config == null) {
78                 //Regen default
79                 Mod.Logger.Warning("Regenerating default config as it was missing / unparsable...");
80                 ClientAPI.StoreModConfig<PersistedConfiguration>(new PersistedConfiguration(defaults: true ), _configFilename);
81                 config = ClientAPI.LoadModConfig<PersistedConfiguration>(_configFilename);
82                 }
83
84                 this.CachedConfiguration = config;
85                 }
86
87                 internal void PersistParameterChange( )
88                 {
89                 //Store altered parameters
90
91                 ClientAPI.StoreModConfig<PersistedConfiguration>(this.CachedConfiguration, _configFilename);
92                 }
93
94                 #region External Interfaces
95                 //Perhaps Other mods can make use of POI / EOI?
96
97                 public ReadOnlyCollection<PointOfInterest> PointsOfInterest {
98                         get 
99                         {
100                                 return _localAutomap.POIs.ToList().AsReadOnly();
101                         }
102                 }
103
104                 public ReadOnlyCollection<EntityOfInterest> EntitiesOfInterest {
105                         get 
106                         {
107                                 return _localAutomap.EOIs.ToList().AsReadOnly();
108                         }
109                 }
110
111                 public CommandType Status
112                 {
113                         get { return _localAutomap.CurrentState; }
114                 }
115
116                 #endregion
117
118
119         }
120 }
121