forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAltitudeAngelPlugin.cs
113 lines (99 loc) · 3.37 KB
/
AltitudeAngelPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Windows.Forms;
using AltitudeAngelWings.Plugin.Properties;
using AltitudeAngelWings.Service;
namespace AltitudeAngelWings.Plugin
{
public class AltitudeAngelPlugin : MissionPlanner.Plugin.Plugin
{
private const string SettingsMenuItemName = "altitudeAngelSettings";
public override string Name => Resources.PluginName;
public override string Version => GetType().Assembly.GetName().Version.ToString();
public override string Author => Resources.PluginAuthor;
private bool _enabled;
static internal AltitudeAngelPlugin Instance;
public override bool Init()
{
Instance = this;
Host.MainForm.Invoke((Action)(() =>
{
Host.FDMenuMap.Items.Add(CreateSettingsMenuItem());
Host.FPMenuMap.Items.Add(CreateSettingsMenuItem());
}));
return true;
}
public override bool Loaded()
{
ServiceLocator.Clear();
if (Host.config.ContainsKey("AA_CheckEnableAltitudeAngel"))
{
_enabled = Host.config.GetBoolean("AA_CheckEnableAltitudeAngel");
}
else
{
AskToEnableAltitudeAngel();
}
if (_enabled)
{
EnableAltitudeAngel();
}
return true;
}
public override bool Exit()
{
ServiceLocator.Clear();
return true;
}
private void AskToEnableAltitudeAngel(bool explicitClick = false)
{
var text = Resources.AskToEnableAltitudeAngel;
if (explicitClick)
{
text = Resources.ExplicitClickPrefix + text;
}
_enabled = CustomMessageBox.Show(
text,
Resources.AskToEnableCaption,
CustomMessageBox.MessageBoxButtons.YesNo) == CustomMessageBox.DialogResult.Yes;
Host.config["AA_CheckEnableAltitudeAngel"] = (_enabled).ToString();
Host.config.Save();
}
private void EnableAltitudeAngel()
{
ConfigureServiceLocator();
var service = ServiceLocator.GetService<IAltitudeAngelService>();
if (!service.IsSignedIn)
{
service.SignInAsync();
}
}
private ToolStripMenuItem CreateSettingsMenuItem()
{
var menuItem = new ToolStripMenuItem
{
Name = SettingsMenuItemName,
Text = Resources.SettingsMenuItemText,
Enabled = true,
Visible = true
};
menuItem.Click += OnSettingsClick;
return menuItem;
}
private void OnSettingsClick(object sender, EventArgs e)
{
if (!_enabled)
{
AskToEnableAltitudeAngel(true);
}
if (!_enabled) return;
EnableAltitudeAngel();
new AASettings().Show(Host.MainForm);
}
private void ConfigureServiceLocator()
{
ServiceLocator.Clear();
ServiceLocator.Register(l => Host);
ServiceLocator.Configure();
}
}
}