-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
318 lines (269 loc) · 9.08 KB
/
Game1.cs
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace FFRMapEditorMono
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Overworld overworld;
private ToolsMenu toolsMenu;
private InfoWindow infoWindow;
private List<WarningWindow> warningWindows;
private List<OptionPicker> optionPickers;
private Point windowSize = new(1200, 800);
private SpriteFont font;
private FileManager fileManager;
private CurrentTool currentTool;
private MouseState mouse;
private List<EditorTask> editorTasks;
private WindowsManager windowsManager;
private bool LastActiveState = true;
private List<string> unplacedTiles;
private int suspendKeyboard = 0;
private int timeToBackup;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
_graphics.PreferredBackBufferWidth = windowSize.X;
_graphics.PreferredBackBufferHeight = windowSize.Y;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
Window.Title = "FFR Map Editor";
Window.AllowUserResizing = true;
// Init Mouse
mouse = new();
// Init Tasks list
editorTasks = new();
unplacedTiles = new();
// Create File Manager
fileManager = new FileManager();
fileManager.LoadSettings();
windowSize = fileManager.Settings.GetResolution();
timeToBackup = fileManager.Settings.GetBackupDelay();
_graphics.PreferredBackBufferWidth = windowSize.X;
_graphics.PreferredBackBufferHeight = windowSize.Y;
_graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
//Load Textures
Texture2D tileSetTexture = Content.Load<Texture2D>("maptiles");
Texture2D domainGroupsIcons = Content.Load<Texture2D>("domainsicons");
Texture2D docksIcons = Content.Load<Texture2D>("docksicons");
Texture2D mapobjectsIcons = Content.Load<Texture2D>("mapobjects");
Texture2D selectors16 = Content.Load<Texture2D>("cursorsmerge16");
Texture2D selectors32 = Content.Load<Texture2D>("cursorsmerge32");
Texture2D infowindowtexture = Content.Load<Texture2D>("windowborder");
Texture2D toolsTexture = Content.Load<Texture2D>("tools");
Texture2D brushesTexture = Content.Load<Texture2D>("smarthbrushes");
Texture2D placingIcons = Content.Load<Texture2D>("placingicons");
Texture2D templatesIcons = Content.Load<Texture2D>("templatesicons");
Texture2D buttonTexture = Content.Load<Texture2D>("button");
font = Content.Load<SpriteFont>("File");
// Create overworld
overworld = new(tileSetTexture, font, domainGroupsIcons, docksIcons, mapobjectsIcons, GraphicsDevice, _spriteBatch, fileManager.Settings.GetUndoDepth());
// Create menus
toolsMenu = new(toolsTexture, selectors32, font);
infoWindow = new(infowindowtexture, font, buttonTexture, windowSize);
currentTool = new();
optionPickers = new()
{
new DomainPicker(domainGroupsIcons, selectors32, font),
new TemplatePicker(templatesIcons, selectors32, font),
new DockPicker(docksIcons, selectors32, placingIcons, font, overworld),
new MapObjectPicker(mapobjectsIcons, selectors16, placingIcons, font, overworld),
new BrushPicker(brushesTexture, selectors16, font),
new TilePicker(tileSetTexture, selectors16, placingIcons, font, overworld)
};
// Warning Windows
warningWindows = new()
{
new ExitWarningWindow(infowindowtexture, font, buttonTexture, windowSize),
new SaveWarningWindow(infowindowtexture, font, buttonTexture, windowSize),
new NewMapWarningWindow(infowindowtexture, font, buttonTexture, windowSize),
new LoadMapWarningWindow(infowindowtexture, font, buttonTexture, windowSize)
};
// Create Windows manager
windowsManager = new(toolsMenu, infoWindow);
windowsManager.RegisterWarningWindows(warningWindows);
windowsManager.RegisterOptionPickers(optionPickers);
}
protected override void Update(GameTime gameTime)
{
if (editorTasks.Contains(new EditorTask() { Type = EditorTasks.ResetBackupCounter }))
{
editorTasks.RemoveAll(t => t.Type == EditorTasks.ResetBackupCounter);
timeToBackup = fileManager.Settings.GetBackupDelay();
}
else if (timeToBackup > 0)
{
timeToBackup--;
}
else
{
editorTasks.Add(new EditorTask() { Type = EditorTasks.SaveBackupMap });
timeToBackup = fileManager.Settings.GetBackupDelay();
}
if (Keyboard.GetState().IsKeyDown(Keys.Escape) || editorTasks.Contains(new EditorTask() { Type = EditorTasks.ExitProgram }))
{
if (overworld.UnsavedChanges)
{
editorTasks.Add(new EditorTask() { Type = EditorTasks.ExitWarningOpen });
editorTasks.RemoveAll(t => t.Type == EditorTasks.ExitProgram);
}
else
{
Exit();
}
}
if (editorTasks.Contains(new EditorTask() { Type = EditorTasks.ExitProgramHard }))
{
Exit();
}
// Check if window is active to reduce misclicks
if (!IsActive)
{
return;
}
if (WindowResized())
{
infoWindow.UpdatePosition(windowSize);
}
if (suspendKeyboard > 0)
{
suspendKeyboard--;
}
else if(Keyboard.GetState().IsKeyDown(Keys.LeftControl) && Keyboard.GetState().IsKeyDown(Keys.Z))
{
if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
{
overworld.Redo();
suspendKeyboard = 20;
}
else
{
overworld.Undo();
suspendKeyboard = 20;
}
}
// Remove None tasks
editorTasks.RemoveAll(t => t.Type == EditorTasks.None);
// Update Mouse Statuts
mouse.Update();
// Select Options
editorTasks.AddRange(toolsMenu.PickOption(mouse));
editorTasks.AddRange(infoWindow.ProcessInput(mouse));
foreach (var picker in optionPickers)
{
editorTasks.AddRange(picker.PickOption(mouse));
}
foreach (var warning in warningWindows)
{
editorTasks.AddRange(warning.ProcessInput(mouse));
}
// Update Selected Tool
currentTool.Update(editorTasks);
currentTool.GetTemplate(editorTasks, optionPickers.OfType<TemplatePicker>().First().CurrentTemplate);
toolsMenu.UpdateBrushSize(currentTool.BrushSize + 1);
// Interact with the map
if (windowsManager.CanInteractWithMap(mouse.Position))
{
overworld.UpdateTile(GraphicsDevice, _spriteBatch, mouse, windowSize, currentTool);
overworld.PlaceTemplate(GraphicsDevice, _spriteBatch, mouse, windowSize, currentTool);
overworld.UpdateDomain(mouse, currentTool);
overworld.UpdateDock(mouse, currentTool);
overworld.UpdateMapObject(mouse, currentTool);
editorTasks.AddRange(overworld.GetTile(mouse));
}
// Update selected tiles
foreach (var picker in optionPickers)
{
picker.ProcessTasks(editorTasks);
}
// Update Windows
windowsManager.ProcessTasks(editorTasks, overworld);
// Update File management
warningWindows.OfType<SaveWarningWindow>().First().ProcessTasks(overworld, optionPickers.OfType<TilePicker>().First().GetUnplacedTiles(), windowSize, editorTasks);
fileManager.ProcessTasks(overworld, unplacedTiles, editorTasks);
overworld.ProcessTasks(fileManager.OverworldData, editorTasks);
// Process Middle Mouse Button
if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
{
currentTool.UpdateBrushScroll(mouse);
}
else
{
overworld.UpdateZoom(mouse, windowSize);
}
if (mouse.MiddleDown)
{
overworld.UpdateView(mouse.GetHoldOffset(), windowSize);
}
else if (mouse.MiddleClick)
{
mouse.SetHoldOffset();
}
// Update Window title
if (fileManager.FilenameUpdated)
{
Window.Title = "FFR Map Editor" + fileManager.GetFileName();
fileManager.FilenameUpdated = false;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
// Draw base canvas
GraphicsDevice.Clear(Color.DarkSlateGray);
// Draw map+overlays
overworld.Draw(_spriteBatch, windowsManager, currentTool, mouse);
// Draw menus
toolsMenu.Draw(_spriteBatch, font, mouse.Position);
infoWindow.Draw(_spriteBatch);
foreach (var picker in optionPickers)
{
picker.Draw(_spriteBatch, font, mouse.Position);
}
foreach (var warning in warningWindows)
{
warning.Draw(_spriteBatch);
}
base.Draw(gameTime);
}
private bool WindowResized()
{
if ((_graphics.PreferredBackBufferWidth != _graphics.GraphicsDevice.Viewport.Width) ||
(_graphics.PreferredBackBufferHeight != _graphics.GraphicsDevice.Viewport.Height))
{
_graphics.PreferredBackBufferWidth = _graphics.GraphicsDevice.Viewport.Width;
_graphics.PreferredBackBufferHeight = _graphics.GraphicsDevice.Viewport.Height;
_graphics.ApplyChanges();
windowSize = new Point(_graphics.GraphicsDevice.Viewport.Width, _graphics.GraphicsDevice.Viewport.Height);
return true;
}
else
{
return false;
}
}
protected override void OnExiting(Object sender, EventArgs args)
{
// Save Setting
fileManager.Settings.SetResolution(windowSize);
fileManager.SaveSettings();
base.OnExiting(sender, args);
}
}
}