-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFMain.pas
292 lines (245 loc) · 7.94 KB
/
FMain.pas
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
{$include lem_directives.inc}
unit FMain;
{-------------------------------------------------------------------------------
This is the main form which does almost nothing.
-------------------------------------------------------------------------------}
interface
uses
LemSystemMessages,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
FBaseDosForm,
LemGame, LemTypes,
AppController,
SharedGlobals;
type
TMainForm = class(TBaseDosForm)
procedure FormActivate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormResize(Sender: TObject);
procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer;
var Resize: Boolean);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer;
MousePos: TPoint; var Handled: Boolean);
procedure FormShow(Sender: TObject);
procedure CNKeyDown(var Message: TWMKeyDown); message CN_KEYDOWN;
private
Started: Boolean;
AppController: TAppController;
fChildForm: TForm;
procedure LMStart(var Msg: TMessage); message LM_START;
procedure LMNext(var Msg: TMessage); message LM_NEXT;
procedure LMExit(var Msg: TMessage); message LM_EXIT;
procedure OnMove(var Msg: TWMMove); message WM_MOVE;
procedure PlayGame;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property ChildForm: TForm read fChildForm write fChildForm;
procedure RestoreDefaultSize;
procedure RestoreDefaultPosition;
end;
var
MainForm: TMainForm;
implementation
uses
Math,
GameControl, GameBaseScreenCommon;
{$R *.dfm}
procedure TMainForm.CNKeyDown(var Message: TWMKeyDown);
var
AssignedEventHandler: TKeyEvent;
begin
AssignedEventHandler := OnKeyDown;
if Message.CharCode = vk_tab then
if Assigned(AssignedEventHandler) then
OnKeyDown(Self, Message.CharCode, KeyDataToShiftState(Message.KeyData));
inherited;
end;
procedure TMainForm.LMStart(var Msg: TMessage);
begin
PlayGame;
end;
procedure TMainForm.OnMove(var Msg: TWMMove);
begin
inherited;
if GameParams <> nil then
begin
GameParams.WindowLeft := Left;
GameParams.WindowTop := Top;
end;
end;
procedure TMainForm.LMNext(var Msg: TMessage);
begin
AppController.FreeScreen;
PlayGame;
end;
procedure TMainForm.LMExit(var Msg: TMessage);
begin
AppController.FreeScreen;
Close;
end;
constructor TMainForm.Create(aOwner: TComponent);
begin
inherited;
GlobalGame := TLemmingGame.Create(nil);
AppController := TAppController.Create(Self);
end;
destructor TMainForm.Destroy;
begin
GlobalGame.Free;
AppController.Free;
inherited;
end;
procedure TMainForm.PlayGame;
begin
try
if not AppController.Execute then
begin
Close;
end else if Assigned(ChildForm.OnActivate) then
ChildForm.OnActivate(ChildForm);
except
on E: Exception do
begin
Application.ShowException(E);
Close;
end;
end;
end;
procedure TMainForm.RestoreDefaultPosition;
begin
Left := (Screen.WorkAreaWidth div 2) - (Width div 2);
Top := (Screen.WorkAreaHeight div 2) - (Height div 2);
end;
procedure TMainForm.RestoreDefaultSize;
var
WindowScale: Integer;
begin
GameParams.ZoomLevel := Max(Max((Screen.Width - 100) div 444 div ResMod,
(Screen.Height - 100) div 200 div ResMod), 1);
WindowScale := Min(Screen.Width div 444, Screen.Height div 200);
WindowScale := Min(WindowScale, GameParams.ZoomLevel * ResMod);
if WindowScale < ResMod then
WindowScale := ResMod;
if GameParams.ShowMinimap then
begin
ClientWidth := Max(WindowScale * 444, 444 * ResMod); // 1776
ClientHeight := ClientWidth * 50 div 111; // 800
end else begin
ClientWidth := Max(WindowScale * 340, 340 * ResMod); // 1360
ClientHeight := ClientWidth * 401 div 680; // 802
end;
end;
procedure TMainForm.FormActivate(Sender: TObject);
begin
if Started then
Exit;
Started := True;
MainFormHandle := Handle;
PostMessage(Handle, LM_START, 0, 0);
end;
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if fChildForm = nil then Exit;
if not Assigned(fChildForm.OnKeyDown) then Exit;
fChildForm.OnKeyDown(Sender, Key, Shift);
end;
procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if fChildForm = nil then Exit;
if not Assigned(fChildForm.OnKeyUp) then Exit;
fChildForm.OnKeyUp(Sender, Key, Shift);
end;
procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if fChildForm = nil then Exit;
if not Assigned(fChildForm.OnMouseDown) then Exit;
fChildForm.OnMouseDown(Sender, Button, Shift, X, Y);
end;
procedure TMainForm.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if fChildForm = nil then Exit;
if not Assigned(fChildForm.OnMouseUp) then Exit;
fChildForm.OnMouseUp(Sender, Button, Shift, X, Y);
end;
procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if fChildForm = nil then Exit;
if not Assigned(fChildForm.OnMouseMove) then Exit;
fChildForm.OnMouseMove(Sender, Shift, X, Y);
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
inherited;
// Unless fullscreen, resize the main window
if not GameParams.FullScreen then
begin
GameParams.MainForm.BorderStyle := bsSizeable;
GameParams.MainForm.WindowState := wsNormal;
GameParams.MainForm.Left := GameParams.LoadedWindowLeft;
GameParams.MainForm.Top := GameParams.LoadedWindowTop;
GameParams.MainForm.ClientWidth := GameParams.LoadedWindowWidth;
GameParams.MainForm.ClientHeight := GameParams.LoadedWindowHeight;
end else begin
GameParams.MainForm.Left := 0;
GameParams.MainForm.Top := 0;
GameParams.MainForm.BorderStyle := bsNone;
GameParams.MainForm.WindowState := wsMaximized;
GameParams.MainForm.ClientWidth := Screen.Width;
GameParams.MainForm.ClientHeight := Screen.Height;
end;
end;
procedure TMainForm.FormResize(Sender: TObject);
begin
if GameParams = nil then Exit;
if not (fChildForm is TGameBaseScreen) then Exit;
TGameBaseScreen(fChildForm).MainFormResized;
GameParams.WindowWidth := ClientWidth;
GameParams.WindowHeight := ClientHeight;
{ Seems pointless? Yes. But apparently, changing between maximized and not maximized
causes the handle to change, which was causing the fullscreen-windowed change glitch. }
MainFormHandle := Handle;
end;
procedure TMainForm.FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean);
var
CWDiff, CHDiff: Integer;
NewCW, NewCH: Integer;
begin
if GameParams = nil then Exit;
if GameParams.FullScreen then
begin
NewWidth := Screen.Width;
NewHeight := Screen.Height;
Exit;
end;
CWDiff := Width - ClientWidth;
CHDiff := Height - ClientHeight;
NewCW := NewWidth - CWDiff;
NewCH := NewHeight - CHDiff;
// Apply minimum dimensions
NewCW := Max(GameParams.MinimumWindowWidth, NewCW);
NewCH := Max(GameParams.MinimumWindowHeight, NewCH);
// Apply maximum dimensions
NewCW := Min(NewCW, Screen.Width - CWDiff);
NewCH := Min(NewCH, Screen.Height - CHDiff);
// Update the new width and height
NewWidth := NewCW + CWDiff;
NewHeight := NewCH + CHDiff;
end;
procedure TMainForm.FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
if Assigned(fChildForm.OnMouseWheel) then
fChildForm.OnMouseWheel(Sender, Shift, WheelDelta, MousePos, Handled);
end;
end.