Files @ 3dd666df982a
Branch filter:

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

jmaltzen
Merge README
using System;
using System.Collections.Generic;

namespace DesertPaintLab
{
    public partial class ReagentWindow : Gtk.Window
    {
        private class ReagentCheckButton : Gtk.CheckButton
        {
            private Reagent reagent;
            public Reagent Reagent
            {
                get {
                    return reagent;
                }
                set {
                    reagent = value;
                }
            }
        }
        private class ReagentEntry : Gtk.Entry
        {
            private Reagent reagent;
            public Reagent Reagent
            {
                get {
                    return reagent;
                }
                set {
                    reagent = value;
                }
            }

            // public ReagentEntry(int size) : base(size)
            //{
            //}

            override protected void OnTextInserted(string text, ref int position)
            {
                uint val;
                if (uint.TryParse(text, out val))
                {
                    base.OnTextInserted(text, ref position);
                }
            }
        }

        bool dirty = false;
        PlayerProfile profile;

        SortedDictionary<string, ReagentCheckButton> ingredientCheckButtons = new SortedDictionary<string, ReagentCheckButton>();
        SortedDictionary<string, ReagentEntry> ingredientCostEntries = new SortedDictionary<string, ReagentEntry>();
        SortedDictionary<string, ReagentEntry> ingredientQuantityEntries = new SortedDictionary<string, ReagentEntry>();
        int numReagents = 14;
        public ReagentWindow(PlayerProfile profile)
            : base(Gtk.WindowType.Toplevel)
        {
            this.profile = profile;
            this.Build();

            numReagents = ReagentManager.Names.Count;
            ingredientTable.NRows = (uint)numReagents;
            ingredientTable.NColumns = 4;
            uint row = 0;
            foreach (string reagentName in ReagentManager.Names)
            {
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                ReagentCheckButton checkButton = new ReagentCheckButton();
                checkButton.Active = reagent.Enabled;
                checkButton.Reagent = reagent;
                checkButton.Toggled += OnEnableToggled;
                ingredientCheckButtons.Add(reagentName, checkButton);
                ingredientTable.Attach(checkButton, 0, 1, row, row+1); // checkbox for enabled
                ingredientTable.Attach(new Gtk.Label(reagentName), 1, 2, row, row+1); // name label
                ReagentEntry costEntry = new ReagentEntry();
                costEntry.MaxLength = 6;
                costEntry.WidthChars = 6;
                costEntry.Text = reagent.Cost.ToString();
                // TODO: set up validator
                // TODO: set up event handler for changed
                costEntry.Reagent = reagent;
                costEntry.TextDeleted += OnCostChanged;
                costEntry.TextInserted += OnCostChanged;
                ingredientCostEntries.Add(reagentName, costEntry);
                ingredientTable.Attach(costEntry, 2, 3, row, row+1); // cost input
                ReagentEntry maxQuantityEntry = new ReagentEntry();
                maxQuantityEntry.Text = reagent.RecipeMax.ToString();
                maxQuantityEntry.MaxLength = 4;
                maxQuantityEntry.WidthChars = 4;
                // TODO: set up validator
                // TODO: set up event handler for changed
                maxQuantityEntry.Reagent = reagent;
                if (reagent.IsCatalyst)
                {
                    maxQuantityEntry.Sensitive = false;
                }
                else
                {
                    maxQuantityEntry.TextDeleted += OnQuantityChanged;
                    maxQuantityEntry.TextInserted += OnQuantityChanged;
                }
                ingredientQuantityEntries.Add(reagentName, maxQuantityEntry);
                ingredientTable.Attach(maxQuantityEntry, 3, 4, row, row+1); // maximum quantity input
                ++row;
            }

            okButton.Clicked += OnOK;
            cancelButton.Clicked += OnCancel;
            ShowAll();
        }

        private void OnCostChanged(object o, EventArgs args)
        {
            ReagentEntry costEntry = (ReagentEntry)o;
            uint newCost;
            if (uint.TryParse(costEntry.Text, out newCost))
            {
                if (costEntry.Reagent.Cost != newCost)
                {
                    dirty = true;
                }
            }
        }

        private void OnQuantityChanged(object o, EventArgs args)
        {
            ReagentEntry qtyEntry = (ReagentEntry)o;
            uint newCost;
            if (uint.TryParse(qtyEntry.Text, out newCost))
            {
                if (qtyEntry.Reagent.Cost != newCost)
                {
                    dirty = true;
                }
            }
        }

        private void OnEnableToggled(object o, EventArgs args)
        {
            ReagentCheckButton btn = (ReagentCheckButton)o;
            if (btn.Active != btn.Reagent.Enabled)
            {
                dirty = true;
            }
        }

        private void OnOK(object obj, EventArgs args)
        {
            if (dirty)
            {
                // save out state
                foreach (string reagentName in ReagentManager.Names)
                {
                    ReagentCheckButton checkButton = ingredientCheckButtons[reagentName];
                    ReagentEntry costEntry = ingredientCostEntries[reagentName];
                    ReagentEntry qtyEntry = ingredientQuantityEntries[reagentName];
                    checkButton.Reagent.Enabled = checkButton.Active;
                    uint val;
                    if (uint.TryParse(costEntry.Text, out val))
                    {
                        costEntry.Reagent.Cost = val;
                    }
                    if (uint.TryParse(qtyEntry.Text, out val))
                    {
                        qtyEntry.Reagent.RecipeMax = val;
                    }
                }

                ReagentManager.SaveProfileReagents(profile.ReagentFile);
            }
            this.Destroy();
        }

        private void OnCancel(object obj, EventArgs args)
        {
            this.Destroy();
        }
    }
}