forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.cs
executable file
·213 lines (174 loc) · 5.61 KB
/
MainWindow.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
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using OpenRa.FileFormats;
using OpenRa.GameRules;
using OpenRa.Graphics;
using OpenRa.Orders;
namespace OpenRa
{
class MainWindow : Form
{
readonly Renderer renderer;
static Size GetResolution(Settings settings)
{
var desktopResolution = Screen.PrimaryScreen.Bounds.Size;
if (Game.Settings.Width > 0 && Game.Settings.Height > 0)
{
desktopResolution.Width = Game.Settings.Width;
desktopResolution.Height = Game.Settings.Height;
}
return new Size(
desktopResolution.Width,
desktopResolution.Height);
}
[DllImport("user32")]
static extern int ShowCursor([MarshalAs(UnmanagedType.Bool)] bool visible);
public MainWindow(Settings settings)
{
Icon = Resources1.OpenRA;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.Black;
StartPosition = FormStartPosition.Manual;
Location = Point.Empty;
Visible = true;
while (!File.Exists("redalert.mix"))
{
var current = Directory.GetCurrentDirectory();
if (Directory.GetDirectoryRoot(current) == current)
throw new InvalidOperationException("Unable to load MIX files.");
Directory.SetCurrentDirectory("..");
}
LoadUserSettings(settings);
UiOverlay.ShowUnitDebug = Game.Settings.UnitDebug;
UiOverlay.ShowBuildDebug = Game.Settings.BuildingDebug;
WorldRenderer.ShowUnitPaths = Game.Settings.PathDebug;
Renderer.SheetSize = Game.Settings.SheetSize;
FileSystem.MountDefaultPackages();
if (Game.Settings.UseAftermath)
FileSystem.MountAftermathPackages();
bool windowed = !Game.Settings.Fullscreen;
renderer = new Renderer(this, GetResolution(settings), windowed);
var controller = new Controller(() => (Modifiers)(int)ModifierKeys); /* a bit of insane input routing */
Game.Initialize(Game.Settings.Map, renderer, new int2(ClientSize),
Game.Settings.Player, Game.Settings.UseAftermath, controller);
ShowCursor(false);
Game.ResetTimer();
}
static void LoadUserSettings(Settings settings)
{
Game.Settings = new UserSettings();
var settingsFile = settings.GetValue("settings", "settings.ini");
FileSystem.MountTemporary(new Folder("./"));
if (FileSystem.Exists(settingsFile))
FieldLoader.Load(Game.Settings,
new IniFile(FileSystem.Open(settingsFile)).GetSection("Settings"));
FileSystem.UnmountTemporaryPackages();
}
internal void Run()
{
while (Created && Visible)
{
Game.Tick();
Application.DoEvents();
}
}
int2 lastPos;
void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e)
{
int sync = Game.world.SyncHash();
Game.viewport.DispatchMouseInput(
new MouseInput
{
Button = (MouseButton)(int)e.Button,
Event = ev,
Location = new int2(e.Location),
Modifiers = (Modifiers)(int)ModifierKeys,
});
if( sync != Game.world.SyncHash() )
throw new InvalidOperationException( "Desync in DispatchMouseInput" );
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
lastPos = new int2(e.Location);
DispatchMouseInput(MouseInputEvent.Down, e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Middle || e.Button == (MouseButtons.Left | MouseButtons.Right))
{
int2 p = new int2(e.Location);
Game.viewport.Scroll(lastPos - p);
lastPos = p;
}
DispatchMouseInput(MouseInputEvent.Move, e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
DispatchMouseInput(MouseInputEvent.Up, e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
int sync = Game.world.SyncHash();
/* hack hack hack */
if (e.KeyCode == Keys.F8 && !Game.orderManager.GameStarted)
{
Game.controller.AddOrder(
new Order( "ToggleReady", Game.LocalPlayer.PlayerActor, null, int2.Zero, "") { IsImmediate = true });
}
/* temporary hack: DO NOT LEAVE IN */
if (e.KeyCode == Keys.F2)
Game.LocalPlayer = Game.players[(Game.LocalPlayer.Index + 1) % 4];
if (e.KeyCode == Keys.F3)
Game.controller.orderGenerator = new SellOrderGenerator();
if (e.KeyCode == Keys.F4)
Game.controller.orderGenerator = new RepairOrderGenerator();
if (!Game.chat.isChatting)
if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
Game.controller.DoControlGroup( (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers );
if( sync != Game.world.SyncHash() )
throw new InvalidOperationException( "Desync in OnKeyDown" );
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
int sync = Game.world.SyncHash();
if (e.KeyChar == '\r')
Game.chat.Toggle();
else if (Game.chat.isChatting)
Game.chat.TypeChar(e.KeyChar);
if( sync != Game.world.SyncHash() )
throw new InvalidOperationException( "Desync in OnKeyPress" );
}
}
[Flags]
public enum MouseButton
{
None = (int)MouseButtons.None,
Left = (int)MouseButtons.Left,
Right = (int)MouseButtons.Right,
Middle = (int)MouseButtons.Middle,
}
[Flags]
public enum Modifiers
{
None = (int)Keys.None,
Shift = (int)Keys.Shift,
Alt = (int)Keys.Alt,
Ctrl = (int)Keys.Control,
}
public struct MouseInput
{
public MouseInputEvent Event;
public int2 Location;
public MouseButton Button;
public Modifiers Modifiers;
}
public enum MouseInputEvent { Down, Move, Up };
}