Skip to content

Commit 3a6408d

Browse files
committed
feat: Project
- Fixed some issues with the project restructure
1 parent 7b53318 commit 3a6408d

File tree

10 files changed

+403
-313
lines changed

10 files changed

+403
-313
lines changed

src/StudioCore/Core/Project/Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void UpdateProjectJSON()
107107

108108
try
109109
{
110-
var fs = new FileStream(ProjectDirectory, FileMode.Create);
110+
var fs = new FileStream($"{ProjectDirectory}/project.json", FileMode.Create);
111111
var data = Encoding.ASCII.GetBytes(jsonString);
112112
fs.Write(data, 0, data.Length);
113113
fs.Flush();
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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+
}

src/StudioCore/Core/Project/ProjectHandler.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static void RecentProjectEntry(CFG.RecentProject project, int id)
9797
RemoveRecentProject(project);
9898
}
9999

100-
if (ImGui.MenuItem($@"Projects: {project.Name}##project{id}"))
100+
if (ImGui.MenuItem($@"Project: {project.Name}##project{id}"))
101101
{
102102
if (File.Exists(project.ProjectFile))
103103
{
@@ -171,30 +171,22 @@ public static bool LoadProject(string path)
171171
}
172172

173173
Warbox.Project.Setup();
174-
175-
AddProjectToRecentList(Warbox.Project);
176-
177-
CFG.Current.LastProjectFile = path;
178174
Warbox.ProjectChanged = true;
179175

180-
return true;
181-
}
182-
183-
public static void AddProjectToRecentList(Project targetProject)
184-
{
185176
// Add to recent project list
186177
CFG.RecentProject recent = new()
187178
{
188-
Name = targetProject.Config.ProjectName,
189-
ProjectFile = targetProject.ProjectJsonPath
179+
Name = Warbox.Project.Config.ProjectName,
180+
ProjectFile = $"{Warbox.Project.ProjectDirectory}/project.json"
190181
};
191182

192-
if (targetProject.ProjectName != "")
183+
if (Warbox.Project.Config.ProjectName != "")
193184
{
194185
CFG.AddMostRecentProject(recent);
195186
}
196-
}
197187

188+
return true;
189+
}
198190
public static ProjectConfiguration ReadProjectConfig(string path)
199191
{
200192
var config = new ProjectConfiguration();
@@ -205,6 +197,13 @@ public static ProjectConfiguration ReadProjectConfig(string path)
205197
{
206198
config = JsonSerializer.Deserialize(stream, ProjectConfigurationSerializationContext.Default.ProjectConfiguration);
207199
}
200+
201+
CFG.Current.LastProjectFile = path;
202+
}
203+
else
204+
{
205+
// Invalidate this if the file doesn't exist
206+
CFG.Current.LastProjectFile = "";
208207
}
209208

210209
return config;
@@ -216,7 +215,7 @@ public static void WriteProjectConfig(Project targetProject)
216215
return;
217216

218217
var config = targetProject.Config;
219-
var writePath = targetProject.ProjectJsonPath;
218+
var writePath = $"{targetProject.Config.ProjectDirectory}/project.json";
220219

221220
if (writePath != "")
222221
{

0 commit comments

Comments
 (0)