ExpressionStep CreateStep(float t)
        {
            ExpressionStep newStep = new ExpressionStep(this, t);

            steps.Add(newStep);

            if (steps.Count == 0)
            {
                morphSelectors.ForEach((selector) =>
                {
                    if (selector.selectedMorph != null)
                    {
                        newStep.morphKeyframe[selector.selectedMorph] = selector.selectedMorph.startValue;
                    }
                });
            }
            else
            {
                morphSelectors.ForEach((selector) =>
                {
                    if (selector.selectedMorph != null)
                    {
                        newStep.morphKeyframe[selector.selectedMorph] = selector.morphValue.val;
                    }
                });
            }

            ReorderSteps();
            SelectStep(newStep);
            CancelDeleteConfirmation();
            return(newStep);
        }
 public void RemoveStep(ExpressionStep step)
 {
     step.Remove();
     steps.Remove(step);
     if (selectedStep == step)
     {
         selectedStep = null;
     }
 }
        public void RemoveAllSteps()
        {
            steps.ForEach((step) =>
            {
                step.Remove();
            });

            steps.Clear();
            selectedStep = null;
        }
 int GetPrevStep(float t)
 {
     for (int i = 0; i < steps.Count; i++)
     {
         ExpressionStep step = steps[i];
         if (step.t >= t)
         {
             return(Math.Max(i - 1, 0));
         }
     }
     return(steps.Count - 1);
 }
