/// <summary> /// Callback fired when our variable changes. /// </summary> private void PageChanged(double dontCare) { try { MASPage newPage = page[pageSelector.AsString()]; currentPage.SetPageActive(false); currentPage = newPage; currentPage.SetPageActive(true); } catch { Utility.LogError(this, "Unable to switch to page '" + pageSelector.AsString() + "'"); } }
/// <summary> /// Startup, initialize, configure, etc. /// </summary> public void Start() { if (HighLogic.LoadedSceneIsFlight) { try { MASFlightComputer comp = MASFlightComputer.Instance(internalProp.part); if (comp == null) { throw new ArgumentNullException("Failed to find MASFlightComputer initializing MASMonitor"); } variableRegistrar = new VariableRegistrar(comp, internalProp); if (string.IsNullOrEmpty(screenTransform)) { throw new ArgumentException("Missing 'transform' in MASMonitor"); } if (string.IsNullOrEmpty(layer)) { throw new ArgumentException("Missing 'layer' in MASMonitor"); } if (string.IsNullOrEmpty(font)) { throw new ArgumentException("Missing 'font' in MASMonitor"); } if (string.IsNullOrEmpty(textColor)) { throw new ArgumentException("Missing 'textColor' in MASMonitor"); } else { textColor_ = Utility.ParseColor32(textColor, comp); } if (string.IsNullOrEmpty(backgroundColor)) { throw new ArgumentException("Missing 'backgroundColor' in MASMonitor"); } else { backgroundColor_ = Utility.ParseColor32(backgroundColor, comp); } if (screenSize.x <= 0.0f || screenSize.y <= 0.0f) { throw new ArgumentException("Invalid 'screenSize' in MASMonitor"); } if (fontSize.x <= 0.0f || fontSize.y <= 0.0f) { throw new ArgumentException("Invalid 'fontSize' in MASMonitor"); } screenWidth = (int)screenSize.x; screenHeight = (int)screenSize.y; screenSpace = new GameObject(); screenSpace.name = Utility.ComposeObjectName(internalProp.propName, this.GetType().Name, screenSpace.GetInstanceID()); screenSpace.layer = drawingLayer; screenSpace.transform.position = Vector3.zero; screenSpace.SetActive(true); screen = new RenderTexture(screenWidth, screenHeight, 24, RenderTextureFormat.ARGB32); if (screen == null) { throw new ArgumentNullException("Failed to find create " + screenWidth + " x " + screenHeight + " render texture initializing MASMonitor"); } if (!screen.IsCreated()) { screen.Create(); screen.DiscardContents(); } Camera.onPreCull += EnablePage; Camera.onPostRender += DisablePage; screenCamera = screenSpace.AddComponent <Camera>(); screenCamera.enabled = true; // Enable = "auto-draw" screenCamera.orthographic = true; screenCamera.aspect = screenSize.x / screenSize.y; screenCamera.eventMask = 0; screenCamera.farClipPlane = 1.0f + depthDelta; screenCamera.nearClipPlane = depthDelta; screenCamera.orthographicSize = screenSize.y * 0.5f; screenCamera.cullingMask = 1 << drawingLayer; screenCamera.transparencySortMode = TransparencySortMode.Orthographic; screenCamera.transform.position = Vector3.zero; screenCamera.transform.LookAt(new Vector3(0.0f, 0.0f, maxDepth), Vector3.up); screenCamera.backgroundColor = backgroundColor_; screenCamera.clearFlags = CameraClearFlags.SolidColor; screenCamera.targetTexture = screen; Transform screenTransformLoc = internalProp.FindModelTransform(screenTransform); if (screenTransformLoc == null) { throw new ArgumentNullException("Failed to find screenTransform \"" + screenTransform + "\" initializing MASMonitor"); } Material screenMat = screenTransformLoc.GetComponent <Renderer>().material; string[] layers = layer.Split(); for (int i = layers.Length - 1; i >= 0; --i) { screenMat.SetTexture(layers[i].Trim(), screen); } defaultFont = MASLoader.GetFont(font.Trim()); if (!string.IsNullOrEmpty(style)) { defaultStyle = MdVTextMesh.FontStyle(style.Trim()); } ConfigNode moduleConfig = Utility.GetPropModuleConfigNode(internalProp.propName, ClassName); if (moduleConfig == null) { throw new ArgumentNullException("No ConfigNode found for MASMonitor in " + internalProp.propName + "!"); } // If an initialization script was supplied, call it. if (!string.IsNullOrEmpty(startupScript)) { Action startup = comp.GetAction(startupScript, internalProp); startup(); } string[] pages = moduleConfig.GetValues("page"); int numPages = pages.Length; for (int i = 0; i < numPages; ++i) { pages[i] = pages[i].Trim(); ConfigNode pageConfig = Utility.GetPageConfigNode(pages[i]); if (pageConfig == null) { throw new ArgumentException("No ConfigNode found for page " + pages[i] + " in MASMonitor in " + internalProp.propName + "!"); } // Parse the page node MASPage newPage = new MASPage(pageConfig, internalProp, comp, this, screenSpace.transform); if (i == 0) { // Select the default page as the current page currentPage = newPage; } newPage.SetPageActive(false); page.Add(pages[i], newPage); //Utility.LogMessage(this, "Page = {0}", pages[i]); } //HackWalkTransforms(screenSpace.transform, 0); if (!string.IsNullOrEmpty(monitorID)) { string variableName = "fc.GetPersistent(\"" + monitorID.Trim() + "\")"; pageSelector = variableRegistrar.RegisterVariableChangeCallback(variableName, PageChanged, false); // See if we have a saved page to restore. if (!string.IsNullOrEmpty(pageSelector.AsString()) && page.ContainsKey(pageSelector.AsString())) { currentPage = page[pageSelector.AsString()]; } comp.RegisterMonitor(monitorID, internalProp, this); } currentPage.SetPageActive(true); initialized = true; Utility.LogMessage(this, "Configuration complete in prop #{0} ({1}) with {2} pages", internalProp.propID, internalProp.propName, numPages); } catch (Exception e) { Utility.ComplainLoudly("MASMonitor configuration failed."); Utility.LogError(this, "Failed to configure prop #{0} ({1})", internalProp.propID, internalProp.propName); Utility.LogError(this, e.ToString()); } } }