void OnBehaviorDownloadedEvent(Dice.EditDie die, Behaviors.EditBehavior behavior)
    {
        // Check whether we should stay connected to some of the dice
        List <EditDie> toDisconnect = new List <EditDie>(connectedDice);

        if (behavior.CollectAudioClips().Any())
        {
            // This die assignment uses a behavior that has audio clips, so stay connected to the die
            if (connectedDice.Contains(die))
            {
                toDisconnect.Remove(die);
            }
            else if (die != null)
            {
                // Connect to the new die
                connectedDice.Add(die);
                DiceManager.Instance.ConnectDie(die, (d, res, _) =>
                {
                    if (!res)
                    {
                        connectedDice.Remove(d);
                    }
                });
            }
        }

        foreach (var d in toDisconnect)
        {
            connectedDice.Remove(d);
            DiceManager.Instance.DisconnectDie(d, null);
        }
    }
Example #2
0
 void DuplicateBehavior(Behaviors.EditBehavior behavior)
 {
     AppDataSet.Instance.DuplicateBehavior(behavior);
     behaviors.Find(p => p.editBehavior == behavior).Expand(false);
     AppDataSet.Instance.SaveData();
     RefreshView();
 }
Example #3
0
 public void ActivateBehavior(Behaviors.EditBehavior behavior, System.Action <EditDie, bool> callback)
 {
     ShowDialogBox(
         "Activate " + behavior.name + "?",
         "Do you want to activate this profile on one of your dice?",
         "Yes",
         "Cancel",
         (res) =>
     {
         if (res)
         {
             // Select the die
             ShowDiePicker("Select Die", null, null, (res2, selectedDie) =>
             {
                 if (res2)
                 {
                     // Attempt to activate the behavior on the die
                     UploadBehavior(behavior, selectedDie, (res3) =>
                     {
                         callback?.Invoke(selectedDie, res3);
                     });
                 }
             });
         }
     });
 }
Example #4
0
    public EditBehavior AddNewDefaultBehavior()
    {
        var newBehavior = new Behaviors.EditBehavior();

        newBehavior.name = "New Profile";
        newBehavior.rules.Add(new Behaviors.EditRule()
        {
            condition = new Behaviors.EditConditionFaceCompare()
            {
                flags     = ConditionFaceCompare_Flags.Equal,
                faceIndex = 19
            },
            actions = new List <Behaviors.EditAction> ()
            {
                new Behaviors.EditActionPlayAnimation()
                {
                    animation = null,
                    faceIndex = 0,
                    loopCount = 1
                }
            }
        });
        behaviors.Add(newBehavior);
        return(newBehavior);
    }
Example #5
0
 void OnBehaviorSelected(bool result, Behaviors.EditBehavior newBehavior)
 {
     if (result && newBehavior != editAssignment.behavior)
     {
         editAssignment.behavior = newBehavior;
         onChange?.Invoke(editAssignment);
         UpdateView();
     }
 }
Example #6
0
 void ActivateBehavior(Behaviors.EditBehavior behavior)
 {
     PixelsApp.Instance.ActivateBehavior(behavior, (die, res) =>
     {
         if (res)
         {
             UpdatePresetAndBehaviorStatuses();
         }
     });
 }
Example #7
0
    public bool ShowBehaviorPicker(string title, Behaviors.EditBehavior previousBehavior, System.Action <bool, Behaviors.EditBehavior> closeAction)
    {
        bool ret = !behaviorPicker.isShown;

        if (ret)
        {
            behaviorPicker.Show(title, previousBehavior, closeAction);
        }
        return(ret);
    }
Example #8
0
        public EditBehavior Duplicate()
        {
            var ret = new EditBehavior();

            ret.name        = name;
            ret.description = description;
            foreach (var r in rules)
            {
                ret.rules.Add(r.Duplicate());
            }
            return(ret);
        }
Example #9
0
 void ExpandBehavior(Behaviors.EditBehavior behavior)
 {
     foreach (var uip in behaviors)
     {
         if (uip.editBehavior == behavior)
         {
             uip.Expand(!uip.isExpanded);
         }
         else
         {
             uip.Expand(false);
         }
     }
 }
