/// <summary> /// Creates a new campaign.json file using the campaign name and the author's name. /// </summary> /// <param name="desiredCampaign"></param> public static void CreateCampaignFile(CampaignObject desiredCampaign) { if (!CheckIfCampaignExists(desiredCampaign.FileTitle)) { CampaignObject newCamp = new CampaignObject(); newCamp.FileTitle = desiredCampaign.FileTitle; newCamp.Title = desiredCampaign.Title; newCamp.Author = desiredCampaign.Author; newCamp.Theme = desiredCampaign.Theme; newCamp.CreationDate = DateTime.Now; newCamp.MusicFileLocations = new List <MusicObject>(); newCamp.CharacterList = new List <CharacterObject>(); //if there is a directory, we set it to be that; if it's empty, we set it to be default if (!(desiredCampaign.CampaignImageFileLocation == "")) { newCamp.CampaignImageFileLocation = desiredCampaign.CampaignImageFileLocation; } else { newCamp.CampaignImageFileLocation = ConfigurationSettings.AppSettings.Get("DefaultSaveLocation") + @"\resources\images\DefaultCampaignIcon.png"; } string newCampJson = JsonConvert.SerializeObject(newCamp); using (StreamWriter streamWriter = new StreamWriter(WorkingDirectory)) { streamWriter.Write(newCampJson); } } else { throw new Exception(); //catch this exception in the menu that calls it and show the user a message //maybe write your own exception? find a concrete one } }
/// <summary> /// User chose to create a new campaign from scratch. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnCreate_Click(object sender, RoutedEventArgs e) { if (ValidateFields()) { try { desiredCampaign = new CampaignObject(); desiredCampaign.FileTitle = txtTitle.Text.Replace(' ', '_'); desiredCampaign.Title = txtTitle.Text; desiredCampaign.Author = txtAuthor.Text; desiredCampaign.Theme = txtTheme.Text; desiredCampaign.CampaignImageFileLocation = txtImageDir.Text; CampaignDataService.CreateCampaignFile(desiredCampaign); CloseForm("New file created and saved!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Please ensure that the title, author, and theme fields are not empty!"); } }
/// <summary> /// When the user clicks a selection from the list of campaigns, it enables some buttons and /// sets the selected campaign to be the one that will be loaded. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="selectedEntry"></param> private void SelectedCampaignEntry(object sender, RoutedEventArgs e, CampaignObject selectedEntry) { btnLaunchSelected.IsEnabled = true; btnEditSelected.IsEnabled = true; btnDeleteSelected.IsEnabled = true; CampaignDataService.SelectedCampaign = selectedEntry; }
/// <summary> /// Loads all the campaign json file into a CampaignObject for use where needed. /// </summary> /// <param name="campaignName"></param> public static void GetCampaignData(string campaignName) { if (CheckIfCampaignExists(campaignName)) { using (StreamReader streamReader = new StreamReader(WorkingDirectory)) { string stringSelectedCampaign = streamReader.ReadToEnd(); SelectedCampaign = JsonConvert.DeserializeObject <CampaignObject>(stringSelectedCampaign); } } else { throw new Exception(); //catch this exception in the menu that calls it and show the user a message //maybe write your own exception? find a concrete one } }
/// <summary> /// Goes through the campaigns directory, gets all of the json files, and then puts them /// into a nice list for us to use. /// NOTE TO SELF: we don't store this as a class variable because we only need to show the /// list one time, plus the data is dynamic and storing it would mean re-reading the files /// every time. /// </summary> /// <returns> /// List of Campaign objects /// </returns> public static List <CampaignObject> GetAllCampaigns() { List <CampaignObject> listOfCampaigns = new List <CampaignObject>();; WorkingDirectory = ConfigurationSettings.AppSettings.Get("DefaultSaveLocation") + "campaigns\\"; foreach (var file in Directory.GetFiles(WorkingDirectory)) { using (StreamReader streamReader = new StreamReader(file)) { string stringFileData = streamReader.ReadToEnd(); CampaignObject fileData = JsonConvert.DeserializeObject <CampaignObject>(stringFileData); listOfCampaigns.Add(fileData); } } return(listOfCampaigns); }
// //Creates a new canvas item to hold some pieces of data of all of the campaigns //in the campaigns directory. // private Canvas ListItem(CampaignObject newEntry) { Canvas campaignEntry = new Canvas { Width = 355, Height = 196, Visibility = Visibility.Visible, IsEnabled = true, Background = (Brush) new BrushConverter().ConvertFrom("#FFFFFF") }; Label newEntryTitle = new Label { Visibility = Visibility.Visible, Content = newEntry.Title, FontSize = 16, Margin = new Thickness(10, 10, 0, 0), Width = 210, Height = 30 }; Label newEntryAuthor = new Label { Visibility = Visibility.Visible, Content = newEntry.Author, FontSize = 16, Margin = new Thickness(149, 46, 0, 0), Width = 210, Height = 30 }; Label newEntryTheme = new Label { Visibility = Visibility.Visible, Content = newEntry.Theme, FontSize = 16, Margin = new Thickness(149, 82, 0, 0), Width = 210, Height = 30 }; Label newEntryLastAccessedDate = new Label { Visibility = Visibility.Visible, Content = "Created On: " + newEntry.CreationDate.ToString("MMMM dd, yyyy"), FontSize = 16, Margin = new Thickness(149, 118, 0, 0), Width = 210, Height = 30 }; Label newEntryCreationDate = new Label { Visibility = Visibility.Visible, Content = "Last Accessed: " + newEntry.LastAccessed.ToString("MMMM dd, yyyy"), FontSize = 16, Margin = new Thickness(149, 154, 0, 0), Width = 210, Height = 30 }; //sets the image BitmapImage bi3 = new BitmapImage(); bi3.BeginInit(); bi3.UriSource = new Uri(newEntry.CampaignImageFileLocation); bi3.EndInit(); Image selectionImage = new Image { Height = 128, Width = 128, Margin = new Thickness(10, 38, 0, 0), Source = bi3 }; campaignEntry.Children.Add(newEntryTitle); campaignEntry.Children.Add(newEntryAuthor); campaignEntry.Children.Add(newEntryTheme); campaignEntry.Children.Add(newEntryLastAccessedDate); campaignEntry.Children.Add(newEntryCreationDate); campaignEntry.Children.Add(selectionImage); campaignEntry.MouseDown += (sender, MouseButtonEventHandler) => { SelectedCampaignEntry(sender, MouseButtonEventHandler, newEntry); }; return(campaignEntry); }