public void SetData(string title, string body, ChoiceCallback yesCallback, ChoiceCallback noCallback) { this.title = title; this.body = body; this.yesCallback = yesCallback; this.noCallback = noCallback; }
public static void OpenChoice(string title, string text, ChoiceOption[] choices, ChoiceCallback cancel) { GameObject modalPrefab = UnityEngine.Resources.Load("ChoicePrefab") as GameObject; GameObject optionPrefab = UnityEngine.Resources.Load("OptionPrefab") as GameObject; var modal = Instantiate(modalPrefab); var choice = modal.GetComponent <Choice>(); choice.cancel = cancel; choice.titleGameObject.GetComponent <Text>().text = title; choice.textGameObject.GetComponent <Text>().text = text.Replace("\\n", "\n"); foreach (var option in choices) { var optionObject = Instantiate(optionPrefab); optionObject.transform.Find("LeftText").GetComponent <Text>().text = option.leftText; optionObject.transform.Find("RightText").GetComponent <Text>().text = option.rightText; optionObject.GetComponent <Button>().interactable = option.active; optionObject.GetComponent <Button>().onClick.AddListener(delegate { choice.CloseModal(); option.callback(); }); optionObject.transform.parent = choice.optionsObject.transform; } modal.transform.parent = Map.GetSingleton().canvas.transform; modal.GetComponent <RectTransform>().offsetMin = new Vector2(0, 0); modal.GetComponent <RectTransform>().offsetMax = new Vector2(0, 0); Map.GetSingleton().PauseMap(); }
public void Run(ChoiceCallback callback = null) { try { RunAsync(callback).Wait(); } catch (Exception ex) { Consoul.Write($"{ex.Message}\r\nStack Trace: {ex.StackTrace}", ConsoleColor.Red); if (RenderOptions.WaitOnError) { Consoul.Wait(); } } }
public async Task RunAsync(ChoiceCallback callback = null) { int idx = -1; do { Prompt prompt = new Prompt(Title, true); foreach (Option option in Options) { prompt.Add(option.BuildMessage(), option.Color); } prompt.Add(_goBackMessage, RenderOptions.SubnoteColor); try { idx = prompt.Run(); if (idx >= 0 && idx < Options.Count) { await Task.Run(() => Options[idx].Action.Invoke()); if (callback != null) { await callback(idx); } idx = -1; } else if (idx == Options.Count) { idx = int.MaxValue; } else if (idx == Consoul.EscapeIndex) { GoBack(); } } catch (Exception ex) { Consoul.Write($"{Title}[{idx}]\t{ex.Message}\r\n\tStack Trace: {ex.StackTrace}", RenderOptions.InvalidColor); if (RenderOptions.WaitOnError) { Consoul.Wait(); } } } while (idx < 0 && !GoBackRequested); }
public void Run(ChoiceCallback callback = null) { RunAsync(callback).Wait(); }