Example #1
0
        public static void InitMasterSword()
        {
            if (MasterSword != null)
            {
                return;
            }
            if (Res.SpecificTags.Get("MasterSword") != null)
            {
                try
                {
                    MasterSword = Sword.FromString(Res.SpecificTags.Get("MasterSword"), "MasterSword");
                    // This will often throw if (e.g.) we've changed the definition of FromString since the last time we saved it.
                    // That's fine, it just means treat it as if there were no saved sword at all.
                    if (MasterSword.EnGardeOrientation == null)
                    {
                        throw new Exception("en garde null");
                    }
                    if (MasterSword.EnGardeOrientation.Average.IsIdentity)
                    {
                        throw new Exception("en garde is identity");
                    }
                    if (MasterSword.KnownForms != null && MasterSword.KnownForms.Count > 0)
                    {
                        var fLast = MasterSword.KnownForms.Last();
                        if (fLast == null || fLast.InitialOrientation == null || fLast.InitialOrientation.IsIdentity)
                        {
                            throw new Exception($"malformed last form ({fLast}).");
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Debug("MasterSword", $"Malformed Master Sword ({e.Message}).  Resetting it.");
                    Res.SpecificTags.Delete("MasterSword");
                    MasterSword = null;
                }
            }

            if (MasterSword == null)
            {
                MasterSword = new Sword("MasterSword");
                //MasterSword.EnGardeOrientation = new AdvancedRollingAverageQuat(10, null, Quaternion.Identity);
            }
            Log.Debug("Tools", $"Current master form list: {MasterFechtbuch.formNames.Join()}");
            foreach (var formName in MasterFechtbuch.formNames)
            {
                var f = MasterFechtbuch.Get(formName);
                if (f != Form.None)
                {
                    MasterSword.LearnForm(f);
                }
            }
        }
        private void SetUpFormButtons()
        {
            var layoutpanel = FindViewById <LinearLayout>(Resource.Id.Form_list_layoutpane);

            formNameTextbox    = FindViewById <EditText>(Resource.Id.form_name_textbox);
            setFormNameButton  = FindViewById <Button>(Resource.Id.Set_form_name_button);
            strokeCountDisplay = FindViewById <TextView>(Resource.Id.stroke_count_display);
            reassessButton     = FindViewById <Button>(Resource.Id.reassess_button);
            pauseButton        = FindViewById <Button>(Resource.Id.pause_button);
            finalizeButton     = FindViewById <Button>(Resource.Id.finalize_button);
            paramAbox          = FindViewById <EditText>(Resource.Id.parameterAtextbox);
            paramBbox          = FindViewById <EditText>(Resource.Id.parameterBtextbox);
            paramCbox          = FindViewById <EditText>(Resource.Id.parameterCtextbox);

            foreach (string formName in MasterFechtbuch.formNames?.DefaultIfEmpty() ?? new string[0])
            {
                var form       = MasterFechtbuch.Get(formName);
                var formButton = new Button(this);
                var codicil    = (form != null && form != Form.None) ? "" : " (Unknown)";
                formButton.SetText(formName + codicil, TextView.BufferType.Normal);
                formButton.SetPadding(20, 20, 20, 20);
                layoutpanel.AddView(formButton);
                formButtons.Add(formButton);

                formButton.Click += async(o, e) =>
                {
                    if (FormBeingTrained != null)
                    {
                        return;                            // Debouncing, basically.
                    }
                    if (form != null && form != Form.None) // The target form does already exist in more than theory.
                    {
                        AppendMode         = true;
                        FormBeingRetrained = form;
                        FormBeingTrained   = form;
                        foreach (Button btn in formButtons)
                        {
                            btn.Enabled = false;
                        }
                        setFormNameButton.Text    = "Retrain Instead";
                        formNameTextbox.Text      = formName;
                        formNameTextbox.Focusable = false;
                        CheckStrokeCount();
                        await Speech.SayAllOf($"Adding more training for {formName}. Ahn garde!");

                        CurrentStage = new EnGardeStage($"En Garde for rep {FormBeingTrained.Strokes.Count}", "Setting up another recording. Wait for the cue...",
                                                        new StrokeTrainingStage($"{Current.FormBeingTrained.FormName} stroke, rep {FormBeingTrained.Strokes.Count}"), true);
                    }
                    else // It only existed as a theory - treat it as if the user had typed in the name.
                    {
                        formNameTextbox.Text = formName;
                        setFormNameButton.CallOnClick();
                    }
                };
            }

            formNameTextbox.KeyPress += (object sender, View.KeyEventArgs e) =>
            {
                e.Handled = false;
                if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter && formNameTextbox.Text.Length > 0)
                {
                    setFormNameButton.CallOnClick();
                }
                if (formNameTextbox.Text.Length > 0)
                {
                    setFormNameButton.Text = "Train";
                }
                else
                {
                    setFormNameButton.Text = "Random";
                }
            };

            setFormNameButton.Click += async(o, e) =>
            {
                if (setFormNameButton.Text == RetrainEnGardeText)
                {
                    CurrentStage.Deactivate();
                    setFormNameButton.Text = "Random";
                    CurrentStage           = new DefineEnGardeStage("Redefining en garde", true);
                    return;
                }
                if (FormBeingRetrained != null)
                {
                    if (AppendMode == true)
                    {
                        AppendMode             = false;
                        FormBeingTrained       = new Form(FormBeingRetrained.FormName, FormBeingRetrained.IsOffense);
                        setFormNameButton.Text = "Erase form";
                        await Speech.SayAllOf("Retraining from start.");

                        return;
                    }
                    else if (setFormNameButton.Text == "Erase form")
                    {
                        setFormNameButton.Text = "Confirm erasure";
                        return;
                    }
                    else if (setFormNameButton.Text == "Confirm erasure")
                    {
                        MasterFechtbuch.Erase(FormBeingRetrained.FormName);
                        ThePlayersSword.ForgetForm(FormBeingRetrained.FormName);
                        await Speech.SayAllOf($"Deleting {FormBeingRetrained.FormName} from the master library.");

                        CurrentStage.Deactivate();
                        CurrentStage = GestureRecognizerStage.NullStage;
                        Finish();
                        return;
                    }
                }
                FormBeingRetrained = null;
                AppendMode         = null;
                if (formNameTextbox.Text.Length == 0)
                {
                    formNameTextbox.Text = GenerateRandomFormName();
                }
                await Task.Delay(100); // Let the screen update.

                FormBeingTrained = new Form(formNameTextbox.Text, !(formNameTextbox.Text.EndsWith("Parry")));
                foreach (Button btn in formButtons)
                {
                    btn.Enabled = false;
                }
                formNameTextbox.Focusable = false;
                await Speech.SayAllOf($"Training {FormBeingTrained.FormName}.  Ahn garde!");

                CheckStrokeCount();
                CurrentStage = new EnGardeStage("EnGarde pre-Form Setup", "", new FormSetupStage($"Init training for {FormBeingTrained.FormName}"), true);
            };

            reassessButton.Click += (o, e) =>
            {
            };

            pauseButton.Click += async(o, e) =>
            {
                if (pauseButton.Text == "Pause Sensors")
                {
                    pauseButton.Text = "Resume Sensors";
                    Res.SFX.StopAll();
                    CurrentStage.Deactivate();
                    await Speech.SayAllOf($"Pausing sensors.  Take your time and play with the parameter buttons.");

                    CurrentStage = GestureRecognizerStage.NullStage;
                }
                else
                {
                    pauseButton.Text = "Pause Sensors";
                    await Speech.SayAllOf($"Resume training for {FormBeingTrained.FormName}. Ahn garde!");

                    CurrentStage = new EnGardeStage($"En Garde for rep {FormBeingTrained.Strokes.Count}", "Setting up another recording. Wait for the cue...",
                                                    new StrokeTrainingStage($"{Current.FormBeingTrained.FormName} stroke, rep {FormBeingTrained.Strokes.Count}"), true);
                }
            };

            finalizeButton.Click += async(o, e) =>
            {
                if (finalizeButton.Text == "Finalize")
                {
                    // Halt ongoing processeses (if not already done via the Pause button).
                    Res.SFX.StopAll();
                    CurrentStage.Deactivate();
                    CurrentStage        = GestureRecognizerStage.NullStage;
                    finalizeButton.Text = "Commit to Fechtbuch";
                }
                else
                {
                    // TODO: Stuff.
                    MasterFechtbuch.Inscribe(FormBeingTrained);
                    ThePlayersSword.LearnForm(FormBeingTrained);

                    if (FormBeingRetrained == null)
                    {
                        await Speech.SayAllOf($"Adding {FormBeingTrained.FormName} to the master fechtbuch.");
                    }
                    else
                    {
                        await Speech.SayAllOf($"Updating form listing for {FormBeingTrained.FormName}.");
                    }
                    Log.Info("MeleeTraining", $"Here's the form string for copy-and-pasting as a constant: {FormBeingTrained.ToString()}");
                    Finish();
                }
            };
        }