Example #10
0
 public void UploadBehavior(Behaviors.EditBehavior behavior, Dice.EditDie die, System.Action <bool> callback)
 {
     UpdateDieDataSet(behavior, die, (res) =>
     {
         if (res)
         {
             // We're done!
             callback?.Invoke(true);
         }
         else
         {
             callback?.Invoke(false);
         }
     });
 }
Example #11
0
    UIBehaviorToken CreateBehaviorToken(Behaviors.EditBehavior behavior)
    {
        // Create the gameObject
        var ret = GameObject.Instantiate <UIBehaviorToken>(behaviorTokenPrefab, Vector3.zero, Quaternion.identity, contentRoot.transform);

        spacer.SetAsLastSibling();

        // When we click on the pattern main button, go to the edit page
        ret.onClick.AddListener(() => NavigationManager.Instance.GoToPage(UIPage.PageId.Behavior, behavior));
        ret.onEdit.AddListener(() => NavigationManager.Instance.GoToPage(UIPage.PageId.Behavior, behavior));
        ret.onDuplicate.AddListener(() => DuplicateBehavior(behavior));
        ret.onRemove.AddListener(() => DeleteBehavior(behavior));
        ret.onExpand.AddListener(() => ExpandBehavior(behavior));

        addBehaviorButton.transform.SetAsLastSibling();
        // Initialize it
        ret.Setup(behavior);
        return(ret);
    }
Example #12
0
    void DeleteBehavior(Behaviors.EditBehavior behavior)
    {
        PixelsApp.Instance.ShowDialogBox("Delete Profile?", "Are you sure you want to delete " + behavior.name + "?", "Ok", "Cancel", res =>
        {
            if (res)
            {
                var dependentPresets = AppDataSet.Instance.CollectPresetsForBehavior(behavior);
                if (dependentPresets.Any())
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append("The following presets depend on ");
                    builder.Append(behavior.name);
                    builder.AppendLine(":");
                    foreach (var b in dependentPresets)
                    {
                        builder.Append("\t");
                        builder.AppendLine(b.name);
                    }
                    builder.Append("Are you sure you want to delete it?");

                    PixelsApp.Instance.ShowDialogBox("Profile In Use!", builder.ToString(), "Ok", "Cancel", res2 =>
                    {
                        if (res2)
                        {
                            AppDataSet.Instance.DeleteBehavior(behavior);
                            AppDataSet.Instance.SaveData();
                            RefreshView();
                        }
                    });
                }
                else
                {
                    AppDataSet.Instance.DeleteBehavior(behavior);
                    AppDataSet.Instance.SaveData();
                    RefreshView();
                }
            }
        });
    }
Example #13
0
 void OnDieUpdatedEvent(Dice.EditDie die, Behaviors.EditBehavior behavior)
 {
     UpdatePresetAndBehaviorStatuses();
 }
