示例#1
0
        /// <summary>
        /// On Update, we move our value based on the defined settings
        /// </summary>
        protected virtual void Update()
        {
            _targetObjectLastFrame    = TargetObject;
            _targetAttributeLastFrame = TargetAttribute;

            if ((TargetObject == null) || (TargetAttribute == null) || (!_attributeFound))
            {
                return;
            }

            switch (ControlMode)
            {
            case ControlModes.PingPong:

                if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration)
                {
                    return;
                }
                PingPong += GetDeltaTime() * _pingPongDirection;

                if (PingPong < 0f)
                {
                    PingPong             = 0f;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }

                if (PingPong > Duration)
                {
                    PingPong             = Duration;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }
                CurrentValue           = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, MinValue, MaxValue, 0f, 1f);
                break;

            case ControlModes.Random:
                _elapsedTime          += GetDeltaTime();
                CurrentValueNormalized = Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift);
                if (RemapNoiseValues)
                {
                    CurrentValue = CurrentValueNormalized;
                    CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, RemapNoiseZero, RemapNoiseOne);
                }
                else
                {
                    CurrentValue = (CurrentValueNormalized * 2.0f - 1.0f) * _randomAmplitude;
                }
                break;

            case ControlModes.OneTime:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(GetTime() - _shakeStartTimestamp, 0f, OneTimeDuration, 0f, 1f);
                CurrentValueNormalized  = OneTimeCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue            = MMMaths.Remap(CurrentValueNormalized, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                CurrentValue           *= OneTimeAmplitude;
                break;

            case ControlModes.AudioAnalyzer:
                if (AudioAnalyzerMode == AudioAnalyzerModes.Beat)
                {
                    CurrentValue = AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier;
                }
                else
                {
                    CurrentValue = AudioAnalyzer.NormalizedBufferedBandLevels[NormalizedLevelID] * AudioAnalyzerMultiplier;
                }
                CurrentValueNormalized = Mathf.Clamp(AudioAnalyzer.Beats[BeatID].CurrentValue, 0f, 1f);
                break;

            case ControlModes.Driven:
                CurrentValue           = DrivenLevel;
                CurrentValueNormalized = Mathf.Clamp(CurrentValue, 0f, 1f);
                break;

            case ControlModes.ToDestination:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(GetTime() - _shakeStartTimestamp, 0f, ToDestinationDuration, 0f, 1f);
                float time = ToDestinationCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue           = Mathf.LerpUnclamped(_initialValue, ToDestinationValue, time);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, _initialValue, ToDestinationValue, 0f, 1f);
                break;
            }


            if (AddToInitialValue)
            {
                CurrentValue += InitialValue;
            }

            if (ControlMode == ControlModes.OneTime)
            {
                if (_shaking && (GetTime() - _shakeStartTimestamp > OneTimeDuration))
                {
                    _shaking = false;
                    if (RevertToInitialValueAfterEnd)
                    {
                        CurrentValue = InitialValue;
                        TargetAttribute.SetValue(CurrentValue);
                    }
                    else
                    {
                        CurrentValue  = OneTimeCurve.Evaluate(1f);
                        CurrentValue  = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                        CurrentValue *= OneTimeAmplitude;
                        TargetAttribute.SetValue(CurrentValue);
                    }
                    if (DisableAfterOneTime)
                    {
                        this.enabled = false;
                    }
                    if (DisableGameObjectAfterOneTime)
                    {
                        this.gameObject.SetActive(false);
                    }
                    return;
                }
            }

            if (ControlMode == ControlModes.ToDestination)
            {
                if (_shaking && (GetTime() - _shakeStartTimestamp > ToDestinationDuration))
                {
                    _shaking = false;
                    if (RevertToInitialValueAfterEnd)
                    {
                        CurrentValue = InitialValue;
                    }
                    else
                    {
                        CurrentValue = ToDestinationValue;
                    }
                    TargetAttribute.SetValue(CurrentValue);

                    if (DisableAfterOneTime)
                    {
                        this.enabled = false;
                    }
                    if (DisableGameObjectAfterOneTime)
                    {
                        this.gameObject.SetActive(false);
                    }
                    return;
                }
            }

            TargetAttribute.SetValue(CurrentValue);
        }
        /// <summary>
        /// Updates the value over time based on the selected options
        /// </summary>
        protected virtual void UpdateValue()
        {
            if (RendererIsNull() || (!PropertyFound))
            {
                return;
            }

            switch (ControlMode)
            {
            case ControlModes.PingPong:
                if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration)
                {
                    return;
                }
                PingPong += GetDeltaTime() * _pingPongDirection;
                if (PingPong < 0f)
                {
                    PingPong             = 0f;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }

                if (PingPong > Duration)
                {
                    PingPong             = Duration;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }
                CurrentValue           = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, MinValue, MaxValue, 0f, 1f);
                break;

            case ControlModes.Random:
                _elapsedTime          += GetDeltaTime();
                CurrentValueNormalized = Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift);
                if (RemapNoiseValues)
                {
                    CurrentValue = CurrentValueNormalized;
                    CurrentValue = MMMaths.Remap(CurrentValue, 0f, 1f, RemapNoiseZero, RemapNoiseOne);
                }
                else
                {
                    CurrentValue = (CurrentValueNormalized * 2.0f - 1.0f) * _randomAmplitude;
                }
                break;

            case ControlModes.OneTime:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(Time.time - _startedTimestamp, 0f, OneTimeDuration, 0f, 1f);
                CurrentValueNormalized  = OneTimeCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue            = MMMaths.Remap(CurrentValueNormalized, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                CurrentValue           *= OneTimeAmplitude;
                break;

            case ControlModes.AudioAnalyzer:
                CurrentValue           = Mathf.Lerp(CurrentValue, AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier + AudioAnalyzerOffset, AudioAnalyzerLerp * Time.deltaTime);
                CurrentValueNormalized = Mathf.Clamp(AudioAnalyzer.Beats[BeatID].CurrentValue, 0f, 1f);
                break;

            case ControlModes.Driven:
                CurrentValue           = DrivenLevel;
                CurrentValueNormalized = Mathf.Clamp(CurrentValue, 0f, 1f);
                break;

            case ControlModes.ToDestination:
                if (!_shaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(Time.time - _startedTimestamp, 0f, ToDestinationDuration, 0f, 1f);
                float time = ToDestinationCurve.Evaluate(_remappedTimeSinceStart);
                CurrentValue           = Mathf.LerpUnclamped(_initialValue, ToDestinationValue, time);
                CurrentValueNormalized = MMMaths.Remap(CurrentValue, _initialValue, ToDestinationValue, 0f, 1f);
                break;
            }

            if (PropertyType == PropertyTypes.Color)
            {
                _currentColor = Color.Lerp(FromColor, ToColor, CurrentValue);
            }

            if (AddToInitialValue)
            {
                CurrentValue += InitialValue;
            }

            if ((ControlMode == ControlModes.OneTime) && _shaking && (Time.time - _startedTimestamp > OneTimeDuration))
            {
                _shaking = false;
                if (RevertToInitialValueAfterEnd)
                {
                    CurrentValue = InitialValue;
                    if (PropertyType == PropertyTypes.Color)
                    {
                        _currentColor = InitialColor;
                    }
                }
                else
                {
                    CurrentValue  = OneTimeCurve.Evaluate(1f);
                    CurrentValue  = MMMaths.Remap(CurrentValue, 0f, 1f, OneTimeRemapMin, OneTimeRemapMax);
                    CurrentValue *= OneTimeAmplitude;
                }
                SetValue(CurrentValue);
                if (DisableAfterOneTime)
                {
                    this.enabled = false;
                }
                if (DisableGameObjectAfterOneTime)
                {
                    this.gameObject.SetActive(false);
                }
                return;
            }

            if ((ControlMode == ControlModes.ToDestination) && _shaking && (Time.time - _startedTimestamp > ToDestinationDuration))
            {
                _shaking  = false;
                FromColor = _fromColorStorage;
                if (RevertToInitialValueAfterEnd)
                {
                    CurrentValue = InitialValue;
                    if (PropertyType == PropertyTypes.Color)
                    {
                        _currentColor = InitialColor;
                    }
                }
                else
                {
                    CurrentValue = ToDestinationValue;
                }
                SetValue(CurrentValue);
                if (DisableAfterToDestination)
                {
                    this.enabled = false;
                }
                return;
            }

            SetValue(CurrentValue);
        }
