Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions AtmosphereAutopilot/AppLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class AppLauncherWindow : GUIWindow
{
public AppLauncherWindow() :
base("", 3920049, new Rect(Screen.width - 280, 38, 230, 30))
{ }
{
hasCloseButton = draggable = false;
}

public void set_left(int left)
{
window.x = left;
windowPosition.x = left;
}

public bool show_while_hover = false;
Expand Down Expand Up @@ -69,7 +71,7 @@ protected override void OnGUICustom()
{
if (show_while_hover)
{
if (!window.Contains(Mouse.screenPos))
if (!windowPosition.Contains(Mouse.screenPos))
{
UnShowGUI();
show_while_hover = false;
Expand All @@ -79,13 +81,13 @@ protected override void OnGUICustom()

internal void set_x_position(float x)
{
window.x = Math.Min(x, Screen.width - window.width);
windowPosition.x = Math.Min(x, Screen.width - windowPosition.width);
}

internal void set_y_position(float y)
{
window.y = Mathf.Min(Mathf.Max(y - window.height / 2.0f, 0.0f), Screen.height - window.height);
window.x = Screen.width - window.width - 42.0f;
windowPosition.y = Mathf.Min(Mathf.Max(y - windowPosition.height / 2.0f, 0.0f), Screen.height - windowPosition.height);
windowPosition.x = Screen.width - windowPosition.width - 42.0f;
}
}
}
}
67 changes: 62 additions & 5 deletions AtmosphereAutopilot/AtmosphereAutopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License
using UnityEngine;
using System.Reflection;
using KSP.UI.Screens;
using UnityEngine.Profiling;
//using ToolbarWrapper;

namespace AtmosphereAutopilot
Expand All @@ -41,6 +42,8 @@ public sealed class AtmosphereAutopilot: MonoBehaviour

// Hotkey manager
internal AutoHotkey hotkeyManager;

public AtmosphereAutopilotSerialized serialized = new AtmosphereAutopilotSerialized();

/// <summary>
/// Get AtmosphereAutopilot addon class instance
Expand Down Expand Up @@ -172,15 +175,21 @@ void OnApplicationUnpause()

void serialize_active_modules()
{
AutoSerialization.Serialize(this.serialized, "AtmosphereAutopilotSerialized", KSPUtil.ApplicationRootPath + "GameData/AtmosphereAutopilot/Global_settings.txt",
typeof(GlobalSerializable));

if (ActiveVessel == null)
return;

foreach (var module in autopilot_module_lists[ActiveVessel].Values)
module.Serialize();

hotkeyManager.Serialize();
}

void OnDestroy()
{
//This seems to not be called reliably on shutdown, it's best to do serialization etc in sceneSwitch as well
serialize_active_modules();
AtmosphereAutopilot.Instance.BackgroundThread.Stop();
}
Expand All @@ -191,7 +200,7 @@ void sceneSwitch(GameScenes scenes)
clean_modules();
if (scenes != GameScenes.FLIGHT)
{
mainMenuClose ();
mainMenuClose();
ActiveVessel = null;
AtmosphereAutopilot.Instance.BackgroundThread.Pause();
}
Expand Down Expand Up @@ -256,10 +265,31 @@ public Dictionary<Type, AutopilotModule> getVesselModules(Vessel v)


#region AppLauncherSection

public sealed class AtmosphereAutopilotSerialized {
[GlobalSerializable("compact_gui")]
public bool compact_gui = false;
}

//These values are not serialized, only deserialized, so they can be read from the file during scene changes without being overwritten.
//Use AtmosphereAutopilotSerialized for values that should be serialized during scene changes.

[GlobalSerializable("use_neo_gui")]
public bool use_neo_gui = false;

[GlobalSerializable("master_switch_key_toggles_gui")]
public bool master_switch_key_toggles_gui = true;

[GlobalSerializable("secondary_wing_level_snap_angle")]
public float secondary_wing_level_snap_angle = 45.0f;

[GlobalSerializable("scroll_wheel_number_field_increment_vertical")]
public float scroll_wheel_number_field_increment_vertical = 0.5f;

[GlobalSerializable("scroll_wheel_number_field_increment_horizontal")]
public float scroll_wheel_number_field_increment_horizontal = 0.1f;


ApplicationLauncherButton launcher_btn;

private Texture launcher_btn_textore_off = null;
Expand Down Expand Up @@ -288,10 +318,13 @@ internal AssetBundle prefabs
// Called when applauncher is ready for population
void onAppLauncherLoad()
{
// deserialize use_neo_gui flag
// deserialize gui options
AutoSerialization.Deserialize(this, "AtmosphereAutopilot",
KSPUtil.ApplicationRootPath + "GameData/AtmosphereAutopilot/Global_settings.txt",
typeof(GlobalSerializable), null);
AutoSerialization.Deserialize(this.serialized, "AtmosphereAutopilotSerialized",
KSPUtil.ApplicationRootPath + "GameData/AtmosphereAutopilot/Global_settings.txt",
typeof(GlobalSerializable), null);

if (prefabs == null)
{
Expand Down Expand Up @@ -487,12 +520,14 @@ void OnGUI()
GUIStyles.Init();
styles_init = true;
}

if (ActiveVessel == null)
return;
if (!autopilot_module_lists.ContainsKey(ActiveVessel))
return;
if (!HighLogic.LoadedSceneIsFlight)
return;

GUIStyles.set_colors();
applauncher.OnGUI();
foreach (var pair in autopilot_module_lists[ActiveVessel])
Expand All @@ -514,20 +549,42 @@ void OnShowUI()
}

#endregion

const string lockName = "AircraftAutopilotCameraLock";

bool shouldUnlockCamera = true;

void Update()
{
if (!HighLogic.LoadedSceneIsFlight)
return;
if (ActiveVessel == null)
if (shouldUnlockCamera) InputLockManager.RemoveControlLock(lockName);
else shouldUnlockCamera = true;

if (!HighLogic.LoadedSceneIsFlight || ActiveVessel == null)
return;

if (autopilot_module_lists.ContainsKey(ActiveVessel))
{
var module_list = autopilot_module_lists[ActiveVessel].Values.ToList();
foreach (var module in module_list)
if (module.Active || module is TopModuleManager)
module.OnUpdate();
}

//if (Input.GetKeyDown(KeyCode.Escape)) GUI.FocusControl(null); //Doesn't work because focused text fields block all input, and there seems to be no way around that with immediate-mode GUI other than using Input.eatKeyPressOnTextFieldFocus, which would probably cause all text-field input to also be sent tothe main game
}

public void LockCameraControlsOnHover() {
if (Event.current.type == EventType.Repaint && shouldUnlockCamera && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
InputLockManager.SetControlLock(ControlTypes.CAMERACONTROLS, lockName);
shouldUnlockCamera = false;
}
}

public void LockCameraControls() {
if (shouldUnlockCamera) {
InputLockManager.SetControlLock(ControlTypes.CAMERACONTROLS, lockName);
shouldUnlockCamera = false;
}
}
}
}
1 change: 1 addition & 0 deletions AtmosphereAutopilot/AtmosphereAutopilot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\AtmosphereAutopilot.XML</DocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
18 changes: 8 additions & 10 deletions AtmosphereAutopilot/AutoHotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public HotkeyField(FieldInfo field, AutoHotkeyAttr attr)

