Files @ 85f42d4d9009
Branch filter:

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

Jason Maltzen
README.md edited online with Bitbucket
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace DesertPaintLab
{
	public class ReagentManager
	{
		static Regex reagentRegex = new Regex(@"(?<name>\w+)\s*\|\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+).*");
		static Regex catalystRegex = new Regex(@"(?<name>\w+)\s*\|\s*catalyst");
		
		static SortedDictionary<string,Reagent> reagents = new SortedDictionary<string, Reagent>();
		
		static Gtk.ListStore nameStore = new Gtk.ListStore(typeof(string));

		static public Gtk.ListStore NameListModel
		{
			get
			{
				return nameStore;	
			}
		}
		
		public ReagentManager ()
		{
			
		}
		
		public static void Load(string file)
		{	
			Match match;
			string line;
			reagents.Clear();
			using (StreamReader reader = new StreamReader(file))
			{
				while ((line = reader.ReadLine()) != null) 
                {
					match = reagentRegex.Match(line); 
					if (match.Success)
					{
						string name = match.Groups["name"].Value;
						reagents.Add(name,
							new Reagent(name,
						        byte.Parse(match.Groups["red"].Value),
						    	byte.Parse(match.Groups["green"].Value),
						        byte.Parse(match.Groups["blue"].Value)));
						nameStore.AppendValues(name);
					}
					else
					{
						match = catalystRegex.Match(line);
						if (match.Success)
						{
							string name = match.Groups["name"].Value;
							reagents.Add(name, new Reagent(name));
							nameStore.AppendValues(name);
						}
					}
                }
			}
		}
		
		public static void InitializeReactions(ref SortedDictionary<string, SortedDictionary<string, Reaction>> reactions)
		{
			foreach (KeyValuePair<string, Reagent> pair1 in reagents)
			{
				SortedDictionary<string, Reaction> dict = new SortedDictionary<string, Reaction>();
				foreach (KeyValuePair<string, Reagent> pair2 in reagents)
				{
					if (pair1.Key != pair2.Key)
					{
						dict.Add(pair2.Key, null);
					}
				}
				reactions.Add(pair1.Key, dict);
			}
		}
		
		public static void PopulateReagents(ref Gtk.ComboBox comboBox)
		{
			comboBox.Clear();
			
			Gtk.CellRendererText cell = new Gtk.CellRendererText();
	        comboBox.PackStart(cell, false);
	        comboBox.AddAttribute(cell, "text", 0);
	        Gtk.ListStore store = new Gtk.ListStore(typeof(string));
	        comboBox.Model = store;
			
			store.AppendValues("");
			foreach (KeyValuePair<string, Reagent> pair in reagents)
			{
				store.AppendValues(pair.Key);
			}
		}
		
		/*
		public static void PopulatePigments(ref Gtk.ComboBox comboBox)
		{
			comboBox.Clear();
			
			Gtk.CellRendererText cell = new Gtk.CellRendererText();
	        comboBox.PackStart(cell, false);
	        comboBox.AddAttribute(cell, "text", 0);
	        Gtk.ListStore store = new Gtk.ListStore(typeof(string));
	        comboBox.Model = store;
			
			store.AppendValues("");
			foreach (KeyValuePair<string, Reagent> pair in reagents)
			{
				if (!pair.Value.IsCatalyst)
				{
					store.AppendValues(pair.Key);
				}
			}			
		}
		*/
		
		public static Reagent GetReagent(string reagentName)
		{
			Reagent returnVal;
			reagents.TryGetValue(reagentName, out returnVal);
			return returnVal;
		}
	}
	
}