示例#3
0
 /// <summary>
 /// Sets the level to the remapped value passed in parameters
 /// </summary>
 /// <param name="normalizedLevel"></param>
 /// <param name="remapZero"></param>
 /// <param name="remapOne"></param>
 public virtual void SetDrivenLevelNormalized(float normalizedLevel, float remapZero, float remapOne)
 {
     DrivenLevel = MMMaths.Remap(normalizedLevel, 0f, 1f, remapZero, remapOne);
 }
示例#4
0
        /// <summary>
        /// Sets the bar value to the one specified
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        public virtual void SetBar(float currentValue, float minValue, float maxValue)
        {
            float newPercent = MMMaths.Remap(currentValue, minValue, maxValue, 0f, 1f);

            SetBar01(newPercent);
        }
示例#5
0
        protected virtual void SetBarInternal(float newAmount, Transform bar, Image image, Vector2 initialSize)
        {
            if (bar == null)
            {
                return;
            }

            switch (FillMode)
            {
            case FillModes.LocalScale:
                _targetLocalScale = Vector3.one;
                switch (BarDirection)
                {
                case BarDirections.LeftToRight:
                    _targetLocalScale.x = newAmount;
                    break;

                case BarDirections.RightToLeft:
                    _targetLocalScale.x = 1f - newAmount;
                    break;

                case BarDirections.DownToUp:
                    _targetLocalScale.y = newAmount;
                    break;

                case BarDirections.UpToDown:
                    _targetLocalScale.y = 1f - newAmount;
                    break;
                }

                bar.localScale = _targetLocalScale;
                break;

            case FillModes.Width:
                if (image == null)
                {
                    return;
                }
                float newSizeX = MMMaths.Remap(newAmount, 0f, 1f, 0, initialSize.x);
                image.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, newSizeX);
                break;

            case FillModes.Height:
                if (image == null)
                {
                    return;
                }
                float newSizeY = MMMaths.Remap(newAmount, 0f, 1f, 0, initialSize.y);
                image.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, newSizeY);
                break;

            case FillModes.FillAmount:
                if (image == null)
                {
                    return;
                }
                image.fillAmount = newAmount;
                break;

            case FillModes.Anchor:
                if (image == null)
                {
                    return;
                }
                switch (BarDirection)
                {
                case BarDirections.LeftToRight:
                    _anchorVector.x = 0f;
                    _anchorVector.y = 0f;
                    image.rectTransform.anchorMin = _anchorVector;
                    _anchorVector.x = newAmount;
                    _anchorVector.y = 1f;
                    image.rectTransform.anchorMax = _anchorVector;
                    break;

                case BarDirections.RightToLeft:
                    _anchorVector.x = newAmount;
                    _anchorVector.y = 0f;
                    image.rectTransform.anchorMin = _anchorVector;
                    _anchorVector.x = 1f;
                    _anchorVector.y = 1f;
                    image.rectTransform.anchorMax = _anchorVector;
                    break;

                case BarDirections.DownToUp:
                    _anchorVector.x = 0f;
                    _anchorVector.y = 0f;
                    image.rectTransform.anchorMin = _anchorVector;
                    _anchorVector.x = 1f;
                    _anchorVector.y = newAmount;
                    image.rectTransform.anchorMax = _anchorVector;
                    break;

                case BarDirections.UpToDown:
                    _anchorVector.x = 0f;
                    _anchorVector.y = newAmount;
                    image.rectTransform.anchorMin = _anchorVector;
                    _anchorVector.x = 1f;
                    _anchorVector.y = 1f;
                    image.rectTransform.anchorMax = _anchorVector;
                    break;
                }
                break;
            }
        }
