From 3220d0ac32ea6fe502c8d19d07a1069c884d52fa Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Fri, 1 Apr 2016 17:27:00 +0100 Subject: [PATCH 01/27] Testing new GUI --- .../InfernalRobotics/Gui/ControlsGUI.cs | 39 +- .../InfernalRobotics/Gui/PanelDragger.cs | 62 ++ .../InfernalRobotics/Gui/PanelFocuser.cs | 22 + .../InfernalRobotics/Gui/ToolbarWrapper.cs | 800 ------------------ .../InfernalRobotics/Gui/UIPrefabLoader.cs | 125 +++ .../InfernalRobotics/Gui/WindowManager.cs | 308 +++++++ .../InfernalRobotics/InfernalRobotics.csproj | 5 +- .../InfernalRobotics/Module/ModuleIRServo.cs | 2 +- .../InfernalRobotics/Utility/TextureLoader.cs | 2 +- 9 files changed, 532 insertions(+), 833 deletions(-) create mode 100644 InfernalRobotics/InfernalRobotics/Gui/PanelDragger.cs create mode 100644 InfernalRobotics/InfernalRobotics/Gui/PanelFocuser.cs delete mode 100644 InfernalRobotics/InfernalRobotics/Gui/ToolbarWrapper.cs create mode 100644 InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs create mode 100644 InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index da185058..69c899c6 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -54,7 +54,6 @@ public class ControlsGUI : MonoBehaviour private static ControlsGUI guiController; private static bool guiSetupDone; - private IButton irMinimizeButton; private ApplicationLauncherButton button; private static Texture2D buttonTexture; private bool guiGroupEditorEnabled; @@ -181,22 +180,14 @@ private void Awake() return; } - if (ToolbarManager.ToolbarAvailable) - { - irMinimizeButton = ToolbarManager.Instance.add("sirkut", "IREditorButton"); - irMinimizeButton.TexturePath = "MagicSmokeIndustries/Textures/icon_button_small"; - irMinimizeButton.ToolTip = "Infernal Robotics"; - irMinimizeButton.Visibility = new GameScenesVisibility(GameScenes.EDITOR, GameScenes.FLIGHT); - irMinimizeButton.OnClick += e => GUIEnabled = !GUIEnabled; - } - else - { - GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequestedForAppLauncher); + - GameEvents.onGUIApplicationLauncherReady.Add (AddAppLauncherButton); + GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequestedForAppLauncher); - Logger.Log("[GUI] Added Toolbar GameEvents Handlers", Logger.Level.Debug); - } + GameEvents.onGUIApplicationLauncherReady.Add (AddAppLauncherButton); + + Logger.Log("[GUI] Added Toolbar GameEvents Handlers", Logger.Level.Debug); + GameEvents.onShowUI.Add(OnShowUI); GameEvents.onHideUI.Add(OnHideUI); @@ -273,16 +264,9 @@ private void OnDestroy() { Logger.Log("[GUI] destroy"); - if (ToolbarManager.ToolbarAvailable) - { - irMinimizeButton.Destroy(); - } - else - { - GameEvents.onGUIApplicationLauncherReady.Remove (AddAppLauncherButton); - GameEvents.onGameSceneLoadRequested.Remove(OnGameSceneLoadRequestedForAppLauncher); - DestroyAppLauncherButton(); - } + GameEvents.onGUIApplicationLauncherReady.Remove (AddAppLauncherButton); + GameEvents.onGameSceneLoadRequested.Remove(OnGameSceneLoadRequestedForAppLauncher); + DestroyAppLauncherButton(); GameEvents.onShowUI.Remove(OnShowUI); GameEvents.onHideUI.Remove(OnHideUI); @@ -1396,8 +1380,6 @@ private void OnGUI() { if (!ServoController.APIReady) { - if (ToolbarManager.ToolbarAvailable) - irMinimizeButton.Visible = false; if (button != null) { button.VisibleInScenes = ApplicationLauncher.AppScenes.SPH | ApplicationLauncher.AppScenes.VAB; @@ -1405,9 +1387,6 @@ private void OnGUI() return; } //at this point we have ServoController active with at least one group in it. - if (ToolbarManager.ToolbarAvailable) - irMinimizeButton.Visible = true; - if (button != null) { button.VisibleInScenes = ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.SPH | ApplicationLauncher.AppScenes.VAB; diff --git a/InfernalRobotics/InfernalRobotics/Gui/PanelDragger.cs b/InfernalRobotics/InfernalRobotics/Gui/PanelDragger.cs new file mode 100644 index 00000000..b604e74c --- /dev/null +++ b/InfernalRobotics/InfernalRobotics/Gui/PanelDragger.cs @@ -0,0 +1,62 @@ +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; +using System.Collections; + +namespace InfernalRobotics.Gui +{ + + public class PanelDragger : MonoBehaviour, IPointerDownHandler, IDragHandler + { + + private Vector2 pointerOffset; + private RectTransform canvasRectTransform; + private RectTransform panelRectTransform; + + void Awake() + { + Canvas canvas = GetComponentInParent(); + if (canvas != null) + { + canvasRectTransform = MainCanvasUtil.MainCanvasRect; + panelRectTransform = transform.parent as RectTransform; + } + } + + public void OnPointerDown(PointerEventData data) + { + panelRectTransform.SetAsLastSibling(); + RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out pointerOffset); + } + + public void OnDrag(PointerEventData data) + { + if (panelRectTransform == null) + return; + + Vector2 pointerPostion = ClampToWindow(data); + + Vector2 localPointerPosition; + if (RectTransformUtility.ScreenPointToLocalPointInRectangle( + canvasRectTransform, pointerPostion, data.pressEventCamera, out localPointerPosition + )) + { + panelRectTransform.localPosition = localPointerPosition - pointerOffset; + } + } + + Vector2 ClampToWindow(PointerEventData data) + { + Vector2 rawPointerPosition = data.position; + + Vector3[] canvasCorners = new Vector3[4]; + canvasRectTransform.GetWorldCorners(canvasCorners); + + float clampedX = Mathf.Clamp(rawPointerPosition.x, canvasCorners[0].x, canvasCorners[2].x); + float clampedY = Mathf.Clamp(rawPointerPosition.y, canvasCorners[0].y, canvasCorners[2].y); + + Vector2 newPointerPosition = new Vector2(clampedX, clampedY); + return newPointerPosition; + } + } +} \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Gui/PanelFocuser.cs b/InfernalRobotics/InfernalRobotics/Gui/PanelFocuser.cs new file mode 100644 index 00000000..a4b7a9f9 --- /dev/null +++ b/InfernalRobotics/InfernalRobotics/Gui/PanelFocuser.cs @@ -0,0 +1,22 @@ +using UnityEngine; +using UnityEngine.EventSystems; + +namespace InfernalRobotics.Gui +{ + + public class PanelFocuser : MonoBehaviour, IPointerDownHandler + { + + private RectTransform panel; + + void Awake() + { + panel = GetComponent(); + } + + public void OnPointerDown(PointerEventData data) + { + panel.SetAsLastSibling(); + } + } +} \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Gui/ToolbarWrapper.cs b/InfernalRobotics/InfernalRobotics/Gui/ToolbarWrapper.cs deleted file mode 100644 index 0e7367e3..00000000 --- a/InfernalRobotics/InfernalRobotics/Gui/ToolbarWrapper.cs +++ /dev/null @@ -1,800 +0,0 @@ -/* -Copyright (c) 2013-2014, Maik Schreiber -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -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 materials provided with the distribution. - -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 -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 -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 -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using UnityEngine; - -namespace InfernalRobotics.Gui -{ - //18.3 - - - /**********************************************************\ - * --- DO NOT EDIT BELOW THIS COMMENT --- * - * * - * This file contains classes and interfaces to use the * - * Toolbar Plugin without creating a hard dependency on it. * - * * - * There is nothing in this file that needs to be edited * - * by hand. * - * * - * --- DO NOT EDIT BELOW THIS COMMENT --- * - \**********************************************************/ - - - /// - /// The global tool bar manager. - /// - public partial class ToolbarManager : IToolbarManager - { - /// - /// Whether the Toolbar Plugin is available. - /// - public static bool ToolbarAvailable - { - get - { - if (toolbarAvailable == null) - { - toolbarAvailable = Instance != null; - } - return (bool) toolbarAvailable; - } - } - - /// - /// The global tool bar manager instance. - /// - public static IToolbarManager Instance - { - get - { - if ((toolbarAvailable != false) && (instance_ == null)) - { - Type type = ToolbarTypes.getType("Toolbar.ToolbarManager"); - if (type != null) - { - object realToolbarManager = ToolbarTypes.getStaticProperty(type, "Instance") - .GetValue(null, null); - instance_ = new ToolbarManager(realToolbarManager); - } - } - return instance_; - } - } - } - - #region interfaces - - /// - /// A toolbar manager. - /// - public interface IToolbarManager - { - /// - /// Adds a new button. - /// - /// - /// To replace an existing button, just add a new button using the old button's namespace and ID. - /// Note that the new button will inherit the screen position of the old button. - /// - /// - /// The new button's namespace. This is usually the plugin's name. Must not include special characters - /// like '.' - /// - /// - /// The new button's ID. This ID must be unique across all buttons in the namespace. Must not include - /// special characters like '.' - /// - /// The button created. - IButton add(string ns, string id); - } - - /// - /// Represents a clickable button. - /// - public interface IButton - { - /// - /// The text displayed on the button. Set to null to hide text. - /// - /// - /// The text can be changed at any time to modify the button's appearance. Note that since this will also - /// modify the button's size, this feature should be used sparingly, if at all. - /// - /// - string Text { set; get; } - - /// - /// The color the button text is displayed with. Defaults to Color.white. - /// - /// - /// The text color can be changed at any time to modify the button's appearance. - /// - Color TextColor { set; get; } - - /// - /// The path of a texture file to display an icon on the button. Set to null to hide icon. - /// - /// - /// - /// A texture path on a button will have precedence over text. That is, if both text and texture path - /// have been set on a button, the button will show the texture, not the text. - /// - /// - /// The texture size must not exceed 24x24 pixels. - /// - /// - /// The texture path must be relative to the "GameData" directory, and must not specify a file name suffix. - /// Valid example: MyAddon/Textures/icon_mybutton - /// - /// - /// The texture path can be changed at any time to modify the button's appearance. - /// - /// - /// - string TexturePath { set; get; } - - /// - /// The button's tool tip text. Set to null if no tool tip is desired. - /// - /// - /// Tool Tip Text Should Always Use Headline Style Like This. - /// - string ToolTip { set; get; } - - /// - /// Whether this button is currently visible or not. Can be used in addition to or as a replacement for - /// . - /// - /// - /// Setting this property to true does not affect the player's ability to hide the button using the configuration. - /// Conversely, setting this property to false does not enable the player to show the button using the configuration. - /// - bool Visible { set; get; } - - /// - /// Determines this button's visibility. Can be used in addition to or as a replacement for . - /// - /// - /// The return value from IVisibility.Visible is subject to the same rules as outlined for - /// . - /// - IVisibility Visibility { set; get; } - - /// - /// Whether this button is currently effectively visible or not. This is a combination of - /// and . - /// - /// - /// Note that the toolbar is not visible in certain game scenes, for example the loading screens. This property - /// does not reflect button invisibility in those scenes. In addition, this property does not reflect the - /// player's configuration of the button's visibility. - /// - bool EffectivelyVisible { get; } - - /// - /// Whether this button is currently enabled (clickable) or not. This does not affect the player's ability to - /// position the button on their toolbar. - /// - bool Enabled { set; get; } - - /// - /// Whether this button is currently "important." Set to false to return to normal button behaviour. - /// - /// - /// - /// This can be used to temporarily force the button to be shown on screen regardless of the toolbar being - /// currently in auto-hidden mode. For example, a button that signals the arrival of a private message in - /// a chat room could mark itself as "important" as long as the message has not been read. - /// - /// - /// Setting this property does not change the appearance of the button. Use to - /// change the button's icon. - /// - /// - /// Setting this property to true does not affect the player's ability to hide the button using the - /// configuration. - /// - /// - /// This feature should be used only sparingly, if at all, since it forces the button to be displayed on - /// screen even when it normally wouldn't. - /// - /// - bool Important { set; get; } - - /// - /// A drawable that is tied to the current button. This can be anything from a popup menu to - /// an informational window. Set to null to hide the drawable. - /// - IDrawable Drawable { set; get; } - - /// - /// Event handler that can be registered with to receive "on click" events. - /// - /// - /// - /// IButton button = ... - /// button.OnClick += (e) => { - /// Debug.Log("button clicked, mouseButton: " + e.MouseButton); - /// }; - /// - /// - event ClickHandler OnClick; - - /// - /// Event handler that can be registered with to receive "on mouse enter" events. - /// - /// - /// - /// IButton button = ... - /// button.OnMouseEnter += (e) => { - /// Debug.Log("mouse entered button"); - /// }; - /// - /// - event MouseEnterHandler OnMouseEnter; - - /// - /// Event handler that can be registered with to receive "on mouse leave" events. - /// - /// - /// - /// IButton button = ... - /// button.OnMouseLeave += (e) => { - /// Debug.Log("mouse left button"); - /// }; - /// - /// - event MouseLeaveHandler OnMouseLeave; - - /// - /// Permanently destroys this button so that it is no longer displayed. - /// Should be used when a plugin is stopped to remove leftover buttons. - /// - void Destroy(); - } - - /// - /// A drawable that is tied to a particular button. This can be anything from a popup menu - /// to an informational window. - /// - public interface IDrawable - { - /// - /// Update any information. This is called once per frame. - /// - void Update(); - - /// - /// Draws GUI widgets for this drawable. This is the equivalent to the OnGUI() message in - /// . - /// - /// - /// The drawable will be positioned near its parent toolbar according to the drawable's current - /// width/height. - /// - /// The left/top position of where to draw this drawable. - /// The current width/height of this drawable. - Vector2 Draw(Vector2 position); - } - - #endregion - - #region events - - /// - /// Event describing a click on a button. - /// - public partial class ClickEvent : EventArgs - { - /// - /// The button that has been clicked. - /// - public readonly IButton Button; - - /// - /// The mouse button which the button was clicked with. - /// - /// - /// Is 0 for left mouse button, 1 for right mouse button, and 2 for middle mouse button. - /// - public readonly int MouseButton; - } - - /// - /// An event handler that is invoked whenever a button has been clicked. - /// - /// An event describing the button click. - public delegate void ClickHandler(ClickEvent e); - - /// - /// Event describing the mouse pointer moving about a button. - /// - public abstract partial class MouseMoveEvent - { - /// - /// The button in question. - /// - public readonly IButton button; - } - - /// - /// Event describing the mouse pointer entering a button's area. - /// - public partial class MouseEnterEvent : MouseMoveEvent - { - } - - /// - /// Event describing the mouse pointer leaving a button's area. - /// - public partial class MouseLeaveEvent : MouseMoveEvent - { - } - - /// - /// An event handler that is invoked whenever the mouse pointer enters a button's area. - /// - /// An event describing the mouse pointer entering. - public delegate void MouseEnterHandler(MouseEnterEvent e); - - /// - /// An event handler that is invoked whenever the mouse pointer leaves a button's area. - /// - /// An event describing the mouse pointer leaving. - public delegate void MouseLeaveHandler(MouseLeaveEvent e); - - #endregion - - #region visibility - - /// - /// Determines visibility of a button. - /// - /// - public interface IVisibility - { - /// - /// Whether a button is currently visible or not. - /// - /// - bool Visible { get; } - } - - /// - /// Determines visibility of a button in relation to the currently running game scene. - /// - /// - /// - /// IButton button = ... - /// button.Visibility = new GameScenesVisibility(GameScenes.EDITOR, GameScenes.SPH); - /// - /// - /// - public class GameScenesVisibility : IVisibility - { - private readonly object realGameScenesVisibility; - private readonly PropertyInfo visibleProperty; - - public GameScenesVisibility(params GameScenes[] gameScenes) - { - Type gameScenesVisibilityType = ToolbarTypes.getType("Toolbar.GameScenesVisibility"); - realGameScenesVisibility = Activator.CreateInstance(gameScenesVisibilityType, new object[] {gameScenes}); - visibleProperty = ToolbarTypes.getProperty(gameScenesVisibilityType, "Visible"); - } - - public bool Visible - { - get { return (bool) visibleProperty.GetValue(realGameScenesVisibility, null); } - } - } - - #endregion - - #region drawable - - /// - /// A drawable that draws a popup menu. - /// - public class PopupMenuDrawable : IDrawable - { - private readonly MethodInfo addOptionMethod; - private readonly MethodInfo addSeparatorMethod; - private readonly MethodInfo destroyMethod; - private readonly MethodInfo drawMethod; - private readonly EventInfo onAnyOptionClickedEvent; - private readonly object realPopupMenuDrawable; - private readonly MethodInfo updateMethod; - - public PopupMenuDrawable() - { - Type popupMenuDrawableType = ToolbarTypes.getType("Toolbar.PopupMenuDrawable"); - realPopupMenuDrawable = Activator.CreateInstance(popupMenuDrawableType, null); - updateMethod = ToolbarTypes.getMethod(popupMenuDrawableType, "Update"); - drawMethod = ToolbarTypes.getMethod(popupMenuDrawableType, "Draw"); - addOptionMethod = ToolbarTypes.getMethod(popupMenuDrawableType, "AddOption"); - addSeparatorMethod = ToolbarTypes.getMethod(popupMenuDrawableType, "AddSeparator"); - destroyMethod = ToolbarTypes.getMethod(popupMenuDrawableType, "Destroy"); - onAnyOptionClickedEvent = ToolbarTypes.getEvent(popupMenuDrawableType, "OnAnyOptionClicked"); - } - - public void Update() - { - updateMethod.Invoke(realPopupMenuDrawable, null); - } - - public Vector2 Draw(Vector2 position) - { - return (Vector2) drawMethod.Invoke(realPopupMenuDrawable, new object[] {position}); - } - - /// - /// Event handler that can be registered with to receive "any menu option clicked" events. - /// - public event Action OnAnyOptionClicked - { - add { onAnyOptionClickedEvent.AddEventHandler(realPopupMenuDrawable, value); } - remove { onAnyOptionClickedEvent.RemoveEventHandler(realPopupMenuDrawable, value); } - } - - /// - /// Adds a new option to the popup menu. - /// - /// The text of the option. - /// A button that can be used to register clicks on the menu option. - public IButton AddOption(string text) - { - object realButton = addOptionMethod.Invoke(realPopupMenuDrawable, new object[] {text}); - return new Button(realButton, new ToolbarTypes()); - } - - /// - /// Adds a separator to the popup menu. - /// - public void AddSeparator() - { - addSeparatorMethod.Invoke(realPopupMenuDrawable, null); - } - - /// - /// Destroys this drawable. This must always be called before disposing of this drawable. - /// - public void Destroy() - { - destroyMethod.Invoke(realPopupMenuDrawable, null); - } - } - - #endregion - - #region private implementations - - public partial class ToolbarManager : IToolbarManager - { - private static bool? toolbarAvailable; - private static IToolbarManager instance_; - - private readonly MethodInfo addMethod; - private readonly Dictionary buttons = new Dictionary(); - private readonly object realToolbarManager; - private readonly ToolbarTypes types = new ToolbarTypes(); - - private ToolbarManager(object realToolbarManager) - { - this.realToolbarManager = realToolbarManager; - - addMethod = ToolbarTypes.getMethod(types.iToolbarManagerType, "add"); - } - - public IButton add(string ns, string id) - { - object realButton = addMethod.Invoke(realToolbarManager, new object[] {ns, id}); - IButton button = new Button(realButton, types); - buttons.Add(realButton, button); - return button; - } - } - - internal class Button : IButton - { - private readonly object realButton; - private readonly Delegate realClickHandler; - private readonly Delegate realMouseEnterHandler; - private readonly Delegate realMouseLeaveHandler; - private readonly ToolbarTypes types; - private IDrawable drawable_; - private IVisibility visibility_; - - internal Button(object realButton, ToolbarTypes types) - { - this.realButton = realButton; - this.types = types; - - realClickHandler = attachEventHandler(types.button.onClickEvent, "clicked", realButton); - realMouseEnterHandler = attachEventHandler(types.button.onMouseEnterEvent, "mouseEntered", realButton); - realMouseLeaveHandler = attachEventHandler(types.button.onMouseLeaveEvent, "mouseLeft", realButton); - } - - public string Text - { - set { types.button.textProperty.SetValue(realButton, value, null); } - get { return (string) types.button.textProperty.GetValue(realButton, null); } - } - - public Color TextColor - { - set { types.button.textColorProperty.SetValue(realButton, value, null); } - get { return (Color) types.button.textColorProperty.GetValue(realButton, null); } - } - - public string TexturePath - { - set { types.button.texturePathProperty.SetValue(realButton, value, null); } - get { return (string) types.button.texturePathProperty.GetValue(realButton, null); } - } - - public string ToolTip - { - set { types.button.toolTipProperty.SetValue(realButton, value, null); } - get { return (string) types.button.toolTipProperty.GetValue(realButton, null); } - } - - public bool Visible - { - set { types.button.visibleProperty.SetValue(realButton, value, null); } - get { return (bool) types.button.visibleProperty.GetValue(realButton, null); } - } - - public IVisibility Visibility - { - set - { - object functionVisibility = null; - if (value != null) - { - functionVisibility = Activator.CreateInstance(types.functionVisibilityType, - new object[] {new Func(() => value.Visible)}); - } - types.button.visibilityProperty.SetValue(realButton, functionVisibility, null); - visibility_ = value; - } - get { return visibility_; } - } - - public bool EffectivelyVisible - { - get { return (bool) types.button.effectivelyVisibleProperty.GetValue(realButton, null); } - } - - public bool Enabled - { - set { types.button.enabledProperty.SetValue(realButton, value, null); } - get { return (bool) types.button.enabledProperty.GetValue(realButton, null); } - } - - public bool Important - { - set { types.button.importantProperty.SetValue(realButton, value, null); } - get { return (bool) types.button.importantProperty.GetValue(realButton, null); } - } - - public IDrawable Drawable - { - set - { - object functionDrawable = null; - if (value != null) - { - functionDrawable = Activator.CreateInstance(types.functionDrawableType, new object[] - { - new Action(() => value.Update()), - new Func(pos => value.Draw(pos)) - }); - } - types.button.drawableProperty.SetValue(realButton, functionDrawable, null); - drawable_ = value; - } - get { return drawable_; } - } - - public event ClickHandler OnClick; - - public event MouseEnterHandler OnMouseEnter; - - public event MouseLeaveHandler OnMouseLeave; - - public void Destroy() - { - detachEventHandler(types.button.onClickEvent, realClickHandler, realButton); - detachEventHandler(types.button.onMouseEnterEvent, realMouseEnterHandler, realButton); - detachEventHandler(types.button.onMouseLeaveEvent, realMouseLeaveHandler, realButton); - - types.button.destroyMethod.Invoke(realButton, null); - } - - private Delegate attachEventHandler(EventInfo @event, string methodName, object realButton) - { - MethodInfo method = GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); - Delegate d = Delegate.CreateDelegate(@event.EventHandlerType, this, method); - @event.AddEventHandler(realButton, d); - return d; - } - - private void clicked(object realEvent) - { - if (OnClick != null) - { - OnClick(new ClickEvent(realEvent, this)); - } - } - - private void mouseEntered(object realEvent) - { - if (OnMouseEnter != null) - { - OnMouseEnter(new MouseEnterEvent(this)); - } - } - - private void mouseLeft(object realEvent) - { - if (OnMouseLeave != null) - { - OnMouseLeave(new MouseLeaveEvent(this)); - } - } - - private void detachEventHandler(EventInfo @event, Delegate d, object realButton) - { - @event.RemoveEventHandler(realButton, d); - } - } - - public partial class ClickEvent : EventArgs - { - internal ClickEvent(object realEvent, IButton button) - { - Type type = realEvent.GetType(); - Button = button; - MouseButton = - (int) type.GetField("MouseButton", BindingFlags.Public | BindingFlags.Instance).GetValue(realEvent); - } - } - - public abstract partial class MouseMoveEvent : EventArgs - { - internal MouseMoveEvent(IButton button) - { - this.button = button; - } - } - - public partial class MouseEnterEvent : MouseMoveEvent - { - internal MouseEnterEvent(IButton button) - : base(button) - { - } - } - - public partial class MouseLeaveEvent : MouseMoveEvent - { - internal MouseLeaveEvent(IButton button) - : base(button) - { - } - } - - internal class ToolbarTypes - { - internal readonly ButtonTypes button; - internal readonly Type functionDrawableType; - internal readonly Type functionVisibilityType; - internal readonly Type iToolbarManagerType; - - internal ToolbarTypes() - { - iToolbarManagerType = getType("Toolbar.IToolbarManager"); - functionVisibilityType = getType("Toolbar.FunctionVisibility"); - functionDrawableType = getType("Toolbar.FunctionDrawable"); - - Type iButtonType = getType("Toolbar.IButton"); - button = new ButtonTypes(iButtonType); - } - - internal static Type getType(string name) - { - return AssemblyLoader.loadedAssemblies - .SelectMany(a => a.assembly.GetExportedTypes()) - .SingleOrDefault(t => t.FullName == name); - } - - internal static PropertyInfo getProperty(Type type, string name) - { - return type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance); - } - - internal static PropertyInfo getStaticProperty(Type type, string name) - { - return type.GetProperty(name, BindingFlags.Public | BindingFlags.Static); - } - - internal static EventInfo getEvent(Type type, string name) - { - return type.GetEvent(name, BindingFlags.Public | BindingFlags.Instance); - } - - internal static MethodInfo getMethod(Type type, string name) - { - return type.GetMethod(name, BindingFlags.Public | BindingFlags.Instance); - } - } - - internal class ButtonTypes - { - internal readonly MethodInfo destroyMethod; - internal readonly PropertyInfo drawableProperty; - internal readonly PropertyInfo effectivelyVisibleProperty; - internal readonly PropertyInfo enabledProperty; - internal readonly Type iButtonType; - internal readonly PropertyInfo importantProperty; - internal readonly EventInfo onClickEvent; - internal readonly EventInfo onMouseEnterEvent; - internal readonly EventInfo onMouseLeaveEvent; - internal readonly PropertyInfo textColorProperty; - internal readonly PropertyInfo textProperty; - internal readonly PropertyInfo texturePathProperty; - internal readonly PropertyInfo toolTipProperty; - internal readonly PropertyInfo visibilityProperty; - internal readonly PropertyInfo visibleProperty; - - internal ButtonTypes(Type iButtonType) - { - this.iButtonType = iButtonType; - - textProperty = ToolbarTypes.getProperty(iButtonType, "Text"); - textColorProperty = ToolbarTypes.getProperty(iButtonType, "TextColor"); - texturePathProperty = ToolbarTypes.getProperty(iButtonType, "TexturePath"); - toolTipProperty = ToolbarTypes.getProperty(iButtonType, "ToolTip"); - visibleProperty = ToolbarTypes.getProperty(iButtonType, "Visible"); - visibilityProperty = ToolbarTypes.getProperty(iButtonType, "Visibility"); - effectivelyVisibleProperty = ToolbarTypes.getProperty(iButtonType, "EffectivelyVisible"); - enabledProperty = ToolbarTypes.getProperty(iButtonType, "Enabled"); - importantProperty = ToolbarTypes.getProperty(iButtonType, "Important"); - drawableProperty = ToolbarTypes.getProperty(iButtonType, "Drawable"); - onClickEvent = ToolbarTypes.getEvent(iButtonType, "OnClick"); - onMouseEnterEvent = ToolbarTypes.getEvent(iButtonType, "OnMouseEnter"); - onMouseLeaveEvent = ToolbarTypes.getEvent(iButtonType, "OnMouseLeave"); - destroyMethod = ToolbarTypes.getMethod(iButtonType, "Destroy"); - } - } - - #endregion -} \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs b/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs new file mode 100644 index 00000000..78f28650 --- /dev/null +++ b/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; + +using KSP.UI.Screens; +using KSPAssets; +using KSPAssets.Loaders; + +namespace InfernalRobotics.Gui +{ + [KSPAddon(KSPAddon.Startup.MainMenu, false)] + public class UIPrefabLoader : MonoBehaviour + { + internal static GameObject controlWindowPrefab; + internal static GameObject controlWindowGroupLinePrefab; + internal static GameObject controlWindowServoLinePrefab; + + internal static List iconAssets; + + public static bool controlWindowPrefabReady = false; + public static bool controlWindowGroupLinePrefabReady = false; + public static bool controlWindowServoLinePrefabReady = false; + + bool once = false; + + public void Start() + { + + } + + public void OnPrefabsLoaded(AssetLoader.Loader loader) + { + Logger.Log ("OnPrefabsLoaded was called"); + + for (int i = 0; i < loader.definitions.Length; i++ ) + { + + Logger.Log ("Examining definition: " + loader.definitions[i].name + + ", objects[i] = " + (loader.objects[i] ==null ? "null" : loader.objects[i].name)); + if(loader.definitions[i].name == "FlightServoControlWindowPrefab" && loader.objects[i] != null) + { + controlWindowPrefab = loader.objects [i] as GameObject; + controlWindowPrefabReady = true; + Logger.Log ("Successfully loaded control window prefab"); + } + if(loader.definitions[i].name == "FlightControllerServoGroupLine" && loader.objects[i] != null) + { + controlWindowGroupLinePrefabReady = loader.objects [i] as GameObject; + controlWindowGroupLinePrefabReady = true; + Logger.Log ("Successfully loaded control window Group prefab"); + } + + if(loader.definitions[i].name == "FlightControllerServoLine" && loader.objects[i] != null) + { + controlWindowServoLinePrefab = loader.objects [i] as GameObject; + controlWindowServoLinePrefabReady = true; + Logger.Log ("Successfully loaded control window Servo prefab"); + } + } + + } + + public void Update() + { + if(!once) + { + if(AssetLoader.Ready) + { + BundleDefinition bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindow"); + + if (bd != null) + { + Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); + foreach(var ad in bd.assets) + { + Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); + + if(ad.type == "GameObject") + { + AssetLoader.LoadAssets(OnPrefabsLoaded, ad); + } + } + } + + bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindowgroupline"); + + if (bd != null) + { + Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); + foreach(var ad in bd.assets) + { + Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); + + if(ad.type == "GameObject") + { + AssetLoader.LoadAssets(OnPrefabsLoaded, ad); + } + } + } + + bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindowservoline"); + + if (bd != null) + { + Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); + foreach(var ad in bd.assets) + { + Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); + + if(ad.type == "GameObject") + { + AssetLoader.LoadAssets(OnPrefabsLoaded, ad); + } + } + } + + once = true; + } + } + } + } +} + diff --git a/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs b/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs new file mode 100644 index 00000000..c66bb3d5 --- /dev/null +++ b/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs @@ -0,0 +1,308 @@ +using InfernalRobotics.Command; +using InfernalRobotics.Control; +using InfernalRobotics.Utility; +using KSP.IO; +using KSP.UI.Screens; +using System; +using System.Linq; +using System.Reflection; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; + +namespace InfernalRobotics.Gui +{ + [KSPAddon(KSPAddon.Startup.Flight, false)] + public class IRFlightWindowManager : WindowManager + { + public override string AddonName { get { return this.name; } } + } + + [KSPAddon(KSPAddon.Startup.EditorAny, false)] + public class IREditorWindowManager : WindowManager + { + public override string AddonName { get { return this.name; } } + } + + public class WindowManager : MonoBehaviour + { + public virtual String AddonName { get; set; } + + private static GameObject _controlWindow; + private static GameObject _editorWindow; + + private static List _servoGroupLines; + private static List _servoLines; + + private static bool useElectricCharge; + private static bool allowServoFlip; + + private static WindowManager _instance; + private static bool guiSetupDone; + private ApplicationLauncherButton appLauncherButton; + private static Texture2D appLauncherButtonTexture; + + private bool guiGroupEditorEnabled; + private bool guiPresetsEnabled; + private IServo associatedServo; + private bool guiPresetMode; + private bool guiHidden; + + private static int editorWindowWidth = 400; + private static int controlWindowWidth = 360; + + public bool GUIEnabled { get; set; } + + public static WindowManager Instance + { + get { return _instance; } + } + + static WindowManager() + { + useElectricCharge = true; + guiSetupDone = false; + appLauncherButtonTexture = new Texture2D(36, 36, TextureFormat.RGBA32, false); + TextureLoader.LoadImageFromFile (appLauncherButtonTexture, "icon_button.png"); + + string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; + + } + + private void Awake() + { + + LoadConfigXml(); + + Logger.Log("[NewGUI] awake, Mode: " + AddonName); + GUIEnabled = true; //for testing + guiGroupEditorEnabled = false; + + GameScenes scene = HighLogic.LoadedScene; + + if (scene == GameScenes.FLIGHT || scene == GameScenes.EDITOR) + { + _instance = this; + + _servoGroupLines = new List (); + _servoLines = new List (); + } + else + { + _instance = null; + //actually we don't need to go further if it's not flight or editor + return; + } + //GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequestedForAppLauncher); + + //GameEvents.onGUIApplicationLauncherReady.Add (AddAppLauncherButton); + + Logger.Log("[GUI] Added Toolbar GameEvents Handlers", Logger.Level.Debug); + + GameEvents.onShowUI.Add(OnShowUI); + GameEvents.onHideUI.Add(OnHideUI); + + Logger.Log("[GUI] awake finished successfully", Logger.Level.Debug); + } + + private void OnShowUI() + { + guiHidden = false; + } + + private void OnHideUI() + { + guiHidden = true; + } + + + public void Start() + { + + } + + public void RebuildUI() + { + //should be called by ServoController when required (Vessel changed and such). + } + + public void Update() + { + if(!guiSetupDone) + { + //here we need to wait until prefabs become available and then Instatiate the window + if(UIPrefabLoader.controlWindowPrefabReady && _controlWindow == null) + { + _controlWindow = GameObject.Instantiate (UIPrefabLoader.controlWindowPrefab); + _controlWindow.transform.SetParent (MainCanvasUtil.MainCanvas.transform, false); + _controlWindow.AddComponent (); + + } + + guiSetupDone = UIPrefabLoader.controlWindowPrefabReady + && UIPrefabLoader.controlWindowGroupLinePrefabReady + && UIPrefabLoader.controlWindowServoLinePrefabReady; + Logger.Log ("[NEWUI] Are prefabs ready: " + guiSetupDone); + if(guiSetupDone) + { + GameObject servoGroupsArea = _controlWindow.GetChild ("FlightServoControlWindowContent").GetChild ("ServoGroupsVLG"); + + for (int i = 0; i < ServoController.Instance.ServoGroups.Count; i++) + { + Logger.Log ("[NEW UI] Trying to draw group via prefab"); + ServoController.ControlGroup g = ServoController.Instance.ServoGroups[i]; + + if (HighLogic.LoadedSceneIsFlight && FlightGlobals.ActiveVessel != g.Vessel) + continue; + + var newServoGroupLine = GameObject.Instantiate (UIPrefabLoader.controlWindowGroupLinePrefab); + newServoGroupLine.transform.SetParent (servoGroupsArea.transform, false); + var hlg = newServoGroupLine.GetChild ("ServoGroupControlsHLG"); + hlg.GetChild ("ServoGroupNameText").GetComponent ().text = g.Name; + hlg.GetChild ("ServoGroupExpandedStatusToggle").GetComponent ().isOn = g.Expanded; + + _servoGroupLines.Add (newServoGroupLine); + + } + + } + } + else + { + //at this poitn we should have window instantiated and filled with groups and servos + //all we need to do is update the fields + + + + } + + } + + private void AddAppLauncherButton() + { + Logger.Log(string.Format("[GUI] AddAppLauncherButton Called, button=null: {0}", (appLauncherButton == null)), Logger.Level.Debug); + + if (appLauncherButton != null) return; + + if (!ApplicationLauncher.Ready) + return; + + try + { + appLauncherButton = ApplicationLauncher.Instance.AddModApplication(delegate { GUIEnabled = true; }, + delegate { GUIEnabled = false; }, null, null, null, null, + ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.VAB | + ApplicationLauncher.AppScenes.SPH, appLauncherButtonTexture); + + ApplicationLauncher.Instance.AddOnHideCallback(OnHideCallback); + } + catch (Exception ex) + { + Logger.Log(string.Format("[GUI AddAppLauncherButton Exception, {0}", ex.Message), Logger.Level.Fatal); + } + + Logger.Log(string.Format("[GUI] AddAppLauncherButton finished, button=null: {0}", (appLauncherButton == null)), Logger.Level.Debug); + } + + private void OnHideCallback() + { + GUIEnabled = false; + } + + void OnGameSceneLoadRequestedForAppLauncher(GameScenes SceneToLoad) + { + DestroyAppLauncherButton(); + } + + private void DestroyAppLauncherButton() + { + try + { + if (appLauncherButton != null && ApplicationLauncher.Instance != null) + { + ApplicationLauncher.Instance.RemoveModApplication(appLauncherButton); + appLauncherButton = null; + } + + if (ApplicationLauncher.Instance != null) + ApplicationLauncher.Instance.RemoveOnHideCallback(OnHideCallback); + } + catch (Exception e) + { + Logger.Log("[GUI] Failed unregistering AppLauncher handlers," + e.Message); + } + } + + private void OnDestroy() + { + Logger.Log("[GUI] destroy"); + + //GameEvents.onGUIApplicationLauncherReady.Remove (AddAppLauncherButton); + //GameEvents.onGameSceneLoadRequested.Remove(OnGameSceneLoadRequestedForAppLauncher); + DestroyAppLauncherButton(); + + GameEvents.onShowUI.Remove(OnShowUI); + GameEvents.onHideUI.Remove(OnHideUI); + + EditorLock(false); + SaveConfigXml(); + Logger.Log("[GUI] OnDestroy finished successfully", Logger.Level.Debug); + } + + + private void RefreshKeysFromGUI() + { + foreach (var g in ServoController.Instance.ServoGroups) + { + g.RefreshKeys(); + } + } + + + /// + /// Applies or removes the lock + /// + /// Which way are we going + internal void EditorLock(Boolean apply) + { + //only do this lock in the editor - no point elsewhere + if (HighLogic.LoadedSceneIsEditor && apply) + { + //only add a new lock if there isnt already one there + if (InputLockManager.GetControlLock("IRGUILockOfEditor") != ControlTypes.EDITOR_LOCK) + { + Logger.Log(String.Format("[GUI] AddingLock-{0}", "IRGUILockOfEditor"), Logger.Level.Debug); + + InputLockManager.SetControlLock(ControlTypes.EDITOR_LOCK, "IRGUILockOfEditor"); + } + } + //Otherwise make sure the lock is removed + else + { + //Only try and remove it if there was one there in the first place + if (InputLockManager.GetControlLock("IRGUILockOfEditor") == ControlTypes.EDITOR_LOCK) + { + Logger.Log(String.Format("[GUI] Removing-{0}", "IRGUILockOfEditor"), Logger.Level.Debug); + InputLockManager.RemoveControlLock("IRGUILockOfEditor"); + } + } + } + + public void LoadConfigXml() + { + PluginConfiguration config = PluginConfiguration.CreateForType(); + config.load(); + useElectricCharge = config.GetValue("useEC"); + allowServoFlip = config.GetValue("allowFlipHack"); + } + + public void SaveConfigXml() + { + PluginConfiguration config = PluginConfiguration.CreateForType(); + config.SetValue("useEC", useElectricCharge); + config.SetValue("allowFlipHack", allowServoFlip); + config.save(); + } + } +} \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/InfernalRobotics.csproj b/InfernalRobotics/InfernalRobotics/InfernalRobotics.csproj index eb616fba..b0fbbf79 100644 --- a/InfernalRobotics/InfernalRobotics/InfernalRobotics.csproj +++ b/InfernalRobotics/InfernalRobotics/InfernalRobotics.csproj @@ -87,7 +87,6 @@ - @@ -102,6 +101,10 @@ + + + + diff --git a/InfernalRobotics/InfernalRobotics/Module/ModuleIRServo.cs b/InfernalRobotics/InfernalRobotics/Module/ModuleIRServo.cs index 51a7a527..e706929c 100644 --- a/InfernalRobotics/InfernalRobotics/Module/ModuleIRServo.cs +++ b/InfernalRobotics/InfernalRobotics/Module/ModuleIRServo.cs @@ -1486,7 +1486,7 @@ public void LoadConfigXml() public void SaveConfigXml() { - PluginConfiguration config = PluginConfiguration.CreateForType(); + PluginConfiguration config = PluginConfiguration.CreateForType(); config.SetValue("useEC", UseElectricCharge); config.save(); } diff --git a/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs b/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs index 607fae3e..f5a1e4b4 100644 --- a/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs +++ b/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs @@ -141,7 +141,7 @@ internal static bool LoadImageFromFile(Texture2D tex, string fileName) { try { - Logger.Log(string.Format("[GUI] Loading: {0}/{1}", pathPluginTextures, fileName)); + //Logger.Log(string.Format("[GUI] Loading: {0}/{1}", pathPluginTextures, fileName)); tex.LoadImage(File.ReadAllBytes(string.Format("{0}/{1}", pathPluginTextures, fileName))); blnReturn = true; } From e7a1f77568678253062e46d71dd29956539da2e9 Mon Sep 17 00:00:00 2001 From: Stepan Andreev Date: Fri, 1 Apr 2016 21:04:41 +0100 Subject: [PATCH 02/27] Got it roughly working --- .../InfernalRobotics/Gui/UIPrefabLoader.cs | 125 +++++------------- .../InfernalRobotics/Gui/WindowManager.cs | 63 ++++++++- 2 files changed, 94 insertions(+), 94 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs b/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs index 78f28650..3eedf175 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/UIPrefabLoader.cs @@ -1,18 +1,18 @@ using System; +using System.IO; +using System.Collections; using System.Collections.Generic; +using System.Reflection; using UnityEngine; -using UnityEngine.UI; -using UnityEngine.EventSystems; -using KSP.UI.Screens; -using KSPAssets; -using KSPAssets.Loaders; namespace InfernalRobotics.Gui { [KSPAddon(KSPAddon.Startup.MainMenu, false)] public class UIPrefabLoader : MonoBehaviour { + private AssetBundle IRAssetBundle; + internal static GameObject controlWindowPrefab; internal static GameObject controlWindowGroupLinePrefab; internal static GameObject controlWindowServoLinePrefab; @@ -23,103 +23,50 @@ public class UIPrefabLoader : MonoBehaviour public static bool controlWindowGroupLinePrefabReady = false; public static bool controlWindowServoLinePrefabReady = false; - bool once = false; - - public void Start() - { - - } - - public void OnPrefabsLoaded(AssetLoader.Loader loader) - { - Logger.Log ("OnPrefabsLoaded was called"); - - for (int i = 0; i < loader.definitions.Length; i++ ) - { - - Logger.Log ("Examining definition: " + loader.definitions[i].name + - ", objects[i] = " + (loader.objects[i] ==null ? "null" : loader.objects[i].name)); - if(loader.definitions[i].name == "FlightServoControlWindowPrefab" && loader.objects[i] != null) - { - controlWindowPrefab = loader.objects [i] as GameObject; - controlWindowPrefabReady = true; - Logger.Log ("Successfully loaded control window prefab"); - } - if(loader.definitions[i].name == "FlightControllerServoGroupLine" && loader.objects[i] != null) - { - controlWindowGroupLinePrefabReady = loader.objects [i] as GameObject; - controlWindowGroupLinePrefabReady = true; - Logger.Log ("Successfully loaded control window Group prefab"); - } - - if(loader.definitions[i].name == "FlightControllerServoLine" && loader.objects[i] != null) - { - controlWindowServoLinePrefab = loader.objects [i] as GameObject; - controlWindowServoLinePrefabReady = true; - Logger.Log ("Successfully loaded control window Servo prefab"); - } - } - - } - - public void Update() + public IEnumerator LoadBundle(string location) { - if(!once) + while (!Caching.ready) + yield return null; + using (WWW www = WWW.LoadFromCacheOrDownload(location, 0)) { - if(AssetLoader.Ready) + yield return www; + IRAssetBundle = www.assetBundle; + var t = IRAssetBundle.LoadAllAssets(); + for (int i=0; i< t.Length; i++) { - BundleDefinition bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindow"); - - if (bd != null) + if(t[i].name == "FlightServoControlWindowPrefab") { - Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); - foreach(var ad in bd.assets) - { - Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); - - if(ad.type == "GameObject") - { - AssetLoader.LoadAssets(OnPrefabsLoaded, ad); - } - } + controlWindowPrefab = t[i] as GameObject; + controlWindowPrefabReady = true; + Logger.Log("Successfully loaded control window prefab"); } - - bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindowgroupline"); - - if (bd != null) + if (t[i].name == "FlightControllerServoGroupLine") { - Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); - foreach(var ad in bd.assets) - { - Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); - - if(ad.type == "GameObject") - { - AssetLoader.LoadAssets(OnPrefabsLoaded, ad); - } - } + controlWindowGroupLinePrefab = t[i] as GameObject; + controlWindowGroupLinePrefabReady = true; + Logger.Log("Successfully loaded control window Group prefab"); } - bd = AssetLoader.GetBundleDefinition ("MagicSmokeIndustries/AssetBundles/ircontrolwindowservoline"); - - if (bd != null) + if (t[i].name == "FlightControllerServoLine") { - Logger.Log ("Found IR UI Bundle Definition. Listing Assets"); - foreach(var ad in bd.assets) - { - Logger.Log ("Found AssetDefinition:" + ad.name + ", type = " + ad.type); - - if(ad.type == "GameObject") - { - AssetLoader.LoadAssets(OnPrefabsLoaded, ad); - } - } + controlWindowServoLinePrefab = t[i] as GameObject; + controlWindowServoLinePrefabReady = true; + Logger.Log("Successfully loaded control window Servo prefab"); } - - once = true; } } } + + public void Start() + { + var assemblyFile = Assembly.GetExecutingAssembly().Location; + var bundlePath = "file://" + assemblyFile.Replace(new FileInfo(assemblyFile).Name, "").Replace("\\","/") + "../AssetBundles/"; + + Logger.Log("Loading bundles from BundlePath: " + bundlePath); + + StartCoroutine(LoadBundle(bundlePath + "ir_ui_objects.ksp")); + } + } } diff --git a/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs b/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs index c66bb3d5..ada9c01d 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/WindowManager.cs @@ -136,14 +136,15 @@ public void Update() { _controlWindow = GameObject.Instantiate (UIPrefabLoader.controlWindowPrefab); _controlWindow.transform.SetParent (MainCanvasUtil.MainCanvas.transform, false); - _controlWindow.AddComponent (); - + _controlWindow.GetChild("FlightServoControlWindowTitle").AddComponent(); + _controlWindow.GetChild("FlightServoControlWindowContent").AddComponent(); + _controlWindow.GetChild("FlightServoControlWindowFooter").AddComponent(); } guiSetupDone = UIPrefabLoader.controlWindowPrefabReady && UIPrefabLoader.controlWindowGroupLinePrefabReady && UIPrefabLoader.controlWindowServoLinePrefabReady; - Logger.Log ("[NEWUI] Are prefabs ready: " + guiSetupDone); + Logger.Log ("[NEW UI] Are prefabs ready: " + guiSetupDone); if(guiSetupDone) { GameObject servoGroupsArea = _controlWindow.GetChild ("FlightServoControlWindowContent").GetChild ("ServoGroupsVLG"); @@ -158,9 +159,61 @@ public void Update() var newServoGroupLine = GameObject.Instantiate (UIPrefabLoader.controlWindowGroupLinePrefab); newServoGroupLine.transform.SetParent (servoGroupsArea.transform, false); + var hlg = newServoGroupLine.GetChild ("ServoGroupControlsHLG"); + var servosVLG = newServoGroupLine.GetChild("ServoGroupServosVLG"); + hlg.GetChild ("ServoGroupNameText").GetComponent ().text = g.Name; - hlg.GetChild ("ServoGroupExpandedStatusToggle").GetComponent ().isOn = g.Expanded; + + var groupToggle = hlg.GetChild("ServoGroupExpandedStatusToggle").GetComponent(); + groupToggle.isOn = g.Expanded; + groupToggle.onValueChanged.AddListener(b => { g.Expanded = b; servosVLG.SetActive(b); }); + + var groupSpeed = hlg.GetChild("ServoGroupSpeedMultiplier").GetComponent(); + groupSpeed.text = g.Speed; + groupSpeed.onEndEdit.AddListener(v => { g.Speed = v; }); + + var groupMoveLeftToggle = hlg.GetChild("ServoGroupMoveLeftToggleButton").GetComponent