|
| 1 | +using ImGuiNET; |
| 2 | +using Octokit; |
| 3 | +using StudioCore.Interface; |
| 4 | +using StudioCore.Platform; |
| 5 | +using StudioCore.Utilities; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Numerics; |
| 10 | + |
| 11 | +namespace StudioCore.Core.Project; |
| 12 | + |
| 13 | +public static class ProjectCreationWindow |
| 14 | +{ |
| 15 | + public static Project NewProject = new(); |
| 16 | + |
| 17 | + public static string NewProjectName = ""; |
| 18 | + public static string NewProject_ProjectDirectory = ""; |
| 19 | + public static string NewProject_GameDirectory = ""; |
| 20 | + |
| 21 | + private static bool MenuOpenState = false; |
| 22 | + |
| 23 | + public static void ToggleMenuVisibility() |
| 24 | + { |
| 25 | + MenuOpenState = !MenuOpenState; |
| 26 | + } |
| 27 | + |
| 28 | + public static void Display() |
| 29 | + { |
| 30 | + var scale = Warbox.GetUIScale(); |
| 31 | + |
| 32 | + if (!MenuOpenState) |
| 33 | + return; |
| 34 | + |
| 35 | + ImGui.SetNextWindowSize(new Vector2(800.0f, 600.0f) * scale, ImGuiCond.FirstUseEver); |
| 36 | + ImGui.PushStyleColor(ImGuiCol.WindowBg, CFG.Current.Imgui_Moveable_MainBg); |
| 37 | + ImGui.PushStyleColor(ImGuiCol.TitleBg, CFG.Current.Imgui_Moveable_TitleBg); |
| 38 | + ImGui.PushStyleColor(ImGuiCol.TitleBgActive, CFG.Current.Imgui_Moveable_TitleBg_Active); |
| 39 | + ImGui.PushStyleColor(ImGuiCol.ChildBg, CFG.Current.Imgui_Moveable_ChildBg); |
| 40 | + ImGui.PushStyleColor(ImGuiCol.Text, CFG.Current.ImGui_Default_Text_Color); |
| 41 | + ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(10.0f, 10.0f) * scale); |
| 42 | + ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(5.0f, 5.0f) * scale); |
| 43 | + ImGui.PushStyleVar(ImGuiStyleVar.IndentSpacing, 20.0f * scale); |
| 44 | + |
| 45 | + var flags = ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoResize; |
| 46 | + |
| 47 | + if (ImGui.Begin("Project Creation", ref MenuOpenState, flags)) |
| 48 | + { |
| 49 | + DisplayNewProjectCreation(); |
| 50 | + } |
| 51 | + |
| 52 | + ImGui.End(); |
| 53 | + |
| 54 | + ImGui.PopStyleVar(3); |
| 55 | + ImGui.PopStyleColor(5); |
| 56 | + } |
| 57 | + |
| 58 | + public static void DisplayNewProjectCreation() |
| 59 | + { |
| 60 | + var width = ImGui.GetWindowWidth(); |
| 61 | + |
| 62 | + var flags = ImGuiTableFlags.SizingFixedFit; |
| 63 | + |
| 64 | + if (ImGui.BeginTable($"ProjectCreationTable", 3, flags)) |
| 65 | + { |
| 66 | + ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthFixed, 130); |
| 67 | + ImGui.TableSetupColumn("Button", ImGuiTableColumnFlags.WidthFixed, 20); |
| 68 | + ImGui.TableSetupColumn("Inputs", ImGuiTableColumnFlags.WidthStretch, 400); |
| 69 | + |
| 70 | + // Project Name |
| 71 | + ImGui.TableNextRow(); |
| 72 | + |
| 73 | + ImGui.TableSetColumnIndex(0); |
| 74 | + ImGui.AlignTextToFramePadding(); |
| 75 | + |
| 76 | + ImGui.Text("Project Name"); |
| 77 | + UIHelper.ShowHoverTooltip("The name of this project.\n" + |
| 78 | + "Used when generating the mod.manifest and patched table files.", |
| 79 | + 600.0f); |
| 80 | + |
| 81 | + ImGui.TableSetColumnIndex(1); |
| 82 | + |
| 83 | + ImGui.TableSetColumnIndex(2); |
| 84 | + ImGui.AlignTextToFramePadding(); |
| 85 | + |
| 86 | + ImGui.SetNextItemWidth(400); |
| 87 | + ImGui.InputText("##newProjectName", ref NewProjectName, 255); |
| 88 | + |
| 89 | + // Game Directory |
| 90 | + ImGui.TableNextRow(); |
| 91 | + |
| 92 | + ImGui.TableSetColumnIndex(0); |
| 93 | + ImGui.AlignTextToFramePadding(); |
| 94 | + |
| 95 | + ImGui.Text("Game Directory:"); |
| 96 | + UIHelper.ShowHoverTooltip("The directory that contains the game data.\n" + |
| 97 | + "Example: G:\\SteamLibrary\\steamapps\\common\\KingdomComeDeliverance2", |
| 98 | + 600.0f); |
| 99 | + |
| 100 | + ImGui.TableSetColumnIndex(1); |
| 101 | + |
| 102 | + if (ImGui.Button($@"{ForkAwesome.FileO}##gameRootDirSelect")) |
| 103 | + { |
| 104 | + if (PlatformUtils.Instance.OpenFolderDialog("Select game directory...", out var path)) |
| 105 | + { |
| 106 | + NewProject_GameDirectory = path; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + ImGui.TableSetColumnIndex(2); |
| 111 | + ImGui.AlignTextToFramePadding(); |
| 112 | + |
| 113 | + ImGui.SetNextItemWidth(400); |
| 114 | + ImGui.InputText("##gameDirectoryInput", ref NewProject_GameDirectory, 255); |
| 115 | + |
| 116 | + // Project Directory |
| 117 | + ImGui.TableNextRow(); |
| 118 | + |
| 119 | + ImGui.TableSetColumnIndex(0); |
| 120 | + ImGui.AlignTextToFramePadding(); |
| 121 | + |
| 122 | + ImGui.Text("Project Directory:"); |
| 123 | + UIHelper.ShowHoverTooltip("The directory that contains the data for this project.\n" + |
| 124 | + "Example: G:\\SteamLibrary\\steamapps\\common\\KingdomComeDeliverance2\\Mods\\MyMod", |
| 125 | + 600.0f); |
| 126 | + |
| 127 | + ImGui.TableSetColumnIndex(1); |
| 128 | + |
| 129 | + if (ImGui.Button($@"{ForkAwesome.FileO}##projectDirSelect")) |
| 130 | + { |
| 131 | + if (PlatformUtils.Instance.OpenFolderDialog("Select project directory...", out var path)) |
| 132 | + { |
| 133 | + NewProject_ProjectDirectory = path; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + ImGui.TableSetColumnIndex(2); |
| 138 | + ImGui.AlignTextToFramePadding(); |
| 139 | + |
| 140 | + ImGui.SetNextItemWidth(400); |
| 141 | + ImGui.InputText("##projectDirectoryInput", ref NewProject_ProjectDirectory, 255); |
| 142 | + |
| 143 | + // Create |
| 144 | + ImGui.TableNextRow(); |
| 145 | + |
| 146 | + ImGui.TableSetColumnIndex(0); |
| 147 | + ImGui.AlignTextToFramePadding(); |
| 148 | + |
| 149 | + ImGui.TableSetColumnIndex(1); |
| 150 | + |
| 151 | + ImGui.TableSetColumnIndex(2); |
| 152 | + ImGui.AlignTextToFramePadding(); |
| 153 | + |
| 154 | + if (ImGui.Button("Create", new Vector2(400, 24))) |
| 155 | + { |
| 156 | + NewProject.Config.ProjectName = NewProjectName; |
| 157 | + NewProject.Config.GameDirectory = NewProject_GameDirectory; |
| 158 | + NewProject.Config.ProjectDirectory = NewProject_ProjectDirectory; |
| 159 | + |
| 160 | + bool validProject = CanCreateNewProject(); |
| 161 | + |
| 162 | + if (validProject) |
| 163 | + { |
| 164 | + ProjectHandler.WriteProjectConfig(NewProject); |
| 165 | + |
| 166 | + Warbox.Project = NewProject; |
| 167 | + |
| 168 | + ProjectHandler.LoadProject($"{NewProject.Config.ProjectDirectory}/project.json"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + ImGui.EndTable(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public static bool CanCreateNewProject() |
| 177 | + { |
| 178 | + var validated = true; |
| 179 | + |
| 180 | + if (NewProject_GameDirectory == null || !Directory.Exists(NewProject_GameDirectory)) |
| 181 | + { |
| 182 | + PlatformUtils.Instance.MessageBox( |
| 183 | + "Your game directory path does not exist. Please select a valid directory.", "Error", |
| 184 | + MessageBoxButtons.OK); |
| 185 | + validated = false; |
| 186 | + } |
| 187 | + if (NewProject_ProjectDirectory == null || !Directory.Exists(NewProject_ProjectDirectory)) |
| 188 | + { |
| 189 | + PlatformUtils.Instance.MessageBox( |
| 190 | + "Your project directory path does not exist. Please select a valid directory.", "Error", |
| 191 | + MessageBoxButtons.OK); |
| 192 | + validated = false; |
| 193 | + } |
| 194 | + |
| 195 | + if (validated && File.Exists($@"{NewProject_ProjectDirectory}\project.json")) |
| 196 | + { |
| 197 | + DialogResult message = PlatformUtils.Instance.MessageBox( |
| 198 | + "Your selected project directory already contains a project.json. Would you like to replace it?", |
| 199 | + "Error", |
| 200 | + MessageBoxButtons.YesNo); |
| 201 | + if (message == DialogResult.No) |
| 202 | + { |
| 203 | + validated = false; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (validated && (NewProjectName == null || NewProjectName == "")) |
| 208 | + { |
| 209 | + PlatformUtils.Instance.MessageBox("You must specify a project name.", "Error", |
| 210 | + MessageBoxButtons.OK); |
| 211 | + validated = false; |
| 212 | + } |
| 213 | + |
| 214 | + return validated; |
| 215 | + } |
| 216 | +} |
0 commit comments