source: trunk/Forms/FormMain.pas

Last change on this file was 107, checked in by chronos, 4 months ago
  • Fixed: Full screen mode switching on Windows.
  • Modified: Updated Common package.
File size: 7.4 KB
Line 
1unit FormMain;
2
3interface
4
5uses
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
11type
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
64var
65 FormMain: TFormMain;
66
67
68implementation
69
70{$R *.lfm}
71
72uses
73 Core;
74
75resourcestring
76 SRound = '%0:s';
77
78{ TFormMain }
79
80procedure TFormMain.TimerDrawTimer(Sender: TObject);
81begin
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;
106end;
107
108procedure TFormMain.LoadConfig;
109var
110 LangCode: string;
111begin
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;
124end;
125
126procedure TFormMain.SaveConfig;
127begin
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;
139end;
140
141procedure TFormMain.UpdateInterface;
142begin
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}
157end;
158
159procedure TFormMain.EraseBackground(DC: HDC);
160begin
161 // Do nothing
162end;
163
164procedure TFormMain.FormCreate(Sender: TObject);
165{$IFDEF UNIX}
166const
167 UnixLanguagesDir = '/usr/share/Tunneler/Languages';
168{$ENDIF}
169begin
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;
192end;
193
194procedure TFormMain.FormDeactivate(Sender: TObject);
195begin
196 if Assigned(Engine) then Engine.KeyBoard.ClearAll;
197end;
198
199procedure TFormMain.FormDestroy(Sender: TObject);
200begin
201 SaveConfig;
202 TimerDraw.Enabled := False;
203 FreeAndNil(PersistentForm);
204 FreeAndNil(Engine);
205end;
206
207procedure TFormMain.AFullScreenExecute(Sender: TObject);
208begin
209 FullScreen := not FullScreen;
210 TFormEx.PersistentForm.Load(Self);
211 TFormEx.PersistentForm.SetFullScreen(FullScreen);
212 UpdateInterface;
213end;
214
215procedure TFormMain.ANewGameExecute(Sender: TObject);
216begin
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;
227end;
228
229procedure TFormMain.AShowMapExecute(Sender: TObject);
230begin
231 if not Assigned(FormMap) then FormMap := TFormMap.Create(Self);
232 FormMap.Show;
233end;
234
235procedure TFormMain.AShowRawImageDescExecute(Sender: TObject);
236begin
237 ShowMessage(Image1.Picture.Bitmap.RawImage.Description.AsString);
238end;
239
240procedure TFormMain.AExitExecute(Sender: TObject);
241begin
242 Close;
243end;
244
245procedure TFormMain.AAboutExecute(Sender: TObject);
246var
247 FormAbout: TFormAbout;
248begin
249 try
250 FormAbout := TFormAbout.Create(nil);
251 FormAbout.ApplicationInfo := Core.Core.ApplicationInfo;
252 FormAbout.ShowModal;
253 finally
254 FreeAndNil(FormAbout);
255 end;
256end;
257
258procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
259 Shift: TShiftState);
260begin
261 Engine.KeyDown(Key);
262 StatusBar1.Panels[0].Text := IntToStr(Key);
263end;
264
265procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
266 );
267begin
268 Engine.KeyUp(Key);
269end;
270
271procedure TFormMain.FormShow(Sender: TObject);
272begin
273 UpdateInterface;
274end;
275
276procedure TFormMain.Image1Resize(Sender: TObject);
277begin
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;
285end;
286
287end.
288
Note: See TracBrowser for help on using the repository browser.