source: tags/1.3.4/LocalPlayer/Enhance.pas

Last change on this file was 558, checked in by chronos, 7 months ago
  • Modified: Code cleanup.
File size: 11.6 KB
Line 
1{$INCLUDE Switches.inc}
2unit Enhance;
3
4interface
5
6uses
7 ScreenTools, BaseWin, Protocol, ClientTools, LCLIntf, LCLType, SysUtils,
8 Classes, IsoEngine, ButtonB, ButtonC,
9 {$IFDEF DPI}Dpi.Graphics, Dpi.Controls, Dpi.Forms, Dpi.Menus{$ELSE}
10 Graphics, Controls, Forms, Menus{$ENDIF};
11
12
13type
14
15 { TEnhanceDlg }
16
17 TEnhanceDlg = class(TFramedDlg)
18 MenuItem1: TMenuItem;
19 ToggleBtn: TButtonB;
20 CloseBtn: TButtonB;
21 job1: TButtonC;
22 job2: TButtonC;
23 job4: TButtonC;
24 job5: TButtonC;
25 job7: TButtonC;
26 job3: TButtonC;
27 job6: TButtonC;
28 job9: TButtonC;
29 Popup: TPopupMenu;
30 procedure FormCreate(Sender: TObject);
31 procedure FormDestroy(Sender: TObject);
32 procedure FormPaint(Sender: TObject);
33 procedure FormShow(Sender: TObject);
34 procedure CloseBtnClick(Sender: TObject);
35 procedure ToggleBtnClick(Sender: TObject);
36 procedure TerrClick(Sender: TObject);
37 procedure JobClick(Sender: TObject);
38 procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
39 private
40 NoMap: TIsoMap;
41 protected
42 Page: Integer;
43 procedure OffscreenPaint; override;
44 public
45 procedure ShowNewContent(NewMode: TWindowMode; TerrType: Integer = -1);
46 end;
47
48
49implementation
50
51uses
52 Help, KeyBindings, Term;
53
54{$R *.lfm}
55
56procedure TEnhanceDlg.FormCreate(Sender: TObject);
57var
58 TerrType: Integer;
59 M: TMenuItem;
60begin
61 inherited;
62 NoMap := TIsoMap.Create;
63 CaptionRight := CloseBtn.Left;
64 CaptionLeft := ToggleBtn.Left + ToggleBtn.Width;
65 InitButtons;
66 HelpContext := 'MACRO';
67 Caption := Phrases.Lookup('TITLE_ENHANCE');
68 ToggleBtn.Hint := Phrases.Lookup('BTN_SELECT');
69
70 for TerrType := fGrass to fMountains do
71 if TerrType <> fJungle then
72 begin
73 M := TMenuItem.Create(Popup);
74 M.RadioItem := True;
75 if TerrType = fGrass then
76 M.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
77 [Phrases.Lookup('TERRAIN', fGrass), Phrases.Lookup('TERRAIN',
78 fGrass + 12)])
79 else if TerrType = fForest then
80 M.Caption := Format(Phrases.Lookup('TWOTERRAINS'),
81 [Phrases.Lookup('TERRAIN', fForest), Phrases.Lookup('TERRAIN',
82 fJungle)])
83 else
84 M.Caption := Phrases.Lookup('TERRAIN', TerrType);
85 M.Tag := TerrType;
86 M.OnClick := TerrClick;
87 Popup.Items.Add(M);
88 end;
89end;
90
91procedure TEnhanceDlg.FormDestroy(Sender: TObject);
92begin
93 FreeAndNil(NoMap);
94end;
95
96procedure TEnhanceDlg.FormPaint(Sender: TObject);
97var
98 I: Integer;
99begin
100 inherited;
101 BtnFrame(Canvas, Rect(job1.Left, job1.Top, job7.Left + job7.Width,
102 job1.Top + job1.Height), MainTexture);
103 BtnFrame(Canvas, Rect(job3.Left, job3.Top, job9.Left + job9.Width,
104 job3.Top + job3.Height), MainTexture);
105 for I := 0 to ControlCount - 1 do
106 if Controls[I] is TButtonC then
107 BitBltCanvas(Canvas, Controls[I].Left + 2, Controls[I].Top - 11, 8, 8,
108 HGrSystem.Data.Canvas, 121 + Controls[I].Tag mod 7 * 9,
109 1 + Controls[I].Tag div 7 * 9);
110end;
111
112procedure TEnhanceDlg.FormShow(Sender: TObject);
113begin
114 OffscreenPaint;
115end;
116
117procedure TEnhanceDlg.ShowNewContent(NewMode: TWindowMode; TerrType: Integer);
118begin
119 if (TerrType < fGrass) or (TerrType > fMountains) then
120 Page := fGrass
121 else
122 Page := TerrType;
123 inherited ShowNewContent(NewMode);
124end;
125
126procedure TEnhanceDlg.OffscreenPaint;
127var
128 I, Stage, TerrType, TileImp, X, EndStage, Cost, LastJob: Integer;
129 S: string;
130 Done: set of jNone..jTrans;
131 TypeChanged: Boolean;
132begin
133 OffscreenUser := Self;
134 Offscreen.Canvas.Font.Assign(UniFont[ftSmall]);
135 FillOffscreen(0, 0, InnerWidth, InnerHeight);
136
137 EndStage := 0;
138 while (EndStage < 5) and (MyData.EnhancementJobs[Page, EndStage] <> jNone) do
139 Inc(EndStage);
140 with NoMap do
141 X := InnerWidth div 2 - xxt - (xxt + 3) * EndStage;
142
143 TerrType := Page;
144 TileImp := 0;
145 Done := [];
146 Cost := 0;
147 for Stage := 0 to EndStage do
148 begin
149 if Stage > 0 then
150 begin
151 Sprite(Offscreen, HGrSystem, X - 10, 66, 14, 14, 80, 1);
152 case MyData.EnhancementJobs[Page, Stage - 1] of
153 jRoad:
154 begin
155 Inc(Cost, Terrain[TerrType].MoveCost * RoadWork);
156 TileImp := TileImp or fRoad;
157 end;
158 jRR:
159 begin
160 Inc(Cost, Terrain[TerrType].MoveCost * RRWork);
161 TileImp := TileImp or fRR;
162 end;
163 jIrr:
164 begin
165 Inc(Cost, Terrain[TerrType].IrrClearWork);
166 TileImp := TileImp and not fTerImp or tiIrrigation;
167 end;
168 jFarm:
169 begin
170 Inc(Cost, Terrain[TerrType].IrrClearWork * FarmWork);
171 TileImp := TileImp and not fTerImp or tiFarm;
172 end;
173 jMine:
174 begin
175 Inc(Cost, Terrain[TerrType].MineAfforestWork);
176 TileImp := TileImp and not fTerImp or tiMine;
177 end;
178 jClear:
179 begin
180 Inc(Cost, Terrain[TerrType].IrrClearWork);
181 TerrType := Terrain[TerrType].ClearTerrain;
182 end;
183 jAfforest:
184 begin
185 Inc(Cost, Terrain[TerrType].MineAfforestWork);
186 TerrType := Terrain[TerrType].AfforestTerrain;
187 end;
188 jTrans:
189 begin
190 Inc(Cost, Terrain[TerrType].TransWork);
191 TerrType := Terrain[TerrType].TransTerrain;
192 end;
193 end;
194 Include(Done, MyData.EnhancementJobs[Page, Stage - 1]);
195 end;
196
197 with NoMap do begin
198 if TerrType < fForest then
199 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
200 1 + TerrType * (xxt * 2 + 1), 1 + yyt)
201 else
202 begin
203 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
204 1 + 2 * (xxt * 2 + 1), 1 + yyt + 2 * (yyt * 3 + 1));
205 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
206 1 + 7 * (xxt * 2 + 1), 1 + yyt + 2 * (2 + TerrType - fForest) *
207 (yyt * 3 + 1));
208 end;
209 if TileImp and fTerImp = tiFarm then
210 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
211 1 + (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1))
212 else if TileImp and fTerImp = tiIrrigation then
213 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2, 1,
214 1 + yyt + 12 * (yyt * 3 + 1));
215 if TileImp and fRR <> 0 then
216 begin
217 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
218 1 + 6 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
219 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
220 1 + 2 * (xxt * 2 + 1), 1 + yyt + 10 * (yyt * 3 + 1));
221 end
222 else if TileImp and fRoad <> 0 then
223 begin
224 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
225 1 + 6 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
226 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
227 1 + 2 * (xxt * 2 + 1), 1 + yyt + 9 * (yyt * 3 + 1));
228 end;
229 if TileImp and fTerImp = tiMine then
230 Sprite(Offscreen, HGrTerrain, X, 64 - yyt, xxt * 2, yyt * 2,
231 1 + 2 * (xxt * 2 + 1), 1 + yyt + 12 * (yyt * 3 + 1));
232 Inc(X, xxt * 2 + 6);
233 end;
234 end;
235
236 for I := 0 to Popup.Items.Count - 1 do
237 if Popup.Items[I].Tag = Page then
238 S := Popup.Items[I].Caption;
239 if Cost > 0 then
240 S := Format(Phrases.Lookup('ENHANCE'), [S, MovementToString(Cost)]);
241 LoweredTextOut(Offscreen.Canvas, -1, MainTexture,
242 (InnerWidth - BiColorTextWidth(Offscreen.Canvas, S)) div 2, 12, S);
243
244 if EndStage > 0 then
245 LastJob := MyData.EnhancementJobs[Page, EndStage - 1]
246 else
247 LastJob := jNone;
248 if jRoad in Done then
249 job1.ButtonIndex := 3
250 else
251 job1.ButtonIndex := 2;
252 if jRR in Done then
253 job2.ButtonIndex := 3
254 else
255 job2.ButtonIndex := 2;
256 if jIrr in Done then
257 job4.ButtonIndex := 3
258 else
259 job4.ButtonIndex := 2;
260 if jFarm in Done then
261 job5.ButtonIndex := 3
262 else
263 job5.ButtonIndex := 2;
264 if jMine in Done then
265 job7.ButtonIndex := 3
266 else
267 job7.ButtonIndex := 2;
268 if LastJob = jClear then
269 job3.ButtonIndex := 3
270 else
271 job3.ButtonIndex := 2;
272 if LastJob = jAfforest then
273 job6.ButtonIndex := 3
274 else
275 job6.ButtonIndex := 2;
276 if LastJob = jTrans then
277 job9.ButtonIndex := 3
278 else
279 job9.ButtonIndex := 2;
280
281 TypeChanged := LastJob in [jClear, jAfforest, jTrans];
282 job1.Visible := (jRoad in Done) or not TypeChanged;
283 job2.Visible := (jRR in Done) or not TypeChanged;
284 job4.Visible := (jIrr in Done) or not TypeChanged and
285 (Terrain[TerrType].IrrEff > 0);
286 job5.Visible := (jFarm in Done) or not TypeChanged and
287 (Terrain[TerrType].IrrEff > 0);
288 job7.Visible := (jMine in Done) or not TypeChanged and
289 (Terrain[TerrType].MineEff > 0);
290 job3.Visible := not TypeChanged and (Terrain[TerrType].ClearTerrain >= 0) and
291 ((TerrType <> fDesert) or (MyRO.Wonder[woGardens].EffectiveOwner = Me)) or
292 (LastJob = jClear);
293 job6.Visible := not TypeChanged and (Terrain[TerrType].AfforestTerrain >= 0)
294 or (LastJob = jAfforest);
295 job9.Visible := not TypeChanged and (Terrain[TerrType].TransTerrain >= 0) or
296 (LastJob = jTrans);
297
298 MarkUsedOffscreen(InnerWidth, InnerHeight);
299end;
300
301procedure TEnhanceDlg.CloseBtnClick(Sender: TObject);
302begin
303 Close;
304end;
305
306procedure TEnhanceDlg.ToggleBtnClick(Sender: TObject);
307var
308 I: Integer;
309begin
310 for I := 0 to Popup.Items.Count - 1 do
311 Popup.Items[I].Checked := Popup.Items[I].Tag = Page;
312 Popup.Popup(Left + ToggleBtn.Left, Top + ToggleBtn.Top + ToggleBtn.Height);
313end;
314
315procedure TEnhanceDlg.TerrClick(Sender: TObject);
316begin
317 Page := TComponent(Sender).Tag;
318 SmartUpdateContent;
319end;
320
321procedure TEnhanceDlg.JobClick(Sender: TObject);
322var
323 Stage, NewJob: Integer;
324 Done: Set of jNone .. jTrans;
325
326 procedure RemoveJob(J: Integer);
327 begin // remove job
328 Stage := 0;
329 while (Stage < 5) and (MyData.EnhancementJobs[Page, Stage] <> jNone) do
330 begin
331 if (MyData.EnhancementJobs[Page, Stage] = J) or (J = jRoad) and
332 (MyData.EnhancementJobs[Page, Stage] = jRR) or (J = jIrr) and
333 (MyData.EnhancementJobs[Page, Stage] = jFarm) then
334 begin
335 if Stage < 4 then
336 Move(MyData.EnhancementJobs[Page, Stage + 1],
337 MyData.EnhancementJobs[Page, Stage], 4 - Stage);
338 MyData.EnhancementJobs[Page, 4] := jNone;
339 end
340 else
341 Inc(Stage);
342 end;
343 end;
344
345begin
346 NewJob := TButtonC(Sender).Tag;
347 Done := [];
348 Stage := 0;
349 while (Stage < 5) and (MyData.EnhancementJobs[Page, Stage] <> jNone) do
350 begin
351 Include(Done, MyData.EnhancementJobs[Page, Stage]);
352 Inc(Stage);
353 end;
354 if NewJob in Done then
355 RemoveJob(NewJob)
356 else
357 begin // add job
358 if NewJob in [jMine, jAfforest] then
359 RemoveJob(jIrr);
360 if NewJob in [jIrr, jFarm, jTrans] then
361 RemoveJob(jMine);
362 if (NewJob = jRR) and not (jRoad in Done) then
363 begin
364 MyData.EnhancementJobs[Page, Stage] := jRoad;
365 Inc(Stage);
366 end;
367 if (NewJob = jFarm) and not (jIrr in Done) then
368 begin
369 MyData.EnhancementJobs[Page, Stage] := jIrr;
370 Inc(Stage);
371 end;
372 MyData.EnhancementJobs[Page, Stage] := NewJob;
373 end;
374 SmartUpdateContent;
375end;
376
377procedure TEnhanceDlg.FormKeyDown(Sender: TObject; var Key: Word;
378 Shift: TShiftState);
379var
380 ShortCut: TShortCut;
381begin
382 ShortCut := KeyToShortCut(Key, Shift);
383 if BHelp.Test(ShortCut) then
384 MainScreen.HelpDlg.ShowNewContent(WindowModeMakePersistent(FWindowMode), hkText,
385 MainScreen.HelpDlg.TextIndex('MACRO'))
386end;
387
388end.
Note: See TracBrowser for help on using the repository browser.