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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | // 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; [assembly: KSPAssemblyDependency("ToadicusTools", 0, 0)] namespace AntennaRange { [KSPAddon(KSPAddon.Startup.SpaceCentre, false)] public class ARConfiguration : MonoBehaviour { private bool showConfigWindow; private Rect configWindowPos; private IButton toolbarButton; private System.Version runningVersion; private KSP.IO.PluginConfiguration _config; private KSP.IO.PluginConfiguration config { get { if (this._config == null) { this._config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>(); } return this._config; } } 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() { // Only runs once, if the Toolbar is available. if (this.toolbarButton == null && ToolbarManager.ToolbarAvailable) { this.runningVersion = this.GetType().Assembly.GetName().Version; 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; }; this.configWindowPos = this.LoadConfigValue("configWindowPos", this.configWindowPos); AntennaRelay.requireLineOfSight = this.LoadConfigValue("requireLineOfSight", false); ARFlightController.requireConnectionForControl = this.LoadConfigValue("requireConnectionForControl", false); ModuleLimitedDataTransmitter.fixedPowerCost = this.LoadConfigValue("fixedPowerCost", false); Debug.Log(string.Format("{0} v{1} - ARonfiguration loaded!", this.GetType().Name, this.runningVersion)); } if (this.showConfigWindow) { Rect configPos = GUILayout.Window(354163056, this.configWindowPos, this.ConfigWindow, string.Format("AntennaRange {0}.{1}", this.runningVersion.Major, this.runningVersion.Minor), 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.BeginHorizontal(GUILayout.ExpandWidth(true)); bool requireConnectionForControl = GUILayout.Toggle( ARFlightController.requireConnectionForControl, "Require Connection for Probe Control" ); if (requireConnectionForControl != ARFlightController.requireConnectionForControl) { ARFlightController.requireConnectionForControl = requireConnectionForControl; this.SaveConfigValue("requireConnectionForControl", requireConnectionForControl); } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); bool fixedPowerCost = GUILayout.Toggle(ModuleLimitedDataTransmitter.fixedPowerCost, "Use fixed power cost"); if (fixedPowerCost != ModuleLimitedDataTransmitter.fixedPowerCost) { ModuleLimitedDataTransmitter.fixedPowerCost = fixedPowerCost; this.SaveConfigValue("fixedPowerCost", fixedPowerCost); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUI.DragWindow(); } public void Destroy() { if (this.toolbarButton != null) { this.toolbarButton.Destroy(); } } private T LoadConfigValue<T>(string key, T defaultValue) { this.config.load(); return config.GetValue(key, defaultValue); } private void SaveConfigValue<T>(string key, T value) { this.config.load(); this.config.SetValue(key, value); this.config.save(); } } } |