Advertisement
TheReadPanda

Current TRP Source Code

May 29th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.99 KB | None | 0 0
  1. using KSP.UI;
  2. using System;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. namespace TRP_Hiring
  8. {
  9. [KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
  10. class KSI_EditACPrefabs_SpaceCentre : MonoBehaviour
  11. {
  12. private void Awake() { gameObject.AddComponent<KSI_EditACPrefab>(); }
  13. }
  14.  
  15. [KSPAddon(KSPAddon.Startup.EditorAny, false)]
  16. class KSI_EditACPrefabs_Editor : KSI_EditACPrefabs_SpaceCentre { }
  17.  
  18.  
  19. class KSI_EditACPrefab : MonoBehaviour
  20. {
  21. private void Awake()
  22. {
  23. try
  24. {
  25. var prefab = GetAstronautComplexScreenPrefab();
  26.  
  27. // There isn't really any specific component we can look for to make it easier
  28. // without also making things less clear so a long transform path it is
  29. var listAndScrollbar = prefab.transform.Find(
  30. "RootCanvas/AstronautComplex anchor/GameObject/bg and aspectFitter/CrewAssignmentDialog");
  31.  
  32. if (listAndScrollbar == null) throw new Exception("Couldn't find dialog Transform");
  33.  
  34. // temporarily detach Panel/scrolllist_applicants
  35. // we can't just delete it (or set it inactive apparently) or AstronautComplex will throw an exception
  36. //
  37. // we CAN, however, delete all the UI bits from it
  38. var panel = listAndScrollbar.transform.Find("Panel");
  39.  
  40. Debug.Log("var Panel Worked");
  41. // panel.SetParent(null);
  42.  
  43. // Debug.Log("var Panel set to null");
  44.  
  45. //panel.GetComponentsInChildren<ScrollRect>(true).ToList().ForEach(Destroy);
  46. //panel.GetComponentsInChildren<Image>(true).ToList().ForEach(Destroy);
  47. //panel.GetComponentsInChildren<CanvasRenderer>(true).ToList().ForEach(Destroy);
  48. //panel.GetComponentsInChildren<Mask>(true).ToList().ForEach(Destroy);
  49. //panel.GetComponentsInChildren<VerticalLayoutGroup>(true).ToList().ForEach(Destroy);
  50. //panel.GetComponentsInChildren<ContentSizeFitter>(true).ToList().ForEach(Destroy);
  51. Debug.Log("Destroy Set");
  52. // destroy all the other children
  53. foreach (Transform ch in listAndScrollbar.transform)
  54. Destroy(ch.gameObject);
  55.  
  56. // reattach panel - AstronautComplex needs something inside here for something but now it won't be
  57. // rendering anything
  58. // panel.SetParent(listAndScrollbar);
  59.  
  60. // we won't know what the dimensions our replacement will take until the UI is actually spawned,
  61. // so we'll slip a little trojan right alongside the component (RectTransform) that will contain
  62. // that info. This helper will create the GUI
  63. // listAndScrollbar.gameObject.AddComponent<CustomAstronautComplexUISpawner>();
  64.  
  65.  
  66. }
  67. catch (Exception e)
  68. {
  69. Debug.LogError("Failed to edit Astronaut Complex: " + e);
  70. }
  71.  
  72. // Destroy(gameObject); // once the spawner is attached to the prefab, this object no longer needed
  73. }
  74.  
  75.  
  76. private static UICanvasPrefab GetAstronautComplexScreenPrefab()
  77. {
  78. // the scene has just been entered and the AC shouldn't have spawned yet
  79. var spawner = FindObjectOfType<KSP.UI.Screens.ACSceneSpawner>();
  80.  
  81. if (spawner == null)
  82. throw new Exception("Did not find AC spawner");
  83. if (spawner.ACScreenPrefab == null)
  84. throw new Exception("AC spawner prefab is null");
  85.  
  86. // this might look bizarre, but Unity seems to crash if you mess with this prefab like we're
  87. // about to do. Luckily ACScreenPrefab is public so we'll quickly make a copy and use that
  88. // as the "prefab" instead
  89. var prefab = Instantiate(spawner.ACScreenPrefab);
  90. prefab.gameObject.SetActive(false);
  91.  
  92. spawner.ACScreenPrefab = prefab;
  93. Debug.Log(prefab.name);
  94. return prefab;
  95. }
  96.  
  97. }
  98.  
  99.  
  100.  
  101. // We'll have to wait until the AstronautComplex UI is actually spawned in
  102. // order to grab the dimensions of the area we'll be replacing. All the
  103. // visual bits should be handled by now so as soon as the panel is actually
  104. // visible, we can grab those dimensions and create the custom GUI using them
  105. class CustomAstronautComplexUISpawner : MonoBehaviour
  106. {
  107. private void Start()
  108. {
  109. #if DEBUG
  110. DebugDimensions();
  111. #endif
  112. // foreach (var textComponent in FindObjectsOfType<Component>()) Debug.Log(textComponent.name); This gave us the names of various 'parts'.
  113. Transform textTransform = FindObjectOfType<Text>().transform;
  114. while(textTransform != null)
  115. {
  116. Debug.Log(textTransform.name);
  117. textTransform = textTransform.transform.parent;
  118. }
  119.  
  120. var vectors = new Vector3[4];
  121. var uiCam = UIMasterController.Instance.GetComponentInChildren<UIMainCamera>();
  122.  
  123. if (uiCam == null)
  124. {
  125. Debug.LogError("UIMainCamera not found");
  126. return;
  127. }
  128.  
  129. var camera = uiCam.GetComponent<Camera>();
  130.  
  131. if (camera == null)
  132. {
  133. Debug.LogError("Camera attached to UIMainCamera not found");
  134. return;
  135. }
  136.  
  137. GetComponent<RectTransform>().GetWorldCorners(vectors);
  138.  
  139.  
  140. for (int i = 0; i < 4; ++i)
  141. vectors[i] = camera.WorldToScreenPoint(vectors[i]);
  142.  
  143.  
  144. // note: these are in screen space
  145. var rect = new Rect(vectors[1].x, Screen.height - vectors[1].y, vectors[2].x - vectors[1].x,
  146. vectors[2].y - vectors[3].y);
  147.  
  148. gameObject.AddComponent<CustomAstronautComplexUI>().Initialize(rect);
  149.  
  150. Destroy(this);
  151. }
  152.  
  153.  
  154. private void DebugDimensions()
  155. {
  156. print("Debugging dimensions");
  157.  
  158. var vectors = new Vector3[4];
  159. var camera = UIMasterController.Instance.GetComponentInChildren<UIMainCamera>().GetComponent<Camera>();
  160.  
  161. GetComponent<RectTransform>().GetWorldCorners(vectors);
  162.  
  163. foreach (var item in vectors)
  164. print("Corner: " + item);
  165.  
  166. for (int i = 0; i < 4; ++i)
  167. {
  168. vectors[i] = camera.GetComponent<Camera>().WorldToScreenPoint(vectors[i]);
  169. print("Transformed corner: " + vectors[i]);
  170. }
  171. }
  172. }
  173.  
  174.  
  175. /// <summary>
  176. /// This bit draws the GUI using the legacy GUI. You could easily use the new GUI instead if you prefer
  177. /// </summary>
  178. class CustomAstronautComplexUI : MonoBehaviour
  179. {
  180. private Rect _areaRect = new Rect(-500f, -500f, 200f, 200f);
  181. private Vector2 _guiScalar = Vector2.one;
  182. private Vector2 _guiPivot = Vector2.zero;
  183. private Vector2 _scrollbar = Vector2.zero;
  184. private readonly Texture2D _portraitMale = AssetBase.GetTexture("kerbalicon_recruit");
  185. private readonly Texture2D _portraitFemale = AssetBase.GetTexture("kerbalicon_recruit_female");
  186. private GUIStyle _backgroundStyle; // the background of the whole area our GUI will cover
  187. private GUIStyle _scrollviewStyle; // style of the whole scrollview
  188. private GUIStyle _nameStyle; // style used for kerbal names
  189. private GUIStyle _listItemEntryStyle; // style used for background of each kerbal entry
  190.  
  191. private void Awake()
  192. {
  193. enabled = false;
  194. _backgroundStyle = new GUIStyle(HighLogic.Skin.window)
  195. {
  196. padding = new RectOffset(),
  197. border = new RectOffset()
  198. };
  199.  
  200. _scrollviewStyle = new GUIStyle(HighLogic.Skin.scrollView)
  201. {
  202. padding = new RectOffset(),
  203. border = new RectOffset()
  204. };
  205.  
  206. _nameStyle = new GUIStyle(HighLogic.Skin.label);
  207. _nameStyle.fontSize = (int)(_nameStyle.fontSize * 1.6);
  208. _nameStyle.fontStyle = FontStyle.Bold;
  209.  
  210. // set up a style that will cause the moused over entry to highlight itself. Makes the UI feel a little more
  211. // interactive
  212. _listItemEntryStyle = new GUIStyle(HighLogic.Skin.box);
  213.  
  214. var clone = Instantiate(_listItemEntryStyle.normal.background);
  215. var pixels = clone.GetPixels32();
  216.  
  217. // poor man's lighting algorithm
  218. for (int i = 0; i < pixels.Length; ++i)
  219. pixels[i] = new Color32(
  220. Math.Min((byte)255, (byte)(pixels[i].r + 25)),
  221. Math.Min((byte)255, (byte)(pixels[i].g + 25)),
  222. Math.Min((byte)255, (byte)(pixels[i].b + 25)),
  223. Math.Min((byte)255, (byte)(pixels[i].a + 25)));
  224.  
  225. clone.SetPixels32(pixels);
  226. clone.Apply();
  227.  
  228. _listItemEntryStyle.hover.background = clone;
  229. }
  230.  
  231.  
  232. public void Initialize(Rect guiRect)
  233. {
  234. var uiScaleMultiplier = GameSettings.UI_SCALE;
  235.  
  236. // the supplied rect will have the UI scalar already factored in
  237. //
  238. // to respect the player's UI scale wishes, work out what the unscaled rect
  239. // would be. Then we'll apply the scale again in OnGUI so all of our GUILayout elements
  240. // will respect the multiplier
  241. var correctedRect = new Rect(guiRect.x, guiRect.y, guiRect.width / uiScaleMultiplier,
  242. guiRect.height / uiScaleMultiplier);
  243.  
  244. _areaRect = correctedRect;
  245.  
  246. _guiPivot = new Vector2(_areaRect.x, _areaRect.y);
  247. _guiScalar = new Vector2(GameSettings.UI_SCALE, GameSettings.UI_SCALE);
  248.  
  249. enabled = true;
  250. }
  251.  
  252.  
  253. private static readonly string[] FlavorText = new[]
  254. {
  255. "Loves space! Does not have any other qualifications",
  256. "Enjoys explosions of all kinds",
  257. "Was dared to do it",
  258. "Craves fame",
  259. "Might have some kind of death wish",
  260. "Heard it paid well",
  261. "Wants to go very fast"
  262. };
  263.  
  264. // these slightly reduce garbage created by avoiding array allocations which is one reason OnGUI
  265. // is so terrible
  266. private static readonly GUILayoutOption[] DefaultLayoutOptions = new GUILayoutOption[0];
  267. private static readonly GUILayoutOption[] PortraitOptions = { GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false) };
  268. private static readonly GUILayoutOption[] FixedWidth = { GUILayout.Width(220f) };
  269. private static readonly GUILayoutOption[] HireButtonOptions = { GUILayout.ExpandHeight(true), GUILayout.MaxHeight(40f), GUILayout.MinWidth(40f) };
  270. private static readonly GUILayoutOption[] StatOptions = { GUILayout.MaxWidth(100f) };
  271. private static readonly GUILayoutOption[] FlavorTextOptions = { GUILayout.MaxWidth(200f) };
  272.  
  273. private void OnGUI()
  274. {
  275. GUIUtility.ScaleAroundPivot(_guiScalar, _guiPivot);
  276.  
  277. GUILayout.BeginArea(_areaRect, _backgroundStyle);
  278. {
  279. _scrollbar = GUILayout.BeginScrollView(_scrollbar, false, true, HighLogic.Skin.horizontalScrollbar, HighLogic.Skin.verticalScrollbar, _scrollviewStyle, DefaultLayoutOptions);
  280. {
  281. for (int i = 0; i < 25; ++i) // make sure the scrollbars and stuff works as expected
  282. {
  283. bool male = i % 2 == 0;
  284.  
  285. GUILayout.BeginHorizontal(_listItemEntryStyle, DefaultLayoutOptions);
  286. {
  287. GUILayout.Space(8f);
  288. GUILayout.Box(male ? _portraitMale : _portraitFemale, GUIStyle.none, PortraitOptions);
  289.  
  290. GUILayout.BeginVertical(DefaultLayoutOptions);
  291. {
  292. GUILayout.Label(male ? "John Kerman" : "Jane Kerman", _nameStyle, FixedWidth);
  293. GUILayout.FlexibleSpace();
  294.  
  295. // indent flavor text just a bit
  296. GUILayout.BeginHorizontal(DefaultLayoutOptions);
  297. {
  298. GUILayout.Space(20f);
  299. GUILayout.Label(FlavorText[i % FlavorText.Length], FlavorTextOptions);
  300. }
  301. GUILayout.EndHorizontal();
  302. }
  303. GUILayout.EndVertical();
  304.  
  305.  
  306. GUILayout.FlexibleSpace();
  307. GUILayout.BeginVertical(DefaultLayoutOptions);
  308. {
  309. GUILayout.FlexibleSpace();
  310. GUILayout.Label("Courage here", StatOptions);
  311. GUILayout.Label("Stupidity here", StatOptions);
  312. GUILayout.FlexibleSpace();
  313. }
  314. GUILayout.EndVertical();
  315.  
  316. GUILayout.BeginVertical(DefaultLayoutOptions);
  317. {
  318. GUILayout.FlexibleSpace();
  319. GUILayout.Button("Hire", HighLogic.Skin.button, HireButtonOptions); // logic to hire Kerbal here
  320. GUILayout.FlexibleSpace();
  321. }
  322. GUILayout.EndVertical();
  323. }
  324. GUILayout.EndHorizontal();
  325. }
  326. }
  327. GUILayout.EndScrollView();
  328. }
  329. GUILayout.EndArea();
  330. }
  331. }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement