Advertisement
thadeausmaximus

ServoMonitor.cs

Jun 28th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.76 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using KSP.UI.Screens;
  7. using USITools;
  8. using System.Collections.Generic;
  9.  
  10. namespace Konstruction
  11. {
  12.     [KSPAddon(KSPAddon.Startup.EditorAny, false)]
  13.     public class ServoMonitor_Editor : ServoMonitor
  14.     {
  15.         public override List<Part> GetParts()
  16.         {
  17.             return EditorLogic.fetch.ship.parts;
  18.         }
  19.     }
  20.  
  21.     [KSPAddon(KSPAddon.Startup.Flight, false)]
  22.     public class ServoMonitor : MonoBehaviour
  23.     {
  24.         private ApplicationLauncherButton servoButton;
  25.         private IButton planLogTButton;
  26.         private Rect _windowPosition = new Rect(300, 60, 830, 400);
  27.         private GUIStyle _windowStyle;
  28.         private GUIStyle _labelStyle;
  29.         private GUIStyle _buttonStyle;
  30.         private GUIStyle _scrollStyle;
  31.         private Vector2 scrollPos = Vector2.zero;
  32.         private bool _hasInitStyles = false;
  33.         private bool windowVisible;
  34.         public static bool renderDisplay = false;
  35.         private List<bool> showServo;
  36.  
  37.  
  38.         public virtual List<Part> GetParts()
  39.         {
  40.             return FlightGlobals.ActiveVessel.parts;
  41.         }
  42.  
  43.         void Awake()
  44.         {
  45.             var texture = new Texture2D(36, 36, TextureFormat.RGBA32, false);
  46.             var textureFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Servo.png");
  47.             print("Loading " + textureFile);
  48.             texture.LoadImage(File.ReadAllBytes(textureFile));
  49.             this.servoButton = ApplicationLauncher.Instance.AddModApplication(GuiOn, GuiOff, null, null, null, null,
  50.                 ApplicationLauncher.AppScenes.ALWAYS, texture);
  51.         }
  52.  
  53.         private void GuiOn()
  54.         {
  55.             renderDisplay = true;
  56.         }
  57.  
  58.         public void Start()
  59.         {
  60.             if (!_hasInitStyles)
  61.                 InitStyles();
  62.             showServo = new List<bool>();
  63.         }
  64.  
  65.         private void GuiOff()
  66.         {
  67.             renderDisplay = false;
  68.         }
  69.  
  70.         private void OnGUI()
  71.         {
  72.             try
  73.             {
  74.                 if (!renderDisplay)
  75.                     return;
  76.  
  77.                 if (Event.current.type == EventType.Repaint || Event.current.isMouse)
  78.                 {
  79.                     //preDrawQueue
  80.                 }
  81.                 Ondraw();
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 print("ERROR in ServoMonitor (OnGui) " + ex.Message);
  86.             }
  87.         }
  88.  
  89.         private void Ondraw()
  90.         {
  91.             _windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Servo Controller", _windowStyle);
  92.         }
  93.  
  94.         private void OnWindow(int windowId)
  95.         {
  96.             GenerateWindow();
  97.         }
  98.  
  99.         string ColorToHex(Color32 color)
  100.         {
  101.             string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
  102.             return hex;
  103.         }
  104.  
  105.         private void GenerateWindow()
  106.         {
  107.             GUILayout.BeginVertical();
  108.             scrollPos = GUILayout.BeginScrollView(scrollPos, _scrollStyle, GUILayout.Width(810), GUILayout.Height(350));
  109.             GUILayout.BeginVertical();
  110.  
  111.             try
  112.             {
  113.                 var numServos = 0;
  114.                 foreach (var p in GetParts())
  115.                 {
  116.                     var servos = p.FindModulesImplementing<ModuleServo>();
  117.                     if (servos.Any())
  118.                     {
  119.                         numServos++;
  120.                         bool setPos = false;
  121.                         int setGoalVal = -1;
  122.                         bool stopAll = false;
  123.                         float speedMult = 1f;
  124.  
  125.                         if (showServo.Count < numServos)
  126.                             showServo.Add(true);
  127.  
  128.                         GUILayout.BeginHorizontal();
  129.  
  130.                         if (showServo[numServos - 1])
  131.                         {
  132.                             if (GUILayout.Button("-", GUILayout.Width(35)))
  133.                                 showServo[numServos - 1] = false;
  134.                         }
  135.                         else
  136.                         {
  137.                             if (GUILayout.Button("+", GUILayout.Width(35)))
  138.                                 showServo[numServos - 1] = true;
  139.                         }
  140.  
  141.  
  142.                         GUILayout.Label(String.Format("<color=#FFFFFF>[{0}] {1}</color>", numServos,p.partInfo.title), _labelStyle, GUILayout.Width(230));
  143.  
  144.                         if (p.HighlightActive)
  145.                         {
  146.                             if (GUILayout.Button("-H", GUILayout.Width(35)))
  147.                             {
  148.                                 p.highlightColor = Color.magenta;
  149.                                 p.HighlightActive = false;
  150.                                 p.Highlight(false);
  151.                             }
  152.                         }
  153.                         else
  154.                         {
  155.                             if (GUILayout.Button("+H", GUILayout.Width(35)))
  156.                             {
  157.                                 p.highlightColor = Color.magenta;
  158.                                 p.HighlightActive = true;
  159.                                 p.Highlight(true);
  160.                             }
  161.                         }
  162.  
  163.  
  164.                         var sGroup = p.FindModuleImplementing<ModuleServoGroup>();
  165.                         if (sGroup != null)
  166.                         {
  167.                             if (sGroup.GroupState == 0)
  168.                             {
  169.                                 if (GUILayout.Button("Group: None", GUILayout.Width(140)))
  170.                                     sGroup.GroupState++;
  171.                             }
  172.                             else if (sGroup.GroupState == 1)
  173.                             {
  174.                                 if (GUILayout.Button("Group: Slave", GUILayout.Width(140)))
  175.                                     sGroup.GroupState++;
  176.                             }
  177.                             else
  178.                             {
  179.                                 if (GUILayout.Button("Group: Master", GUILayout.Width(140)))
  180.                                     sGroup.GroupState = 0;
  181.                             }
  182.  
  183.                             if (sGroup.GroupID < 6)
  184.                             {
  185.                                 if (GUILayout.Button("ID: " + sGroup.GroupID, GUILayout.Width(70)))
  186.                                     sGroup.GroupID++;
  187.                             }
  188.                             else
  189.                             {
  190.                                 if (GUILayout.Button("ID: " + sGroup.GroupID, GUILayout.Width(70)))
  191.                                     sGroup.GroupID = 0;
  192.                             }
  193.                         }
  194.  
  195.  
  196.                         if (showServo[numServos - 1])
  197.                         {
  198.                             if (GUILayout.Button("All Free", GUILayout.Width(70)))
  199.                                 setGoalVal = 0;
  200.                             if (GUILayout.Button("All Goal", GUILayout.Width(70)))
  201.                                 setGoalVal = 1;
  202.                             if (GUILayout.Button("All Stop", GUILayout.Width(70)))
  203.                                 stopAll = true;
  204.  
  205.                         }
  206.  
  207.  
  208.  
  209.  
  210.                         GUILayout.EndHorizontal();
  211.  
  212.                         if (showServo[numServos - 1])
  213.                         {
  214.                             foreach (var servo in servos)
  215.                             {
  216.                                 servo.ServoSpeed *= speedMult;
  217.  
  218.                                 if (stopAll)
  219.                                     servo.ServoSpeed = 0;
  220.                                 if (setGoalVal == 0)
  221.                                     servo.MoveToGoal = false;
  222.                                 if (setGoalVal == 1)
  223.                                     servo.MoveToGoal = true;
  224.                                 GUILayout.BeginHorizontal();
  225.                                 GUILayout.Label("", _labelStyle, GUILayout.Width(30));
  226.                                 GUILayout.Label(String.Format("{0}", servo.menuName), _labelStyle, GUILayout.Width(130));
  227.                                 var goal = GUILayout.TextField(servo.GoalString, 10, GUILayout.Width(50));
  228.                                 GUILayout.Label(String.Format("<color=#fce700>G: [{0}]</color>", servo.goalValue), _labelStyle, GUILayout.Width(80));
  229.                                 servo.GoalString = goal;
  230.                                 var tmp = 0f;
  231.                                 if (float.TryParse(goal, out tmp))
  232.                                     servo.goalValue = tmp;
  233.                                 GUILayout.Label(String.Format("{0:0.00}", servo.DisplayPosition), _labelStyle, GUILayout.Width(50));
  234.                                 if(servo.MoveToGoal)
  235.                                 {
  236.                                     if (GUILayout.Button("-F-", GUILayout.Width(35)))
  237.                                         servo.MoveToGoal = false;
  238.                                 }
  239.                                 else
  240.                                 {
  241.                                     if (GUILayout.Button("-G-", GUILayout.Width(35)))
  242.                                         servo.MoveToGoal = true;
  243.                                 }
  244.                                 if (servo.GroupBehavior == 0)
  245.                                 {
  246.                                     if (GUILayout.Button("+", GUILayout.Width(25)))
  247.                                         servo.GroupBehavior += 1;
  248.                                 }
  249.                                 else if (servo.GroupBehavior == 1)
  250.                                 {
  251.                                     if (GUILayout.Button("-", GUILayout.Width(25)))
  252.                                     {
  253.                                         servo.GroupBehavior += 1;
  254.                                         servo.ServoSpeed = 0;
  255.                                     }
  256.                                 }
  257.                                 else
  258.                                 {
  259.                                     if (GUILayout.Button("o", GUILayout.Width(25)))
  260.                                         servo.GroupBehavior = 0;
  261.                                 }
  262.  
  263.                                 GUILayout.Label("", _labelStyle, GUILayout.Width(30));
  264.  
  265.                                 if (GUILayout.Button("-10", GUILayout.Width(35)))
  266.                                     servo.ServoSpeed -= 10;
  267.                                 if (GUILayout.Button("-1", GUILayout.Width(35)))
  268.                                     servo.ServoSpeed -= 1;
  269.                                 if (GUILayout.Button("<->", GUILayout.Width(35)))
  270.                                     servo.ServoSpeed *= -1;
  271.  
  272.  
  273.  
  274.                                 GUILayout.Label("", _labelStyle, GUILayout.Width(5));
  275.                                 GUILayout.Label(String.Format("<color=#FFD900>{0:0}</color>", servo.ServoSpeed), _labelStyle, GUILayout.Width(40));
  276.                                 GUILayout.Label("", _labelStyle, GUILayout.Width(5));
  277.                                 if (GUILayout.Button("-0-", GUILayout.Width(35)))
  278.                                     servo.ServoSpeed = 0;
  279.  
  280.                                 if (GUILayout.Button("+1", GUILayout.Width(35)))
  281.                                     servo.ServoSpeed += 1;
  282.                                 if (GUILayout.Button("+10", GUILayout.Width(35)))
  283.                                     servo.ServoSpeed += 10;
  284.                                 GUILayout.EndHorizontal();
  285.                             }
  286.                         }
  287.                     }
  288.                 }
  289.             }
  290.             catch (Exception ex)
  291.             {
  292.                 Debug.Log(ex.StackTrace);
  293.             }
  294.             finally
  295.             {
  296.                 GUILayout.EndVertical();
  297.                 GUILayout.EndScrollView();
  298.                 GUILayout.EndVertical();
  299.                 GUI.DragWindow();
  300.             }
  301.         }
  302.  
  303.         internal void OnDestroy()
  304.         {
  305.             if (servoButton != null)
  306.             {
  307.                 ApplicationLauncher.Instance.RemoveModApplication(servoButton);
  308.                 servoButton = null;
  309.             }
  310.             if (planLogTButton != null)
  311.             {
  312.                 planLogTButton.Destroy();
  313.                 planLogTButton = null;
  314.             }
  315.         }
  316.  
  317.         private void InitStyles()
  318.         {
  319.             _windowStyle = new GUIStyle(HighLogic.Skin.window);
  320.             _windowStyle.fixedWidth = 830f;
  321.             _windowStyle.fixedHeight = 400f;
  322.             _labelStyle = new GUIStyle(HighLogic.Skin.label);
  323.             _buttonStyle = new GUIStyle(HighLogic.Skin.button);
  324.             _scrollStyle = new GUIStyle(HighLogic.Skin.scrollView);
  325.             _hasInitStyles = true;
  326.         }
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement