Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit f6119d9

Browse files
committed
Saving
1 parent 26aac67 commit f6119d9

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

BuildPlate.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ public class PositionInt
5050
public int y;
5151
public int z;
5252

53+
public PositionInt(int _x, int _y, int _z)
54+
{
55+
x = _x;
56+
y = _y;
57+
z = _z;
58+
}
59+
5360
public static implicit operator Vector3i(PositionInt p) => new Vector3i(p.x, p.y, p.z);
61+
public static implicit operator PositionInt(Vector3i p) => new PositionInt(p.X, p.Y, p.Z);
5462

5563
public override string ToString() => $"X: {x}, Y: {y}, Z: {z}";
5664
}

UI/GUI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public static void Init(Font font)
9393
UIImage.CreatePixel(new Vector2i(240, 260), new Vector2i(0, 40), Textures["Green"]), // loading bar
9494
UItext.CreateCenter("Loading...", 0, 50, 5f, font),
9595
},
96-
new List<IGUIElement>() { },
96+
new List<IGUIElement>() { }, // in game
97+
new List<IGUIElement>() { }, // save
9798
};
9899
AddToScenes(font);
99100
}

Window.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ protected override void OnUpdateFrame(FrameEventArgs e)
179179
Camera.position.Y -= delta * 6f;
180180
}
181181

182+
if (GUI.Scene == 1 && wantToSave) {
183+
GUI.SetScene(2);
184+
UnlockMouse();
185+
Thread saveThread = new Thread(() =>
186+
{
187+
System.Windows.Forms.DialogResult res = System.Windows.Forms.MessageBox.Show("Are you sure you want to save", "Confirm save",
188+
System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question,
189+
System.Windows.Forms.MessageBoxDefaultButton.Button1);
190+
if (res == System.Windows.Forms.DialogResult.Yes)
191+
World.Save();
192+
193+
wantToSave = false;
194+
GUI.SetScene(1);
195+
});
196+
saveThread.Start();
197+
}
198+
182199
Console.Title = Camera.position.ToString();
183200

184201
// Other keyboard
@@ -190,7 +207,7 @@ protected override void OnUpdateFrame(FrameEventArgs e)
190207
GUI.Update(delta);
191208

192209
float FPS = 1f / delta;
193-
Title = $"BuildPlate_Editor FPS: {SystemPlus.MathPlus.Round(FPS, 2)}";
210+
Title = $"BuildPlate_Editor FPS: {MathPlus.Round(FPS, 2)}";
194211
}
195212

196213
protected override void OnRenderFrame(FrameEventArgs e)
@@ -202,21 +219,21 @@ protected override void OnRenderFrame(FrameEventArgs e)
202219
Camera.UpdateView(Width, Height);
203220
shader.UploadMat4("uProjection", ref Camera.projMatrix);
204221
shader.UploadMat4("uView", ref Camera.viewMatrix);
205-
if (GUI.Scene == 1)
222+
if (GUI.Scene > 0)
206223
World.Render(shader);
207224

208225
colShader.Bind();
209226
colShader.UploadMat4("uProjection", ref Camera.projMatrix);
210227
colShader.UploadMat4("uView", ref Camera.viewMatrix);
211-
if (GUI.Scene == 1)
228+
if (GUI.Scene > 0)
212229
outline.Render(colShader);
213230

214231
skyboxShader.Bind();
215232
skyboxShader.UploadMat4("uProjection", ref Camera.projMatrix);
216233
skyboxShader.UploadMat4("uView", ref Camera.viewMatrix);
217234
GL.Disable(EnableCap.CullFace);
218235
SkyBox.pos = Camera.position;
219-
if (GUI.Scene == 1)
236+
if (GUI.Scene > 0)
220237
SkyBox.Render(skyboxShader);
221238
GL.Enable(EnableCap.CullFace);
222239

@@ -227,10 +244,9 @@ protected override void OnRenderFrame(FrameEventArgs e)
227244
SwapBuffers();
228245
}
229246

247+
bool wantToSave = false;
230248
protected override void OnKeyDown(KeyboardKeyEventArgs e)
231249
{
232-
keyboardState = e.Keyboard;
233-
234250
if (e.Key == Key.P) {
235251
Vector3i pos = (Vector3i)outline.Position;
236252
World.GetBlockIndex(pos, out int sbi, out int bi);
@@ -250,7 +266,12 @@ protected override void OnKeyDown(KeyboardKeyEventArgs e)
250266
else
251267
World.BlockToPlace = input;
252268
}
269+
} else if (e.Key == Key.S && (e.Modifiers & KeyModifiers.Control) == KeyModifiers.Control) // save
270+
{
271+
wantToSave = true;
253272
}
273+
else
274+
keyboardState = e.Keyboard;
254275

255276
GUI.OnKeyDown(e.Key, e.Modifiers);
256277
}

World.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,6 @@ public static class World
19381938

19391939
public static string BlockToPlace = "stone";
19401940

1941-
private static string fileName;
19421941
public static string targetFilePath;
19431942
private static int fileRev;
19441943

@@ -2286,6 +2285,24 @@ public static void Init()
22862285
}
22872286
}
22882287

2288+
public static void Save()
2289+
{
2290+
plate.sub_chunks.Clear();
2291+
for (int i = 0; i < chunks.Length; i++) {
2292+
BuildPlate.SubChunk sc = new BuildPlate.SubChunk() { position = chunks[i].pos, blocks = new List<int>(),
2293+
block_palette = new List<BuildPlate.PaletteBlock>() };
2294+
2295+
for (int j = 0; j < chunks[i].blocks.Length; j++)
2296+
sc.blocks.Add((int)chunks[i].blocks[j]);
2297+
for (int j = 0; j < chunks[i].palette.Length; j++)
2298+
sc.block_palette.Add(new BuildPlate.PaletteBlock() { name = chunks[i].palette[j].name, data = chunks[i].palette[j].data });
2299+
2300+
plate.sub_chunks.Add(sc);
2301+
}
2302+
BuildPlate.Save(plate, targetFilePath, targetFilePath);
2303+
Console.WriteLine($"Saved to {targetFilePath}");
2304+
}
2305+
22892306
public static void InitChunks()
22902307
{
22912308
Console.WriteLine("Initializing chunks");

0 commit comments

Comments
 (0)