diff --git a/BTDToolbox/Classes/JsonEditorHandler.cs b/BTDToolbox/Classes/JsonEditorHandler.cs index 3c40cf8..b90a6b8 100644 --- a/BTDToolbox/Classes/JsonEditorHandler.cs +++ b/BTDToolbox/Classes/JsonEditorHandler.cs @@ -18,9 +18,9 @@ public static void ValidateEditor() if (jeditor == null) { jeditor = new New_JsonEditor(); - jeditor.userControls = new JsonEditor_Instance[0]; - jeditor.tabPages = new TabPage[0]; - jeditor.tabFilePaths = new string[0]; + jeditor.tabPages = new List(); + jeditor.tabFilePaths = new List(); + jeditor.userControls = new List(); jeditor.Show(); } else if (jeditor.Visible == false) @@ -33,19 +33,11 @@ public static void OpenFile(string path) if (Serializer.Deserialize_Config().useExternalEditor == false) { ValidateEditor(); - - int i = 0; - bool isFileOpened = false; - foreach (string tp in jeditor.tabFilePaths) + if(jeditor.tabFilePaths.Contains(path)) { - if (path == tp) - { - isFileOpened = true; - jeditor.tabControl1.SelectedTab = jeditor.tabPages[i]; - } - i++; + jeditor.tabControl1.SelectedIndex = jeditor.tabFilePaths.IndexOf(path); } - if (!isFileOpened) + else { jeditor.NewTab(path); } @@ -54,16 +46,16 @@ public static void OpenFile(string path) public static void OpenOriginalFile(string path) { string[] split = path.Split('\\'); - string filename = split[split.Length - 1]; - string backupProj = Environment.CurrentDirectory + "\\Backups\\" + Main.gameName + "_BackupProject\\" + path.Replace(Environment.CurrentDirectory,"").Replace(Serializer.Deserialize_Config().LastProject + "\\", ""); + string backupProj = ""; + if (!path.Contains("\\Backups\\" + Main.gameName + "_BackupProject\\")) + backupProj = Environment.CurrentDirectory + "\\Backups\\" + Main.gameName + "_BackupProject\\" + path.Replace(Environment.CurrentDirectory, "").Replace(Serializer.Deserialize_Config().LastProject + "\\", ""); + else + backupProj = path; + if (File.Exists(backupProj)) - { OpenFile(backupProj); - } else - { ConsoleHandler.appendLog_CanRepeat("Could not find file in backup project... Unable to view original file"); - } } public static void CloseFile(string path) { diff --git a/BTDToolbox/Classes/ProjectConfig.cs b/BTDToolbox/Classes/ProjectConfig.cs index 07b60ca..5f78bcd 100644 --- a/BTDToolbox/Classes/ProjectConfig.cs +++ b/BTDToolbox/Classes/ProjectConfig.cs @@ -61,7 +61,7 @@ public class ConfigFile public int JSON_Editor_PosX { get; set; } public int JSON_Editor_PosY { get; set; } public float JSON_Editor_FontSize { get; set; } - public string[] JsonEditor_OpenedTabs { get; set; } + public List JsonEditor_OpenedTabs { get; set; } } } } diff --git a/BTDToolbox/Classes/SerializeOps.cs b/BTDToolbox/Classes/SerializeOps.cs index f974309..ec033ec 100644 --- a/BTDToolbox/Classes/SerializeOps.cs +++ b/BTDToolbox/Classes/SerializeOps.cs @@ -62,9 +62,11 @@ public static void SaveJSONEditor_Instance(JsonEditor_Instance jeditor, ConfigFi public static void SaveJSONEditor_Tabs(ConfigFile cfg) { if (JsonEditorHandler.jeditor != null) + { cfg.JsonEditor_OpenedTabs = JsonEditorHandler.jeditor.tabFilePaths; + } else - cfg.JsonEditor_OpenedTabs = new string[0]; + cfg.JsonEditor_OpenedTabs = new List(); string output_Cfg = JsonConvert.SerializeObject(cfg, Formatting.Indented); @@ -170,7 +172,7 @@ public static ConfigFile Deserialize_Config() programData.useExternalEditor = false; programData.disableUpdates = false; - programData.JsonEditor_OpenedTabs = new string[0]; + programData.JsonEditor_OpenedTabs = new List(); programData.BTD5_Directory = ""; diff --git a/BTDToolbox/JetForm.cs b/BTDToolbox/JetForm.cs index be90b83..09e474b 100644 --- a/BTDToolbox/JetForm.cs +++ b/BTDToolbox/JetForm.cs @@ -46,7 +46,7 @@ public JetForm(DirectoryInfo dirInfo, Main Form, string projName) if (projName == Serializer.Deserialize_Config().LastProject) { - if (Serializer.Deserialize_Config().JsonEditor_OpenedTabs.Length > 0) + if (Serializer.Deserialize_Config().JsonEditor_OpenedTabs.Count > 0) { DialogResult dialogResult = MessageBox.Show("Do you want to re-open your previous files?", "Reopen previous files?", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) @@ -56,14 +56,14 @@ public JetForm(DirectoryInfo dirInfo, Main Form, string projName) } else { - programData.JsonEditor_OpenedTabs = new string[0]; + programData.JsonEditor_OpenedTabs = new List(); Serializer.SaveJSONEditor_Tabs(programData); } } } else { - programData.JsonEditor_OpenedTabs = new string[0]; + programData.JsonEditor_OpenedTabs = new List(); Serializer.SaveJSONEditor_Tabs(programData); } @@ -752,7 +752,6 @@ private void paste() private void restoreOriginal() { string filepath = this.Text + "\\" + listView1.SelectedItems[0].Text; - if (listView1.SelectedItems.Count == 1) { if(listView1.SelectedItems[0].Text.Contains(".")) diff --git a/BTDToolbox/JsonEditor_Instance.cs b/BTDToolbox/JsonEditor_Instance.cs index c2eb487..0f7c4aa 100644 --- a/BTDToolbox/JsonEditor_Instance.cs +++ b/BTDToolbox/JsonEditor_Instance.cs @@ -706,7 +706,6 @@ private void Editor_TextBox_RightClicked(object sender, MouseEventArgs e) } } } - private void ShowFindMenu_Button_Click(object sender, EventArgs e) { if (Find_Panel.Visible == false) @@ -721,7 +720,6 @@ private void ShowFindMenu_Button_Click(object sender, EventArgs e) FindText(); } - private void ShowReplaceMenu_Button_Click(object sender, EventArgs e) { if (Find_Panel.Visible == false) @@ -753,37 +751,62 @@ private void EZCard_Button_Click(object sender, EventArgs e) private void RestoreToOriginal_Button_Click(object sender, EventArgs e) { - string backupProj = Environment.CurrentDirectory + "\\Backups\\" + Main.gameName + "_BackupProject\\" + path.Replace(Environment.CurrentDirectory, "").Replace("\\" + Serializer.Deserialize_Config().LastProject + "\\", ""); - if (File.Exists(backupProj)) + RestoreToOriginal(); + } + public void RestoreToOriginal() + { + if (!filename.EndsWith("_original")) { - if (!filename.Contains("_original")) + DialogResult diag = MessageBox.Show("You are trying to restore this file to the original unmodded version. Are you sure you want to do this?", "Restore to original?", MessageBoxButtons.YesNo); + if (diag == DialogResult.Yes) { - if (path.Contains(".")) + string backupProj = Environment.CurrentDirectory + "\\Backups\\" + Main.gameName + "_BackupProject\\" + path.Replace(Environment.CurrentDirectory, "").Replace("\\" + Serializer.Deserialize_Config().LastProject + "\\", ""); + if (File.Exists(backupProj)) { - if (File.Exists(path)) + if (path.Contains(".")) { - JsonEditorHandler.CloseFile(path); - File.Delete(path); + if (File.Exists(path)) + { + JsonEditorHandler.CloseFile(path); + File.Delete(path); + } + File.Copy(backupProj, path); + JsonEditorHandler.OpenFile(path); + ConsoleHandler.appendLog_CanRepeat(filename + "has been restored"); } - File.Copy(backupProj, path); - JsonEditorHandler.OpenFile(path); - ConsoleHandler.appendLog_CanRepeat(filename + "has been restored"); + } + else + { + ConsoleHandler.appendLog_CanRepeat("Could not find file in backup project... Unable to restore file"); } } + else + { + ConsoleHandler.appendLog_CanRepeat("User cancelled restore"); + } } else { - ConsoleHandler.appendLog_CanRepeat("Could not find file in backup project... Unable to restore file"); - } + ConsoleHandler.appendLog_CanRepeat("You can't restore the backup to original because it IS the original"); + } } - private void OpenInFileExplorerToolStripMenuItem_Click(object sender, EventArgs e) + { + OpenInFileExplorer(); + } + public void OpenInFileExplorer() { string[] split = path.Split('\\'); string foldername = path.Replace(split[split.Length - 1], ""); - Process.Start(foldername); + if(!foldername.Contains("BackupProject")) + { + Process.Start(foldername); + } + else + { + ConsoleHandler.appendNotice("Operation cancelled. We don't want you to edit the backup on accident... If you really need to look at it, you can find it in the Backups folder"); + } } - private void ViewOriginalToolStripMenuItem_Click(object sender, EventArgs e) { JsonEditorHandler.OpenOriginalFile(path); diff --git a/BTDToolbox/New_JsonEditor.Designer.cs b/BTDToolbox/New_JsonEditor.Designer.cs index 6825e36..18c3c3c 100644 --- a/BTDToolbox/New_JsonEditor.Designer.cs +++ b/BTDToolbox/New_JsonEditor.Designer.cs @@ -70,6 +70,7 @@ private void InitializeComponent() this.tabControl1.TabIndex = 0; this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.TabControl1_DrawItem); this.tabControl1.SizeChanged += new System.EventHandler(this.TabControl1_SizeChanged); + this.tabControl1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TabControl1_MouseMove); // // New_JsonEditor // diff --git a/BTDToolbox/New_JsonEditor.cs b/BTDToolbox/New_JsonEditor.cs index 5d52fda..4c6ad1f 100644 --- a/BTDToolbox/New_JsonEditor.cs +++ b/BTDToolbox/New_JsonEditor.cs @@ -22,14 +22,20 @@ public partial class New_JsonEditor : ThemedForm public static int JsonEditor_Height = 0; public static string selectedPath = ""; public static bool isJsonError = false; - public JsonEditor_Instance[] userControls; - public TabPage[] tabPages; - public string[] tabFilePaths; + public Point mouseClickPos; + private ContextMenuStrip selMenu; + + public List tabPages; + public List tabFilePaths; + public List userControls; ConfigFile programData; public New_JsonEditor() { InitializeComponent(); + initSelContextMenu(); + tabControl1.MouseUp += Mouse_RightClick; + this.MdiParent = Main.getInstance(); this.Font = new Font("Consolas", 11); JsonEditor_Width = tabControl1.Width; @@ -44,21 +50,60 @@ public New_JsonEditor() // //Open stuff // + private void initSelContextMenu() + { + selMenu = new ContextMenuStrip(); + selMenu.Items.Add("Close"); + selMenu.Items.Add("View original"); + selMenu.Items.Add("Restore to original"); + selMenu.Items.Add("Open in File Explorer"); + selMenu.ItemClicked += ContextClicked; + } + private void ContextClicked(object sender, ToolStripItemClickedEventArgs e) + { + int i = GetTabUnderMouse(mouseClickPos.X, mouseClickPos.Y); + if (i != 9999) + { + if (e.ClickedItem.Text == "Close") + { + CloseTab(tabFilePaths[i]); + } + else if (e.ClickedItem.Text == "View original") + { + JsonEditorHandler.OpenOriginalFile(tabFilePaths[i]); + } + else if (e.ClickedItem.Text == "Restore to original") + { + userControls[i].RestoreToOriginal(); + } + else if (e.ClickedItem.Text == "Open in File Explorer") + { + userControls[i].OpenInFileExplorer(); + } + } + + } + private void Mouse_RightClick(object sender, MouseEventArgs e) + { + mouseClickPos = new Point(e.X, e.Y); + if (e.Button == MouseButtons.Right) + { + selMenu.Show(tabControl1, e.Location); + } + else if (e.Button == MouseButtons.Middle) + { + int i = GetTabUnderMouse(mouseClickPos.X, mouseClickPos.Y); + if (i != 9999) + CloseTab(tabFilePaths[i]); + } + } public void NewTab(string path) { if (path != "" && path != null) { - //handle user control stuff - Array.Resize(ref userControls, userControls.Length + 1); - userControls[userControls.Length - 1] = new JsonEditor_Instance(); - - //handle tab pages - Array.Resize(ref tabPages, tabPages.Length + 1); - tabPages[tabPages.Length - 1] = new TabPage(); - - //handle file path array - Array.Resize(ref tabFilePaths, tabFilePaths.Length + 1); - tabFilePaths[tabFilePaths.Length - 1] = path; + tabFilePaths.Add(path); + tabPages.Add(new TabPage()); + userControls.Add(new JsonEditor_Instance()); //create the tab and do required processing @@ -67,21 +112,21 @@ public void NewTab(string path) if (path.Contains("BackupProject")) { filename = filename + "_original"; - userControls[userControls.Length - 1].Editor_TextBox.ReadOnly = true; + userControls[userControls.Count - 1].Editor_TextBox.ReadOnly = true; } - tabPages[tabPages.Length - 1].Text = filename; - tabPages[tabPages.Length - 1].Controls.Add(userControls[userControls.Length - 1]); - userControls[userControls.Length - 1].path = path; - userControls[userControls.Length - 1].filename = filename; + tabPages[tabPages.Count - 1].Text = filename; + tabPages[tabPages.Count - 1].Controls.Add(userControls[userControls.Count - 1]); + userControls[userControls.Count - 1].path = path; + userControls[userControls.Count - 1].filename = filename; AddText(path); - this.tabControl1.TabPages.Add(tabPages[tabPages.Length - 1]); - + tabControl1.TabPages.Add(tabPages[tabPages.Count - 1]); + OpenTab(path); ConsoleHandler.appendLog_CanRepeat("Opened " + filename); - userControls[userControls.Length - 1].FinishedLoading(); + userControls[userControls.Count - 1].FinishedLoading(); } else @@ -96,11 +141,11 @@ private void AddText(string path) { JToken jt = JToken.Parse(unformattedText); string formattedText = jt.ToString(Formatting.Indented); - userControls[userControls.Length - 1].Editor_TextBox.Text = formattedText; + userControls[userControls.Count - 1].Editor_TextBox.Text = formattedText; } catch (Exception) { - userControls[userControls.Length - 1].Editor_TextBox.Text = unformattedText; + userControls[userControls.Count - 1].Editor_TextBox.Text = unformattedText; } } public void OpenTab(string path) @@ -120,152 +165,54 @@ public void OpenTab(string path) // //Methods // - private string GetTabFilePath() + public int GetTabUnderMouse(int mouseX, int mouseY) { - int i = 0; - foreach (TabPage x in tabPages) + for (int i = 0; i < tabControl1.TabCount; i++) { - if (x.Text == tabControl1.SelectedTab.Text) + if (tabControl1.GetTabRect(i).Contains(mouseX, mouseY)) { - selectedPath = tabFilePaths[i]; + return i; } - i++; } - return selectedPath; + return 9999; } - // //Closing stuff // public void CloseTab(string path) { - int i = tabControl1.SelectedIndex; - - //Remove the closed filepath - int j = 0; - string[] tempFilePaths = new string[tabFilePaths.Length - 1]; - foreach (string tf in tabFilePaths) - { - if (j != i) - { - if (i == 0) - { - if (j == 0) - { - tempFilePaths[j] = tf; - } - else - { - tempFilePaths[j - 1] = tf; - } - } - else if (j + 1 <= tempFilePaths.Length) - { - tempFilePaths[j] = tf; - } - else if (j + 1 == tempFilePaths.Length + 1) - tempFilePaths[j - 1] = tf; - } - j++; - } - Array.Resize(ref tabFilePaths, tabFilePaths.Length - 1); - Array.Copy(tempFilePaths, 0, tabFilePaths, 0, tempFilePaths.Length); - - - //Remove the closed usercontrol - j = 0; - JsonEditor_Instance[] tempUserControl = new JsonEditor_Instance[userControls.Length - 1]; - foreach (JsonEditor_Instance tf in userControls) - { - if (j != i) - { - if (i == 0) - { - if (j == 0) - { - tempUserControl[j] = tf; - } - else - { - tempUserControl[j - 1] = tf; - } - } - else if (j + 1 <= tempUserControl.Length) - { - tempUserControl[j] = tf; - } - else if (j + 1 == tempUserControl.Length + 1) - tempUserControl[j - 1] = tf; - /*else - tempUserControl[j] = tf;*/ - } - j++; - } - Array.Resize(ref userControls, userControls.Length - 1); - Array.Copy(tempUserControl, 0, userControls, 0, tempUserControl.Length); + int i = tabFilePaths.IndexOf(path); + int indexBeforeDelete = tabControl1.SelectedIndex; + tabControl1.TabPages.Remove(tabPages[i]); + if (indexBeforeDelete + 1 <= tabControl1.TabPages.Count) + tabControl1.SelectedIndex = indexBeforeDelete; + else + tabControl1.SelectedIndex = indexBeforeDelete - 1; - //Remove the closed tab page - j = 0; - TabPage[] tempTabPages = new TabPage[tabPages.Length - 1]; - foreach (TabPage tf in tabPages) - { - if (j != i) - { - if (i == 0) - { - if (j == 0) - { - tempTabPages[j] = tf; - } - else - { - tempTabPages[j - 1] = tf; - } - } - else if (j + 1 <= tempTabPages.Length) - { - tempTabPages[j] = tf; - } - else if (j + 1 == tempTabPages.Length + 1) - tempTabPages[j - 1] = tf; - //else - //tempTabPages[j] = tf; - } - j++; - } - Array.Resize(ref tabPages, tabPages.Length - 1); - Array.Copy(tempTabPages, 0, tabPages, 0, tempTabPages.Length); + tabFilePaths.RemoveAt(i); + tabPages.RemoveAt(i); + userControls.RemoveAt(i); - if (tabControl1.TabPages.Count - 1 <= 0) + if (tabControl1.TabPages.Count <= 0) this.Close(); - - tabControl1.TabPages.Remove(tabControl1.SelectedTab); - - if (i + 1 <= tabControl1.TabPages.Count) - tabControl1.SelectedIndex = i; - else - tabControl1.SelectedIndex = i - 1; } private void New_JsonEditor_FormClosing(object sender, FormClosingEventArgs e) { Serializer.SaveConfig(this, "json editor", programData); Serializer.SaveJSONEditor_Tabs(programData); - if(userControls.Length >0) + if(userControls.Count >0) Serializer.SaveJSONEditor_Instance(userControls[tabControl1.SelectedIndex], programData); JsonEditorHandler.jeditor = null; } private void Close_button_Click(object sender, EventArgs e) { Serializer.SaveConfig(this, "json editor", programData); - Serializer.SaveJSONEditor_Tabs(programData); if (JsonEditorHandler.AreJsonErrors()) { - //tabControl1.SelectedIndex = i; DialogResult diag = MessageBox.Show(tabControl1.SelectedTab.Text + " has a Json Error! Your mod will break if you don't fix it.\nClose anyways?", "WARNING!!", MessageBoxButtons.YesNo); if (diag == DialogResult.Yes) this.Close(); - } } @@ -302,11 +249,10 @@ private void TabControl1_DrawItem(object sender, DrawItemEventArgs e) //TextRenderer.DrawText(e.Graphics, page.Text, e.Font, paddedBounds, page.ForeColor); TextRenderer.DrawText(e.Graphics, page.Text, e.Font, paddedBounds, Color.White); } - private void Button1_Click(object sender, EventArgs e) + + private void TabControl1_MouseMove(object sender, MouseEventArgs e) { - ConsoleHandler.appendLog_CanRepeat("Tab Pages: " + tabPages.Length.ToString()); - ConsoleHandler.appendLog_CanRepeat("User Controls: " + userControls.Length.ToString()); - ConsoleHandler.appendLog_CanRepeat("File paths: " + tabFilePaths.Length.ToString()); + } } }