-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindowForm.cs
More file actions
358 lines (296 loc) · 10.1 KB
/
MainWindowForm.cs
File metadata and controls
358 lines (296 loc) · 10.1 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
using WeifenLuo.WinFormsUI.Docking;
using ReaLTaiizor.Controls;
using System.ComponentModel;
namespace SwimEditor
{
public partial class MainWindowForm : Form
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public static MainWindowForm? Instance { get; private set; }
private DockPanel dockPanel;
private ThemeBase theme;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HierarchyDock Hierarchy { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public InspectorDock Inspector { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GameViewDock GameView { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public UtilityDock Console { get; private set; }
private string layoutPath;
// transport buttons (stored as fields so we can update state/colors)
private CrownButton playButton;
private CrownButton editButton;
private CrownButton pauseButton;
private CrownButton stopButton;
public MainWindowForm()
{
Instance = this;
InitializeComponent();
InitializeDockingUi();
CreateAndShowPanes();
}
// TODO: top bar for like file, settings, etc in top left corner
private void InitializeDockingUi()
{
Text = "Swim Engine Editor v1.0";
WindowState = FormWindowState.Maximized;
IsMdiContainer = true;
theme = new VS2015DarkTheme();
// Non-selectable host bar (no Crown header/blue line)
var topBar = new System.Windows.Forms.Panel
{
Dock = DockStyle.Top,
Height = 44,
BackColor = SwimEditorTheme.Bg,
Margin = Padding.Empty,
Padding = Padding.Empty,
TabStop = false
};
// Centered row of buttons
var centerRow = new System.Windows.Forms.FlowLayoutPanel
{
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
WrapContents = false,
Margin = Padding.Empty,
Padding = Padding.Empty,
BackColor = Color.Transparent
};
CrownButton MakeBtn(string text)
{
var btn = new CrownButton
{
Text = text,
AutoSize = false,
Size = new Size(96, 28),
Margin = new Padding(8, 8, 8, 8),
Padding = new Padding(10, 2, 10, 2),
BackColor = SwimEditorTheme.Bg,
ForeColor = SwimEditorTheme.Text,
TabStop = false // prevents focus and thus avoids blue outline
};
// Disable any remaining focus/hover highlight
btn.GotFocus += (s, e) => btn.TabStop = false;
return btn;
}
playButton = MakeBtn("Play");
editButton = MakeBtn("Edit");
pauseButton = MakeBtn("Pause");
stopButton = MakeBtn("Stop");
// subtle hover cue
void AccentOn(object? s, EventArgs e) => ((CrownButton)s!).ForeColor = SwimEditorTheme.Accent;
void AccentOff(object? s, EventArgs e) => ((CrownButton)s!).ForeColor = SwimEditorTheme.Text;
foreach (var b in new[] { playButton, editButton, pauseButton, stopButton })
{
b.MouseEnter += AccentOn;
b.MouseLeave += AccentOff;
}
// wire clicks
playButton.Click += OnPlayClicked;
editButton.Click += OnEditClicked;
pauseButton.Click += OnPauseClicked;
stopButton.Click += OnStopClicked;
centerRow.Controls.Add(playButton);
centerRow.Controls.Add(editButton);
centerRow.Controls.Add(pauseButton);
centerRow.Controls.Add(stopButton);
// center the row within the bar
void LayoutCenter()
{
var pref = centerRow.PreferredSize;
int x = Math.Max(0, (topBar.ClientSize.Width - pref.Width) / 2);
int y = Math.Max(0, (topBar.ClientSize.Height - pref.Height) / 2);
centerRow.Location = new Point(x, y);
}
topBar.Controls.Add(centerRow);
topBar.Resize += (s, e) => LayoutCenter();
LayoutCenter();
// DockPanel workspace
dockPanel = new DockPanel
{
Dock = DockStyle.Fill,
Theme = theme,
DocumentStyle = DocumentStyle.DockingMdi,
BackColor = SwimEditorTheme.Bg
};
Controls.Add(dockPanel);
Controls.Add(topBar);
string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
layoutPath = Path.Combine(appData, "SwimEditor", "layout.xml");
Directory.CreateDirectory(Path.GetDirectoryName(layoutPath)!);
}
// TODO: serialize each panels width and height on last program close and their position and dock state
private void CreateAndShowPanes()
{
Hierarchy = new HierarchyDock { Text = "Hierarchy" };
Inspector = new InspectorDock { Text = "Inspector" };
GameView = new GameViewDock { Text = "Game View" };
Console = new UtilityDock { Text = "Utility" };
GameView.Show(dockPanel, DockState.Document);
Hierarchy.Show(dockPanel, DockState.DockLeft);
Inspector.Show(dockPanel, DockState.DockRight);
// Give the bottom more verticality before showing console
dockPanel.DockBottomPortion = 300d; // or 0.35;
Console.Show(dockPanel, DockState.DockBottom);
Console.AppendLog("Swim Engine Editor v1.0");
// Make it so game view cout stream callback logs to the console
GameView.EngineConsoleLine += line => Console.AppendLog(line);
GameView.RawEngineMessage += line => Hierarchy.Command(line);
// keep inspector synced:
// - Hierarchy raises with the selected CrownTreeNode
// - We send the *Tag* (SceneEntity or SceneComponent) to the inspector
Hierarchy.OnSelectionChanged += obj =>
{
if (obj is CrownTreeNode node)
{
// Prefer inspecting the underlying data model; fall back to the node itself
Inspector.SetInspectedObject(node.Tag ?? node);
}
else
{
Inspector.SetInspectedObject(obj);
}
};
// Initial toolbar: assume the GameView will auto-start the engine on Shown.
// We want Play disabled, Pause/Stop enabled right away.
playButton.Enabled = false;
pauseButton.Enabled = true;
stopButton.Enabled = true;
SetActive(playButton, false);
SetActive(pauseButton, false);
SetActive(stopButton, false);
// When the engine actually starts/stops/pauses, keep the toolbar in sync.
GameView.EngineStateChanged += () =>
{
// marshal UI update
if (InvokeRequired) BeginInvoke(new Action(UpdateTransportUi));
else UpdateTransportUi();
};
// Only load layout if it exists; it will override sizes
LoadLayoutIfExists();
// Final pass (in case layout changes visibility/creation timing)
// Schedule after idle to catch GameView’s Shown/start.
BeginInvoke(new Action(UpdateTransportUi));
}
private void LoadLayoutIfExists()
{
if (File.Exists(layoutPath))
{
try
{
dockPanel.LoadFromXml(layoutPath, DeserializeDockContent);
}
catch
{
// Ignore layout errors
}
}
}
private IDockContent DeserializeDockContent(string persistString)
{
if (persistString == typeof(HierarchyDock).FullName)
return Hierarchy;
if (persistString == typeof(InspectorDock).FullName)
return Inspector;
if (persistString == typeof(GameViewDock).FullName)
return GameView;
if (persistString == typeof(UtilityDock).FullName)
return Console;
return null;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
try
{
dockPanel.SaveAsXml(layoutPath);
}
catch
{
// Ignore save errors
}
}
// --- Transport logic wiring ---
private void OnPlayClicked(object? sender, EventArgs e)
{
if (GameView == null || !GameView.IsEngineRunning) return;
// gameView.PlayEngine(); // starts or resumes as needed
GameView.GoIntoPlayMode();
UpdateTransportUi();
}
private void OnEditClicked(object? sender, EventArgs e)
{
if (GameView == null || !GameView.IsEngineRunning) return;
if (GameView.IsEngineEditing)
{
GameView.GoIntoGameMode();
}
else
{
GameView.GoIntoEditMode();
}
UpdateTransportUi();
}
private void OnPauseClicked(object? sender, EventArgs e)
{
if (GameView == null || !GameView.IsEngineRunning) return;
if (!GameView.IsEnginePaused)
{
// gameView.PauseEngine();
GameView.GoIntoPauseMode();
}
else
{
// gameView.ResumeEngine();
GameView?.GoIntoResumedMode();
}
UpdateTransportUi();
}
private void OnStopClicked(object? sender, EventArgs e)
{
if (GameView == null || !GameView.IsEngineRunning) return;
// gameView.StopEngine();
GameView.GoIntoStoppedMode();
UpdateTransportUi();
}
private void UpdateTransportUi()
{
if (GameView == null)
{
return;
}
bool stopped = GameView.IsEngineStopped;
if (GameView.IsEnginePaused)
{
pauseButton.Text = "Resume";
}
else
{
pauseButton.Text = "Pause";
}
if (GameView.IsEngineEditing)
{
editButton.Text = "Game"; // TODO: better text
}
else
{
editButton.Text = "Edit";
}
// Enablement rules
playButton.Enabled = stopped; // can play if stopped
pauseButton.Enabled = !stopped;
stopButton.Enabled = !stopped;
// Visual state (simple color UX)
SetActive(playButton, stopped);
SetActive(pauseButton, !stopped);
SetActive(stopButton, !stopped);
}
private void SetActive(CrownButton btn, bool active)
{
// Accent background for active, keep others on Bg
btn.BackColor = active ? SwimEditorTheme.Accent : SwimEditorTheme.Bg;
btn.ForeColor = active ? Color.Black : SwimEditorTheme.Text;
}
} // class MainWindowForm
} // Namespace SwimEditor