Example #14
0
    public void UpdateDieDataSet(Behaviors.EditBehavior behavior, Dice.EditDie die, System.Action <bool> callback)
    {
        // Make sure the die is ready!
        ShowProgrammingBox("Connecting to " + die.name + "...");
        DiceManager.Instance.ConnectDie(die, (editDie, res, message) =>
        {
            if (res)
            {
                // The die is ready to be uploaded to

                // Generate the data to be uploaded
                EditDataSet editSet = new EditDataSet();

                // Grab the behavior
                editSet.behavior = behavior.Duplicate();

                // And add the animations that this behavior uses
                var animations = editSet.behavior.CollectAnimations();

                // Add default rules and animations to behavior / set
                if (AppDataSet.Instance.defaultBehavior != null)
                {
                    // Rules that are in fact copied over
                    var copiedRules = new List <EditRule>();

                    foreach (var rule in AppDataSet.Instance.defaultBehavior.rules)
                    {
                        if (!editSet.behavior.rules.Any(r => r.condition.type == rule.condition.type))
                        {
                            var ruleCopy = rule.Duplicate();
                            copiedRules.Add(ruleCopy);
                            editSet.behavior.rules.Add(ruleCopy);
                        }
                    }

                    // Copied animations
                    var copiedAnims = new Dictionary <Animations.EditAnimation, Animations.EditAnimation>();

                    // Add animations used by default rules
                    foreach (var editAnim in AppDataSet.Instance.defaultBehavior.CollectAnimations())
                    {
                        foreach (var copiedRule in copiedRules)
                        {
                            if (copiedRule.DependsOnAnimation(editAnim))
                            {
                                Animations.EditAnimation copiedAnim = null;
                                if (!copiedAnims.TryGetValue(editAnim, out copiedAnim))
                                {
                                    copiedAnim = editAnim.Duplicate();
                                    animations.Add(copiedAnim);
                                    copiedAnims.Add(editAnim, copiedAnim);
                                }
                                copiedRule.ReplaceAnimation(editAnim, copiedAnim);
                            }
                        }
                    }
                }

                editSet.animations.AddRange(animations);

                foreach (var pattern in AppDataSet.Instance.patterns)
                {
                    bool asRGB = false;
                    if (animations.Any(anim => anim.DependsOnPattern(pattern, out asRGB)))
                    {
                        if (asRGB)
                        {
                            editSet.rgbPatterns.Add(pattern);
                        }
                        else
                        {
                            editSet.patterns.Add(pattern);
                        }
                    }
                }

                // Set the behavior
                var dataSet = editSet.ToDataSet();

                // Check the dataset against the one stored in the die
                var hash = dataSet.ComputeHash();

                // Get the hash directly from the die
                editDie.die.GetDieInfo((info_res) =>
                {
                    if (info_res)
                    {
                        if (hash != editDie.die.dataSetHash)
                        {
                            // We need to upload the dataset first
                            Debug.Log("Uploading dataset to die " + editDie.name);
                            var dataSetDataSize = dataSet.ComputeDataSetDataSize();
                            Debug.Log("Dataset data size " + dataSetDataSize);
                            UpdateProgrammingBox(0.0f, "Uploading data to " + editDie.name + "...");
                            editDie.die.UploadDataSet(dataSet,
                                                      (pct) =>
                            {
                                UpdateProgrammingBox(pct, "Uploading data to " + editDie.name + "...");
                            },
                                                      (res2, errorMsg) =>
                            {
                                if (res2)
                                {
                                    editDie.die.GetDieInfo(res3 =>
                                    {
                                        if (res3)
                                        {
                                            HideProgrammingBox();
                                            if (hash != editDie.die.dataSetHash)
                                            {
                                                ShowDialogBox("Error verifying data sent to " + editDie.name, message, "Ok", null, null);
                                                callback?.Invoke(false);
                                            }
                                            else
                                            {
                                                die.currentBehavior = behavior;
                                                AppDataSet.Instance.SaveData();
                                                onDieBehaviorUpdatedEvent?.Invoke(die, die.currentBehavior);
                                                callback?.Invoke(true);
                                            }
                                            DiceManager.Instance.DisconnectDie(editDie, null);
                                        }
                                        else
                                        {
                                            HideProgrammingBox();
                                            ShowDialogBox("Error fetching profile hash value from " + editDie.name, message, "Ok", null, null);
                                            DiceManager.Instance.DisconnectDie(editDie, null);
                                            callback?.Invoke(false);
                                        }
                                    });
                                }
                                else
                                {
                                    HideProgrammingBox();
                                    ShowDialogBox("Error uploading data to " + editDie.name, errorMsg, "Ok", null, null);
                                    DiceManager.Instance.DisconnectDie(editDie, null);
                                    callback?.Invoke(false);
                                }
                            });
                        }
                        else
                        {
                            Debug.Log("Die " + editDie.name + " already has preset with hash 0x" + hash.ToString("X8") + " programmed.");
                            HideProgrammingBox();
                            ShowDialogBox("Profile already Programmed", "Die " + editDie.name + " already has profile \"" + behavior.name + "\" programmed.", "Ok", null, null);
                            DiceManager.Instance.DisconnectDie(editDie, null);
                            callback?.Invoke(true);
                        }
                    }
                    else
                    {
                        HideProgrammingBox();
                        ShowDialogBox("Error verifying profile hash on " + editDie.name, message, "Ok", null, null);
                        DiceManager.Instance.DisconnectDie(editDie, null);
                        callback?.Invoke(false);
                    }
                });
            }
            else
            {
                HideProgrammingBox();
                ShowDialogBox("Error connecting to " + editDie.name, message, "Ok", null, null);
                callback(false);
            }
        });
    }
Example #15
0
 public IEnumerable <Presets.EditPreset> CollectPresetsForBehavior(Behaviors.EditBehavior behavior)
 {
     return(presets.Where(b => b.DependsOnBehavior(behavior)));
 }