/
MainScreen.cs
446 lines (373 loc) · 18.3 KB
/
MainScreen.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using SadConsole.Surfaces;
using SadConsole;
using Console = SadConsole.Console;
using System.Linq;
using System;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using SadConsole.Input;
namespace SadConsoleEditor
{
class MainScreen : Screen
{
public static MainScreen Instance;
public Dictionary<Type, FileLoaders.IFileLoader[]> EditorFileTypes;
public Dictionary<string, Editors.Editors> Editors;
private Console topBarPane;
private Consoles.BorderConsole borderConsole;
private Consoles.BrushConsoleContainer brushScreen;
public Consoles.ToolPane ToolsPane;
public Consoles.QuickSelectPane QuickSelectPane;
public List<Editors.IEditor> OpenEditors;
public Rectangle InnerEmptyBounds;
public Rectangle InnerEmptyBoundsPixels;
public Point InnerBorderPosition => borderConsole.Position + new Point(1);
private string topBarLayerName = "None";
private string topBarToolName = "None";
private Point topBarMousePosition;
private SadConsole.GameHelpers.GameObject brush;
public SadConsole.GameHelpers.GameObject Brush
{
get { return brush; }
set
{
if (brush != null)
brush.Parent = null;
brush = value;
if (value != null)
value.Parent = borderConsole;
}
}
public bool AllowKeyboardToMoveConsole;
public Editors.IEditor ActiveEditor { get; private set; }
public string LayerName
{
set
{
topBarLayerName = value;
RefreshBackingPanel();
}
}
public string ToolName
{
set
{
topBarToolName = value;
RefreshBackingPanel();
}
}
public Point SurfaceMouseLocation
{
set
{
topBarMousePosition = value;
RefreshBackingPanel();
}
}
public MainScreen()
{
MainScreen.Instance = this;
// Create the basic consoles
QuickSelectPane = new SadConsoleEditor.Consoles.QuickSelectPane();
QuickSelectPane.Redraw();
QuickSelectPane.IsVisible = false;
topBarPane = new SadConsole.Console(Settings.Config.WindowWidth, 1);
topBarPane.TextSurface.DefaultBackground = Settings.Color_MenuBack;
topBarPane.Clear();
topBarPane.FocusOnMouseClick = false;
topBarPane.IsVisible = false;
borderConsole = new SadConsoleEditor.Consoles.BorderConsole(10, 10);
borderConsole.IsVisible = false;
//borderConsole.UseMouse = false;
borderConsole.MouseHandler = ProcessMouseForBrush;
ToolsPane = new Consoles.ToolPane();
ToolsPane.Position = new Point(Settings.Config.WindowWidth - ToolsPane.Width - 1, 1);
ToolsPane.IsVisible = false;
brushScreen = new Consoles.BrushConsoleContainer();
var boundsLocation = new Point(0, topBarPane.TextSurface.Height).TranslateFont(topBarPane.TextSurface.Font, Settings.Config.ScreenFont) + new Point(1);
InnerEmptyBounds = new Rectangle(boundsLocation, new Point(1, QuickSelectPane.Position.Y).PixelLocationToConsole(QuickSelectPane.TextSurface.Font.Size.X, QuickSelectPane.TextSurface.Font.Size.Y) - boundsLocation);
InnerEmptyBounds.Width = new Point(ToolsPane.Position.X - 1, 0).TranslateFont(SadConsole.Global.FontDefault, Settings.Config.ScreenFont).X - 1;
InnerEmptyBoundsPixels = new Rectangle(InnerEmptyBounds.Location.ConsoleLocationToPixel(Settings.Config.ScreenFont), InnerEmptyBounds.Size.ConsoleLocationToPixel(Settings.Config.ScreenFont));
// Add the consoles to the main console list
Children.Add(borderConsole);
Children.Add(brushScreen);
Children.Add(QuickSelectPane);
Children.Add(topBarPane);
Children.Add(ToolsPane);
// Setup the file types for base editors.
EditorFileTypes = new Dictionary<Type, FileLoaders.IFileLoader[]>(3);
OpenEditors = new List<SadConsoleEditor.Editors.IEditor>();
//EditorFileTypes.Add(typeof(Editors.DrawingEditor), new FileLoaders.IFileLoader[] { new FileLoaders.BasicSurface() });
// Add valid editors
Editors = new Dictionary<string, SadConsoleEditor.Editors.Editors>();
Editors.Add("Console Draw", SadConsoleEditor.Editors.Editors.Console);
Editors.Add("Animated Game Object", SadConsoleEditor.Editors.Editors.GameObject);
Editors.Add("Game Scene", SadConsoleEditor.Editors.Editors.Scene);
//Editors.Add("User Interface Console", SadConsoleEditor.Editors.Editors.GUI);
}
private bool ProcessMouseForBrush(IConsole console, MouseConsoleState state)
{
//return ActiveEditor.RenderedConsole.ProcessMouse(new MouseConsoleState(ActiveEditor.RenderedConsole, state.Mouse));
return false;
}
public void ShowStartup()
{
Window.Prompt("Create new or open existing?", "New", "Open",
(b) =>
{
if (b)
{
Windows.NewConsolePopup popup = new Windows.NewConsolePopup();
popup.Center();
popup.Closed += (s, e) => { if (!popup.DialogResult) ShowStartup(); else CreateNewEditor(popup.Editor, popup.SettingWidth, popup.SettingHeight, popup.SettingForeground, popup.SettingBackground); };
popup.Show(true);
}
else
{
Windows.SelectFilePopup popup = new Windows.SelectFilePopup();
popup.Center();
popup.Closed += (s, e) => { if (!popup.DialogResult) ShowStartup(); else LoadEditor(popup.SelectedFile, popup.SelectedLoader); };
popup.FileLoaderTypes = new FileLoaders.IFileLoader[] { new FileLoaders.LayeredSurface(), new FileLoaders.BasicSurface(), new FileLoaders.Scene(), new FileLoaders.GameObject() };
popup.Show(true);
}
});
}
private void CreateNewEditor(Editors.Editors editorType, int width, int height, Color defaultForeground, Color defaultBackground)
{
Editors.IEditor editor = null;
switch (editorType)
{
case SadConsoleEditor.Editors.Editors.Console:
editor = new Editors.LayeredConsoleEditor();
editor.New(defaultForeground, defaultBackground, width, height);
break;
case SadConsoleEditor.Editors.Editors.GameObject:
editor = new Editors.GameObjectEditor();
editor.New(defaultForeground, defaultBackground, width, height);
break;
case SadConsoleEditor.Editors.Editors.Scene:
editor = new Editors.SceneEditor();
editor.New(defaultForeground, defaultBackground, width, height);
break;
case SadConsoleEditor.Editors.Editors.GUI:
break;
default:
break;
}
if (editor != null)
{
AddEditor(editor, true);
}
topBarPane.IsVisible = true;
ToolsPane.IsVisible = true;
}
private void LoadEditor(string file, FileLoaders.IFileLoader loader)
{
Editors.IEditor editor = null;
if (loader is FileLoaders.LayeredSurface || loader is FileLoaders.BasicSurface)
{
editor = new Editors.LayeredConsoleEditor();
AddEditor(editor, false);
editor.Load(file, loader);
}
else if (loader is FileLoaders.GameObject)
{
editor = new Editors.GameObjectEditor();
AddEditor(editor, false);
editor.Load(file, loader);
}
else if (loader is FileLoaders.Scene)
{
editor = new Editors.SceneEditor();
AddEditor(editor, false);
editor.Load(file, loader);
}
if (editor != null)
{
//editor.RenderedConsole.TextSurface.RenderArea = new Rectangle(0, 0, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
ChangeActiveEditor(editor);
}
topBarPane.IsVisible = true;
ToolsPane.IsVisible = true;
}
public void ShowCloseConsolePopup()
{
Window.Prompt(new SadConsole.ColoredString("Are you sure? You will lose any unsaved changes."), "Yes", "No", (r) =>
{
if (r)
RemoveEditor(ActiveEditor);
});
}
public void ShowNewEditorPopup()
{
Windows.NewConsolePopup popup = new Windows.NewConsolePopup();
popup.Center();
popup.Closed += (s, e) => { if (popup.DialogResult) CreateNewEditor(popup.Editor, popup.SettingWidth, popup.SettingHeight, popup.SettingForeground, popup.SettingBackground); };
popup.Show(true);
}
public void ShowLoadEditorPopup()
{
Windows.SelectFilePopup popup = new Windows.SelectFilePopup();
popup.Center();
popup.Closed += (s, e) => { if (popup.DialogResult) LoadEditor(popup.SelectedFile, popup.SelectedLoader); };
popup.FileLoaderTypes = new FileLoaders.IFileLoader[] { new FileLoaders.LayeredSurface(), new FileLoaders.Scene(), new FileLoaders.GameObject() };
popup.Show(true);
}
public void ShowResizeEditorPopup()
{
if (ActiveEditor != null)
{
Windows.ResizeSurfacePopup popup = new Windows.ResizeSurfacePopup(ActiveEditor.Width, ActiveEditor.Height);
popup.Center();
popup.Closed += (s, e) =>
{
if (popup.DialogResult)
{
ActiveEditor.Resize(popup.SettingWidth, popup.SettingHeight);
}
};
popup.Show(true);
}
}
public void AddEditor(Editors.IEditor editor, bool show)
{
OpenEditors.Add(editor);
ToolsPane.PanelFiles.DocumentsListbox.Items.Add(editor);
if (show)
ChangeActiveEditor(editor);
}
public void RemoveEditor(Editors.IEditor editor)
{
ToolsPane.PanelFiles.DocumentsListbox.Items.Remove(editor);
editor.OnClosed();
OpenEditors.Remove(editor);
if (OpenEditors.Count == 0)
ShowStartup();
else
ChangeActiveEditor(OpenEditors[0]);
}
public void RefreshBorder()
{
borderConsole.SetContent(ActiveEditor.Surface, ActiveEditor.Renderer);
CenterEditor();
}
public void ChangeActiveEditor(Editors.IEditor editor)
{
AllowKeyboardToMoveConsole = true;
if (ActiveEditor != null)
{
ActiveEditor.OnDeselected();
borderConsole.IsVisible = false;
}
if (OpenEditors.Contains(editor))
{
ActiveEditor = editor;
ActiveEditor.OnSelected();
ToolsPane.RedrawPanels();
borderConsole.IsVisible = true;
borderConsole.SetContent(ActiveEditor.Surface, ActiveEditor.Renderer);
//Consoles.Children.Insert(0, ActiveEditor.RenderedConsole);
RefreshBorder();
if (ToolsPane.PanelFiles.DocumentsListbox.SelectedItem != editor)
ToolsPane.PanelFiles.DocumentsListbox.SelectedItem = editor;
}
}
public void SaveEditor()
{
if (ActiveEditor != null)
ActiveEditor.Save();
}
public void CenterEditor()
{
Point position = new Point();
//if (ActiveEditor.Width > InnerEmptyBounds.Width || ActiveEditor.Height > InnerEmptyBounds.Height)
//{
// // Need scrolling console
// if (ActiveEditor.Width > InnerEmptyBounds.Width && ActiveEditor.Height > InnerEmptyBounds.Height)
// {
// position = InnerEmptyBounds.Location;
// ActiveEditor.RenderedConsole.TextSurface.RenderArea = new Rectangle(0, 0, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
// }
// else if (ActiveEditor.Width > InnerEmptyBounds.Width)
// {
// position = new Point(InnerEmptyBounds.Location.X, (InnerEmptyBounds.Height + InnerEmptyBounds.Y - ActiveEditor.Height) / 2);
// ActiveEditor.RenderedConsole.TextSurface.RenderArea = new Rectangle(0, 0, InnerEmptyBounds.Width, ActiveEditor.Height);
// }
// else if (ActiveEditor.Height > InnerEmptyBounds.Height)
// {
// position = new Point((InnerEmptyBounds.Width + InnerEmptyBounds.X - ActiveEditor.Width) / 2, InnerEmptyBounds.Location.Y);
// ActiveEditor.RenderedConsole.TextSurface.RenderArea = new Rectangle(0, 0, ActiveEditor.Width, InnerEmptyBounds.Height);
// }
//}
//else
//{
// // Center normal
// position = new Point((InnerEmptyBounds.Width + InnerEmptyBounds.X - ActiveEditor.Width) / 2, (InnerEmptyBounds.Height + InnerEmptyBounds.Y - ActiveEditor.Height) / 2);
//}
//if (position.X < InnerEmptyBounds.Left)
// position.X = InnerEmptyBounds.Left;
//if (position.Y < InnerEmptyBounds.Top)
// position.Y = InnerEmptyBounds.Top;
//ActiveEditor.Move(position.X, position.Y);
borderConsole.Position = new Point((InnerEmptyBounds.Width / 2) - (borderConsole.Width / 2), (InnerEmptyBounds.Height / 2) - (borderConsole.Height / 2)) + InnerEmptyBounds.Location;
}
private void RefreshBackingPanel()
{
topBarPane.Clear();
var text = new SadConsole.ColoredString(" X: ", Settings.Appearance_Text) + new SadConsole.ColoredString(topBarMousePosition.X.ToString(), Settings.Appearance_TextValue) +
new SadConsole.ColoredString(" Y: ", Settings.Appearance_Text) + new SadConsole.ColoredString(topBarMousePosition.Y.ToString(), Settings.Appearance_TextValue) +
new SadConsole.ColoredString(" Layer: ", Settings.Appearance_Text) + new SadConsole.ColoredString(topBarLayerName, Settings.Appearance_TextValue) +
new SadConsole.ColoredString(" Tool: ", Settings.Appearance_Text) + new SadConsole.ColoredString(topBarToolName, Settings.Appearance_TextValue);
topBarPane.Print(0, 0, text);
}
public override void Update(TimeSpan timeElapsed)
{
base.Update(timeElapsed);
bool movekeyPressed = false;
var position = new Point(borderConsole.Position.X + 1, borderConsole.Position.Y + 1);
//var result = base.ProcessKeyboard(info);
if (AllowKeyboardToMoveConsole && ActiveEditor != null && ActiveEditor.Surface != null)
{
bool shifted = Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift) || Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.RightShift);
var oldRenderArea = ActiveEditor.Surface.RenderArea;
if (!shifted && Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
ActiveEditor.Surface.RenderArea = new Rectangle(ActiveEditor.Surface.RenderArea.Left - 1, ActiveEditor.Surface.RenderArea.Top, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
else if (!shifted && Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
ActiveEditor.Surface.RenderArea = new Rectangle(ActiveEditor.Surface.RenderArea.Left + 1, ActiveEditor.Surface.RenderArea.Top, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
if (!shifted && Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
ActiveEditor.Surface.RenderArea = new Rectangle(ActiveEditor.Surface.RenderArea.Left, ActiveEditor.Surface.RenderArea.Top - 1, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
else if (!shifted && Global.KeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
ActiveEditor.Surface.RenderArea = new Rectangle(ActiveEditor.Surface.RenderArea.Left, ActiveEditor.Surface.RenderArea.Top + 1, InnerEmptyBounds.Width, InnerEmptyBounds.Height);
movekeyPressed = oldRenderArea != ActiveEditor.Surface.RenderArea;
}
if (!movekeyPressed)
{
//if (info.IsKeyReleased(Microsoft.Xna.Framework.Input.Keys.Subtract))
//{
// SelectedEditor.Surface.ResizeCells(SelectedEditor.Surface.CellSize.X / 2, SelectedEditor.Surface.CellSize.Y / 2);
//}
//else if (info.IsKeyReleased(Microsoft.Xna.Framework.Input.Keys.Add))
//{
// SelectedEditor.Surface.ResizeCells(SelectedEditor.Surface.CellSize.X * 2, SelectedEditor.Surface.CellSize.Y * 2);
//}
//else
{
// Look for tool hotkeys
if (ToolsPane.ProcessKeyboard(Global.KeyboardState))
{
return;
}
// Look for quick select F* keys
else if (QuickSelectPane.ProcessKeyboard(Global.KeyboardState))
{
return;
}
else if (ActiveEditor != null)
{
ActiveEditor.ProcessKeyboard(Global.KeyboardState);
}
}
}
}
}
}