Ejemplo n.º 1
0
        /// <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));
        }
Ejemplo n.º 2
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));
            }
        }
Ejemplo n.º 3
0
        /// <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());
                }
            }
        }
Ejemplo n.º 4
0
        internal HitBox InitHitBox(int idx, ConfigNode hitBoxConfig, InternalProp prop, MASFlightComputer comp)
        {
            HitBox hb = new HitBox();

            string positionString = string.Empty;

            if (!hitBoxConfig.TryGetValue("position", ref positionString))
            {
                Utility.LogError(this, "Missing 'position' in hitbox for MASPage " + name);
                return(null);
            }
            string[] positions = Utility.SplitVariableList(positionString);
            if (positions.Length != 2)
            {
                Utility.LogError(this, "Incorrect number of values in 'position' in hitbox for MASPage " + name);
                return(null);
            }

            float x1, y1;

            if (!(float.TryParse(positions[0], out x1) && float.TryParse(positions[1], out y1)))
            {
                Utility.LogError(this, "Unable to parse 'position' in hitbox for MASPage " + name);
                return(null);
            }

            string sizeString = string.Empty;

            if (!hitBoxConfig.TryGetValue("size", ref sizeString))
            {
                Utility.LogError(this, "Missing 'size' in hitbox for MASPage " + name);
                return(null);
            }
            string[] sizes = Utility.SplitVariableList(sizeString);
            if (sizes.Length != 2)
            {
                Utility.LogError(this, "Incorrect number of values in 'size' in hitbox for MASPage " + name);
                return(null);
            }

            float w, h;

            if (!(float.TryParse(sizes[0], out w) && float.TryParse(sizes[1], out h)))
            {
                Utility.LogError(this, "Unable to parse 'size' in hitbox for MASPage " + name);
                return(null);
            }
            hb.bounds = new Rect(x1, y1, w, h);

            string onClickString = string.Empty;

            if (hitBoxConfig.TryGetValue("onClick", ref onClickString))
            {
                Action <Vector2> onClick = comp.GetColliderAction(onClickString, idx, "click", prop);
                if (onClick == null)
                {
                    Utility.LogError(this, "Unable to configure 'onClick' in hitbox for MASPage " + name);
                    return(null);
                }
                hb.onClick = onClick;
            }

            string onDragString = string.Empty;

            if (hitBoxConfig.TryGetValue("onDrag", ref onDragString))
            {
                Action <Vector2> onDrag = comp.GetColliderAction(onDragString, idx, "drag", prop);
                if (onDrag == null)
                {
                    Utility.LogError(this, "Unable to configure 'onDrag' in hitbox for MASPage " + name);
                    return(null);
                }
                hb.onDrag = onDrag;
            }

            string onReleaseString = string.Empty;

            if (hitBoxConfig.TryGetValue("onRelease", ref onReleaseString))
            {
                Action <Vector2> onRelease = comp.GetColliderAction(onReleaseString, idx, "release", prop);
                if (onRelease == null)
                {
                    Utility.LogError(this, "Unable to configure 'onRelease' in hitbox for MASPage " + name);
                    return(null);
                }
                hb.onRelease = onRelease;
            }

            if (hb.onClick == null && hb.onDrag == null && hb.onRelease == null)
            {
                Utility.LogError(this, "No 'onClick', 'onDrag', or 'onRelease' entries in hitbox for MASPage " + name);
                return(null);
            }

            string variableString = string.Empty;

            if (hitBoxConfig.TryGetValue("variable", ref variableString))
            {
                // Force the state to false to begin with.
                hb.VariableCallback(0.0);
                variableRegistrar.RegisterVariableChangeCallback(variableString, hb.VariableCallback);
            }
            return(hb);
        }
Ejemplo n.º 5
0
            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;
                }
            }