ARConfiguration: Changed config assignment to be persistent, and added handling for requireConnectionForControl.
ARConfiguration: Changed config assignment to be persistent, and added handling for requireConnectionForControl.

// AntennaRange © 2014 toadicus // AntennaRange © 2014 toadicus
// //
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a // 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/ // copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
   
using KSP; using KSP;
using System; using System;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
[assembly: KSPAssemblyDependency("ToadicusTools", 0, 0)] [assembly: KSPAssemblyDependency("ToadicusTools", 0, 0)]
   
namespace AntennaRange namespace AntennaRange
{ {
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)] [KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class ARConfiguration : MonoBehaviour public class ARConfiguration : MonoBehaviour
{ {
private bool showConfigWindow; private bool showConfigWindow;
private Rect configWindowPos; private Rect configWindowPos;
   
private IButton toolbarButton; private IButton toolbarButton;
   
  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() public void Awake()
{ {
Tools.PostDebugMessage(this, "Waking up."); Tools.PostDebugMessage(this, "Waking up.");
   
this.showConfigWindow = false; this.showConfigWindow = false;
this.configWindowPos = new Rect(Screen.width / 4, Screen.height / 2, 180, 15); this.configWindowPos = new Rect(Screen.width / 4, Screen.height / 2, 180, 15);
   
Tools.PostDebugMessage(this, "Awake."); Tools.PostDebugMessage(this, "Awake.");
} }
   
public void OnGUI() public void OnGUI()
{ {
if (this.toolbarButton == null && ToolbarManager.ToolbarAvailable) if (this.toolbarButton == null && ToolbarManager.ToolbarAvailable)
{ {
Tools.PostDebugMessage(this, "Toolbar available; initializing button."); Tools.PostDebugMessage(this, "Toolbar available; initializing button.");
   
this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration"); this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration");
this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER); this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER);
this.toolbarButton.Text = "AR"; this.toolbarButton.Text = "AR";
this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon"; this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon";
this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst; this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst;
this.toolbarButton.OnClick += delegate(ClickEvent e) this.toolbarButton.OnClick += delegate(ClickEvent e)
{ {
this.showConfigWindow = !this.showConfigWindow; this.showConfigWindow = !this.showConfigWindow;
}; };
   
var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>(); this.configWindowPos = this.LoadConfigValue("configWindowPos", this.configWindowPos);
  AntennaRelay.requireLineOfSight = this.LoadConfigValue("requireLineOfSight", false);
config.load(); ARFlightController.requireConnectionForControl =
  this.LoadConfigValue("requireConnectionForControl", false);
this.configWindowPos = config.GetValue<Rect>("configWindowPos", this.configWindowPos);  
AntennaRelay.requireLineOfSight = config.GetValue<bool>("requireLineOfSight", false);  
   
config.save();  
} }
   
if (this.showConfigWindow) if (this.showConfigWindow)
{ {
Rect configPos = GUILayout.Window(354163056, Rect configPos = GUILayout.Window(354163056,
this.configWindowPos, this.configWindowPos,
this.ConfigWindow, this.ConfigWindow,
"AntennaRange Configuration", "AntennaRange Configuration",
GUILayout.ExpandHeight(true), GUILayout.ExpandHeight(true),
GUILayout.ExpandWidth(true) GUILayout.ExpandWidth(true)
); );
   
configPos = Tools.ClampRectToScreen(configPos, 20); configPos = Tools.ClampRectToScreen(configPos, 20);
   
if (configPos != this.configWindowPos) if (configPos != this.configWindowPos)
{ {
this.configWindowPos = configPos; this.configWindowPos = configPos;
this.SaveConfigValue("configWindowPos", this.configWindowPos); this.SaveConfigValue("configWindowPos", this.configWindowPos);
} }
} }
} }
   
public void ConfigWindow(int _) public void ConfigWindow(int _)
{ {
GUILayout.BeginVertical(GUILayout.ExpandHeight(true)); GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
   
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
   
bool requireLineOfSight = GUILayout.Toggle(AntennaRelay.requireLineOfSight, "Require Line of Sight"); bool requireLineOfSight = GUILayout.Toggle(AntennaRelay.requireLineOfSight, "Require Line of Sight");
if (requireLineOfSight != AntennaRelay.requireLineOfSight) if (requireLineOfSight != AntennaRelay.requireLineOfSight)
{ {
AntennaRelay.requireLineOfSight = requireLineOfSight; AntennaRelay.requireLineOfSight = requireLineOfSight;
this.SaveConfigValue("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.EndHorizontal();
   
GUILayout.EndVertical(); GUILayout.EndVertical();
   
GUI.DragWindow(); GUI.DragWindow();
} }
   
public void Destroy() public void Destroy()
{ {
if (this.toolbarButton != null) if (this.toolbarButton != null)
{ {
this.toolbarButton.Destroy(); this.toolbarButton.Destroy();
} }
} }
   
private T LoadConfigValue<T>(string key, T defaultValue) private T LoadConfigValue<T>(string key, T defaultValue)
{ {
var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>(); this.config.load();
   
config.load();  
   
return config.GetValue(key, defaultValue); return config.GetValue(key, defaultValue);
} }
   
private void SaveConfigValue<T>(string key, T value) private void SaveConfigValue<T>(string key, T value)
{ {
var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>(); this.config.load();
   
config.load(); this.config.SetValue(key, value);
   
config.SetValue(key, value); this.config.save();
   
config.save();  
} }
} }
} }