protected override void _drawGUI(int id)
{
close_button();
GUILayout.BeginVertical();

int new_editing_index = editing_index;
Expand All @@ -83,7 +82,6 @@ protected override void _drawGUI(int id)
}

GUILayout.EndVertical();
GUI.DragWindow();

// now perform input binding
editing_index = new_editing_index;
Expand Down Expand Up @@ -117,24 +115,24 @@ protected override void _drawGUI(int id)
[GlobalSerializable("window_x")]
public float WindowLeft
{
get { return window.xMin; }
get { return windowPosition.xMin; }
set
{
float width = window.width;
window.xMin = value;
window.xMax = window.xMin + width;
float width = windowPosition.width;
windowPosition.xMin = value;
windowPosition.xMax = windowPosition.xMin + width;
}
}

[GlobalSerializable("window_y")]
public float WindowTop
{
get { return window.yMin; }
get { return windowPosition.yMin; }
set
{
float height = window.height;
window.yMin = value;
window.yMax = window.yMin + height;
float height = windowPosition.height;
windowPosition.yMin = value;
windowPosition.yMax = windowPosition.yMin + height;
}
}

Expand Down
4 changes: 1 addition & 3 deletions AtmosphereAutopilot/AutoSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,13 @@ public static void Serialize(object obj, string node_name, string filename, Type
ConfigNode fileNode = ConfigNode.Load(filename);
if (fileNode == null)
fileNode = new ConfigNode();
else
fileNode.RemoveNode(node_name);
ConfigNode node = new ConfigNode(node_name);
SerializeToNode(node, obj, attribute_type);
if (OnSerialize != null)
OnSerialize(node, attribute_type);
if (node.HasData)
{
fileNode.AddNode(node);
fileNode.SetNode(node.name, node, true);
fileNode.Save(filename);
}
}
Expand Down
1 change: 1 addition & 0 deletions AtmosphereAutopilot/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static Quaternion normalizeQuaternion(Quaternion quat)
/// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
/// </summary>
/// <param name="q">Rotation</param>
/// <param name="mat"></param>
/// <returns>Transformation matrix representing rotation</returns>
public static void rotationMatrix(Quaternion q, Matrix mat)
{
Expand Down
71 changes: 61 additions & 10 deletions AtmosphereAutopilot/GUI/AutoGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,23 @@ public abstract class GUIWindow : IWindow
string wndname;
int wnd_id;
bool gui_shown = false;
protected Rect window;
protected Rect windowPosition;

protected bool hasCloseButton = true, draggable = true;

public const float WINDOW_TITLE_BAR_HEIGHT = 17.0f; //For the close button and window dragging

/// <summary>
/// Create window instance.
/// </summary>
/// <param name="wndname">Window header</param>
/// <param name="wnd_id">Unique for Unity engine id</param>
/// <param name="window">Initial window position rectangle</param>
internal GUIWindow(string wndname, int wnd_id, Rect window)
/// <param name="windowPosition">Initial window position rectangle</param>
internal GUIWindow(string wndname, int wnd_id, Rect windowPosition)
{
this.wndname = wndname;
this.wnd_id = wnd_id;
this.window = window;
this.windowPosition = windowPosition;
}

/// <summary>
Expand All @@ -127,10 +131,14 @@ public void OnGUI()
return;

// forbid windows not on screen
window.xMin = Common.Clampf(window.xMin, 5.0f - window.width, Screen.width - 5.0f);
window.yMin = Common.Clampf(window.yMin, 5.0f - window.height, Screen.height - 5.0f);

window = GUILayout.Window(wnd_id, window, _drawGUI, wndname);
windowPosition.xMin = Common.Clampf(windowPosition.xMin, 5.0f - windowPosition.width, Screen.width - 5.0f);
windowPosition.yMin = Common.Clampf(windowPosition.yMin, 5.0f - windowPosition.height, Screen.height - 5.0f);

windowPosition = GUILayout.Window(wnd_id, windowPosition, (int windowId) => {
if (hasCloseButton) close_button();
_drawGUI(windowId);
if (draggable && Event.current.button == 0 /* LMB */) GUI.DragWindow(new Rect(0, 0, 9000000.0f, WINDOW_TITLE_BAR_HEIGHT));
}, wndname);
OnGUICustom();
}

