/// <summary> /// Configure the common fields. /// </summary> /// <param name="config"></param> /// <param name="prop"></param> /// <param name="comp"></param> internal IMASSubComponent(ConfigNode config, InternalProp prop, MASFlightComputer comp) { variableRegistrar = new VariableRegistrar(comp, prop); if (!config.TryGetValue("name", ref name)) { name = "anonymous"; } }
/// <summary> /// Initialize the MASNavBall components. /// </summary> public void Start() { MASFlightComputer comp = MASFlightComputer.Instance(internalProp.part); if (comp == null) { throw new ArgumentNullException("Unable to find MASFlightComputer in part - please check part configs"); } variableRegistrar = new VariableRegistrar(comp, null); lastOrientation = navBall.rotation; variableRegistrar.RegisterVariableChangeCallback(variable, (double newValue) => currentState = (newValue > 0.0)); }
/// <summary> /// Callback used to tell the mode to refresh its shader properties. /// </summary> /// <param name="comp"></param> public void UpdateShaderProperties(MASFlightComputer comp) { if (this.comp == comp) { return; } UnregisterShaderProperties(); this.comp = comp; variableRegistrar = new VariableRegistrar(comp, null); for (int i = 0; i < propertyValue.Length; ++i) { int id = propertyId[i]; variableRegistrar.RegisterVariableChangeCallback(propertyValue[i], (double newValue) => postProcShader.SetFloat(id, (float)newValue)); } }
/// <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()); } } }
internal MASPage(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform rootTransform) { if (!config.TryGetValue("name", ref name)) { throw new ArgumentException("Invalid or missing 'name' in MASPage"); } variableRegistrar = new VariableRegistrar(comp, prop); string[] softkeys = config.GetValues("softkey"); int numSoftkeys = softkeys.Length; for (int i = 0; i < numSoftkeys; ++i) { string[] pair = Utility.SplitVariableList(softkeys[i]); if (pair.Length == 2) { int id; if (int.TryParse(pair[0], out id)) { Action action = comp.GetAction(pair[1], prop); if (action != null) { softkeyAction[id] = action; } } } } ConfigNode[] hitboxes = config.GetNodes("hitbox"); int numHitboxes = hitboxes.Length; for (int i = 0; i < numHitboxes; ++i) { HitBox hb = InitHitBox(i, hitboxes[i], prop, comp); if (hb != null) { hitboxActions.Add(hb); } } string entryMethod = string.Empty; if (config.TryGetValue("onEntry", ref entryMethod)) { onEntry = comp.GetAction(entryMethod, prop); } string exitMethod = string.Empty; if (config.TryGetValue("onExit", ref exitMethod)) { onExit = comp.GetAction(exitMethod, prop); } pageRoot = new GameObject(); pageRoot.name = Utility.ComposeObjectName(this.GetType().Name, name, prop.propID); pageRoot.layer = rootTransform.gameObject.layer; pageRoot.transform.parent = rootTransform; pageRoot.transform.Translate(0.0f, 0.0f, 1.0f); float depth = 0.0f; ConfigNode[] components = config.GetNodes(); int numComponents = components.Length; if (Array.Exists(components, x => x.name == "SUB_PAGE")) { // Need to collate List <ConfigNode> newComponents = new List <ConfigNode>(); for (int i = 0; i < numComponents; ++i) { ConfigNode node = components[i]; if (node.name == "SUB_PAGE") { newComponents.AddRange(ResolveSubPage(node, monitor.fontSize)); } else { newComponents.Add(node); } } components = newComponents.ToArray(); numComponents = components.Length; } for (int i = 0; i < numComponents; ++i) { try { var pageComponent = CreatePageComponent(components[i], prop, comp, monitor, pageRoot.transform, depth); if (pageComponent != null) { component.Add(pageComponent); depth -= MASMonitor.depthDelta; } } catch (Exception e) { string componentName = string.Empty; if (!components[i].TryGetValue("name", ref componentName)) { componentName = "anonymous"; } string error = string.Format("Error configuring MASPage " + name + " " + config.name + " " + componentName + ":"); Utility.LogError(this, error); Utility.LogError(this, "{0}", e.ToString()); Utility.ComplainLoudly(error); } } if (numComponents > 256) { Utility.LogWarning(this, "{0} elements were used in MASPage {1}. This may exceed the number of supported elements.", numComponents, name); } }
internal MenuItem(ConfigNode itemNode, GameObject rootObject, Font font, Vector2 fontSize, FontStyle style, Color32 defaultColor, MASFlightComputer comp, InternalProp prop, VariableRegistrar variableRegistrar, int itemId) { string itemIdStr = itemId.ToString(); if (!itemNode.TryGetValue("name", ref name)) { name = "anonymous"; } string selectEventStr = string.Empty; if (itemNode.TryGetValue("selectEvent", ref selectEventStr)) { selectEventStr = selectEventStr.Replace("%ITEMID%", itemIdStr); selectEvent = comp.GetAction(selectEventStr, prop); } enabled = true; string activeColorStr = string.Empty; if (!itemNode.TryGetValue("activeColor", ref activeColorStr) || string.IsNullOrEmpty(activeColorStr)) { activeColor = defaultColor; } else { activeColor = Utility.ParseColor32(activeColorStr, comp); } string activeTextStr = string.Empty; if (!itemNode.TryGetValue("activeText", ref activeTextStr)) { throw new ArgumentException("Missing 'activeText' in ITEM " + name); } activeTextStr = activeTextStr.Replace("%ITEMID%", itemIdStr); activeObject = new GameObject(); activeObject.name = rootObject.name + "_activeitem_" + name; activeObject.layer = rootObject.layer; activeObject.transform.parent = rootObject.transform; activeObject.transform.position = rootObject.transform.position; activeText = activeObject.AddComponent <MdVTextMesh>(); activeText.material = new Material(MASLoader.shaders["MOARdV/TextMonitor"]); activeText.SetFont(font, fontSize); activeText.SetColor(activeColor); activeText.material.SetFloat(Shader.PropertyToID("_EmissiveFactor"), 1.0f); activeText.fontStyle = style; activeText.SetText(activeTextStr, false, true, comp, prop); activeText.SetRenderEnabled(false); string passiveColorStr = string.Empty; if (!itemNode.TryGetValue("passiveColor", ref passiveColorStr) || string.IsNullOrEmpty(passiveColorStr)) { passiveColor = activeColor; } else { passiveColor = Utility.ParseColor32(passiveColorStr, comp); } string passiveTextStr = string.Empty; if (!itemNode.TryGetValue("passiveText", ref passiveTextStr)) { passiveObject = activeObject; passiveText = activeText; } else { passiveTextStr = passiveTextStr.Replace("%ITEMID%", itemIdStr); passiveObject = new GameObject(); passiveObject.name = rootObject.name + "_passiveitem_" + name; passiveObject.layer = rootObject.layer; passiveObject.transform.parent = rootObject.transform; passiveObject.transform.position = rootObject.transform.position; passiveText = passiveObject.AddComponent <MdVTextMesh>(); passiveText.material = new Material(MASLoader.shaders["MOARdV/TextMonitor"]); passiveText.SetFont(font, fontSize); passiveText.SetColor(passiveColor); passiveText.material.SetFloat(Shader.PropertyToID("_EmissiveFactor"), 1.0f); passiveText.fontStyle = style; passiveText.SetText(passiveTextStr, false, true, comp, prop); passiveText.SetRenderEnabled(false); } string activeVariableStr = string.Empty; if (itemNode.TryGetValue("activeVariable", ref activeVariableStr)) { active = false; activeVariableStr = activeVariableStr.Replace("%ITEMID%", itemIdStr); variableRegistrar.RegisterVariableChangeCallback(activeVariableStr, (double newValue) => active = (newValue > 0.0)); } else { active = true; } string enabledVariableStr = string.Empty; if (itemNode.TryGetValue("enabledVariable", ref enabledVariableStr)) { usesDisabled = true; enabled = false; enabledVariableStr = enabledVariableStr.Replace("%ITEMID%", itemIdStr); variableRegistrar.RegisterVariableChangeCallback(enabledVariableStr, (double newValue) => enabled = (newValue > 0.0)); string disabledTextStr = string.Empty; if (!itemNode.TryGetValue("disabledText", ref disabledTextStr)) { throw new ArgumentException("Missing 'disabledText' in ITEM " + name); } disabledTextStr = disabledTextStr.Replace("%ITEMID%", itemIdStr); Color32 disabledColor; string disabledColorStr = string.Empty; if (!itemNode.TryGetValue("disabledColor", ref disabledColorStr) || string.IsNullOrEmpty(disabledColorStr)) { disabledColor = defaultColor; } else { disabledColor = Utility.ParseColor32(disabledColorStr, comp); } disabledObject = new GameObject(); disabledObject.name = rootObject.name + "_disableditem_" + name; disabledObject.layer = rootObject.layer; disabledObject.transform.parent = rootObject.transform; disabledObject.transform.position = rootObject.transform.position; disabledText = disabledObject.AddComponent <MdVTextMesh>(); disabledText.material = new Material(MASLoader.shaders["MOARdV/TextMonitor"]); disabledText.SetFont(font, fontSize); disabledText.SetColor(disabledColor); disabledText.material.SetFloat(Shader.PropertyToID("_EmissiveFactor"), 1.0f); disabledText.fontStyle = style; disabledText.SetText(disabledTextStr, false, true, comp, prop); disabledText.SetRenderEnabled(false); } else { enabled = true; usesDisabled = false; } }