This repository was archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindow.cs
More file actions
360 lines (306 loc) · 12.3 KB
/
Window.cs
File metadata and controls
360 lines (306 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using OpenTK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
using System.Drawing;
using OpenTK.Input;
using System.Runtime.InteropServices;
using BuildPlate_Editor.Maths;
using System.Diagnostics;
using System.IO;
using Font = BuildPlate_Editor.Font;
using BuildPlate_Editor.UI;
using System.Threading;
using SystemPlus;
namespace BuildPlate_Editor
{
public class Window : GameWindow
{
public float AspectRatio;
Shader shader;
Shader shader2;
Shader colShader;
Shader skyboxShader;
Shader uiShader;
bool worldLoaded = false;
public static BlockOutline outline;
// Mouse
bool mouseLocked;
Point lastMousePos;
Point origCursorPosition; // position before lock
public Font font;
public KeyboardState keyboardState;
public Vector2 mousePos;
public Window()
{
Width = 1280;
Height = 720;
AspectRatio = (float)Width / (float)Height;
Title = "BuildPlate_Editor";
}
public DebugProc debMessageCallback;
protected override void OnLoad(EventArgs e)
{
MakeCurrent();
GL.Enable(EnableCap.DebugOutput);
debMessageCallback = new DebugProc(MessageCallback); // Fixed error: A callback was made on a garbage collected delegate
GL.DebugMessageCallback(debMessageCallback, IntPtr.Zero);
// load shaders
shader = new Shader(); // chunk shader
shader.Compile("shader");
shader2 = new Shader(); // not used by anything now
shader2.Compile("shader2");
colShader = new Shader(); // used by block outline
colShader.Compile("colShader");
skyboxShader = new Shader();
skyboxShader.Compile("skybox");
uiShader = new Shader(); // Used by UI
uiShader.Compile("ui");
BlockToPlace.Init();
// all UI stuff copyed from my minecraft repo
// load font
font = new Font("Minecraft", 32);
GUI.Init(font);
GUI.SetScene(0); // loading
#if DEBUG
World.Init();
#else
try {
World.Init();
}
catch (Exception ex) {
Util.Exit(EXITCODE.World_Unknown, ex);
}
#endif
Thread worldLoadThread = new Thread(() =>
{
World.InitChunks();
worldLoaded = true;
});
worldLoadThread.Start();
SkyBox.Init("Cold_Sunset", Camera.position, 100f);
outline = new BlockOutline(1.1f, 4f, 3.0f, Color.Red);
base.WindowBorder = WindowBorder.Fixed;
base.WindowState = WindowState.Normal;
GL.Viewport(0, 0, Width, Height);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.CullFace);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
shader.Bind();
Camera.SetRotation(new Vector3(0f, 180f, 0f));
Camera.UpdateView(Width, Height);
shader.UploadMat4("uProjection", ref Camera.projMatrix);
shader.UploadMat4("uView", ref Camera.viewMatrix);
Icon = Icon.ExtractAssociatedIcon(Process.GetCurrentProcess().MainModule.FileName);
LockMouse();
}
private void MessageCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam)
{
if (id == 131185)
return;
byte[] managedArray = new byte[length];
Marshal.Copy(message, managedArray, 0, length);
Console.WriteLine($"MessageCallback: Source:{source}, Type:{type}, id:{id}, " +
$"Severity:{severity}, Message: {Encoding.ASCII.GetString(managedArray)}");
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
float delta = (float)e.Time;
if (GUI.Scene == 1) {
// Rotation
if (keyboardState.IsKeyDown(Key.Left))
Camera.Rotation.Y += delta * 160f;
else if (keyboardState.IsKeyDown(Key.Right))
Camera.Rotation.Y -= delta * 160f;
if (keyboardState.IsKeyDown(Key.Up))
Camera.Rotation.X -= delta * 80f;
else if (keyboardState.IsKeyDown(Key.Down))
Camera.Rotation.X += delta * 80f;
}
if (mouseLocked) {
var mouseDelta = System.Windows.Forms.Cursor.Position - new Size(lastMousePos);
if (mouseDelta != Point.Empty) {
Camera.Rotation.X += mouseDelta.Y * 0.25f;
Camera.Rotation.Y += -mouseDelta.X * 0.25f;
CenterCursor();
}
}
if (Camera.Rotation.X < -85)
Camera.Rotation.X = -85;
else if (Camera.Rotation.X > 85)
Camera.Rotation.X = 85;
// Movement
if (GUI.Scene == 1) {
if (keyboardState.IsKeyDown(Key.W))
Camera.Move(0f, delta * 8f);
else if (keyboardState.IsKeyDown(Key.S))
Camera.Move(180f, delta * 8f);
if (keyboardState.IsKeyDown(Key.A))
Camera.Move(90f, delta * 8f);
else if (keyboardState.IsKeyDown(Key.D))
Camera.Move(270f, delta * 8f);
if (keyboardState.IsKeyDown(Key.Space))
Camera.position.Y += delta * 6f;
else if (keyboardState.IsKeyDown(Key.ShiftLeft))
Camera.position.Y -= delta * 6f;
Camera.UpdateView(Width, Height);
}
if (GUI.Scene == 1 && wantToSave) {
GUI.SetScene(2);
UnlockMouse();
Thread saveThread = new Thread(() =>
{
System.Windows.Forms.DialogResult res = System.Windows.Forms.MessageBox.Show("Are you sure you want to save", "Confirm save",
System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question,
System.Windows.Forms.MessageBoxDefaultButton.Button1);
if (res == System.Windows.Forms.DialogResult.Yes)
World.Save();
wantToSave = false;
GUI.SetScene(1);
});
saveThread.Start();
}
Console.Title = Camera.position.ToString();
// Other keyboard
if (keyboardState.IsKeyDown(Key.Escape))
UnlockMouse();
//if (!outline.Raycast)
outline.Update();
GUI.Update(delta);
float FPS = 1f / delta;
Title = $"BuildPlate_Editor FPS: {MathPlus.Round(FPS, 2)}";
}
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.ClearColor(Color.FromArgb(92, 157, 255));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
colShader.Bind();
colShader.UploadMat4("uProjection", ref Camera.projMatrix);
colShader.UploadMat4("uView", ref Camera.viewMatrix);
shader.Bind();
shader.UploadMat4("uProjection", ref Camera.projMatrix);
shader.UploadMat4("uView", ref Camera.viewMatrix);
if (GUI.Scene > 0)
World.Render(shader, colShader);
colShader.Bind();
if (GUI.Scene > 0)
outline.Render(colShader);
skyboxShader.Bind();
skyboxShader.UploadMat4("uProjection", ref Camera.projMatrix);
skyboxShader.UploadMat4("uView", ref Camera.viewMatrix);
GL.Disable(EnableCap.CullFace);
SkyBox.pos = Camera.position;
if (GUI.Scene > 0)
SkyBox.Render(skyboxShader);
GL.Enable(EnableCap.CullFace);
GL.DepthMask(false);
GUI.Render(uiShader);
GL.DepthMask(true);
SwapBuffers();
LateUpdate();
}
private void LateUpdate()
{
if (wantToTakeInput) {
string input = BlockToPlace.TakeInput();
if (input != string.Empty) {
if (!World.WillCreateValidTextures(input, 0)) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\"{input}\" isn't valid block");
Console.ResetColor();
}
else
World.BlockToPlace = input;
}
GUI.SetScene(0);
wantToTakeInput = false;
}
}
bool wantToSave = false;
bool wantToTakeInput = false;
protected override void OnKeyDown(KeyboardKeyEventArgs e)
{
if (e.Key == Key.P) {
Vector3i pos = (Vector3i)outline.Position;
World.GetBlockIndex(pos, out int sbi, out int bi);
if (sbi != -1 && bi != -1)
Console.WriteLine($"Palette Id: {World.GetBlock(sbi, bi)}, Texture Id: {World.GetBlockPalette(sbi, bi).textures[0]}," +
$"Name: {World.GetBlockPalette(sbi, bi).name}, Data: {World.GetBlockPalette(sbi, bi).data}, Chunk Index: {sbi}, Block Index: {bi}");
else
Console.WriteLine("Couldn't get block index or sub chunk");
}
else if (e.Key == Key.E) {
GUI.SetScene(3);
wantToTakeInput = true;
}
else if (e.Key == Key.S && (e.Modifiers & KeyModifiers.Control) == KeyModifiers.Control) // save
wantToSave = true;
else if (e.Key == Key.C)
World.ShowChunkOutlines = !World.ShowChunkOutlines;
else if (e.Key == Key.M)
outline.Raycast = !outline.Raycast;
else
keyboardState = e.Keyboard;
GUI.OnKeyDown(e.Key, e.Modifiers);
}
protected override void OnKeyUp(KeyboardKeyEventArgs e)
{
keyboardState = e.Keyboard;
GUI.OnKeyUp(e.Key, e.Modifiers);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
GUI.OnKeyPress(e.KeyChar);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
LockMouse();
GUI.OnMouseDown(e.Button, e.Position);
if (GUI.Scene == 1) {
if (outline.Raycast)
outline.Update();
if (e.Button == MouseButton.Left)
World.SetBlock((Vector3i)outline.raycastResult.HitPos, "air");
if (e.Button == MouseButton.Right)
World.SetBlock((Vector3i)outline.raycastResult.LastPos, World.BlockToPlace);
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
GUI.OnMouseUp(e.Button, e.Position);
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
const float speed = 1.0f;
if (e.Delta > 0) {
outline.blocksFromCam += speed;
} else if (e.Delta < 0) {
outline.blocksFromCam -= speed;
}
outline.blocksFromCam = MathPlus.Clamp(outline.blocksFromCam, 1f, 20f);
}
// Mouse
private void CenterCursor()
{
System.Windows.Forms.Cursor.Position = new Point(Width / 2 + Location.X, Height / 2 + Location.Y);
lastMousePos = System.Windows.Forms.Cursor.Position;
}
protected void LockMouse()
{
mouseLocked = true;
origCursorPosition = System.Windows.Forms.Cursor.Position;
CursorVisible = false;
CenterCursor();
}
protected void UnlockMouse()
{
mouseLocked = false;
CursorVisible = true;
System.Windows.Forms.Cursor.Position = origCursorPosition;
}
}
}