Expand All @@ -139,7 +147,8 @@ public void OnGUI()
/// </summary>
protected void close_button()
{
Rect close_btn_rect = new Rect(window.width - 16.0f, 1.0f, 15.0f, 16.0f);
const float BUTTON_SIZE = WINDOW_TITLE_BAR_HEIGHT - 1.0f;
Rect close_btn_rect = new Rect(windowPosition.width - BUTTON_SIZE, 1.0f, BUTTON_SIZE - 1.0f, BUTTON_SIZE);
bool close = GUI.Button(close_btn_rect, "x", GUIStyles.toggleButtonStyle);
if (close)
this.UnShowGUI();
Expand All @@ -151,7 +160,7 @@ protected void close_button()
protected virtual void OnGUICustom() { }

/// <summary>
// Executed on every OnGUI regardless of gui_shown or UIHidden
/// Executed on every OnGUI regardless of gui_shown or UIHidden
/// </summary>
protected virtual void OnGUICustomAlways() { }

Expand Down Expand Up @@ -231,6 +240,48 @@ public static void AutoDrawObject(object obj)
draw_element(field, obj);
}

public static void HandleToggleButton(bool currentToggleValue, string label, GUIStyle style, Action<bool> onLeftClick, Action<bool> onRightClick) {
bool newToggleValue = GUILayout.Toggle(currentToggleValue, label, style);
if (newToggleValue != currentToggleValue) {
if (Event.current.button == 1 /* RMB */) onRightClick(newToggleValue);
else onLeftClick(newToggleValue);
}
}

//GUILayoutUtility.GetLastRect is supposed to only work properly in repaint events, so we might just be getting lucky here, or the engine's changed but documentation hasn't
public static float GetNumberTextBoxScrollWheelChange() {
if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
if (Event.current.type == EventType.Repaint) AtmosphereAutopilot.Instance.LockCameraControls();
else if (Event.current.type == EventType.ScrollWheel) {
bool smallIncrement = Input.GetKey(KeyCode.RightControl), //Alt causes the mouse wheel to FoV-zoom, so we don't want to use that
largeIncrement = Input.GetKey(KeyCode.RightShift);
return (Event.current.delta.y / 3.0f * (smallIncrement ? 0.1f : largeIncrement ? 1.0f : AtmosphereAutopilot.Instance.scroll_wheel_number_field_increment_vertical)) +
(Event.current.delta.x / 3.0f * (smallIncrement ? 0.05f : largeIncrement ? 0.2f : AtmosphereAutopilot.Instance.scroll_wheel_number_field_increment_horizontal)); //Unity seems to not support horizontal scroll wheel :(
}
}
return 0;
}

//See GetNumberTextBoxScrollWheelChange for a potential bug with this
public static bool CheckForRightClick() {
if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
if (Event.current.type == EventType.Repaint) AtmosphereAutopilot.Instance.LockCameraControls();
else return Event.current.type == EventType.MouseDown && Event.current.button == 1;
}
return false;
}

//See GetNumberTextBoxScrollWheelChange for a potential bug with this
public static void CheckForClick(Callback onLeftClick, Callback onRightClick) {
if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
if (Event.current.type == EventType.Repaint) AtmosphereAutopilot.Instance.LockCameraControls();
else if (Event.current.type == EventType.MouseDown) {
if (Event.current.button == 0) onLeftClick();
else if (Event.current.button == 1) onRightClick();
}
}
}


#region FieldPropertyUniversal

Expand Down
Loading