Added packet size throttle.
[AntennaRange.git] /
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
114
115
116
117
118
119
120
121
122
123
124
125
// AntennaRange © 2014 toadicus
//
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

using KSP;
using System;
using ToadicusTools;
using UnityEngine;

namespace AntennaRange
{
	[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
	public class ARConfiguration : MonoBehaviour
	{
		private bool showConfigWindow;
		private Rect configWindowPos;

		private IButton toolbarButton;

		public void Awake()
		{
			Tools.PostDebugMessage(this, "Waking up.");

			this.showConfigWindow = false;
			this.configWindowPos = new Rect(Screen.width / 4, Screen.height / 2, 180, 15);

			Tools.PostDebugMessage(this, "Awake.");
		}

		public void OnGUI()
		{
			if (this.toolbarButton == null && ToolbarManager.ToolbarAvailable)
			{
				Tools.PostDebugMessage(this, "Toolbar available; initializing button.");

				this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration");
				this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER);
				this.toolbarButton.Text = "AR";
				this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon";
				this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst;
				this.toolbarButton.OnClick += delegate(ClickEvent e)
				{
					this.showConfigWindow = !this.showConfigWindow;
				};

				var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();

				config.load();

				this.configWindowPos = config.GetValue<Rect>("configWindowPos", this.configWindowPos);
				AntennaRelay.requireLineOfSight = config.GetValue<bool>("requireLineOfSight", false);

				config.save();
			}

			if (this.showConfigWindow)
			{
				Rect configPos = GUILayout.Window(354163056,
					this.configWindowPos,
					this.ConfigWindow,
					"AntennaRange Configuration",
					GUILayout.ExpandHeight(true),
					GUILayout.ExpandWidth(true)
				);

				configPos = Tools.ClampRectToScreen(configPos, 20);

				if (configPos != this.configWindowPos)
				{
					this.configWindowPos = configPos;
					this.SaveConfigValue("configWindowPos", this.configWindowPos);
				}
			}
		}

		public void ConfigWindow(int _)
		{
			GUILayout.BeginVertical(GUILayout.ExpandHeight(true));

			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
			bool requireLineOfSight = GUILayout.Toggle(AntennaRelay.requireLineOfSight, "Require Line of Sight");
			if (requireLineOfSight != AntennaRelay.requireLineOfSight)
			{
				AntennaRelay.requireLineOfSight = requireLineOfSight;
				this.SaveConfigValue("requireLineOfSight", requireLineOfSight);
			}

			GUILayout.EndHorizontal();

			GUILayout.EndVertical();

			GUI.DragWindow();
		}

		public void Destroy()
		{
			if (this.toolbarButton != null)
			{
				this.toolbarButton.Destroy();
			}
		}

		private T LoadConfigValue<T>(string key, T defaultValue)
		{
			var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();

			config.load();

			return config.GetValue(key, defaultValue);
		}

		private void SaveConfigValue<T>(string key, T value)
		{
			var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();

			config.load();

			config.SetValue(key, value);

			config.save();
		}
	}
}