示例#6
0
        /// <summary>
        /// Updates the bar's values based on the specified parameters
        /// </summary>
        /// <param name="currentValue">Current value.</param>
        /// <param name="minValue">Minimum value.</param>
        /// <param name="maxValue">Max value.</param>
        public virtual void UpdateBar(float currentValue, float minValue, float maxValue)
        {
            if (!_initialized)
            {
                Initialization();
            }

            _newPercent = MMMaths.Remap(currentValue, minValue, maxValue, MinimumBarFillValue, MaximumBarFillValue);

            _actualUpdate = (BarTarget != _newPercent);

            if (!_actualUpdate)
            {
                return;
            }

            if (CurrentState != MMProgressBarStates.Idle)
            {
                if ((CurrentState == MMProgressBarStates.Decreasing) ||
                    (CurrentState == MMProgressBarStates.InDecreasingDelay))
                {
                    if (_newPercent >= BarTarget)
                    {
                        StopCoroutine(_coroutine);
                        SetBar01(BarTarget);
                    }
                }
                if ((CurrentState == MMProgressBarStates.Increasing) ||
                    (CurrentState == MMProgressBarStates.InIncreasingDelay))
                {
                    if (_newPercent <= BarTarget)
                    {
                        StopCoroutine(_coroutine);
                        SetBar01(BarTarget);
                    }
                }
            }

            _percentLastTimeBarWasUpdated = BarProgress;
            _delayedBarDecreasingProgress = DelayedBarDecreasingProgress;
            _delayedBarIncreasingProgress = DelayedBarIncreasingProgress;

            BarTarget = _newPercent;

            if ((_newPercent != _percentLastTimeBarWasUpdated) && !Bumping)
            {
                Bump();
            }

            DetermineDeltaTime();
            _lastUpdateTimestamp = _time;

            DetermineDirection();
            if (_direction < 0)
            {
                OnBarMovementDecreasingStart?.Invoke();
            }
            else
            {
                OnBarMovementIncreasingStart?.Invoke();
            }

            if (_coroutine != null)
            {
                StopCoroutine(_coroutine);
            }
            _coroutineShouldRun = true;

            if (!this.gameObject.activeInHierarchy)
            {
                this.gameObject.SetActive(true);
            }

            if (this.gameObject.activeInHierarchy)
            {
                _coroutine = StartCoroutine(UpdateBarsCo());
            }

            UpdateText();
        }
        protected virtual void DrawRawSpectrum()
        {
            GUILayout.Space(10);
            GUILayout.Label("Raw Spectrum", EditorStyles.boldLabel);

            // box
            GUILayout.Box("", GUILayout.Width(_inspectorWidth - _externalMargin), GUILayout.Height(_rawSpectrumBoxHeight));
            _spectrumBoxPosition     = GUILayoutUtility.GetLastRect().position;
            _spectrumBoxSize         = GUILayoutUtility.GetLastRect().size;
            _spectrumBoxBottomY      = _spectrumBoxPosition.y + _spectrumBoxSize.y;
            _spectrumMaxColumnHeight = _spectrumBoxSize.y - 2 * _externalMargin;
            Handles.BeginGUI();

            // horizontal axis
            Handles.color = Color.grey;
            for (int i = 0; i < _numberOfAxisSpectrum; i++)
            {
                _axisOrigin.x      = _spectrumBoxPosition.x;
                _axisOrigin.y      = _spectrumBoxBottomY - i * (_spectrumBoxSize.y / _numberOfAxisSpectrum);
                _axisDestination.x = _spectrumBoxPosition.x + _spectrumBoxSize.x;
                _axisDestination.y = _axisOrigin.y;
                Handles.DrawLine(_axisOrigin, _axisDestination);
            }

            if (Active)
            {
                // spectrum
                for (int i = 1; i < RawSpectrum.arraySize - 1; i++)
                {
                    float xPosition = _spectrumBoxPosition.x + _externalMargin + MMMaths.Remap(i, 0, RawSpectrum.arraySize, 0f, _spectrumBoxSize.x - _externalMargin * 2);
                    float yPosition = _spectrumBoxPosition.y + _spectrumBoxSize.y / 2;
                    float deltaX    = (_spectrumBoxSize.x - _externalMargin * 2) / RawSpectrum.arraySize;

                    float spectrumValue         = RawSpectrum.GetArrayElementAtIndex(i).floatValue;
                    float spectrumValuePrevious = RawSpectrum.GetArrayElementAtIndex(i - 1).floatValue;

                    float factor = _spectrumBoxSize.y / 2;

                    spectrumValue         = -(1 / Mathf.Log(spectrumValue)) * factor;;
                    spectrumValuePrevious = -(1 / Mathf.Log(spectrumValuePrevious)) * factor;

                    spectrumValue         = Mathf.Clamp(spectrumValue, 0f, _spectrumBoxSize.y / 2f);
                    spectrumValuePrevious = Mathf.Clamp(spectrumValuePrevious, 0f, _spectrumBoxSize.y / 2f);

                    Handles.color      = _spectrumColor;
                    _axisOrigin.x      = xPosition - deltaX;
                    _axisOrigin.y      = yPosition + spectrumValuePrevious;
                    _axisDestination.x = xPosition;
                    _axisDestination.y = (i % 2 == 0) ? yPosition + spectrumValue : yPosition - spectrumValue;
                    Handles.DrawLine(_axisOrigin, _axisDestination);
                }
            }
            else
            {
                int points = 100;
                for (int i = 1; i < points - 1; i++)
                {
                    float xPosition = _spectrumBoxPosition.x + _externalMargin + MMMaths.Remap(i, 0, points, 0f, _spectrumBoxSize.x - _externalMargin * 2);
                    float yPosition = _spectrumBoxPosition.y + _spectrumBoxSize.y / 2;
                    float deltaBetweenXandXPrevious = (_spectrumBoxSize.x - _externalMargin * 2) / points;

                    float spectrumValue         = Foobar(i);
                    float spectrumValuePrevious = Foobar(i - 1);

                    float factor = _spectrumBoxSize.y / 2;

                    Handles.color      = _inactiveColor;
                    _axisOrigin.x      = xPosition - deltaBetweenXandXPrevious;
                    _axisOrigin.y      = yPosition + spectrumValuePrevious;
                    _axisDestination.x = xPosition;
                    _axisDestination.y = yPosition + spectrumValue;

                    var p1        = _axisOrigin;
                    var p2        = _axisDestination;
                    var thickness = 3;
                    Handles.DrawBezier(p1, p2, p1, p2, _spectrumColor, null, thickness);
                }
            }
            Handles.EndGUI();
        }
        protected virtual void DrawBandVisualizationNormalized()
        {
            GUILayout.Space(10);
            GUILayout.Label("Normalized Visualization", EditorStyles.boldLabel);

            // box
            GUILayout.Box("", GUILayout.Width(_inspectorWidth - _externalMargin), GUILayout.Height(_bandsValuesBoxHeight));
            _boxPosition     = GUILayoutUtility.GetLastRect().position;
            _boxSize         = GUILayoutUtility.GetLastRect().size;
            _boxBottomY      = _boxPosition.y + _boxSize.y - _externalMargin - _lineHeight;
            _columnWidth     = (_boxSize.x - (_numberOfBands + 1) * _internalMargin) / _numberOfBands;
            _maxColumnHeight = _boxSize.y - 2 * _externalMargin - _lineHeight;

            // lines
            Handles.BeginGUI();

            // horizontal axis
            Handles.color = Color.grey;
            for (int i = 0; i < _numberOfAxis; i++)
            {
                _axisOrigin.x      = _boxPosition.x;
                _axisOrigin.y      = _boxBottomY + _lineHeight / _numberOfAxis - i * (_boxSize.y / _numberOfAxis);
                _axisDestination.x = _boxPosition.x + _boxSize.x;
                _axisDestination.y = _axisOrigin.y;
                Handles.DrawLine(_axisOrigin, _axisDestination);
            }

            Handles.EndGUI();


            // amplitude cursors
            _columnHeight = MMMaths.Remap(NormalizedAmplitude.floatValue, 0f, 1f, 0f, _maxColumnHeight);
            _positionX    = _boxPosition.x - _externalMargin / 4;
            _positionY    = _boxBottomY - _columnHeight;

            _rect.x      = _positionX;
            _rect.y      = _positionY;
            _rect.width  = _externalMargin / 2;
            _rect.height = _externalMargin / 2;
            EditorGUI.DrawRect(_rect, _normalizedAmplitudeColor);

            _columnHeight = MMMaths.Remap(NormalizedBufferedAmplitude.floatValue, 0f, 1f, 0f, _maxColumnHeight);
            _positionX    = _boxPosition.x + _boxSize.x - _externalMargin / 4;
            _positionY    = _boxBottomY - _columnHeight;

            _rect.x      = _positionX;
            _rect.y      = _positionY;
            _rect.width  = _externalMargin / 2;
            _rect.height = _externalMargin / 2;
            EditorGUI.DrawRect(_rect, _normalizedAmplitudeColor);

            // buffered bars
            for (int i = 0; i < _numberOfBands; i++)
            {
                if (Active)
                {
                    float bandLevel = NormalizedBufferedBandLevels.GetArrayElementAtIndex(i).floatValue;
                    _columnHeight = MMMaths.Remap(bandLevel, 0f, 1f, 0f, _maxColumnHeight);
                    _barColor     = (Time.time - LastPeaksAt.GetArrayElementAtIndex(i).floatValue < _peakShowDuration / 3f) ? _activePeakColor : _bufferedBarColor;

                    _positionX = _boxPosition.x + _internalMargin * (i + 1) + _columnWidth * i;
                    _positionY = _boxBottomY;

                    // bar rectangle
                    _rect.x      = _positionX;
                    _rect.y      = _positionY;
                    _rect.width  = _columnWidth;
                    _rect.height = -_columnHeight;
                    EditorGUI.DrawRect(_rect, _barColor);
                }
            }

            // bars
            for (int i = 0; i < _numberOfBands; i++)
            {
                if (Active)
                {
                    float bandLevel = NormalizedBandLevels.GetArrayElementAtIndex(i).floatValue;
                    _columnHeight = MMMaths.Remap(bandLevel, 0f, 1f, 0f, _maxColumnHeight);
                    _barColor     = (Time.time - LastPeaksAt.GetArrayElementAtIndex(i).floatValue < _peakShowDuration) ? _peakColor : _normalNormalizedBarColor;
                }
                else
                {
                    _barColor     = _inactiveColor;
                    _columnHeight = (i + 1) * (_maxColumnHeight / (_numberOfBands + 1));
                }

                _positionX = _boxPosition.x + _internalMargin * (i + 1) + _columnWidth * i;
                _positionY = _boxBottomY;

                // bar rectangle
                _rect.x      = _positionX;
                _rect.y      = _positionY;
                _rect.width  = _columnWidth;
                _rect.height = -_columnHeight;
                EditorGUI.DrawRect(_rect, _barColor);
                // bar number label
                float labelCorrection = (i > 9) ? -5f : 0f;

                _rect.x      = _positionX + _columnWidth / 2 - 5 + labelCorrection;
                _rect.y      = _boxBottomY + _lineHeight / 4;
                _rect.width  = _columnWidth;
                _rect.height = _lineHeight;
                EditorGUI.LabelField(_rect, i.ToString(), EditorStyles.boldLabel);
            }
        }
        protected virtual void DrawRawSpectrum()
        {
            _deltaTime   = (EditorApplication.timeSinceStartup - _lastTime);
            _signalTime += _deltaTime;
            if (_signalTime > _duration.floatValue)
            {
                _signalTime = 0f;
            }
            _lastTime       = EditorApplication.timeSinceStartup;
            _normalizedTime = MMMaths.Remap((float)_signalTime, 0f, _duration.floatValue, 0f, 1f);

            GUILayout.Space(10);
            GUILayout.Label("Preview", EditorStyles.boldLabel);

            EditorGUILayout.PropertyField(_animatedPreview);

            EditorGUILayout.Space(20);
            // box
            GUILayout.Box("", GUILayout.Width(_inspectorWidth - _externalMargin), GUILayout.Height(_rawSpectrumBoxHeight));
            _spectrumBoxPosition     = GUILayoutUtility.GetLastRect().position;
            _spectrumBoxSize         = GUILayoutUtility.GetLastRect().size;
            _spectrumBoxBottomY      = _spectrumBoxPosition.y + _spectrumBoxSize.y;
            _spectrumMaxColumnHeight = _spectrumBoxSize.y - 2 * _externalMargin;
            Handles.BeginGUI();

            // horizontal axis
            Handles.color = Color.grey;
            for (int i = 0; i <= _numberOfAxisSpectrum; i++)
            {
                _axisOrigin.x      = _spectrumBoxPosition.x;
                _axisOrigin.y      = _spectrumBoxBottomY - i * (_spectrumBoxSize.y / _numberOfAxisSpectrum);
                _axisDestination.x = _spectrumBoxPosition.x + _spectrumBoxSize.x;
                _axisDestination.y = _axisOrigin.y;
                Handles.DrawLine(_axisOrigin, _axisDestination);
            }

            // y one label
            _rect.x      = _axisOrigin.x - 12;
            _rect.y      = _spectrumBoxBottomY - _spectrumBoxSize.y - 20;
            _rect.width  = 40;
            _rect.height = 40;
            EditorGUI.LabelField(_rect, "1", EditorStyles.boldLabel);

            float minX = _axisOrigin.x - 12;
            float maxX = _axisOrigin.x + _spectrumBoxSize.x - 2;


            float zeroX = minX;
            float oneX  = maxX;

            if (_animatedPreview.boolValue)
            {
                float currentTime    = (float)EditorApplication.timeSinceStartup;
                float normalizedTime = currentTime - Mathf.Floor(currentTime);
                zeroX = maxX - MMMaths.Remap(_normalizedTime, 0f, 1f, _spectrumBoxPosition.x + _externalMargin, _spectrumBoxPosition.x + _spectrumBoxSize.x);
                oneX  = zeroX - 10;
            }

            // zero label
            _rect.x      = zeroX;
            _rect.y      = _spectrumBoxBottomY - 20;
            _rect.width  = 40;
            _rect.height = 40;
            EditorGUI.LabelField(_rect, "0", EditorStyles.boldLabel);

            // one label
            _rect.x      = oneX;
            _rect.y      = _spectrumBoxBottomY - 20;
            _rect.width  = 40;
            _rect.height = 40;
            EditorGUI.LabelField(_rect, "1", EditorStyles.boldLabel);

            // level
            if (Application.isPlaying)
            {
                _rect.x      = _axisOrigin.x + _spectrumBoxSize.x - 40;
                _rect.y      = _spectrumBoxBottomY - _spectrumBoxSize.y - 40;
                _rect.width  = 40;
                _rect.height = 40;
                EditorGUI.LabelField(_rect, _currentLevel.floatValue.ToString("F3"), EditorStyles.boldLabel);
            }

            // cube
            _rect.x = _spectrumBoxPosition.x + _externalMargin / 4;
            if (_duration.floatValue > 0f)
            {
                float boxSpectrumValue;
                if (Application.isPlaying)
                {
                    boxSpectrumValue = MMMaths.Remap(_radioSignal.GraphValue(_driverTime.floatValue), 0f, 1f, 0f, _spectrumBoxSize.y);
                }
                else
                {
                    boxSpectrumValue = MMMaths.Remap(_radioSignal.GraphValue(_normalizedTime), 0f, 1f, 0f, _spectrumBoxSize.y);
                }
                _rect.y = _spectrumBoxBottomY - boxSpectrumValue - _externalMargin / 4;
            }
            else
            {
                _rect.y = _spectrumBoxBottomY;
            }
            _rect.width  = _externalMargin / 2;
            _rect.height = _externalMargin / 2;
            EditorGUI.DrawRect(_rect, _spectrumBoxColor);

            // progress line
            if (Application.isPlaying && !_animatedPreview.boolValue)
            {
                _rect.x = _spectrumBoxPosition.x
                          + MMMaths.Remap(_driverTime.floatValue, 0f, 1f, 0f, _spectrumBoxSize.x);
                _rect.y      = _spectrumBoxBottomY - _spectrumBoxSize.y;
                _rect.width  = 1;
                _rect.height = _spectrumBoxSize.y;
                EditorGUI.DrawRect(_rect, _spectrumBoxColor);
            }

            for (int i = 1; i < _spectrumPointsCount; i++)
            {
                float xPosition = _spectrumBoxPosition.x + _externalMargin + MMMaths.Remap(i, 0, _spectrumPointsCount, 0f, _spectrumBoxSize.x - _externalMargin * 2);
                float deltaBetweenXandXPrevious = (_spectrumBoxSize.x - _externalMargin * 2) / _spectrumPointsCount;

                float time         = i * (1 / _spectrumPointsCount);
                float timePrevious = (i - 1) * (1 / _spectrumPointsCount);

                if (_animatedPreview.boolValue)
                {
                    if (Application.isPlaying)
                    {
                        time         += _driverTime.floatValue;
                        timePrevious += _driverTime.floatValue;
                    }
                    else
                    {
                        time         += (float)_normalizedTime;
                        timePrevious += (float)_normalizedTime;
                    }
                    if (time > _duration.floatValue)
                    {
                        time = 0f;
                    }
                    if (timePrevious > _duration.floatValue)
                    {
                        timePrevious = 0f;
                    }
                }

                float t2 = Mathf.Pow(time, _bias.floatValue);

                float spectrumValue         = MMMaths.Remap(_radioSignal.GraphValue(time), 0f, 1f, 0f, _spectrumBoxSize.y);
                float spectrumValuePrevious = MMMaths.Remap(_radioSignal.GraphValue(timePrevious), 0f, 1f, 0f, _spectrumBoxSize.y);

                Handles.color      = _spectrumColor;
                _axisOrigin.x      = xPosition - deltaBetweenXandXPrevious;
                _axisOrigin.y      = _spectrumBoxBottomY - spectrumValuePrevious;
                _axisDestination.x = xPosition;
                _axisDestination.y = _spectrumBoxBottomY - spectrumValue;

                var p1        = _axisOrigin;
                var p2        = _axisDestination;
                var thickness = 3;
                Handles.DrawBezier(p1, p2, p1, p2, _spectrumColor, null, thickness);
            }

            EditorGUILayout.Space(50);

            Handles.EndGUI();
        }
示例#10
0
        /// <summary>
        /// On Update, we move our value based on the defined settings
        /// </summary>
        protected virtual void Update()
        {
            if ((TargetObject == null) || (TargetAttribute == null) || (!Active) || (!_attributeFound))
            {
                return;
            }

            switch (ControlMode)
            {
            case ControlModes.PingPong:

                if (GetTime() - _lastPingPongPauseAt < PingPongPauseDuration)
                {
                    return;
                }
                PingPong += GetDeltaTime() * _pingPongDirection;

                if (PingPong < 0f)
                {
                    PingPong             = 0f;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }

                if (PingPong > Duration)
                {
                    PingPong             = Duration;
                    _pingPongDirection   = -_pingPongDirection;
                    _lastPingPongPauseAt = GetTime();
                }


                CurrentValue = MMTween.Tween(PingPong, 0f, Duration, MinValue, MaxValue, Curve);
                break;

            case ControlModes.Random:
                _elapsedTime += GetDeltaTime();
                CurrentValue  = (Mathf.PerlinNoise(_randomFrequency * _elapsedTime, _randomShift) * 2.0f - 1.0f) * _randomAmplitude;
                break;

            case ControlModes.OneTime:
                if (!_oneTimeShaking)
                {
                    return;
                }
                _remappedTimeSinceStart = MMMaths.Remap(Time.time - _oneTimeStartedTimestamp, 0f, OneTimeDuration, 0f, 1f);
                CurrentValue            = OneTimeCurve.Evaluate(_remappedTimeSinceStart) * OneTimeAmplitude;
                if (AddToInitialValue)
                {
                    CurrentValue += InitialValue;
                }
                break;

            case ControlModes.AudioAnalyzer:
                CurrentValue = AudioAnalyzer.Beats[BeatID].CurrentValue * AudioAnalyzerMultiplier;
                break;
            }


            if (AddToInitialValue)
            {
                CurrentValue += InitialValue;
            }

            if (_oneTimeShaking && (Time.time - _oneTimeStartedTimestamp > OneTimeDuration))
            {
                _oneTimeShaking = false;
                CurrentValue    = InitialValue;
            }

            TargetAttribute.SetValue(CurrentValue);
        }