source: branches/delphi/LocalPlayer/Diagram.pas

Last change on this file was 6, checked in by chronos, 8 years ago
  • Modified: Formated all project source files using Delphi formatter as original indentation and other formatting was really bad.
File size: 11.5 KB
Line 
1{$INCLUDE switches}
2unit Diagram;
3
4interface
5
6uses
7 BaseWin,
8
9 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
10 ButtonB, ButtonBase, Menus;
11
12type
13 TDiaDlg = class(TFramedDlg)
14 CloseBtn: TButtonB;
15 ToggleBtn: TButtonB;
16 Popup: TPopupMenu;
17 procedure CloseBtnClick(Sender: TObject);
18 procedure FormPaint(Sender: TObject);
19 procedure FormShow(Sender: TObject);
20 procedure FormCreate(Sender: TObject);
21 procedure ToggleBtnClick(Sender: TObject);
22 procedure PlayerClick(Sender: TObject);
23 procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
24
25 public
26 procedure OffscreenPaint; override;
27 procedure ShowNewContent_Charts(NewMode: integer);
28 procedure ShowNewContent_Ship(NewMode: integer; p: integer = -1);
29
30 private
31 Kind: (dkChart, dkShip);
32 Player, Mode: integer;
33 end;
34
35var
36 DiaDlg: TDiaDlg;
37
38procedure PaintColonyShip(canvas: TCanvas; Player, Left, Width, Top: integer);
39
40implementation
41
42uses
43 Protocol, ScreenTools, ClientTools, Term, Tribes;
44
45{$R *.DFM}
46
47const
48 Border = 24;
49 RoundPixels: array [0 .. nStat - 1] of integer = (0, 0, 0, 5, 5, 5);
50
51 yArea = 48;
52 xComp: array [0 .. 5] of integer = (-60, -28, 4, 4, 36, 68);
53 yComp: array [0 .. 5] of integer = (-40, -40, -79, -1, -40, -40);
54 xPow: array [0 .. 3] of integer = (-116, -116, -116, -116);
55 yPow: array [0 .. 3] of integer = (-28, 0, -44, 16);
56 xHab: array [0 .. 1] of integer = (23, 23);
57 yHab: array [0 .. 1] of integer = (-81, 1);
58
59procedure PaintColonyShip(canvas: TCanvas; Player, Left, Width, Top: integer);
60var
61 i, x, r, nComp, nPow, nHab: integer;
62begin
63 with canvas do
64 begin
65 Brush.Color := $000000;
66 FillRect(Rect(Left, Top, Left + Width, Top + 200));
67 Brush.Style := bsClear;
68 Frame(canvas, Left - 1, Top - 1, Left + Width, Top + 200,
69 MainTexture.clBevelShade, MainTexture.clBevelLight);
70 RFrame(canvas, Left - 2, Top - 2, Left + Width + 1, Top + 200 + 1,
71 MainTexture.clBevelShade, MainTexture.clBevelLight);
72
73 // stars
74 RandSeed := Player * 11111;
75 for i := 1 to Width - 16 do
76 begin
77 x := Random((Width - 16) * 200);
78 r := Random(13) + 28;
79 Pixels[x div 200 + 8, x mod 200 + Top] :=
80 (r * r * r * r div 10001) * $10101;
81 end;
82
83 nComp := MyRO.Ship[Player].Parts[spComp];
84 nPow := MyRO.Ship[Player].Parts[spPow];
85 nHab := MyRO.Ship[Player].Parts[spHab];
86 if nComp > 6 then
87 nComp := 6;
88 if nPow > 4 then
89 nPow := 4;
90 if nHab > 2 then
91 nHab := 2;
92 for i := 0 to nHab - 1 do
93 Sprite(canvas, HGrSystem2, Left + Width div 2 + xHab[i],
94 Top + 100 + yHab[i], 80, 80, 34, 1);
95 for i := 0 to nComp - 1 do
96 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[i],
97 Top + 100 + yComp[i], 32, 80, 1, 1);
98 if nComp > 0 then
99 for i := 3 downto nPow do
100 Sprite(canvas, HGrSystem2, Left + Width div 2 + xPow[i] + 40,
101 Top + 100 + yPow[i], 16, 27, 1, 82);
102 for i := nPow - 1 downto 0 do
103 Sprite(canvas, HGrSystem2, Left + Width div 2 + xPow[i],
104 Top + 100 + yPow[i], 56, 28, 58, 82);
105 if (nComp < 3) and (nHab >= 1) then
106 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[2] + 32 - 16,
107 Top + 100 + 7 + yComp[2], 16, 27, 1, 82);
108 if (nComp >= 3) and (nHab < 1) then
109 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[2] + 32,
110 Top + 100 + 7 + yComp[2], 16, 27, 18, 82);
111 if (nComp < 4) and (nHab >= 2) then
112 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[3] + 32 - 16,
113 Top + 100 + 46 + yComp[3], 16, 27, 1, 82);
114 if (nComp >= 4) and (nHab < 2) then
115 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[3] + 32,
116 Top + 100 + 46 + yComp[3], 16, 27, 18, 82);
117 if (nComp <> 6) and (nComp <> 2) and not((nComp = 0) and (nPow < 1)) then
118 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[nComp],
119 Top + 100 + 7 + yComp[nComp], 16, 27, 18, 82);
120 if (nComp <> 6) and (nComp <> 3) and not((nComp = 0) and (nPow < 2)) then
121 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[nComp],
122 Top + 100 + 46 + yComp[nComp], 16, 27, 18, 82);
123 if nComp = 2 then
124 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[3],
125 Top + 100 + 7 + yComp[3], 16, 27, 18, 82);
126 if nComp = 3 then
127 Sprite(canvas, HGrSystem2, Left + Width div 2 + xComp[4],
128 Top + 100 + 7 + yComp[4], 16, 27, 18, 82);
129 end
130end;
131
132procedure TDiaDlg.FormCreate(Sender: TObject);
133begin
134 inherited;
135 TitleHeight := WideFrame + 20;
136 InnerHeight := ClientHeight - TitleHeight - NarrowFrame;
137 CaptionRight := CloseBtn.Left;
138 CaptionLeft := ToggleBtn.Left + ToggleBtn.Width;
139 InitButtons();
140end;
141
142procedure TDiaDlg.CloseBtnClick(Sender: TObject);
143begin
144 Close;
145end;
146
147procedure TDiaDlg.OffscreenPaint;
148type
149 TLine = array [0 .. 99999, 0 .. 2] of Byte;
150var
151 p, T, max, x, y, y0, Stop, r, RoundRange, LineStep: integer;
152 s: string;
153 List: ^TChart;
154
155 function Round(T: integer): integer;
156 var
157 n, i: integer;
158 begin
159 if T < RoundRange then
160 n := T
161 else
162 n := RoundRange;
163 result := 0;
164 for i := T - n to T do
165 inc(result, List[i]);
166 result := result div (n + 1);
167 end;
168
169 procedure ShareBar(x, y: integer; Cap: string; val0, val1: integer);
170 begin
171 LoweredTextOut(offscreen.canvas, -1, MainTexture, x - 2, y, Cap);
172 DLine(offscreen.canvas, x - 2, x + 169, y + 16, MainTexture.clTextShade,
173 MainTexture.clTextLight);
174 if val0 > 0 then
175 s := Format(Phrases.Lookup('SHARE'), [val0, val1])
176 else
177 s := '0';
178 RisedTextOut(offscreen.canvas,
179 x + 170 - BiColorTextWidth(offscreen.canvas, s), y, s);
180 end;
181
182begin
183 inherited;
184 if Kind = dkChart then
185 with offscreen.canvas do
186 begin
187 Font.Assign(UniFont[ftTiny]);
188 Font.Color := $808080;
189
190 RoundRange := RoundPixels[Mode] * (MyRO.Turn - 1)
191 div (InnerWidth - 2 * Border);
192
193 GetMem(List, 4 * (MyRO.Turn + 2));
194 if Mode = stExplore then
195 max := G.lx * G.ly
196 else
197 begin
198 max := -1;
199 for p := 0 to nPl - 1 do
200 if (G.Difficulty[p] > 0) and
201 (Server(sGetChart + Mode shl 4, me, p, List^) >= rExecuted) then
202 for T := 0 to MyRO.Turn - 1 do
203 begin
204 r := Round(T);
205 if r > max then
206 max := r;
207 end;
208 end;
209
210 Brush.Color := $000000;
211 FillRect(Rect(0, 0, InnerWidth, InnerHeight));
212 Brush.Style := bsClear;
213 Pen.Color := $606060;
214 MoveTo(Border, InnerHeight - Border);
215 LineTo(InnerWidth - Border, InnerHeight - Border);
216 if MyRO.Turn >= 800 then
217 LineStep := 200
218 else if MyRO.Turn >= 400 then
219 LineStep := 100
220 else
221 LineStep := 50;
222 for T := 0 to (MyRO.Turn - 1) div LineStep do
223 begin
224 x := Border + (InnerWidth - 2 * Border) * T *
225 LineStep div (MyRO.Turn - 1);
226 MoveTo(x, Border);
227 LineTo(x, InnerHeight - Border);
228 s := IntToStr(abs(TurnToYear(T * LineStep)));
229 Textout(x - TextWidth(s) div 2, Border - 16, s);
230 end;
231
232 if max > 0 then
233 begin
234 for p := 0 to nPl - 1 do
235 if (G.Difficulty[p] > 0) and
236 (Server(sGetChart + Mode shl 4, me, p, List^) >= rExecuted) then
237 begin
238 Pen.Color := Tribe[p].Color;
239 Stop := MyRO.Turn - 1;
240 while (Stop > 0) and (List[Stop] = 0) do
241 dec(Stop);
242 for T := 0 to Stop do
243 begin
244 r := Round(T);
245 x := Border + (InnerWidth - 2 * Border) * T div (MyRO.Turn - 1);
246 y := InnerHeight - Border - (InnerHeight - 2 * Border) *
247 r div max;
248 if T = 0 then
249 MoveTo(x, y)
250 // else if Mode=stTerritory then
251 // begin LineTo(x,y0); LineTo(x,y) end
252 else if RoundPixels[Mode] = 0 then
253 begin
254 if (y <> y0) or (T = Stop) then
255 LineTo(x, y)
256 end
257 else
258 LineTo(x, y);
259 y0 := y;
260 end;
261 end;
262 end;
263 FreeMem(List);
264 end
265 else
266 with offscreen.canvas do
267 begin
268 Font.Assign(UniFont[ftSmall]);
269 FillOffscreen(0, 0, InnerWidth, InnerHeight);
270
271 PaintColonyShip(offscreen.canvas, Player, 8, InnerWidth - 16, yArea);
272
273 ShareBar(InnerWidth div 2 - 85, InnerHeight - 62,
274 Phrases.Lookup('SHIPHAB'), MyRO.Ship[Player].Parts[spHab], 2);
275 ShareBar(InnerWidth div 2 - 85, InnerHeight - 43,
276 Phrases.Lookup('SHIPPOW'), MyRO.Ship[Player].Parts[spPow], 4);
277 ShareBar(InnerWidth div 2 - 85, InnerHeight - 24,
278 Phrases.Lookup('SHIPCOMP'), MyRO.Ship[Player].Parts[spComp], 6);
279 end;
280 MarkUsedOffscreen(InnerWidth, InnerHeight);
281end; // OffscreenPaint
282
283procedure TDiaDlg.FormPaint(Sender: TObject);
284var
285 s: string;
286begin
287 inherited;
288 canvas.Font.Assign(UniFont[ftNormal]);
289 if Kind = dkChart then
290 s := Phrases.Lookup('DIAGRAM', Mode)
291 else
292 s := Tribe[Player].TPhrase('SHORTNAME');
293 LoweredTextOut(canvas, -1, MainTexture,
294 (ClientWidth - BiColorTextWidth(canvas, s)) div 2, 31, s);
295end;
296
297procedure TDiaDlg.FormShow(Sender: TObject);
298begin
299 if WindowMode = wmModal then
300 begin { center on screen }
301 Left := (Screen.Width - Width) div 2;
302 Top := (Screen.Height - Height) div 2;
303 end;
304 OffscreenPaint;
305end;
306
307procedure TDiaDlg.ShowNewContent_Charts(NewMode: integer);
308begin
309 Kind := dkChart;
310 Mode := stPop;
311 ToggleBtn.ButtonIndex := 15;
312 ToggleBtn.Hint := Phrases.Lookup('BTN_PAGE');
313 Caption := Phrases.Lookup('TITLE_DIAGRAMS');
314 inherited ShowNewContent(NewMode);
315end;
316
317procedure TDiaDlg.ShowNewContent_Ship(NewMode, p: integer);
318begin
319 Kind := dkShip;
320 if p < 0 then
321 begin
322 Player := me;
323 while MyRO.Ship[Player].Parts[spComp] + MyRO.Ship[Player].Parts[spPow] +
324 MyRO.Ship[Player].Parts[spHab] = 0 do
325 Player := (Player + 1) mod nPl;
326 end
327 else
328 Player := p;
329 ToggleBtn.ButtonIndex := 28;
330 ToggleBtn.Hint := Phrases.Lookup('BTN_SELECT');
331 Caption := Phrases.Lookup('TITLE_SHIPS');
332 inherited ShowNewContent(NewMode);
333end;
334
335procedure TDiaDlg.ToggleBtnClick(Sender: TObject);
336var
337 p1: integer;
338 m: TMenuItem;
339begin
340 if Kind = dkChart then
341 begin
342 Mode := (Mode + 1) mod nStat;
343 OffscreenPaint;
344 Invalidate;
345 end
346 else
347 begin
348 EmptyMenu(Popup.Items);
349 for p1 := 0 to nPl - 1 do
350 if MyRO.Ship[p1].Parts[spComp] + MyRO.Ship[p1].Parts[spPow] +
351 MyRO.Ship[p1].Parts[spHab] > 0 then
352 begin
353 m := TMenuItem.Create(Popup);
354 m.RadioItem := true;
355 m.Caption := Tribe[p1].TPhrase('SHORTNAME');
356 m.Tag := p1;
357 m.OnClick := PlayerClick;
358 if p1 = Player then
359 m.Checked := true;
360 Popup.Items.Add(m);
361 end;
362 Popup.Popup(Left + ToggleBtn.Left, Top + ToggleBtn.Top + ToggleBtn.Height);
363 end
364end;
365
366procedure TDiaDlg.PlayerClick(Sender: TObject);
367begin
368 ShowNewContent_Ship(FWindowMode, TComponent(Sender).Tag);
369end;
370
371procedure TDiaDlg.FormKeyDown(Sender: TObject; var Key: word;
372 Shift: TShiftState);
373begin
374 if (Key = VK_F6) and (Kind = dkChart) then // my key
375 ToggleBtnClick(nil)
376 else if (Key = VK_F8) and (Kind = dkShip) then // my other key
377 else
378 inherited
379end;
380
381end.
Note: See TracBrowser for help on using the repository browser.