Files @ d7ce199a7c3a
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/ReactionStatusWindow.cs

Jason Maltzen
Removed tag T9_V4
using System;

namespace DesertPaintLab
{
    public partial class ReactionStatusWindow : Gtk.Window
    {
        PlayerProfile _profile;

        Gtk.ListStore reagentListStore;

        public ReactionStatusWindow(PlayerProfile profile) : base(Gtk.WindowType.Toplevel)
        {
            _profile = profile;
            this.Build();

            //Gtk.CellRendererText reagentColumnCell = new Gtk.CellRendererText();

            SetupReagentList();

            int count = 0;
            foreach (string name1 in ReagentManager.Names)
            {
                Reagent reagent1 = ReagentManager.GetReagent(name1);
                foreach (string name2 in ReagentManager.Names)
                {
                    if (name1.Equals(name2))
                    {
                        continue;
                    }
                    Reagent reagent2 = ReagentManager.GetReagent(name2);
                    Reaction reaction = profile.FindReaction(reagent1, reagent2);
                    if (reaction == null)
                    {
                        if (count == 0)
                        {
                            Gtk.Label header = new Gtk.Label("Missing Reactions:");
                            resultbox.PackStart(header, false, false, 0);
                            Gtk.HSeparator sep = new Gtk.HSeparator();
                            resultbox.PackStart(sep, false, false, 0);
                        }
                        Gtk.Label label = new Gtk.Label(name1 + " + " + name2);
                        resultbox.PackStart(label, false, false, 0);

                        ++count;
                    }
                }
            }

            if (count == 0)
            {
                Gtk.Label header = new Gtk.Label("All reactions recorded!");
                resultbox.PackStart(header, false, false, 0);
            }
            ShowAll();
        }

        public void RefreshReactions()
        {
            while (resultbox.Children.Length > 0)
            {
                Gtk.Widget child = resultbox.Children[0];
                resultbox.Remove(child);
            }
            int count = 0;
            foreach (string name1 in ReagentManager.Names)
            {
                Reagent reagent1 = ReagentManager.GetReagent(name1);
                foreach (string name2 in ReagentManager.Names)
                {
                    if (name1.Equals(name2))
                    {
                        continue;
                    }
                    Reagent reagent2 = ReagentManager.GetReagent(name2);
                    if (!reagent1.Enabled || !reagent2.Enabled) continue;
                    Reaction reaction = _profile.FindReaction(reagent1, reagent2);
                    if (reaction == null)
                    {
                        if (count == 0)
                        {
                            Gtk.Label header = new Gtk.Label("Missing Reactions:");
                            resultbox.PackStart(header, false, false, 0);
                            Gtk.HSeparator sep = new Gtk.HSeparator();
                            resultbox.PackStart(sep, false, false, 0);
                        }
                        Gtk.Label label = new Gtk.Label(name1 + " + " + name2);
                        resultbox.PackStart(label, false, false, 0);

                        ++count;
                    }
                }
            }

            if (count == 0)
            {
                Gtk.Label header = new Gtk.Label("All reactions recorded!");
                resultbox.PackStart(header, false, false, 0);
            }
            ShowAll();
        }

        private void OnReagentToggled(object o, Gtk.ToggledArgs args)
        {
            Gtk.TreeIter iter;
            reagentListStore.GetIter(out iter, new Gtk.TreePath(args.Path));

            Reagent reagent = (Reagent)reagentListStore.GetValue(iter, 0);
            reagent.Enabled = !reagent.Enabled;
            // TODO: SaveReagentSettings();
            RefreshReactions();
        }

        // Reagent view handling
        private void RenderReagentToggle(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
        {
            Reagent reagent = (Reagent)model.GetValue(iter, 0);
            Gtk.CellRendererToggle toggle = (cell as Gtk.CellRendererToggle);
            toggle.Active = reagent.Enabled;
            toggle.Activatable = !reagent.IsCatalyst;
            toggle.Mode = reagent.IsCatalyst ? Gtk.CellRendererMode.Inert : Gtk.CellRendererMode.Activatable;
        }

        void SetupReagentList()
        {
            // initialize reagent list
            reagentListStore = new Gtk.ListStore(typeof(Reagent), typeof(string), typeof(Reagent), typeof(Reagent));
            foreach (string reagentName in ReagentManager.Names)
            {
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                reagentListStore.AppendValues(reagent, reagentName); // , reagent, reagent);
            }

            // Add the columns to the TreeView
            Gtk.TreeViewColumn reagentEnabledColumn = new Gtk.TreeViewColumn();
            reagentEnabledColumn.Title = "Enabled";
            Gtk.CellRendererToggle reagentEnabledCell = new Gtk.CellRendererToggle();
            reagentEnabledCell.Activatable = true;
            reagentEnabledCell.Sensitive = true;
            reagentEnabledCell.Mode = Gtk.CellRendererMode.Activatable;
            reagentEnabledCell.Visible = true;
            reagentEnabledCell.Toggled += new Gtk.ToggledHandler(OnReagentToggled);
            reagentEnabledColumn.PackStart(reagentEnabledCell, true);
            //reagentEnabledColumn.AddAttribute(reagentEnabledCell, "active", 0);

            Gtk.TreeViewColumn reagentNameColumn = new Gtk.TreeViewColumn();
            reagentNameColumn.Title = "Ingredient";
            Gtk.CellRendererText reagentNameCell = new Gtk.CellRendererText();
            reagentNameCell.Mode = Gtk.CellRendererMode.Inert;
            reagentNameColumn.PackStart(reagentNameCell, true);
            reagentNameColumn.AddAttribute(reagentNameCell, "text", 1);
            reagentNameColumn.Expand = true;

            reagentListStore = new Gtk.ListStore(typeof(Reagent), typeof(string), typeof(Reagent), typeof(Reagent));
            foreach (string reagentName in ReagentManager.Names)
            {
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                reagentListStore.AppendValues(reagent, reagentName); // , reagent, reagent);
            }

            reagentEnabledColumn.SetCellDataFunc(reagentEnabledCell, new Gtk.TreeCellDataFunc(RenderReagentToggle));

            // Assign the model to the TreeView
            reagentListView.Model = reagentListStore;
            reagentListView.Sensitive = true;

            reagentListView.AppendColumn(reagentEnabledColumn);
            reagentListView.AppendColumn(reagentNameColumn);
        }
    }
}