private void btnSaveWordsearch_Click(object sender, EventArgs e) { //check that we currently have a Wordsearch on screen if(currentWordsearchId != null) { //Validate the characters entered string[] lines = rtbChars.Text.ToUpper().Split('\n'); bool validChars = validateCharsStr(lines); if(validChars) { char[,] chars = getChars(lines); string[] words = getWords(); //Make a Wordsearch object Wordsearch wordsearch = new Wordsearch(currentWordsearchId, chars, words); //If this wordsearch is already in the database, remove it if(ImageMarkupDatabase.ContainsWordsearch(currentWordsearchId)) { ImageMarkupDatabase.RemoveWordsearch(currentWordsearchId); } //Store the Wordsearch object and write it out ImageMarkupDatabase.AddWordsearch(wordsearch); ImageMarkupDatabase.WriteDatabase(); } else { MessageBox.Show("Please check the chars entered"); } } else //Otherwise there is no wordsearch to save data for { MessageBox.Show("No wordsearch currently on screen to save data for"); } }
public static void AddWordsearch(string wordsearchId, Wordsearch wordsearch) { //If the dataset has been loaded if(wordsearches != null) { wordsearches.Add(wordsearchId, wordsearch); } else //Otherwise the data hasn't been loaded { throw new DatabaseNotInitialisedException(); } }
//Private Methods private static void loadDatabase() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(XML_DOC_LOCATION); XmlNode rootNode = xmlDoc.SelectSingleNode(XML_ROOT_EL); //Images XmlNode imagesNode = rootNode.SelectSingleNode(XML_IMAGES_EL); XmlNodeList imageNodes = imagesNode.SelectNodes(XML_IMAGE_EL); images = new Dictionary<string, Image>(); foreach(XmlNode imageNode in imageNodes) { string path = imageNode.SelectSingleNode(XML_IMAGE_FILE_PATH_EL).InnerText; string hash = imageNode.SelectSingleNode(XML_IMAGE_HASH_EL).InnerText; //Wordsearch Images XmlNode wordsearchImagesNode = imageNode.SelectSingleNode(XML_WORDSEARCH_IMAGES_EL); XmlNodeList wordsearcheImageNodes = wordsearchImagesNode.SelectNodes(XML_WORDSEARCH_IMAGE_EL); List<WordsearchImage> wordsearchImages = new List<WordsearchImage>(); foreach(XmlNode wiNode in wordsearcheImageNodes) { //Coordinates string strCoords = wiNode.SelectSingleNode(XML_WORDSEARCH_IMAGE_COORDINATES_EL).InnerText; string[] pairs = strCoords.Trim().Split('.'); char[] trimChars = new char[] { '(', ')', ' ' }; string[] parts = pairs[0].Split(','); string x = parts[0].Trim(trimChars); string y = parts[1].Trim(trimChars); IntPoint topLeft = new IntPoint(int.Parse(x), int.Parse(y)); parts = pairs[1].Split(','); x = parts[0].Trim(trimChars); y = parts[1].Trim(trimChars); IntPoint topRight = new IntPoint(int.Parse(x), int.Parse(y)); parts = pairs[2].Split(','); x = parts[0].Trim(trimChars); y = parts[1].Trim(trimChars); IntPoint bottomRight = new IntPoint(int.Parse(x), int.Parse(y)); parts = pairs[3].Split(','); x = parts[0].Trim(trimChars); y = parts[1].Trim(trimChars); IntPoint bottomLeft = new IntPoint(int.Parse(x), int.Parse(y)); uint rows = uint.Parse(wiNode.SelectSingleNode(XML_WORDSEARCH_IMAGE_ROWS_EL).InnerText); uint cols = uint.Parse(wiNode.SelectSingleNode(XML_WORDSEARCH_IMAGE_COLS_EL).InnerText); //Meta Data XmlNode metaDataNode = wiNode.SelectSingleNode(XML_WORDSEARCH_IMAGE_METADATA_EL); XmlNodeList metaDataNodes = metaDataNode.ChildNodes; Dictionary<string, string> metaData = new Dictionary<string, string>(); foreach(XmlNode metaDataEntryNode in metaDataNodes) { metaData.Add(metaDataEntryNode.Name, metaDataEntryNode.InnerText); } //Optional WordsearchID field string wordsearchId = null; try { wordsearchId = wiNode.SelectSingleNode(XML_WORDSEARCH_IMAGE_WORDSEARCH_ID_EL).InnerText; } catch { } //If there was a Wordsearch ID, pass that to the constructor WordsearchImage wordsearchImage; if(wordsearchId != null) { wordsearchImage = new WordsearchImage(topLeft, topRight, bottomRight, bottomLeft, rows, cols, metaData, hash, wordsearchId); } else //Otherwise there is no Wordsearch ID { wordsearchImage = new WordsearchImage(topLeft, topRight, bottomRight, bottomLeft, rows, cols, metaData, hash); } wordsearchImages.Add(wordsearchImage); } //Meta Data XmlNodeList imageMetaDataNodes = imageNode.SelectSingleNode(XML_IMAGE_METADATA_EL).ChildNodes; Dictionary<string, string> imageMetaData = new Dictionary<string, string>(); foreach(XmlNode metaDataEntryNode in imageMetaDataNodes) { imageMetaData.Add(metaDataEntryNode.Name, metaDataEntryNode.InnerText); } Image image = new Image(path, hash, wordsearchImages.ToArray(), imageMetaData); images.Add(hash, image); } //Wordsearches XmlNode wordsearchesNode = rootNode.SelectSingleNode(XML_WORDSEARCHES_EL); XmlNodeList wordsearchNodes = wordsearchesNode.SelectNodes(XML_WORDSEARCH_EL); wordsearches = new Dictionary<string, Wordsearch>(); foreach(XmlNode wordsearchNode in wordsearchNodes) { string wordsearchId = wordsearchNode.SelectSingleNode(XML_WORDSEARCH_ID_EL).InnerText; //Chars string strChars = wordsearchNode.SelectSingleNode(XML_WORDSEARCH_CHARS_EL).InnerText.Replace("\r", ""); //Strip carriage returns string[] strCharsRows = strChars.Split('\n'); char[,] chars = new char[strCharsRows[0].Length, strCharsRows.Length]; for(int i = 0; i < strCharsRows.Length; i++) { for(int j = 0; j < strCharsRows[i].Length; j++) { chars[j, i] = strCharsRows[i][j]; } } //Words XmlNode wordsNode = wordsearchNode.SelectSingleNode(XML_WORDSEARCH_WORDS_EL); XmlNodeList wordNodes = wordsNode.SelectNodes(XML_WORDSEARCH_WORD_EL); List<string> words = new List<string>(); foreach(XmlNode wordNode in wordNodes) { words.Add(wordNode.InnerText); } Wordsearch wordsearch = new Wordsearch(wordsearchId, chars, words.ToArray()); wordsearches.Add(wordsearchId, wordsearch); } }
public static void AddWordsearch(Wordsearch wordsearch) { AddWordsearch(wordsearch.Id, wordsearch); }