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();  
} }
} }
} }
   
// AntennaRange // AntennaRange
// //
// ARFlightController.cs // ARFlightController.cs
// //
// Copyright © 2014, toadicus // Copyright © 2014, toadicus
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, // Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met: // are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, // 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, // 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution. // materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be used // 3. Neither the name of the copyright holder nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission. // to endorse or promote products derived from this software without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
  using KSP;
using System; using System;
  using ToadicusTools;
  using UnityEngine;
   
namespace AntennaRange namespace AntennaRange
{ {
public class ARFlightController [KSPAddon(KSPAddon.Startup.Flight, false)]
  public class ARFlightController : MonoBehaviour
{ {
public ARFlightController() #region Static Members
  public static bool requireConnectionForControl;
  #endregion
   
  #region Properties
  public ControlTypes currentControlLock
{ {
  get
  {
  if (this.lockID == string.Empty)
  {
  return ControlTypes.None;
  }
   
  return InputLockManager.GetControlLock(this.lockID);
  }
} }
   
  public string lockID
  {
  get;
  protected set;
  }
   
  public ControlTypes lockSet
  {
  get
  {
  return ControlTypes.GROUPS_ALL | ControlTypes.STAGING | ControlTypes.SAS | ControlTypes.RCS;
  }
  }
   
  public Vessel vessel
  {
  get
  {
  if (FlightGlobals.ready && FlightGlobals.ActiveVessel != null)
  {
  return FlightGlobals.ActiveVessel;
  }
   
  return null;
  }
  }
  #endregion
   
  #region MonoBehaviour LifeCycle
  protected void Awake()
  {
  this.lockID = "ARConnectionRequired";
  }
   
  protected void FixedUpdate()
  {
  if (
  requireConnectionForControl &&
  this.vessel != null &&
  !this.vessel.hasCrewCommand() &&
  this.vessel.IsControllable
  )
  {
  if (this.vessel.HasConnectedRelay())
  {
  if (currentControlLock != ControlTypes.None)
  {
  InputLockManager.RemoveControlLock(this.lockID);
  }
  }
  else
  {
  if (currentControlLock == ControlTypes.None)
  {
  InputLockManager.SetControlLock(this.lockSet, this.lockID);
  }
  }
  }
  }
   
  protected void Destroy()
  {
   
  }
  #endregion
} }
} }