Commit Description:
Tweak UI appearance.
Commit Description:
Tweak UI appearance.
File last commit:
Show/Diff file:
Action:
isometric-park-fna/UI/Dialog.cs
179 lines | 5.4 KiB | text/x-csharp | CSharpLexer
using System;
using System.Linq;
using System.Collections.Generic;
using ImGuiNET;
using isometricparkfna.Utils;
using TraceryNet;
using Num = System.Numerics;
#nullable enable
namespace isometricparkfna.UI
{
//#nullable enable
public struct DialogOption
{
public String? choice;
public String response;
public String speaker;
}
public static class DialogTrees
{
public static Node<DialogOption> introTree = new Node<DialogOption>(
new DialogOption{response = "Welcome to your new park, director! You can use the mouse or arrow keys to move around, and the plus and minus keys to zoom in and out. B opens the budget and F lets you adjust Forest Policy.",
speaker = "The Governor"
},
new Node<DialogOption>(new DialogOption { choice = "Okay",
response = "Make sure that you keep visitors happy and the budget in the black! You're currently getting an annual grant out of my budget—it'd sure be nice if you park were self-sufficient so we could drop that expense!",
speaker = "The Governor"
},
new Node<DialogOption>[]{
new Node<DialogOption>(new DialogOption {choice="And I need to keep the forest healthy, too, right?", response="Uhh yeah.", speaker = "The Governor" },
new Node<DialogOption>(new DialogOption {choice="...", speaker = "The Governor"})),
new Node<DialogOption>(new DialogOption {choice="Sounds good!", response="I'll check in soon.", speaker = "The Governor" })
})
);
public static Node<DialogOption> testTree = new Node<DialogOption>(
new DialogOption
{
response = "#[name:player]addressGreeting#",
speaker = "#assistantName#"
},
new Node<DialogOption>[]{
new Node<DialogOption>(new DialogOption {choice="Hi.", response="Bye!",
speaker = "#assistantName#" }),
new Node<DialogOption>(new DialogOption {choice="How are you?", response="#howdoing#",
speaker = "#assistantName#" })
}
);
public static Node<DialogOption> testTree2 = new Node<DialogOption>(
new DialogOption
{
response = "#whatever#",
speaker = "#assistantName#"
},
new Node<DialogOption>[]{
new Node<DialogOption>(new DialogOption {choice="Hi.", response="Bye!",
speaker = "#assistantName#" }),
new Node<DialogOption>(new DialogOption {choice="How are you?", response="#howdoing#",
speaker = "#assistantName#" })
}
);
public static Node<DialogOption> flatten (Node<DialogOption> node, Grammar grammar) {
DialogOption new_data = new DialogOption
{
choice = node.data.choice != null ? grammar.Flatten(node.data.choice) : node.data.choice,
response = grammar.Flatten(node.data.response),
speaker = grammar.Flatten(node.data.speaker)
};
if (node.children != null) {
List<Node<DialogOption>> new_children = new List<Node<DialogOption>>();
foreach (Node<DialogOption> child in node.children)
{
new_children.Add(flatten(child, grammar));
}
return new Node<DialogOption>(new_data, new_children.ToArray());
}
else
{
return new Node<DialogOption>(new_data);
}
}
}
public static class DialogInterface
{
public static bool had_focus = false;
public static Node<DialogOption> RenderDialog(ref bool show, ref bool paused, ImFontPtr font, Node<DialogOption> currentNode)
{
Node<DialogOption> new_child = currentNode;
if (show)
{
ImGui.PushFont(font);
ImGui.GetStyle().WindowMenuButtonPosition = ImGuiDir.None;
StyleSet.pushStyleVarSet(StyleSet.defaultWindowVars);
StyleSet.pushColorSet(StyleSet.defaultWindowColors);
ImGui.SetNextWindowSize(new Num.Vector2(400, 200));
if(ContractsWindow.had_focus)
{
ImGui.PushStyleColor(ImGuiCol.Text, StyleSet.white);
}
ImGui.Begin(currentNode.data.speaker, ref show, ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoSavedSettings);
if (ContractsWindow.had_focus)
{
ImGui.PopStyleColor();
}
ContractsWindow.had_focus = ImGui.IsWindowFocused();
if (currentNode.data.response != null)
{
string messageText = currentNode.data.response;
ImGui.TextWrapped(messageText);
}
if ((currentNode.children != null) && currentNode.children.Length > 0)
{
//Filter out nulls
foreach ((Node<DialogOption> child, string choice) in currentNode.children.Select((child) => (child, child.data.choice)).Where( (pair, choice) => pair.choice != null))
{
string buttonText = choice;
if (ImGui.Button(buttonText))
{
new_child = child;
}
}
}
else
{
if (ImGui.Button("Okay"))
{
show = false;
paused = false;
}
}
if (currentNode.data.response == null)
{
show = false;
paused = false;
}
ImGui.End();
ImGui.GetStyle().WindowMenuButtonPosition = ImGuiDir.Left;
StyleSet.popStyleVarSet(StyleSet.defaultWindowVars);
StyleSet.popColorSet(StyleSet.defaultWindowColors);
ImGui.PopFont();
}
return new_child;
}
}
}