Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 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. public class TimeWarpDecorator {
  9. /// <summary>
  10. /// Craped Timewarpobject from stock
  11. /// </summary>
  12. private TimeWarp mTimewarpObject;
  13. private UnityEngine.UI.Image mTimewarpImage;
  14. /// <summary>
  15. /// Delay-Text style
  16. /// </summary>
  17. private GUIStyle mTextStyle;
  18.  
  19. /// <summary>
  20. /// Green Flightcomputer button
  21. /// </summary>
  22. private readonly GUIStyle mFlightButtonGreen;
  23. /// <summary>
  24. /// Red Flightcomputer button
  25. /// </summary>
  26. private readonly GUIStyle mFlightButtonRed;
  27. /// <summary>
  28. /// Yellow Flightcomputer button
  29. /// </summary>
  30. private readonly GUIStyle mFlightButtonYellow;
  31. /// <summary>
  32. /// Delay-Timer Background
  33. /// </summary>
  34. private readonly Texture2D mTexBackground;
  35. /// <summary>
  36. /// Activ Vessel
  37. /// </summary>
  38. private VesselSatellite mVessel { get { return RTCore.Instance.Satellites[FlightGlobals.ActiveVessel]; } }
  39.  
  40. private String DisplayText {
  41. get {
  42. var vs = this.mVessel;
  43. if (vs == null) {
  44. return "N/A";
  45. } else if (vs.HasLocalControl) {
  46. return "Local Control";
  47. } else if (vs.Connections.Any()) {
  48. if (RTSettings.Instance.EnableSignalDelay) {
  49. return "D+ " + vs.Connections[0].Delay.ToString("F5") + "s";
  50. } else {
  51. return "Connected";
  52. }
  53. }
  54. return "No Connection";
  55. }
  56. }
  57.  
  58. /// <summary>
  59. /// Returns the Style of the Flightcomputer button by the connected status
  60. /// </summary>
  61. private GUIStyle ButtonStyle {
  62. get {
  63. var vs = this.mVessel;
  64. if (vs == null) {
  65. return mFlightButtonRed;
  66. } else if (vs.HasLocalControl) {
  67. return mFlightButtonYellow;
  68. } else if (vs.Connections.Any()) {
  69. return mFlightButtonGreen;
  70. }
  71. return mFlightButtonRed;
  72. }
  73. }
  74.  
  75. private Texture2D[,] fcTextures;
  76.  
  77. private void saveFcTextures() {
  78. fcTextures = new Texture2D[3, 4];
  79.  
  80. fcTextures[0, 0] = mFlightButtonGreen.normal.background;
  81. fcTextures[0, 1] = mFlightButtonGreen.hover.background;
  82. fcTextures[0, 2] = mFlightButtonGreen.active.background;
  83. fcTextures[0, 3] = mFlightButtonGreen.focused.background;
  84.  
  85. fcTextures[1, 0] = mFlightButtonYellow.normal.background;
  86. fcTextures[1, 1] = mFlightButtonYellow.hover.background;
  87. fcTextures[1, 2] = mFlightButtonYellow.active.background;
  88. fcTextures[1, 3] = mFlightButtonYellow.focused.background;
  89.  
  90. fcTextures[2, 0] = mFlightButtonRed.normal.background;
  91. fcTextures[2, 1] = mFlightButtonRed.hover.background;
  92. fcTextures[2, 2] = mFlightButtonRed.active.background;
  93. fcTextures[2, 3] = mFlightButtonRed.focused.background;
  94. }
  95.  
  96. private void restoreFcTextures() {
  97. if (mFlightButtonGreen.normal.background == null || mFlightButtonYellow.normal.background == null || mFlightButtonRed.normal.background == null) {
  98. mFlightButtonGreen.normal.background = fcTextures[0, 0];
  99. mFlightButtonGreen.hover.background = fcTextures[0, 1];
  100. mFlightButtonGreen.active.background = fcTextures[0, 2];
  101. mFlightButtonGreen.onNormal.background = fcTextures[0, 2];
  102. mFlightButtonGreen.onHover.background = fcTextures[0, 2];
  103. mFlightButtonGreen.onActive.background = fcTextures[0, 2];
  104. mFlightButtonGreen.onFocused.background = fcTextures[0, 2];
  105. mFlightButtonGreen.focused.background = fcTextures[0, 3];
  106.  
  107. mFlightButtonYellow.normal.background = fcTextures[1, 0];
  108. mFlightButtonYellow.hover.background = fcTextures[1, 1];
  109. mFlightButtonYellow.active.background = fcTextures[1, 2];
  110. mFlightButtonYellow.onNormal.background = fcTextures[1, 2];
  111. mFlightButtonYellow.onHover.background = fcTextures[1, 2];
  112. mFlightButtonYellow.onActive.background = fcTextures[1, 2];
  113. mFlightButtonYellow.onFocused.background = fcTextures[1, 2];
  114. mFlightButtonYellow.focused.background = fcTextures[1, 3];
  115.  
  116. mFlightButtonRed.normal.background = fcTextures[2, 0];
  117. mFlightButtonRed.hover.background = fcTextures[2, 1];
  118. mFlightButtonRed.active.background = fcTextures[2, 2];
  119. mFlightButtonRed.onNormal.background = fcTextures[2, 2];
  120. mFlightButtonRed.onHover.background = fcTextures[2, 2];
  121. mFlightButtonRed.onActive.background = fcTextures[2, 2];
  122. mFlightButtonRed.onFocused.background = fcTextures[2, 2];
  123. mFlightButtonRed.focused.background = fcTextures[2, 3];
  124. }
  125. }
  126.  
  127. public TimeWarpDecorator() {
  128. mFlightButtonGreen = GUITextureButtonFactory.CreateFromFilename("texFlightGreen.png", "texFlightGreenOver.png", "texFlightGreenDown.png", "texFlightGreenOver.png");
  129. mFlightButtonYellow = GUITextureButtonFactory.CreateFromFilename("texFlightYellow.png", "texFlightYellowOver.png", "texFlightYellowDown.png", "texFlightYellowOver.png");
  130. mFlightButtonRed = GUITextureButtonFactory.CreateFromFilename("texFlightRed.png", "texFlightRed.png", "texFlightRed.png", "texFlightRed.png");
  131.  
  132. saveFcTextures();
  133.  
  134. mFlightButtonGreen.fixedHeight = mFlightButtonGreen.fixedWidth = 0;
  135. mFlightButtonYellow.fixedHeight = mFlightButtonYellow.fixedWidth = 0;
  136. mFlightButtonRed.fixedHeight = mFlightButtonRed.fixedWidth = 0;
  137. mFlightButtonGreen.stretchHeight = mFlightButtonGreen.stretchWidth = true;
  138. mFlightButtonYellow.stretchHeight = mFlightButtonYellow.stretchWidth = true;
  139. mFlightButtonRed.stretchHeight = mFlightButtonRed.stretchWidth = true;
  140.  
  141. // Crap timewarp object
  142.  
  143. mTimewarpObject = TimeWarp.fetch;
  144. GameObject go = GameObject.Find("TimeQuadrant");
  145. if (go)
  146. mTimewarpImage = go.GetComponent<UnityEngine.UI.Image>();
  147.  
  148. // objects on this scene?
  149. if (mTimewarpObject == null || mTimewarpObject.timeQuadrantTab == null) {
  150. // to skip the draw calls
  151. mTimewarpObject = null;
  152. return;
  153. }
  154.  
  155. var text = mTimewarpObject.timeQuadrantTab.transform.FindChild("MET timer").GetComponent<ScreenSafeGUIText>();
  156. mTextStyle = new GUIStyle(text.textStyle);
  157. mTextStyle.fontSize = (int)(text.textSize * ScreenSafeUI.PixelRatio);
  158.  
  159. // Put the draw function to the DrawQueue
  160. /*RenderingManager.AddToPostDrawQueue(0, Draw);*/
  161. //RTCore.Instance.OnGuiUpdate += Draw;
  162.  
  163. // create the Background
  164. RTUtil.LoadImage(out mTexBackground, "TimeQuadrantFcStatus.png");
  165. }
  166.  
  167. /// <summary>
  168. /// Draws the TimeQuadrantFcStatus.png, Delay time and the Flightcomputerbutton under the timewarp object
  169. /// </summary>
  170. public void Draw() {
  171. // no drawing without timewarp object
  172. if (mTimewarpObject == null)
  173. return;
  174.  
  175. restoreFcTextures();
  176.  
  177. var tr = mTimewarpImage.rectTransform.position;
  178. Vector2 screenCoord = UIMainCamera.Camera.WorldToScreenPoint(tr);
  179.  
  180. // TODO: Might need to fix this also
  181. float scale = GameSettings.UI_SCALE;
  182.  
  183. float topLeftTotimeQuadrant = (Screen.height - screenCoord.y + mTimewarpImage.preferredHeight - 1) * scale;
  184. float texBackgroundHeight = mTexBackground.height * 0.7f * scale;
  185. float texBackgroundWidth = (mTexBackground.width * 0.8111f) * scale;
  186.  
  187. Rect delaytextPosition = new Rect((screenCoord.x + 12.0f) * scale, topLeftTotimeQuadrant, 50.0f * scale, 20.0f * scale);
  188.  
  189. // calc the position under the timewarp object
  190. Rect pos = new Rect(screenCoord.x * scale,
  191. topLeftTotimeQuadrant,
  192. texBackgroundWidth, texBackgroundHeight);
  193. // draw the image
  194. GUI.DrawTexture(pos, mTexBackground);
  195.  
  196. // draw the delay-text
  197. GUI.Label(delaytextPosition, DisplayText, mTextStyle);
  198.  
  199. // draw the flightcomputer button to the right relativ to the delaytext position
  200. Rect btnPos = new Rect(pos.x + (125.0f * scale), topLeftTotimeQuadrant, 21.0f, 21.0f);
  201.  
  202. // TODO: Fix this buttons icon, somehow it doesn't display but it still functions
  203. if (GUI.Button(btnPos, "", ButtonStyle)) {
  204. var satellite = RTCore.Instance.Satellites[FlightGlobals.ActiveVessel];
  205. if (satellite == null || satellite.SignalProcessor.FlightComputer == null) return;
  206. satellite.SignalProcessor.FlightComputer.Window.Show();
  207. }
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement