// add a new key
 public void addKey(AMITarget itarget, OnAddKey addCall, int _frame, Camera camera = null,/*int? type = null, Camera camera = null, Color? color = null,*/ AMCameraSwitcherKey keyToClone = null)
 {
     foreach(AMCameraSwitcherKey key in keys) {
         // if key exists on frame, update key
         if(key.frame == _frame) {
             if(camera != null) {
                 key.setCamera(itarget, camera);
                 key.type = 0;
                 updateCache(itarget);
             }
             return;
         }
     }
     AMCameraSwitcherKey a = addCall(gameObject, typeof(AMCameraSwitcherKey)) as AMCameraSwitcherKey;
     if(keyToClone) {
         keyToClone.CopyTo(a);
     }
     else {
         a.type = 0;
         a.still = !AMTakeData.isProLicense;
         a.easeType = (int)EaseType.EaseOutSine;
     }
     a.frame = _frame;
     if(camera != null) {
         a.setCamera(itarget, camera);
         a.type = 0;
     }
     // add a new key
     keys.Add(a);
     // update cache
     updateCache(itarget);
 }
 public AMPlugCameraSwitcher(AMCameraSwitcherKey camSwitcher, int frameRate, AMITarget itarget, Camera[] allCameras)
     : base(null, false)
 {
     mCamSwitcher = camSwitcher;
     mFrameRate = frameRate;
     mITarget = itarget;
     mAllCams = allCameras;
 }
            //private float[] mFadeParams;
            public void Apply(AMCameraSwitcherKey camSwitcher, int frameRate, AMITarget itarget, Camera[] allCameras, bool backwards)
            {
                if(mCamSwitcher != camSwitcher) {
                    mCamSwitcher = camSwitcher;

                    float[] fadeParams = mCamSwitcher.cameraFadeParameters.ToArray();
                    mIsReversed = AMUtil.isTransitionReversed(mCamSwitcher.type, fadeParams);
                    mCam = mCamSwitcher.getCamera(itarget);
                    mCamEnd = mCamSwitcher.getCameraEnd(itarget);
                    mAllCams = allCameras;

                    if(mCamSwitcher.cameraFadeType == (int)Fade.None) {
                        mCamSwitcher.CameraFadeNoneTargets(mCamSwitcher.typeEnd, mCamSwitcher.colorEnd, mCamEnd, mAllCams);
                        mCamSwitcher.CameraEnd(mIsReversed, mCam, mCamEnd, mAllCams);
                    }
                    else {
                        mCamSwitcher.CameraGenerateFadeTargets(mIsReversed, mCam, mCamEnd, mAllCams, fadeParams);
                    }
                }

                mBackwards = backwards;
            }
 public void setCameraEnd(AMCameraSwitcherKey nextKey)
 {
     _cameraEnd = nextKey._camera;
     _cameraEndPath = nextKey._cameraPath;
 }
        // set render texture or colors if render texture is used (stills handled in AMTake)
        private void setRenderTexture(AMITarget itarget,  AMCameraFade cf, float frame, int firstTargetType, int secondTargetType, bool isReversed, 
	        AMCameraSwitcherKey action)
        {
            Camera firstCamera = (isReversed ? action.getCameraEnd(itarget) : action.getCamera(itarget));
            Camera secondCamera = (isReversed ? action.getCamera(itarget) : action.getCameraEnd(itarget));

            if(isReversed && frame == action.frame) {
                if(firstTargetType == 0) AMUtil.SetTopCamera(firstCamera, GetCachedCameras(itarget));
            }
            else {
                if(secondTargetType == 0) AMUtil.SetTopCamera(secondCamera, GetCachedCameras(itarget));
            }

            if(action.still || (firstTargetType != 0 && secondTargetType != 0)) return;

            bool isPro = AMTakeData.isProLicense;
            // first target is camera, set render texture
            if(firstTargetType == 0) {
                // if should update render texture
                if(/*!cf.tex ||*/ cf.shouldUpdateRenderTexture || (isPro && (!firstCamera.targetTexture || !cf.isRenderTextureSetupFor(firstCamera)/*|| firstCamera.targetTexture != cf.tex*/))) {
                    if(isPro) {
                        cf.setupRenderTexture(firstCamera);

                    }
                    else {
                        cf.useRenderTexture = false;
                        // show place-holder if non-pro
                        cf.colorTex = Color.white;
                        cf.hasColorTex = false;
                        cf.clearScreenTex();
                        cf.placeholder = true;
                    }
                }
            }
        }
        private void setColors(AMCameraFade cf, int firstTargetType, int secondTargetType, bool isReversed, AMCameraSwitcherKey action)
        {
            //if(firstTargetType != 1 && secondTargetType != 1) return;
            Color firstColor = (isReversed ? action.colorEnd : action.color);
            Color secondColor = (isReversed ? action.color : action.colorEnd);

            if(firstTargetType == 1) {
                cf.colorTex = firstColor;
                cf.hasColorTex = true;
            }
            else {
                cf.hasColorTex = false;
            }

            if(secondTargetType == 1) {
                cf.colorBG = secondColor;
                cf.hasColorBG = true;
            }
            else {
                cf.hasColorBG = false;
            }

            // send event to game view to repaint OnGUI
            if(!Application.isPlaying && (firstTargetType == 1 || secondTargetType == 1)) {
                cf.transform.position = new Vector3(cf.transform.position.x, cf.transform.position.y, cf.transform.position.z);
            }
        }
        private void previewCameraFade(AMITarget itarget, float frame, AMCameraSwitcherKey action)
        {
            // if transition is None, show end camera / color
            if(action.cameraFadeType == (int)AMCameraSwitcherKey.Fade.None) {
                // reset camera fade if visible
                // camera
                if(action.typeEnd == 0) {
                    Camera endCam = action.getCameraEnd(itarget);
                    if(endCam) AMUtil.SetTopCamera(endCam, GetCachedCameras(itarget));
                    AMCameraFade.reset();
                }
                else {
                    showColor(action.colorEnd);
                }
                return;
            }
            // Get camerafade
            AMCameraFade cf = AMCameraFade.getCameraFade();
            cf.isReset = false;
            bool isReversed = action.isReversed();
            int firstTargetType = (isReversed ? action.typeEnd : action.type);
            int secondTargetType = (isReversed ? action.type : action.typeEnd);
            // Set render texture or colors if render texture is used
            setRenderTexture(itarget, cf, frame, firstTargetType, secondTargetType, isReversed, action);
            setColors(cf, firstTargetType, secondTargetType, isReversed, action);

            if(cf.irisShape != action.irisShape) cf.irisShape = action.irisShape;
            cf.mode = action.cameraFadeType;
            cf.setupMaterials();
            cf.r = action.cameraFadeParameters.ToArray();

            float percentage = (float)(frame-action.frame)/(float)(action.endFrame-action.frame);
            float value;

            // calculate and set value
            if(action.hasCustomEase()) {
                value = AMUtil.EaseCustom(1.0f, -1.0f, percentage, action.easeCurve);
            }
            else {
                TweenDelegate.EaseFunc ease = AMUtil.GetEasingFunction((EaseType)action.easeType);
                value = ease(percentage, 1.0f, -1.0f, 1.0f, action.amplitude, action.period);
            }

            cf.value = value;
            cf.percent = percentage;
        }
 void OnDisable()
 {
     window = null;
     justSet = false;
     key = null;
     track = null;
     aData = null;
 }
        public void reloadAnimatorData()
        {
            aData = null;
            loadAnimatorData();
            bool shouldClose = true;
            if(aData != null) {
                AMTakeData take = aData.currentTake;
                // update references for track and key

                foreach(AMTrack _track in take.trackValues) {
                    if(track ==	_track) {
                        track = _track;
                        foreach(AMCameraSwitcherKey _key in track.keys) {
                            if(key == _key) {
                                key = _key;
                                shouldClose = false;
                            }
                        }
                    }
                }
            }
            if(shouldClose) this.Close();
        }
        public static void setValues(AMCameraSwitcherKey _key, AMTrack _track)
        {
            justSet = true;
            key = _key;
            track = _track;

            selectedTransition = key.cameraFadeType;
            parameters = new List<float>(key.cameraFadeParameters);
            irisShape = key.irisShape;
        }
 public static void setDefaultParametersForKey(ref AMCameraSwitcherKey cKey)
 {
     Vector2 temp = new Vector2(0f, 0f);
     setDefaultParametersFor(cKey.cameraFadeType, ref cKey.cameraFadeParameters, ref cKey.irisShape, ref temp);
 }