Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using RemoteTech.SimpleTypes;
- using UnityEngine;
- using KSP.UI;
- namespace RemoteTech.UI {
- public class TimeWarpDecorator {
- /// <summary>
- /// Craped Timewarpobject from stock
- /// </summary>
- private TimeWarp mTimewarpObject;
- private UnityEngine.UI.Image mTimewarpImage;
- /// <summary>
- /// Delay-Text style
- /// </summary>
- private GUIStyle mTextStyle;
- /// <summary>
- /// Green Flightcomputer button
- /// </summary>
- private readonly GUIStyle mFlightButtonGreen;
- /// <summary>
- /// Red Flightcomputer button
- /// </summary>
- private readonly GUIStyle mFlightButtonRed;
- /// <summary>
- /// Yellow Flightcomputer button
- /// </summary>
- private readonly GUIStyle mFlightButtonYellow;
- /// <summary>
- /// Delay-Timer Background
- /// </summary>
- private readonly Texture2D mTexBackground;
- /// <summary>
- /// Activ Vessel
- /// </summary>
- private VesselSatellite mVessel { get { return RTCore.Instance.Satellites[FlightGlobals.ActiveVessel]; } }
- private String DisplayText {
- get {
- var vs = this.mVessel;
- if (vs == null) {
- return "N/A";
- } else if (vs.HasLocalControl) {
- return "Local Control";
- } else if (vs.Connections.Any()) {
- if (RTSettings.Instance.EnableSignalDelay) {
- return "D+ " + vs.Connections[0].Delay.ToString("F5") + "s";
- } else {
- return "Connected";
- }
- }
- return "No Connection";
- }
- }
- /// <summary>
- /// Returns the Style of the Flightcomputer button by the connected status
- /// </summary>
- private GUIStyle ButtonStyle {
- get {
- var vs = this.mVessel;
- if (vs == null) {
- return mFlightButtonRed;
- } else if (vs.HasLocalControl) {
- return mFlightButtonYellow;
- } else if (vs.Connections.Any()) {
- return mFlightButtonGreen;
- }
- return mFlightButtonRed;
- }
- }
- private Texture2D[,] fcTextures;
- private void saveFcTextures() {
- fcTextures = new Texture2D[3, 4];
- fcTextures[0, 0] = mFlightButtonGreen.normal.background;
- fcTextures[0, 1] = mFlightButtonGreen.hover.background;
- fcTextures[0, 2] = mFlightButtonGreen.active.background;
- fcTextures[0, 3] = mFlightButtonGreen.focused.background;
- fcTextures[1, 0] = mFlightButtonYellow.normal.background;
- fcTextures[1, 1] = mFlightButtonYellow.hover.background;
- fcTextures[1, 2] = mFlightButtonYellow.active.background;
- fcTextures[1, 3] = mFlightButtonYellow.focused.background;
- fcTextures[2, 0] = mFlightButtonRed.normal.background;
- fcTextures[2, 1] = mFlightButtonRed.hover.background;
- fcTextures[2, 2] = mFlightButtonRed.active.background;
- fcTextures[2, 3] = mFlightButtonRed.focused.background;
- }
- private void restoreFcTextures() {
- if (mFlightButtonGreen.normal.background == null || mFlightButtonYellow.normal.background == null || mFlightButtonRed.normal.background == null) {
- mFlightButtonGreen.normal.background = fcTextures[0, 0];
- mFlightButtonGreen.hover.background = fcTextures[0, 1];
- mFlightButtonGreen.active.background = fcTextures[0, 2];
- mFlightButtonGreen.onNormal.background = fcTextures[0, 2];
- mFlightButtonGreen.onHover.background = fcTextures[0, 2];
- mFlightButtonGreen.onActive.background = fcTextures[0, 2];
- mFlightButtonGreen.onFocused.background = fcTextures[0, 2];
- mFlightButtonGreen.focused.background = fcTextures[0, 3];
- mFlightButtonYellow.normal.background = fcTextures[1, 0];
- mFlightButtonYellow.hover.background = fcTextures[1, 1];
- mFlightButtonYellow.active.background = fcTextures[1, 2];
- mFlightButtonYellow.onNormal.background = fcTextures[1, 2];
- mFlightButtonYellow.onHover.background = fcTextures[1, 2];
- mFlightButtonYellow.onActive.background = fcTextures[1, 2];
- mFlightButtonYellow.onFocused.background = fcTextures[1, 2];
- mFlightButtonYellow.focused.background = fcTextures[1, 3];
- mFlightButtonRed.normal.background = fcTextures[2, 0];
- mFlightButtonRed.hover.background = fcTextures[2, 1];
- mFlightButtonRed.active.background = fcTextures[2, 2];
- mFlightButtonRed.onNormal.background = fcTextures[2, 2];
- mFlightButtonRed.onHover.background = fcTextures[2, 2];
- mFlightButtonRed.onActive.background = fcTextures[2, 2];
- mFlightButtonRed.onFocused.background = fcTextures[2, 2];
- mFlightButtonRed.focused.background = fcTextures[2, 3];
- }
- }
- public TimeWarpDecorator() {
- mFlightButtonGreen = GUITextureButtonFactory.CreateFromFilename("texFlightGreen.png", "texFlightGreenOver.png", "texFlightGreenDown.png", "texFlightGreenOver.png");
- mFlightButtonYellow = GUITextureButtonFactory.CreateFromFilename("texFlightYellow.png", "texFlightYellowOver.png", "texFlightYellowDown.png", "texFlightYellowOver.png");
- mFlightButtonRed = GUITextureButtonFactory.CreateFromFilename("texFlightRed.png", "texFlightRed.png", "texFlightRed.png", "texFlightRed.png");
- saveFcTextures();
- mFlightButtonGreen.fixedHeight = mFlightButtonGreen.fixedWidth = 0;
- mFlightButtonYellow.fixedHeight = mFlightButtonYellow.fixedWidth = 0;
- mFlightButtonRed.fixedHeight = mFlightButtonRed.fixedWidth = 0;
- mFlightButtonGreen.stretchHeight = mFlightButtonGreen.stretchWidth = true;
- mFlightButtonYellow.stretchHeight = mFlightButtonYellow.stretchWidth = true;
- mFlightButtonRed.stretchHeight = mFlightButtonRed.stretchWidth = true;
- // Crap timewarp object
- mTimewarpObject = TimeWarp.fetch;
- GameObject go = GameObject.Find("TimeQuadrant");
- if (go)
- mTimewarpImage = go.GetComponent<UnityEngine.UI.Image>();
- // objects on this scene?
- if (mTimewarpObject == null || mTimewarpObject.timeQuadrantTab == null) {
- // to skip the draw calls
- mTimewarpObject = null;
- return;
- }
- var text = mTimewarpObject.timeQuadrantTab.transform.FindChild("MET timer").GetComponent<ScreenSafeGUIText>();
- mTextStyle = new GUIStyle(text.textStyle);
- mTextStyle.fontSize = (int)(text.textSize * ScreenSafeUI.PixelRatio);
- // Put the draw function to the DrawQueue
- /*RenderingManager.AddToPostDrawQueue(0, Draw);*/
- //RTCore.Instance.OnGuiUpdate += Draw;
- // create the Background
- RTUtil.LoadImage(out mTexBackground, "TimeQuadrantFcStatus.png");
- }
- /// <summary>
- /// Draws the TimeQuadrantFcStatus.png, Delay time and the Flightcomputerbutton under the timewarp object
- /// </summary>
- public void Draw() {
- // no drawing without timewarp object
- if (mTimewarpObject == null)
- return;
- restoreFcTextures();
- var tr = mTimewarpImage.rectTransform.position;
- Vector2 screenCoord = UIMainCamera.Camera.WorldToScreenPoint(tr);
- // TODO: Might need to fix this also
- float scale = GameSettings.UI_SCALE;
- float topLeftTotimeQuadrant = (Screen.height - screenCoord.y + mTimewarpImage.preferredHeight - 1) * scale;
- float texBackgroundHeight = mTexBackground.height * 0.7f * scale;
- float texBackgroundWidth = (mTexBackground.width * 0.8111f) * scale;
- Rect delaytextPosition = new Rect((screenCoord.x + 12.0f) * scale, topLeftTotimeQuadrant, 50.0f * scale, 20.0f * scale);
- // calc the position under the timewarp object
- Rect pos = new Rect(screenCoord.x * scale,
- topLeftTotimeQuadrant,
- texBackgroundWidth, texBackgroundHeight);
- // draw the image
- GUI.DrawTexture(pos, mTexBackground);
- // draw the delay-text
- GUI.Label(delaytextPosition, DisplayText, mTextStyle);
- // draw the flightcomputer button to the right relativ to the delaytext position
- Rect btnPos = new Rect(pos.x + (125.0f * scale), topLeftTotimeQuadrant, 21.0f, 21.0f);
- // TODO: Fix this buttons icon, somehow it doesn't display but it still functions
- if (GUI.Button(btnPos, "", ButtonStyle)) {
- var satellite = RTCore.Instance.Satellites[FlightGlobals.ActiveVessel];
- if (satellite == null || satellite.SignalProcessor.FlightComputer == null) return;
- satellite.SignalProcessor.FlightComputer.Window.Show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement