public static void Execute_DisplayConsoleChoice(object sender, PresenterExecuteEventArgs eventArgs) { IChoicePresenter cp = (IChoicePresenter)sender; Console.WriteLine(eventArgs.TextToDisplay); if (eventArgs.ChoicesToDisplay.Length > 0) { Debug.Assert(eventArgs.ChoicesToDisplay.Length < 10); for (int i = 0; i < eventArgs.ChoicesToDisplay.Length; i++) { Console.Write($"{i}. "); Console.WriteLine(eventArgs.ChoicesToDisplay[i]); } ConsoleKeyInfo keyInfo; do { keyInfo = Console.ReadKey(true); }while (!char.IsDigit(keyInfo.KeyChar)); // Activate the KC_IDSelectionRequest stored on the selected choice unit. uint choiceMade = uint.Parse(keyInfo.KeyChar.ToString()); cp.SelectChoice(eventArgs.Choices, choiceMade); } }
/* * fixme: Generalize this so that it has a general button container with a layouy policy that you set (by dragging) in the inspector. */ public void DisplayUnityChoice(object sender, PresenterExecuteEventArgs eventArgs) { // Get the ChoicePresenter knowledge source that fired this event. Currently the choice info is stored on the knowledge source. IChoicePresenter cp = (IChoicePresenter)sender; if (buttons.Count > 0) { // Some buttons have already been created. Destroy them. Debug.Log("Clearing buttons: Button count: " + buttons.Count); foreach (Button button in buttons) { Destroy(button); } buttons.Clear(); } // Change the text displayed Text textToChange = m_textDisplay.GetComponent <Text>(); textToChange.text = eventArgs.TextToDisplay; // Create the buttons for choices if (eventArgs.ChoicesToDisplay.Length > 0) { Debug.Assert(eventArgs.ChoicesToDisplay.Length < 5); // Iterate through the number of choices, creating a button for each choice. Debug.Log($"Number of choices = {eventArgs.ChoicesToDisplay.Length}"); for (uint i = 0; i < eventArgs.ChoicesToDisplay.Length; i++) { // Create buttons with appropriate handlers Debug.Log("Creating button with transform: " + transform); Button button = Instantiate(ButtonPrefab); Text textChild = button.GetComponentInChildren <Text>(); textChild.text = eventArgs.ChoicesToDisplay[i]; RectTransform rect = (RectTransform)button.transform; rect.SetParent(transform); rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 10 + i * 40, 30); rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 600, 150); uint choice = i; // Create a new variable with a copy of i so that the closure below closes a unique variable per choice // register a handler with the button that calls cp.SelectChoice() with the appropriate choice. button.onClick.AddListener(() => ButtonCallback(eventArgs.Choices, choice, cp)); buttons.Add(button); } } }
void ButtonCallback(Unit[] choices, uint selection, IChoicePresenter cp) { cp.SelectChoice(choices, selection); }