1 | unit UCore;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UTheme, UPersistentForm, UApplicationInfo, UTranslator,
|
---|
9 | URegistry, UGame, ActnList, Forms, Controls, Dialogs;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TCore }
|
---|
14 |
|
---|
15 | TCore = class(TDataModule)
|
---|
16 | AAbout: TAction;
|
---|
17 | AComputer: TAction;
|
---|
18 | AHistory: TAction;
|
---|
19 | AHelp: TAction;
|
---|
20 | AUndo: TAction;
|
---|
21 | ASettings: TAction;
|
---|
22 | ActionList1: TActionList;
|
---|
23 | AExit: TAction;
|
---|
24 | ANew: TAction;
|
---|
25 | ApplicationInfo1: TApplicationInfo;
|
---|
26 | PersistentForm1: TPersistentForm;
|
---|
27 | ThemeManager1: TThemeManager;
|
---|
28 | Translator1: TTranslator;
|
---|
29 | procedure AAboutExecute(Sender: TObject);
|
---|
30 | procedure AComputerExecute(Sender: TObject);
|
---|
31 | procedure AExitExecute(Sender: TObject);
|
---|
32 | procedure AHelpExecute(Sender: TObject);
|
---|
33 | procedure AHistoryExecute(Sender: TObject);
|
---|
34 | procedure ANewExecute(Sender: TObject);
|
---|
35 | procedure ASettingsExecute(Sender: TObject);
|
---|
36 | procedure AUndoExecute(Sender: TObject);
|
---|
37 | procedure DataModuleCreate(Sender: TObject);
|
---|
38 | procedure DataModuleDestroy(Sender: TObject);
|
---|
39 | private
|
---|
40 | procedure GameChange(Sender: TObject);
|
---|
41 | procedure GameWin(Sender: TObject);
|
---|
42 | procedure GameOver(Sender: TObject);
|
---|
43 | public
|
---|
44 | Game: TGame;
|
---|
45 | procedure UpdateInterface;
|
---|
46 | procedure LoadConfig;
|
---|
47 | procedure SaveConfig;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | var
|
---|
51 | Core: TCore;
|
---|
52 |
|
---|
53 | implementation
|
---|
54 |
|
---|
55 | {$R *.lfm}
|
---|
56 |
|
---|
57 | uses
|
---|
58 | UFormMain, UFormSettings, UFormAbout, UFormNew, UFormHelp, UFormComputer,
|
---|
59 | UFormHistory;
|
---|
60 |
|
---|
61 | resourcestring
|
---|
62 | SGameOverCaption = 'Lost';
|
---|
63 | SGameOverMessage = 'Game over!';
|
---|
64 | SWinCaption = 'Win';
|
---|
65 | SWinMessage = 'You reached %d and won! You can continue to play to get higher score.';
|
---|
66 |
|
---|
67 | { TCore }
|
---|
68 |
|
---|
69 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
70 | begin
|
---|
71 | {$IFDEF Linux}
|
---|
72 | // If installed in Linux system then use installation directory for po files
|
---|
73 | if Application.ExeName = '/usr/bin/Game2048' then
|
---|
74 | Translator1.POFilesFolder := '/usr/share/Game2048/languages';
|
---|
75 | {$ENDIF}
|
---|
76 |
|
---|
77 | Randomize;
|
---|
78 | Game := TGame.Create;
|
---|
79 | Game.Board.Size := Point(4, 4);
|
---|
80 | Game.OnChange := GameChange;
|
---|
81 | Game.OnWin := GameWin;
|
---|
82 | Game.OnGameOver := GameOver;
|
---|
83 | LoadConfig;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TCore.ASettingsExecute(Sender: TObject);
|
---|
87 | begin
|
---|
88 | FormSettings := TFormSettings.Create(nil);
|
---|
89 | try
|
---|
90 | if FormSettings.ShowModal = mrOk then begin
|
---|
91 | FormMain.UpdateInterface;
|
---|
92 | UpdateInterface;
|
---|
93 | end;
|
---|
94 | finally
|
---|
95 | FreeAndNil(FormSettings);
|
---|
96 | end;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TCore.AUndoExecute(Sender: TObject);
|
---|
100 | begin
|
---|
101 | Game.Undo;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TCore.AAboutExecute(Sender: TObject);
|
---|
105 | begin
|
---|
106 | FormAbout := TFormAbout.Create(nil);
|
---|
107 | try
|
---|
108 | FormAbout.ApplicationInfo := Core.ApplicationInfo1;
|
---|
109 | FormAbout.ShowModal;
|
---|
110 | finally
|
---|
111 | FreeAndNil(FormAbout);
|
---|
112 | end;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TCore.AComputerExecute(Sender: TObject);
|
---|
116 | begin
|
---|
117 | FormComputer := TFormComputer.Create(nil);
|
---|
118 | try
|
---|
119 | FormComputer.ShowModal;
|
---|
120 | finally
|
---|
121 | FreeAndNil(FormComputer);
|
---|
122 | end;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TCore.AExitExecute(Sender: TObject);
|
---|
126 | begin
|
---|
127 | FormMain.Close;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TCore.AHelpExecute(Sender: TObject);
|
---|
131 | begin
|
---|
132 | FormHelp := TFormHelp.Create(nil);
|
---|
133 | try
|
---|
134 | FormHelp.ShowModal;
|
---|
135 | finally
|
---|
136 | FreeAndNil(FormHelp);
|
---|
137 | end;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TCore.AHistoryExecute(Sender: TObject);
|
---|
141 | begin
|
---|
142 | FormHistory := TFormHistory.Create(nil);
|
---|
143 | try
|
---|
144 | FormHistory.ShowModal;
|
---|
145 | finally
|
---|
146 | FreeAndNil(FormHistory);
|
---|
147 | end;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TCore.ANewExecute(Sender: TObject);
|
---|
151 | begin
|
---|
152 | FormNew := TFormNew.Create(nil);
|
---|
153 | try
|
---|
154 | FormNew.Load(Core.Game);
|
---|
155 | if FormNew.ShowModal = mrOk then begin
|
---|
156 | FormNew.Save(Core.Game);
|
---|
157 | Game.New;
|
---|
158 | UpdateInterface;
|
---|
159 | FormMain.UpdateInterface;
|
---|
160 | end;
|
---|
161 | finally
|
---|
162 | FreeAndNil(FormNew);
|
---|
163 | end;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
167 | begin
|
---|
168 | SaveConfig;
|
---|
169 | FreeAndNil(Game);
|
---|
170 | end;
|
---|
171 |
|
---|
172 | procedure TCore.GameChange(Sender: TObject);
|
---|
173 | begin
|
---|
174 | FormMain.Redraw;
|
---|
175 | UpdateInterface;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TCore.GameWin(Sender: TObject);
|
---|
179 | begin
|
---|
180 | MessageDlg(SWinCaption, Format(SWinMessage, [Game.WinScore]), mtInformation, [mbOk], 0);
|
---|
181 | end;
|
---|
182 |
|
---|
183 | procedure TCore.GameOver(Sender: TObject);
|
---|
184 | begin
|
---|
185 | MessageDlg(SGameOverCaption, SGameOverMessage, mtInformation, [mbOK], 0);
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TCore.UpdateInterface;
|
---|
189 | begin
|
---|
190 | AUndo.Enabled := Game.CanUndo;
|
---|
191 | AHistory.Enabled := Game.RecordHistory;
|
---|
192 | {$IFDEF DEBUG}
|
---|
193 | AComputer.Enabled := True;
|
---|
194 | AComputer.Visible := True;
|
---|
195 | {$ELSE}
|
---|
196 | AComputer.Enabled := False;
|
---|
197 | AComputer.Visible := False;
|
---|
198 | {$ENDIF}
|
---|
199 | end;
|
---|
200 |
|
---|
201 | procedure TCore.LoadConfig;
|
---|
202 | begin
|
---|
203 | with TRegistryEx.Create do
|
---|
204 | try
|
---|
205 | CurrentContext := ApplicationInfo1.GetRegistryContext;
|
---|
206 |
|
---|
207 | if ValueExists('LanguageCode') then
|
---|
208 | Translator1.Language := Translator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
|
---|
209 | else Translator1.Language := Translator1.Languages.SearchByCode('');
|
---|
210 | finally
|
---|
211 | Free;
|
---|
212 | end;
|
---|
213 | Game.LoadFromRegistry(ApplicationInfo1.GetRegistryContext);
|
---|
214 | end;
|
---|
215 |
|
---|
216 | procedure TCore.SaveConfig;
|
---|
217 | begin
|
---|
218 | with TRegistryEx.Create do
|
---|
219 | try
|
---|
220 | CurrentContext := ApplicationInfo1.GetRegistryContext;
|
---|
221 |
|
---|
222 | if Assigned(Translator1.Language) and (Translator1.Language.Code <> '') then
|
---|
223 | WriteString('LanguageCode', Translator1.Language.Code)
|
---|
224 | else DeleteValue('LanguageCode');
|
---|
225 | finally
|
---|
226 | Free;
|
---|
227 | end;
|
---|
228 | Game.SaveToRegistry(ApplicationInfo1.GetRegistryContext);
|
---|
229 | end;
|
---|
230 |
|
---|
231 | end.
|
---|
232 |
|
---|