コード例 #1
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);
        }
コード例 #2
0
        internal MASComponentColliderAdvanced(ConfigNode config, InternalProp internalProp, MASFlightComputer comp)
            : base(config, internalProp, comp)
        {
            string collider = string.Empty;

            if (!config.TryGetValue("collider", ref collider))
            {
                throw new ArgumentException("Missing 'collider' in COLLIDER_ADVANCED " + name);
            }

            string clickAction   = string.Empty;
            string dragAction    = string.Empty;
            string releaseAction = string.Empty;
            string monitorID     = string.Empty;

            if (!config.TryGetValue("monitorID", ref monitorID))
            {
                config.TryGetValue("onClick", ref clickAction);
                config.TryGetValue("onDrag", ref dragAction);
                config.TryGetValue("onRelease", ref releaseAction);

                if (string.IsNullOrEmpty(clickAction) && string.IsNullOrEmpty(dragAction) && string.IsNullOrEmpty(releaseAction))
                {
                    throw new ArgumentException("Missing 'monitorID', 'onClick', 'onDrag', or 'onRelease' in COLLIDER_ADVANCED " + name);
                }
            }

            string clickX = string.Empty;

            if (!config.TryGetValue("clickX", ref clickX))
            {
                throw new ArgumentException("Missing 'clickX' in COLLIDER_ADVANCED " + name);
            }
            string clickY = string.Empty;

            if (!config.TryGetValue("clickY", ref clickY))
            {
                throw new ArgumentException("Missing 'clickY' in COLLIDER_ADVANCED " + name);
            }

            Transform tr = internalProp.FindModelTransform(collider.Trim());

            if (tr == null)
            {
                throw new ArgumentException("Unable to find transform '" + collider + "' in prop for COLLIDER_ADVANCED " + name);
            }

            float volume = -1.0f;

            if (config.TryGetValue("volume", ref volume))
            {
                volume = Mathf.Clamp01(volume);
            }
            else
            {
                volume = -1.0f;
            }

            string sound = string.Empty;

            if (!config.TryGetValue("sound", ref sound) || string.IsNullOrEmpty(sound))
            {
                sound = string.Empty;
            }

            AudioClip clip = null;

            if (string.IsNullOrEmpty(sound) && (volume >= 0.0f))
            {
                throw new ArgumentException("'volume', but no 'sound', found in COLLIDER_ADVANCED " + name);
            }

            if (!string.IsNullOrEmpty(sound))
            {
                //Try Load audio
                clip = GameDatabase.Instance.GetAudioClip(sound);
                if (clip == null)
                {
                    throw new ArgumentException("Unable to load 'sound' " + sound + " in COLLIDER_ADVANCED " + name);
                }
                if (volume < 0.0f)
                {
                    volume = 1.0f;
                }
            }

            buttonObject        = tr.gameObject.AddComponent <AdvancedButtonObject>();
            buttonObject.parent = this;
            if (string.IsNullOrEmpty(monitorID))
            {
                if (!string.IsNullOrEmpty(clickAction))
                {
                    onClick = comp.GetColliderAction(clickAction, 0, "click", internalProp);
                }
                if (!string.IsNullOrEmpty(dragAction))
                {
                    onDrag = comp.GetColliderAction(dragAction, 0, "drag", internalProp);
                }
                if (!string.IsNullOrEmpty(releaseAction))
                {
                    onRelease = comp.GetColliderAction(releaseAction, 0, "release", internalProp);
                }

                buttonObject.onTouch = (Vector2 hitCoordinate, EventType eventType) =>
                {
                    if (eventType == EventType.MouseDown)
                    {
                        if (onClick != null)
                        {
                            onClick(hitCoordinate);
                        }
                    }
                    else if (eventType == EventType.MouseDrag)
                    {
                        if (onDrag != null)
                        {
                            onDrag(hitCoordinate);
                        }
                    }
                    else if (eventType == EventType.MouseUp)
                    {
                        if (onRelease != null)
                        {
                            onRelease(hitCoordinate);
                        }
                    }
                };
            }
            else
            {
                buttonObject.onTouch = comp.GetHitAction(monitorID, internalProp, comp.HandleTouchEvent);
            }
            buttonObject.hitTransformation = comp.GetColliderTransformation(clickX, clickY, name, internalProp);
            Collider btnCollider = tr.gameObject.GetComponent <Collider>();

            if (btnCollider == null)
            {
                throw new ArgumentException("Unable to retrieve Collider from GameObject in COLLIDER_ADVANCED " + name);
            }
            BoxCollider boxCollider = btnCollider as BoxCollider;

            if (boxCollider == null)
            {
                throw new ArgumentException("Collider for COLLIDER_ADVANCED " + name + " is not a BoxCollider");
            }

            buttonObject.InitBoxCollider(boxCollider);
            if (!config.TryGetValue("logHits", ref buttonObject.debugEnabled))
            {
                buttonObject.debugEnabled = false;
            }

            string variableName = string.Empty;

            if (config.TryGetValue("variable", ref variableName))
            {
                variableName = variableName.Trim();

                buttonObject.colliderEnabled = false;
                comp.RegisterVariableChangeCallback(variableName, internalProp, VariableCallback);
            }
            else
            {
                variableEnabled = true;
            }
            comp.RegisterVariableChangeCallback("fc.CrewConscious(-1)", internalProp, KerbalCallback);

            if (clip != null)
            {
                AudioSource audioSource = tr.gameObject.AddComponent <AudioSource>();
                audioSource.clip = clip;
                audioSource.Stop();
                audioSource.volume       = GameSettings.SHIP_VOLUME * volume;
                audioSource.rolloffMode  = AudioRolloffMode.Logarithmic;
                audioSource.maxDistance  = 8.0f;
                audioSource.minDistance  = 2.0f;
                audioSource.dopplerLevel = 0.0f;
                audioSource.panStereo    = 0.0f;
                audioSource.playOnAwake  = false;
                audioSource.loop         = false;
                audioSource.pitch        = 1.0f;
                buttonObject.audioSource = audioSource;
            }
        }