Esempio n. 5
0
        public void OnSelect(ExpressionStep step)
        {
            //morphPopup.gameObject.SetActive(true);
            //groupPopup.gameObject.SetActive(true);
            //morphSlider.gameObject.SetActive(true);

            if (selectedMorph != null)
            {
                selectedMorph.SetValue(morphValue.val);
            }

            step.morphKeyframe.Keys.ToList().ForEach((morph) =>
            {
                if (morph == selectedMorph)
                {
                    float value    = step.morphKeyframe[morph];
                    morphValue.val = value;
                }
            });
        }
        float GetValueBetweenSteps(float t, DAZMorph morph, ExpressionStep prevStep, ExpressionStep nextStep)
        {
            float prevValue = prevStep.morphKeyframe[morph];
            float nextValue = nextStep.morphKeyframe[morph];
            float prevT     = prevStep.t;
            float nextT     = nextStep.t;

            float tToPrev = t - prevT;
            float deltaT  = nextT - prevT;

            if (deltaT <= 0)
            {
                return(prevValue);
            }

            float morphAlpha = tToPrev / deltaT;
            float morphValue = prevValue + (nextValue - prevValue) * morphAlpha;

            return(morphValue);
        }
        public void SelectStep(ExpressionStep step)
        {
            DeselectAll();

            selectedStep = step;

            morphSelectors.ForEach((selector) =>
            {
                selector.OnSelect(step);
            });

            step.OnSelect();

            playback.SetVal(step.t);

            if (currentClip != null)
            {
                SampleAudioClipAtT(currentClip, step.t);
            }

            step.ApplyMorphs();

            CancelDeleteConfirmation();
        }
        void SetMorphAtT(float t)
        {
            if (steps.Count <= 0)
            {
                return;
            }

            int            current  = GetPrevStep(t);
            ExpressionStep prevStep = steps[current];
            ExpressionStep nextStep = steps[Math.Min(current + 1, steps.Count - 1)];

            prevStep.morphKeyframe.Keys.ToList().ForEach((morph) =>
            {
                float morphValue = GetValueBetweenSteps(t, morph, prevStep, nextStep);
                //morphSelectors.ForEach((selector) =>
                //{
                //    if (selector.selectedMorph == morph)
                //    {
                //        selector.morphValue.SetVal(morphValue);
                //    }
                //});
                morph.SetValue(morphValue);
            });
        }
        public override void Init()
        {
            try
            {
                PATH_WHEN_LOADED = SuperController.singleton.currentLoadDir;

                SuperController sc = SuperController.singleton;

                CreateButton("New").button.onClick.AddListener(() =>
                {
                    audioChoice.SetVal("");
                    RemoveAllSteps();
                    RemoveAllSelectors();
                    CreateStep(0);
                    CreateStep(1);
                    playback.SetVal(0);
                    totalDuration.SetVal(totalDuration.defaultVal);
                });

                CreateButton("Save").button.onClick.AddListener(() =>
                {
                    if (steps.Count <= 0)
                    {
                        return;
                    }

                    List <ExpressionKeyframe> keyframes = steps.Select((step) => step.ToKeyFrame()).ToList();

                    string audioUID = "";
                    if (currentClip != null)
                    {
                        audioUID = currentClip.uid;
                    }

                    ExpressionAnimation animation = new ExpressionAnimation()
                    {
                        audio     = audioUID,
                        duration  = totalDuration.val,
                        keyframes = keyframes
                    };

                    JSONNode json = animation.GetJSON();
                    //Debug.Log(json.ToString());

                    //sc.activeUI = SuperController.ActiveUI.None;
                    sc.fileBrowserUI.defaultPath = PATH_WHEN_LOADED;
                    sc.fileBrowserUI.SetTextEntry(true);

                    sc.fileBrowserUI.Show((path) =>
                    {
                        //  cancel or invalid
                        if (string.IsNullOrEmpty(path))
                        {
                            return;
                        }

                        //  ensure extension
                        if (!path.EndsWith(".json"))
                        {
                            path += ".json";
                        }

                        sc.SaveStringIntoFile(path, json.ToString(""));
                        SuperController.LogMessage("Wrote expression file: " + path);
                    });

                    //  set default filename
                    if (sc.fileBrowserUI.fileEntryField != null)
                    {
                        sc.fileBrowserUI.fileEntryField.text = currentClip.uid + ".json";
                        sc.fileBrowserUI.ActivateFileNameField();
                    }
                });
                CreateSpacer().height = 12;

                CreateButton("Load", true).button.onClick.AddListener(() =>
                {
                    sc.fileBrowserUI.defaultPath = PATH_WHEN_LOADED;
                    sc.fileBrowserUI.SetTextEntry(false);
                    sc.fileBrowserUI.Show((path) =>
                    {
                        if (string.IsNullOrEmpty(path))
                        {
                            return;
                        }

                        string jsonString = sc.ReadFileIntoString(path);

                        /*
                         * Example:
                         *
                         * {
                         * "audio" : "holding it 1.mp3",
                         * "keyframes" : [
                         *    {
                         *       "time" : "0",
                         *       "Brow Down" : "0"
                         *    },
                         *    {
                         *       "time" : "1",
                         *       "Brow Down" : "1"
                         *    }
                         * ]
                         * }
                         *
                         */

                        JSONClass jsc = JSON.Parse(jsonString).AsObject;


                        string clipId = jsc["audio"].Value;

                        //  validate this audio clip exists
                        NamedAudioClip nac = URLAudioClipManager.singleton.GetClip(clipId);
                        if (nac == null)
                        {
                            //SuperController.LogError("Clip " + clipId + " is not loaded to scene. Add it first.");
                            //return;
                        }

                        audioChoice.SetVal(jsc["audio"].Value);
                        totalDuration.SetVal(jsc["duration"].AsFloat);

                        RemoveAllSteps();
                        RemoveAllSelectors();

                        JSONArray keyframes = jsc["keyframes"].AsArray;

                        HashSet <DAZMorph> morphSet             = new HashSet <DAZMorph>();
                        GenerateDAZMorphsControlUI morphControl = GetMorphControl();

                        for (int i = 0; i < keyframes.Count; i++)
                        {
                            JSONClass keyframe = keyframes[i].AsObject;
                            float time         = keyframe["time"].AsFloat;

                            ExpressionStep step = CreateStep(time);

                            keyframe.Keys.ToList().ForEach((key =>
                            {
                                if (key == "time")
                                {
                                    return;
                                }

                                string morphName = key;

                                DAZMorph morph = morphControl.GetMorphByDisplayName(morphName);
                                if (morph == null)
                                {
                                    SuperController.LogError("Expression has a morph not imported: " + morphName);
                                    return;
                                }

                                if (morphSet.Contains(morph) == false)
                                {
                                    morphSet.Add(morph);
                                    MorphSelectUI selector = CreateMorphSelector();
                                    selector.morphString.SetVal(morphName);
                                    selector.SetCollapse(morphsCollapsed);
                                }

                                float value = keyframe[morphName].AsFloat;
                                step.morphKeyframe[morph] = value;
                            }));
                        }

                        playback.SetVal(0);
                    });
                });
                CreateSpacer(true).height = 12;

                totalDuration = new JSONStorableFloat("duration", 2.0f, (float newDuration) =>
                {
                }, 0.0f, 10.0f, false);

                UIDynamicSlider slider = CreateSlider(totalDuration);
                slider.label = "Duration (based on audio clip if selected)";

                audioChoice = new JSONStorableStringChooser("clip", GetAudioClipIds(), "", "Audio Clip", (string clipId) => {
                    NamedAudioClip nac = URLAudioClipManager.singleton.GetClip(clipId);
                    if (nac == null)
                    {
                        currentClip = null;
                        return;
                    }

                    currentClip = nac;
                    PlayAudioClipAtT(nac, 0);

                    totalDuration.SetVal(nac.sourceClip.length);
                });
                //RegisterStringChooser(audioChoice);

                UIDynamicPopup popup = CreateScrollablePopup(audioChoice);
                popup.popup.onOpenPopupHandlers += () =>
                {
                    audioChoice.choices = GetAudioClipIds();
                };

                playback = new JSONStorableFloat("time", 0, (float t) =>
                {
                    if (isTesting == false)
                    {
                        if (currentClip != null)
                        {
                            SampleAudioClipAtT(currentClip, t);
                        }
                        SetMorphAtT(t);
                    }

                    createKeyButton.label = "Key At " + t;

                    CancelDeleteConfirmation();
                }, 0, 1, true, true);

                UIDynamicSlider playbackSlider = CreateSlider(playback);
                playbackSlider.quickButtonsEnabled  = false;
                playbackSlider.rangeAdjustEnabled   = false;
                playbackSlider.defaultButtonEnabled = false;

                CreateButton("Test").button.onClick.AddListener(() =>
                {
                    if (isTesting == true)
                    {
                        isTesting = false;
                        return;
                    }

                    isTesting     = true;
                    testStartTime = Time.time;
                    if (currentClip != null)
                    {
                        PlayAudioClipAtT(currentClip, 0);
                    }

                    CancelDeleteConfirmation();
                });

                createKeyButton = CreateButton("Key At 0");
                createKeyButton.button.onClick.AddListener(() => {
                    CreateStep(playback.val);
                });

                CreateSpacer();


                UIDynamicButton addMorphButton = CreateButton("Add Morph", true);
                addMorphButton.button.onClick.AddListener(() =>
                {
                    CreateMorphSelector();
                });

                UIDynamicButton minimizeViewButton = CreateButton("Minimize Morph Controls", true);
                minimizeViewButton.button.onClick.AddListener(() =>
                {
                    morphsCollapsed = !morphsCollapsed;
                    morphSelectors.ForEach((selector) =>
                    {
                        selector.SetCollapse(morphsCollapsed);
                    });

                    CancelDeleteConfirmation();

                    minimizeViewButton.label = morphsCollapsed ? "Maximize Morph Controls" : "Minimize Morph Controls";
                });

                deleteButton = CreateButton("Delete Selected", false);
                deleteButton.button.onClick.AddListener(() =>
                {
                    if (selectedStep == null)
                    {
                        return;
                    }

                    if (deleteConfirmation == false)
                    {
                        BeginDeleteConfirmation();
                        return;
                    }
                    else
                    {
                        RemoveStep(selectedStep);
                        CancelDeleteConfirmation();
                    }
                });

                CreateStep(0);
                CreateStep(1);
                playback.SetVal(0);
            }
            catch (Exception e)
            {
                SuperController.LogError("Exception caught: " + e);
            }
        }