Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using RemoteTech.SimpleTypes;
  4. using UnityEngine;
  5. using KSP.UI;
  6.  
  7. namespace RemoteTech.UI
  8. {
  9. public class TimeWarpDecorator
  10. {
  11. /// <summary>
  12. /// Craped Timewarpobject from stock
  13. /// </summary>
  14. private TimeWarp mTimewarpObject;
  15. /// <summary>
  16. /// Delay-Text style
  17. /// </summary>
  18. private GUIStyle mTextStyle;
  19.  
  20. /// <summary>
  21. /// Green Flightcomputer button
  22. /// </summary>
  23. private readonly GUIStyle mFlightButtonGreen;
  24. /// <summary>
  25. /// Red Flightcomputer button
  26. /// </summary>
  27. private readonly GUIStyle mFlightButtonRed;
  28. /// <summary>
  29. /// Yellow Flightcomputer button
  30. /// </summary>
  31. private readonly GUIStyle mFlightButtonYellow;
  32. /// <summary>
  33. /// Delay-Timer Background
  34. /// </summary>
  35. private readonly Texture2D mTexBackground;
  36. /// <summary>
  37. /// Activ Vessel
  38. /// </summary>
  39. private VesselSatellite mVessel { get{ return RTCore.Instance.Satellites[FlightGlobals.ActiveVessel]; } }
  40.  
  41. private String DisplayText
  42. {
  43. get
  44. {
  45. var vs = this.mVessel;
  46. if (vs == null)
  47. {
  48. return "N/A";
  49. }
  50. else if (vs.HasLocalControl)
  51. {
  52. return "Local Control";
  53. }
  54. else if (vs.Connections.Any())
  55. {
  56. if (RTSettings.Instance.EnableSignalDelay)
  57. {
  58. return "D+ " + vs.Connections[0].Delay.ToString("F5") + "s";
  59. }
  60. else
  61. {
  62. return "Connected";
  63. }
  64. }
  65. return "No Connection";
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// Returns the Style of the Flightcomputer button by the connected status
  71. /// </summary>
  72. private GUIStyle ButtonStyle
  73. {
  74. get
  75. {
  76. var vs = this.mVessel;
  77. if (vs == null)
  78. {
  79. return mFlightButtonRed;
  80. }
  81. else if (vs.HasLocalControl)
  82. {
  83. return mFlightButtonYellow;
  84. }
  85. else if (vs.Connections.Any())
  86. {
  87. return mFlightButtonGreen;
  88. }
  89. return mFlightButtonRed;
  90. }
  91. }
  92.  
  93. public TimeWarpDecorator()
  94. {
  95. mFlightButtonGreen = GUITextureButtonFactory.CreateFromFilename("texFlightGreen.png","texFlightGreenOver.png","texFlightGreenDown.png","texFlightGreenOver.png");
  96. mFlightButtonYellow = GUITextureButtonFactory.CreateFromFilename("texFlightYellow.png","texFlightYellowOver.png","texFlightYellowDown.png","texFlightYellowOver.png");
  97. mFlightButtonRed = GUITextureButtonFactory.CreateFromFilename("texFlightRed.png","texFlightRed.png","texFlightRed.png","texFlightRed.png");
  98.  
  99. mFlightButtonGreen.fixedHeight = mFlightButtonGreen.fixedWidth = 0;
  100. mFlightButtonYellow.fixedHeight = mFlightButtonYellow.fixedWidth = 0;
  101. mFlightButtonRed.fixedHeight = mFlightButtonRed.fixedWidth = 0;
  102. mFlightButtonGreen.stretchHeight = mFlightButtonGreen.stretchWidth = true;
  103. mFlightButtonYellow.stretchHeight = mFlightButtonYellow.stretchWidth = true;
  104. mFlightButtonRed.stretchHeight = mFlightButtonRed.stretchWidth = true;
  105.  
  106. // Crap timewarp object
  107. mTimewarpObject = TimeWarp.fetch;
  108.  
  109. // objects on this scene?
  110. if (mTimewarpObject == null || mTimewarpObject.timeQuadrantTab == null)
  111. {
  112. // to skip the draw calls
  113. mTimewarpObject = null;
  114. return;
  115. }
  116.  
  117. var text = mTimewarpObject.timeQuadrantTab.transform.FindChild("MET timer").GetComponent<ScreenSafeGUIText>();
  118. mTextStyle = new GUIStyle(text.textStyle);
  119. mTextStyle.fontSize = (int)(text.textSize * ScreenSafeUI.PixelRatio);
  120.  
  121. // Put the draw function to the DrawQueue
  122. //RenderingManager.AddToPostDrawQueue(0, Draw);
  123. RTCore.Instance.OnGuiUpdate += Draw;
  124.  
  125. // create the Background
  126. RTUtil.LoadImage(out mTexBackground, "TimeQuadrantFcStatus.png");
  127. }
  128.  
  129. /// <summary>
  130. /// Draws the TimeQuadrantFcStatus.png, Delay time and the Flightcomputerbutton under the timewarp object
  131. /// </summary>
  132. public void Draw()
  133. {
  134. // no drawing without timewarp object
  135. if (mTimewarpObject == null)
  136. return;
  137.  
  138.  
  139. Vector2 screenCoord = UIMainCamera.Camera.WorldToScreenPoint(mTimewarpObject.timeQuadrantTab.transform.position);
  140. float scale = ScreenSafeUI.VerticalRatio * 900.0f / Screen.height;
  141.  
  142. float topLeftTotimeQuadrant = Screen.height - screenCoord.y;
  143. float texBackgroundHeight = mTexBackground.height * 0.7f / scale;
  144. float texBackgroundWidth = (mTimewarpObject.timeQuadrantTab.GetComponent<Renderer>().material.mainTexture.width * 0.8111f) / scale;
  145.  
  146.  
  147. Rect delaytextPosition = new Rect(12.0f / scale, topLeftTotimeQuadrant + texBackgroundHeight - 1, 50.0f / scale, 20.0f / scale);
  148.  
  149. // calc the position under the timewarp object
  150. Rect pos = new Rect(mTimewarpObject.transform.position.x,
  151. topLeftTotimeQuadrant + texBackgroundHeight - 3.0f,
  152. texBackgroundWidth, texBackgroundHeight);
  153.  
  154. // draw the image
  155. GUI.DrawTexture(pos, mTexBackground);
  156. // draw the delay-text
  157. GUI.Label(delaytextPosition, DisplayText, mTextStyle);
  158.  
  159. // draw the flightcomputer button to the right relativ to the delaytext position
  160. delaytextPosition.width = 21.0f / scale;
  161. delaytextPosition.x += 125 / scale;
  162.  
  163. if (GUI.Button(delaytextPosition, "", ButtonStyle))
  164. {
  165. var satellite = RTCore.Instance.Satellites[FlightGlobals.ActiveVessel];
  166. if (satellite == null || satellite.SignalProcessor.FlightComputer == null) return;
  167. satellite.SignalProcessor.FlightComputer.Window.Show();
  168. }
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement