/*****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************
                * Core implementation of this component.
                *
                * TO-DO
                *
                * Will interact like this:
                *
                * do
                *   1. yield wait until a result if available
                *   2. check if any selected element is invalid
                *      Notes: The very checking of the validity should trigger the display of the
                *        appropriate error messages.
                * while any selected element is invalid
                *****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************
                *****************************************************************************************/

                /**
                 * Executes the interaction by looping like this:
                 * 1. Expect a result of selected items.
                 * 2. Expect all of them be valid elements. Otherwise, messages will be reported to
                 *      the console (interactive message). Perhaps even providing heading and trailing
                 *      messages for the overall errors.
                 * 3. Keep valid items.
                 */
                protected override IEnumerator Input(InteractiveMessage message)
                {
                    if (cancelButton == null && !AtLeastOneSelectableItem())
                    {
                        throw new Types.Exception("A list interactor with no cancel button must provide at least one item in the list of selectable items when calling Input(). Otherwise, the interaction will no way of ending");
                    }

                    // Reset the list's position
                    Rewind();

                    // Start this whole loop
                    bool allSelectedItemsAreValid;

                    do
                    {
                        allSelectedItemsAreValid = true;
                        // On each loop we will:
                        // 1. Release the fact that we have a result.
                        // 2. Wait for a result (i.e. a selection).
                        HasResult = false;
                        yield return(new WaitUntil(() => HasResult));

                        System.Collections.Generic.List <InteractiveMessage.Prompt> prompt = new System.Collections.Generic.List <InteractiveMessage.Prompt>();
                        ValidateSelection(SelectedItems, (InteractiveMessage.Prompt[] reported) => prompt.AddRange(reported));
                        if (prompt.Count > 0)
                        {
                            allSelectedItemsAreValid = false;
                            yield return(message.PromptMessages(prompt.ToArray()));
                        }
                        // 4. Repeat until the validation does not fail.
                    }while (!allSelectedItemsAreValid);
                    // At this point, each item in SelectedItems is valid
                }
Example #2
0
                private IEnumerator WrappedInteraction(InteractiveMessage interactiveMessage, InteractiveMessage.Prompt[] prompt)
                {
                    if (interactionRunning)
                    {
                        throw new Types.Exception("Cannot run the interaction: A previous interaction is already running");
                    }

                    interactionRunning = true;
                    // We may add extra spaces to the last message to be rendered.
                    // This helps us allocating more visual space so the displayed
                    //   interface does not hide the text in the message.
                    int length = prompt.Length;

                    if (length > 0 && (prompt[length - 1] is InteractiveMessage.MessagePrompt))
                    {
                        // Adds newlines to the last prompt.
                        string extraSpaces = new String('\n', (int)newlinesToAddWhenShowing);
                        InteractiveMessage.MessagePrompt lastPrompt = prompt[length - 1] as InteractiveMessage.MessagePrompt;
                        prompt[length - 1] = new InteractiveMessage.MessagePrompt(lastPrompt.Message + extraSpaces);
                    }
                    yield return(interactiveMessage.PromptMessages(prompt));

                    interactionDisplaying = true;
                    yield return(StartCoroutine(Input(interactiveMessage)));

                    interactionDisplaying = false;
                    interactionRunning    = false;
                }