Commit Description:
Attribute Iosevka and fix links.
Commit Description:
Attribute Iosevka and fix links.
Show/Diff file:
Action:
isometric-park-fna/UI/Graph.cs
172 lines | 6.7 KiB | text/x-csharp | CSharpLexer
using System;
using Num = System.Numerics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using ImGuiNET;
using ImPlotNET;
using JM.LinqFaster;
using isometricparkfna.Messages;
using isometricparkfna.Components;
using isometricparkfna.Engines;
namespace isometricparkfna.UI
{
public static class GraphWindow
{
public static bool hadFocus = false;
public static int year = 1;
public static bool show_totals = false;
public static bool show_subsidies = false;
public static bool show_upkeep = false;
private static string[] money_series = { "Total Funds",
"Subsidies", "Upkeep", "Contracts", "Cashflow", "Misc"};
private static string[] tree_series = { "Total trees", "Dead trees", "Crowded trees"};
public static Dictionary<string, IEnumerable<double>> data_sets = new Dictionary<string, IEnumerable<double>>();
public static Dictionary<String, bool> data_sets_show = new List<string>()
.Concat(money_series)
.Concat(tree_series)
.Select(e => (e, false))
.ToDictionary(t => t.Item1, t=> t.Item2);
public static void Render(ImFontPtr font, Simulation sim, ImGuiWindowBridgeEngine engine)
{
bool newShow = true;
ImGui.PushFont(font);
ImGui.GetStyle().WindowMenuButtonPosition = ImGuiDir.None;
StyleSets.defaultSet.push();
if(GraphWindow.hadFocus)
{
ImGui.PushStyleColor(ImGuiCol.Text, StyleSets.white);
}
ImGui.SetNextWindowSize(new Num.Vector2(400, 400));
ImGui.Begin("Graph", ref newShow, ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoSavedSettings);
if (GraphWindow.hadFocus)
{
ImGui.PopStyleColor();
}
GraphWindow.hadFocus = ImGui.IsWindowFocused();
data_sets["Total Funds"] = sim.allBudgets().Select(b => (double)b.money);
data_sets["Subsidies"] = sim.allBudgets().Select(b => (double)b.subsidy);
data_sets["Upkeep"] = sim.allBudgets().Select(b => (double)b.upkeep);
data_sets["Contracts"] = sim.allBudgets().Select(b => (double)b.contracts);
data_sets["Misc"] = sim.allBudgets().Select(b => (double)b.misc);
data_sets["Cashflow"] = sim.allBudgets().Select(b => (double)b.cashflow);
data_sets["Total trees"] = sim.allBudgets().Select(b => (double)b.trees);
data_sets["Dead trees"] = sim.allBudgets().Select(b => (double)b.dead_trees);
data_sets["Crowded trees"] = sim.allBudgets().Select(b => (double)b.crowded_trees);
var periods = 12.0d * GraphWindow.year;
var keys = data_sets_show.Keys.ToList();
var totals = data_sets["Total Funds"];
var max = 0.0d;
var min = 0.0d;
foreach( var key in keys) {
if (data_sets_show[key] && totals.Count() > 0) {
var series_max = data_sets[key].Max() * 1.10f;
max = Math.Max(series_max, max);
}
}
foreach( var key in keys) {
if (data_sets_show[key] && totals.Count() > 0) {
var series_min = data_sets[key].Min();
series_min = series_min >= 0? series_min * 0.90f : series_min *1.10f;
min = Math.Min(series_min, min);
}
}
ImPlot.PushStyleVar(ImPlotStyleVar.LineWeight, 2.0f);
ImPlot.PushStyleVar(ImPlotStyleVar.MinorAlpha, 0.0f);
// ImPlot.PushStyleVar(ImPlotStyleVar.MajorGridSize, new Num.Vector2(0.0f, 0.0f));
// ImPlot.PushStyleVar(ImPlotStyleVar.MinorGridSize, new Num.Vector2(0.0f, 0.0f));
ImPlot.SetNextPlotLimits(totals.Count()-periods, totals.Count(), min, max , ImGuiCond.Always);
if( ImPlot.BeginPlot("My Plot", null, null, new Num.Vector2(-1,0), ImPlotFlags.NoLegend | ImPlotFlags.NoMousePos )) {
// Span<double> totals = new Span<double>(new double[]{0.0, 1.0, 2.0, 9.0});
foreach (var key in keys) {
var show = data_sets_show[key];
var data = data_sets[key];
var data_array = data_sets[key].ToArray();
if (data_array.Length > 0 && show)
{
ImPlot.PlotLine(key, ref data_array[0], data_array.Length);
ImPlot.AnnotateClamped(data_array.Length-1, data_array[data_array.Length-1],
new Num.Vector2(5, -10), StyleSets.grey, key);
}
}
ImPlot.EndPlot();
}
ImPlot.PopStyleVar(2);
ImGui.RadioButton("1 year", ref GraphWindow.year, 1);
ImGui.SameLine();
ImGui.RadioButton("5 years", ref GraphWindow.year, 5);
ImGui.SameLine();
ImGui.RadioButton("25 years", ref GraphWindow.year, 25);
ImGui.SameLine();
ImGui.RadioButton("100 years", ref GraphWindow.year, 100);
ImGui.Text("Trees:");
for (int i = 0; i < tree_series.Length; i++)
{
var key = tree_series[i];
if (Menu.activeButton(key, data_sets_show[key], StyleSets.selected, StyleSets.white))
{
data_sets_show[key] = !data_sets_show[key];
}
if (i != tree_series.Length-1)
{
ImGui.SameLine();
}
}
ImGui.Text("Money:");
for (int i = 0; i < money_series.Length; i++)
{
var key = money_series[i];
if (Menu.activeButton(key, data_sets_show[key], StyleSets.selected, StyleSets.white))
{
data_sets_show[key] = !data_sets_show[key];
}
if ((i % 4 != 3) && (i != money_series.Length-1)) {
ImGui.SameLine();
}
}
ImGui.End();
ImGui.GetStyle().WindowMenuButtonPosition = ImGuiDir.Left;
StyleSets.defaultSet.pop();
ImGui.PopFont();
}
}
}