1 | unit FormMain;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, LCLType, FormEx,
|
---|
7 | Dialogs, ExtCtrls, ComCtrls, Menus, ActnList, Engine, Platform, Math,
|
---|
8 | DateUtils, GraphType, PersistentForm, ApplicationInfo, Translator,
|
---|
9 | RegistryEx, FormAbout, FormNewGame, FormMap;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormMain }
|
---|
14 |
|
---|
15 | TFormMain = class(TFormEx)
|
---|
16 | AAbout: TAction;
|
---|
17 | AShowRawImageDesc: TAction;
|
---|
18 | AShowMap: TAction;
|
---|
19 | ANewGame: TAction;
|
---|
20 | AExit: TAction;
|
---|
21 | AFullScreen: TAction;
|
---|
22 | ActionList1: TActionList;
|
---|
23 | Image1: TImage;
|
---|
24 | MainMenu1: TMainMenu;
|
---|
25 | MenuItem1: TMenuItem;
|
---|
26 | MenuItem2: TMenuItem;
|
---|
27 | MenuItem3: TMenuItem;
|
---|
28 | MenuItem4: TMenuItem;
|
---|
29 | MenuItem5: TMenuItem;
|
---|
30 | MenuItem6: TMenuItem;
|
---|
31 | MenuItem7: TMenuItem;
|
---|
32 | MenuItem8: TMenuItem;
|
---|
33 | MenuItem9: TMenuItem;
|
---|
34 | StatusBar1: TStatusBar;
|
---|
35 | TimerDraw: TTimer;
|
---|
36 | TimerEngineTick: TTimer;
|
---|
37 | procedure AAboutExecute(Sender: TObject);
|
---|
38 | procedure AExitExecute(Sender: TObject);
|
---|
39 | procedure AFullScreenExecute(Sender: TObject);
|
---|
40 | procedure ANewGameExecute(Sender: TObject);
|
---|
41 | procedure AShowMapExecute(Sender: TObject);
|
---|
42 | procedure AShowRawImageDescExecute(Sender: TObject);
|
---|
43 | procedure FormCreate(Sender: TObject);
|
---|
44 | procedure FormDestroy(Sender: TObject);
|
---|
45 | procedure FormDeactivate(Sender: TObject);
|
---|
46 | procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
|
---|
47 | procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
|
---|
48 | procedure FormShow(Sender: TObject);
|
---|
49 | procedure Image1Resize(Sender: TObject);
|
---|
50 | procedure TimerDrawTimer(Sender: TObject);
|
---|
51 | private
|
---|
52 | StartTime: TDateTime;
|
---|
53 | Drawing: Boolean;
|
---|
54 | procedure LoadConfig;
|
---|
55 | procedure SaveConfig;
|
---|
56 | procedure UpdateInterface;
|
---|
57 | public
|
---|
58 | Engine: TEngine;
|
---|
59 | FormNewGame: TFormNewGame;
|
---|
60 | FormMap: TFormMap;
|
---|
61 | procedure EraseBackground(DC: HDC); override;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | var
|
---|
65 | FormMain: TFormMain;
|
---|
66 |
|
---|
67 |
|
---|
68 | implementation
|
---|
69 |
|
---|
70 | {$R *.lfm}
|
---|
71 |
|
---|
72 | uses
|
---|
73 | Core;
|
---|
74 |
|
---|
75 | resourcestring
|
---|
76 | SRound = '%0:s';
|
---|
77 |
|
---|
78 | { TFormMain }
|
---|
79 |
|
---|
80 | procedure TFormMain.TimerDrawTimer(Sender: TObject);
|
---|
81 | begin
|
---|
82 | if not Drawing then
|
---|
83 | try
|
---|
84 | Drawing := True;
|
---|
85 | StartTime := NowPrecise;
|
---|
86 | //Engine.Draw;
|
---|
87 | if Assigned(Engine) then begin
|
---|
88 | Engine.Lock.Acquire;
|
---|
89 | try
|
---|
90 | //StatusBar1.Panels[1].Text := IntToStr(TPlayer(Engine.Players[0]).Position.X) + ', ' +
|
---|
91 | // IntToStr(TPlayer(Engine.Players[0]).Position.Y) + ' ' +
|
---|
92 | // IntToStr(TPlayer(Engine.Players[0]).Direction);
|
---|
93 | StatusBar1.Panels[2].Text := FloatToStr(RoundTo(Engine.DrawDuration / OneMillisecond, -2));
|
---|
94 | StatusBar1.Panels[3].Text := Format(SRound, [IntToStr(Engine.CurrentRound)]);
|
---|
95 | finally
|
---|
96 | Engine.Lock.Release;
|
---|
97 | end;
|
---|
98 | end;
|
---|
99 | finally
|
---|
100 | Drawing := False;
|
---|
101 | end;
|
---|
102 | if Engine.ThreadErrorMessage <> '' then begin
|
---|
103 | ShowMessage(Engine.ThreadErrorMessage);
|
---|
104 | Engine.ThreadErrorMessage := '';
|
---|
105 | end;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TFormMain.LoadConfig;
|
---|
109 | var
|
---|
110 | LangCode: string;
|
---|
111 | begin
|
---|
112 | with TRegistryEx.Create do
|
---|
113 | try
|
---|
114 | CurrentContext := Core.Core.ApplicationInfo.GetRegistryContext;
|
---|
115 |
|
---|
116 | if ValueExists('LanguageCode') then begin
|
---|
117 | LangCode := ReadStringWithDefault('LanguageCode', '');
|
---|
118 | Core.Core.Translator1.Language := Core.Core.Translator1.Languages.SearchByCode(LangCode);
|
---|
119 | end else Core.Core.Translator1.Language := Core.Core.Translator1.Languages.SearchByCode('');
|
---|
120 | Engine.LoadFromRegistry(CurrentContext);
|
---|
121 | finally
|
---|
122 | Free;
|
---|
123 | end;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TFormMain.SaveConfig;
|
---|
127 | begin
|
---|
128 | with TRegistryEx.Create do
|
---|
129 | try
|
---|
130 | CurrentContext := Core.Core.ApplicationInfo.GetRegistryContext;
|
---|
131 |
|
---|
132 | if Assigned(Core.Core.Translator1.Language) and (Core.Core.Translator1.Language.Code <> '') then
|
---|
133 | WriteString('LanguageCode', Core.Core.Translator1.Language.Code)
|
---|
134 | else DeleteValue('LanguageCode');
|
---|
135 | Engine.SaveToRegistry(CurrentContext);
|
---|
136 | finally
|
---|
137 | Free;
|
---|
138 | end;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TFormMain.UpdateInterface;
|
---|
142 | begin
|
---|
143 | {$IFDEF LCLQT5}
|
---|
144 | MenuItem1.Visible := not FullScreen;
|
---|
145 | MenuItem4.Visible := not FullScreen;
|
---|
146 | MenuItem7.Visible := not FullScreen;
|
---|
147 | {$ELSE}
|
---|
148 | if FullScreen then Menu := nil
|
---|
149 | else Menu := MainMenu1;
|
---|
150 | {$ENDIF}
|
---|
151 | {$IFDEF DEBUG}
|
---|
152 | StatusBar1.Visible := not FullScreen;
|
---|
153 | AShowMap.Visible := True;
|
---|
154 | ANewGame.Visible := True;
|
---|
155 | AShowRawImageDesc.Visible := True;
|
---|
156 | {$ENDIF}
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TFormMain.EraseBackground(DC: HDC);
|
---|
160 | begin
|
---|
161 | // Do nothing
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TFormMain.FormCreate(Sender: TObject);
|
---|
165 | {$IFDEF UNIX}
|
---|
166 | const
|
---|
167 | UnixLanguagesDir = '/usr/share/Tunneler/Languages';
|
---|
168 | {$ENDIF}
|
---|
169 | begin
|
---|
170 | {$IFDEF UNIX}
|
---|
171 | // If installed in UNIX system then use installation directory for po files
|
---|
172 | if not DirectoryExists(Core.Core.Translator1.POFilesFolder) and DirectoryExists(UnixLanguagesDir) then
|
---|
173 | Core.Core.Translator1.POFilesFolder := UnixLanguagesDir;
|
---|
174 | {$ENDIF}
|
---|
175 |
|
---|
176 | Image1.ControlStyle := Image1.ControlStyle + [csOpaque];
|
---|
177 | FullScreen := True;
|
---|
178 |
|
---|
179 | Application.OnDeactivate := FormDeactivate;
|
---|
180 | Randomize;
|
---|
181 | DoubleBuffered := False;
|
---|
182 | Engine := TEngine.Create(nil);
|
---|
183 | Engine.OnClose := AExitExecute;
|
---|
184 | Engine.InitPlayerPool;
|
---|
185 | Engine.HomePage := Core.Core.ApplicationInfo.HomePage;
|
---|
186 | LoadConfig;
|
---|
187 | Engine.Translate;
|
---|
188 | Engine.Bitmap := Image1.Picture.Bitmap;
|
---|
189 | Engine.NewGame;
|
---|
190 | Image1Resize(Self);
|
---|
191 | Engine.Active := True;
|
---|
192 | end;
|
---|
193 |
|
---|
194 | procedure TFormMain.FormDeactivate(Sender: TObject);
|
---|
195 | begin
|
---|
196 | if Assigned(Engine) then Engine.KeyBoard.ClearAll;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TFormMain.FormDestroy(Sender: TObject);
|
---|
200 | begin
|
---|
201 | SaveConfig;
|
---|
202 | TimerDraw.Enabled := False;
|
---|
203 | FreeAndNil(PersistentForm);
|
---|
204 | FreeAndNil(Engine);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TFormMain.AFullScreenExecute(Sender: TObject);
|
---|
208 | begin
|
---|
209 | FullScreen := not FullScreen;
|
---|
210 | TFormEx.PersistentForm.Load(Self);
|
---|
211 | TFormEx.PersistentForm.SetFullScreen(FullScreen);
|
---|
212 | UpdateInterface;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | procedure TFormMain.ANewGameExecute(Sender: TObject);
|
---|
216 | begin
|
---|
217 | FormNewGame := TFormNewGame.Create(nil);
|
---|
218 | try
|
---|
219 | FormNewGame.LoadData(Engine);
|
---|
220 | if FormNewGame.ShowModal = mrOk then begin
|
---|
221 | FormNewGame.SaveData(Engine);
|
---|
222 | Engine.NewGame;
|
---|
223 | end;
|
---|
224 | finally
|
---|
225 | FreeAndNil(FormNewGame);
|
---|
226 | end;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | procedure TFormMain.AShowMapExecute(Sender: TObject);
|
---|
230 | begin
|
---|
231 | if not Assigned(FormMap) then FormMap := TFormMap.Create(Self);
|
---|
232 | FormMap.Show;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | procedure TFormMain.AShowRawImageDescExecute(Sender: TObject);
|
---|
236 | begin
|
---|
237 | ShowMessage(Image1.Picture.Bitmap.RawImage.Description.AsString);
|
---|
238 | end;
|
---|
239 |
|
---|
240 | procedure TFormMain.AExitExecute(Sender: TObject);
|
---|
241 | begin
|
---|
242 | Close;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TFormMain.AAboutExecute(Sender: TObject);
|
---|
246 | var
|
---|
247 | FormAbout: TFormAbout;
|
---|
248 | begin
|
---|
249 | try
|
---|
250 | FormAbout := TFormAbout.Create(nil);
|
---|
251 | FormAbout.ApplicationInfo := Core.Core.ApplicationInfo;
|
---|
252 | FormAbout.ShowModal;
|
---|
253 | finally
|
---|
254 | FreeAndNil(FormAbout);
|
---|
255 | end;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
|
---|
259 | Shift: TShiftState);
|
---|
260 | begin
|
---|
261 | Engine.KeyDown(Key);
|
---|
262 | StatusBar1.Panels[0].Text := IntToStr(Key);
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
|
---|
266 | );
|
---|
267 | begin
|
---|
268 | Engine.KeyUp(Key);
|
---|
269 | end;
|
---|
270 |
|
---|
271 | procedure TFormMain.FormShow(Sender: TObject);
|
---|
272 | begin
|
---|
273 | UpdateInterface;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | procedure TFormMain.Image1Resize(Sender: TObject);
|
---|
277 | begin
|
---|
278 | Engine.Lock.Acquire;
|
---|
279 | try
|
---|
280 | Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height);
|
---|
281 | Engine.ResizePlayerFrames;
|
---|
282 | finally
|
---|
283 | Engine.Lock.Release;
|
---|
284 | end;
|
---|
285 | end;
|
---|
286 |
|
---|
287 | end.
|
---|
288 |
|
---|