/// <summary>
        /// Register a variable-changed callback.
        /// </summary>
        /// <param name="name">The name of the variable.</param>
        /// <param name="action">The action to trigger.</param>
        /// <param name="initializeNow">Trigger the callback before this method returns (okay for normal use,
        /// may be a problem if the Variable that's returned needs to be initialized first.</param>
        /// <returns>The Variable created (or null if it failed for some reason).</returns>
        internal Variable RegisterVariableChangeCallback(string name, Action <double> action, bool initializeNow = true)
        {
            name = name.Trim();
            Variable v = comp.RegisterVariableChangeCallback(name, internalProp, action, initializeNow);

            if (v != null)
            {
                variableAction.Add(action);
                variable.Add(v);
            }
            return(v);
        }
Esempio n. 2
0
        internal MASComponentTriggerEvent(ConfigNode config, InternalProp prop, MASFlightComputer comp)
            : base(config, prop, comp)
        {
            string variableName = string.Empty;

            if (!config.TryGetValue("variable", ref variableName) || string.IsNullOrEmpty(variableName))
            {
                throw new ArgumentException("Invalid or missing 'variable' in TRIGGER_EVENT " + name);
            }
            variableName = variableName.Trim();

            if (!config.TryGetValue("autoRepeat", ref autoRepeat))
            {
                autoRepeat = false;
            }

            string triggerEventName = string.Empty;

            config.TryGetValue("event", ref triggerEventName);
            if (string.IsNullOrEmpty(triggerEventName))
            {
                throw new ArgumentException("Invalid or missing 'event' in TRIGGER_EVENT " + name);
            }

            triggerEvent = comp.GetAction(triggerEventName, prop);
            if (triggerEvent == null)
            {
                throw new ArgumentException("Unable to create event '" + triggerEventName + "' in TRIGGER_EVENT " + name);
            }

            triggerEventName = string.Empty;
            if (config.TryGetValue("exitEvent", ref triggerEventName))
            {
                exitEvent = comp.GetAction(triggerEventName, prop);
                if (exitEvent == null)
                {
                    throw new ArgumentException("Unable to create event '" + triggerEventName + "' in TRIGGER_EVENT " + name);
                }
            }

            this.comp        = comp;
            callbackVariable = comp.RegisterVariableChangeCallback(variableName, prop, VariableCallback);
        }
Esempio n. 3
0
        internal MASComponentColliderEvent(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_EVENT " + name);
            }

            string clickEvent = string.Empty, releaseEvent = string.Empty, dragEventX = string.Empty, dragEventY = string.Empty;

            config.TryGetValue("onClick", ref clickEvent);
            config.TryGetValue("onRelease", ref releaseEvent);
            config.TryGetValue("onDragX", ref dragEventX);
            config.TryGetValue("onDragY", ref dragEventY);
            if (string.IsNullOrEmpty(clickEvent) && string.IsNullOrEmpty(releaseEvent) && string.IsNullOrEmpty(dragEventX) && string.IsNullOrEmpty(dragEventY))
            {
                throw new ArgumentException("None of 'onClick', 'onRelease', 'onDragX', nor 'onDragY' found in COLLIDER_EVENT " + name);
            }

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

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

            float autoRepeat = 0.0f;

            if (!config.TryGetValue("autoRepeat", ref autoRepeat))
            {
                autoRepeat = 0.0f;
            }

            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("Only one of 'sound' or 'volume' found in COLLIDER_EVENT " + name);
            }

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

            buttonObject            = tr.gameObject.AddComponent <ButtonObject>();
            buttonObject.parent     = this;
            buttonObject.autoRepeat = (autoRepeat > 0.0f);
            buttonObject.repeatRate = autoRepeat;

            string variableName = string.Empty;

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

                buttonObject.colliderEnabled = false;
                comp.RegisterVariableChangeCallback(variableName, internalProp, VariableCallback);
            }

            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;
            }

            if (!string.IsNullOrEmpty(clickEvent))
            {
                buttonObject.onClick = comp.GetAction(clickEvent, internalProp);
            }
            if (!string.IsNullOrEmpty(releaseEvent))
            {
                buttonObject.onRelease = comp.GetAction(releaseEvent, internalProp);
            }
            if (!string.IsNullOrEmpty(dragEventX))
            {
                buttonObject.onDragX = comp.GetDragAction(dragEventX, name, internalProp);
                if (buttonObject.onDragX != null)
                {
                    buttonObject.drag = true;
                    float dragSensitivity = 1.0f;
                    if (!config.TryGetValue("dragSensitivity", ref dragSensitivity))
                    {
                        dragSensitivity = 1.0f;
                    }
                    buttonObject.normalizationScalar = 0.01f * dragSensitivity;
                }
                else
                {
                    throw new ArgumentException("Unable to create 'onDragX' event for COLLIDER_EVENT " + name);
                }
            }
            if (!string.IsNullOrEmpty(dragEventY))
            {
                buttonObject.onDragY = comp.GetDragAction(dragEventY, name, internalProp);
                if (buttonObject.onDragY != null)
                {
                    buttonObject.drag = true;
                    float dragSensitivity = 1.0f;
                    if (!config.TryGetValue("dragSensitivity", ref dragSensitivity))
                    {
                        dragSensitivity = 1.0f;
                    }
                    buttonObject.normalizationScalar = 0.01f * dragSensitivity;
                }
                else
                {
                    throw new ArgumentException("Unable to create 'onDragY' event for COLLIDER_EVENT " + name);
                }
            }
            if (buttonObject.onDragX != null && buttonObject.onDragY != null)
            {
                config.TryGetValue("singleAxisDrag", ref buttonObject.singleAxisDrag);
            }
        }
Esempio n. 4
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;
            }
        }