source: branches/delphi/Forms/UFormMain.pas

Last change on this file was 44, checked in by chronos, 10 years ago
  • Added: Test Delphi conversion.
File size: 7.5 KB
Line 
1unit UFormMain;
2
3interface
4
5uses
6 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
7 UGame, Menus, ActnList, ComCtrls, types, dateutils, System.Actions,
8 Vcl.ToolWin;
9
10const
11 ZoomFactor = 1.5;
12
13type
14
15 { TFormMain }
16
17 TFormMain = class(TForm)
18 AZoomIn: TAction;
19 AZoomAll: TAction;
20 AZoomOut: TAction;
21 ActionList1: TActionList;
22 MainMenu1: TMainMenu;
23 MenuItem1: TMenuItem;
24 MenuItem10: TMenuItem;
25 MenuItem11: TMenuItem;
26 MenuItem12: TMenuItem;
27 MenuItem13: TMenuItem;
28 MenuItem14: TMenuItem;
29 MenuItem15: TMenuItem;
30 MenuItem2: TMenuItem;
31 MenuItem3: TMenuItem;
32 MenuItem4: TMenuItem;
33 MenuItem5: TMenuItem;
34 MenuItem6: TMenuItem;
35 MenuItem7: TMenuItem;
36 MenuItem8: TMenuItem;
37 MenuItem9: TMenuItem;
38 PaintBox1: TPaintBox;
39 StatusBar1: TStatusBar;
40 Timer1: TTimer;
41 ToolBar1: TToolBar;
42 ToolButton1: TToolButton;
43 ToolButton2: TToolButton;
44 ToolButton3: TToolButton;
45 ToolButton4: TToolButton;
46 ToolButton5: TToolButton;
47 procedure AZoomAllExecute(Sender: TObject);
48 procedure AZoomInExecute(Sender: TObject);
49 procedure AZoomOutExecute(Sender: TObject);
50 procedure FormActivate(Sender: TObject);
51 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
52 procedure FormShow(Sender: TObject);
53 procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
54 Shift: TShiftState; X, Y: Integer);
55 procedure PaintBox1MouseLeave(Sender: TObject);
56 procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
57 Y: Integer);
58 procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
59 Shift: TShiftState; X, Y: Integer);
60 procedure PaintBox1MouseWheelDown(Sender: TObject; Shift: TShiftState;
61 MousePos: TPoint; var Handled: Boolean);
62 procedure PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState;
63 MousePos: TPoint; var Handled: Boolean);
64 procedure PaintBox1Paint(Sender: TObject);
65 procedure PaintBox1Resize(Sender: TObject);
66 procedure Timer1Timer(Sender: TObject);
67 private
68 StartMousePoint: TPoint;
69 StartViewPoint: TPoint;
70 MoveActive: Boolean;
71 RedrawPending: Boolean;
72 public
73 procedure Redraw;
74 end;
75
76var
77 FormMain: TFormMain;
78
79implementation
80
81{$R *.dfm}
82
83uses
84 UFormNew, UFormMove, UCore;
85
86resourcestring
87 STurn = 'turn';
88
89{ TFormMain }
90
91procedure TFormMain.PaintBox1Paint(Sender: TObject);
92begin
93 //if Core.Game.Running then
94 if Assigned(Core.Game.CurrentPlayer) then
95 with Core.Game.CurrentPlayer do begin
96 View.DestRect := Bounds(0, 0, PaintBox1.Width, PaintBox1.Height);
97 Paint(PaintBox1);
98 end;
99end;
100
101procedure TFormMain.PaintBox1Resize(Sender: TObject);
102begin
103 if Assigned(Core.Game.CurrentPlayer) then
104 with Core.Game.CurrentPlayer do
105 View.DestRect := Bounds(0, 0, PaintBox1.Width, PaintBox1.Height);
106 Redraw;
107end;
108
109procedure TFormMain.Timer1Timer(Sender: TObject);
110var
111 NewCaption: string;
112 DrawStart: TDateTime;
113begin
114 if RedrawPending then begin
115 RedrawPending := False;
116 DrawStart := Now;
117 PaintBox1.Repaint;
118 StatusBar1.Panels[1].Text := IntToStr(Trunc((Now - DrawStart) / OneMillisecond)) + ' ms';
119
120 NewCaption := 'xTactics';
121 if Assigned(Core.Game.CurrentPlayer) then
122 NewCaption := Core.Game.CurrentPlayer.Name + ' - ' + STurn + ' ' + IntToStr(Core.Game.TurnCounter) + ' - ' + NewCaption;
123 Caption := NewCaption;
124 end;
125end;
126
127procedure TFormMain.Redraw;
128begin
129 RedrawPending := True;
130end;
131
132procedure TFormMain.FormActivate(Sender: TObject);
133begin
134 if not Core.Initialized then begin
135 Core.Init;
136 end;
137end;
138
139procedure TFormMain.AZoomAllExecute(Sender: TObject);
140var
141 Factor: TFloatPoint;
142 MapRect: TRect;
143begin
144 with Core.Game, CurrentPlayer, View do begin
145 MapRect := Map.GetPixelRect;
146 if ((MapRect.Right - MapRect.Left) <> 0) and
147 ((MapRect.Bottom - MapRect.Top) <> 0) then begin
148 Factor := FloatPoint((DestRect.Right - DestRect.Left) / (MapRect.Right - MapRect.Left),
149 (DestRect.Bottom - DestRect.Top) / (MapRect.Bottom - MapRect.Top));
150 if Factor.X < Factor.Y then Zoom := Factor.X
151 else Zoom := Factor.Y;
152 end else Zoom := 1;
153 CenterMap;
154 end;
155 Redraw;
156end;
157
158procedure TFormMain.AZoomInExecute(Sender: TObject);
159begin
160 with Core.Game.CurrentPlayer do begin
161 View.Zoom := View.Zoom * ZoomFactor;
162 end;
163 Redraw;
164end;
165
166procedure TFormMain.AZoomOutExecute(Sender: TObject);
167var
168 D: TPoint;
169begin
170 with Core.Game.CurrentPlayer do begin
171 //D := Point(Trunc(MousePos.X - View.Left / ViewZoom),
172 // Trunc(MousePos.Y - View.Top / ViewZoom));
173 View.Zoom := View.Zoom / ZoomFactor;
174 //View := Bounds(Trunc((D.X - MousePos.X) * ViewZoom),
175 // Trunc((D.Y - MousePos.Y) * ViewZoom),
176 // View.Right - View.Left,
177 // View.Bottom - View.Top);
178 end;
179 Redraw;
180end;
181
182procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
183begin
184 Core.Game.Running := False;
185end;
186
187procedure TFormMain.FormShow(Sender: TObject);
188begin
189 Redraw;
190end;
191
192procedure TFormMain.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
193 Shift: TShiftState; X, Y: Integer);
194begin
195 if Button = mbLeft then begin
196 if Core.Game.CurrentPlayer.Mode = pmHuman then begin
197 StartMousePoint := Point(X, Y);
198 StartViewPoint := Core.Game.CurrentPlayer.View.SourceRect.TopLeft;
199 MoveActive := True;
200 end;
201 end;
202end;
203
204procedure TFormMain.PaintBox1MouseLeave(Sender: TObject);
205begin
206 MoveActive := False;
207end;
208
209procedure TFormMain.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
210 Y: Integer);
211var
212 Cell: TCell;
213 OldCell: TCell;
214 CellPos: TPoint;
215begin
216 if Assigned(Core.Game.CurrentPlayer) then begin
217 if MoveActive then
218 with Core.Game.CurrentPlayer do begin
219 if Mode = pmHuman then begin
220 View.SourceRect := Bounds(Trunc(StartViewPoint.X + (StartMousePoint.X - X) / View.Zoom),
221 Trunc(StartViewPoint.Y + (StartMousePoint.Y - Y) / View.Zoom),
222 View.SourceRect.Right - View.SourceRect.Left,
223 View.SourceRect.Bottom - View.SourceRect.Top);
224 Redraw;
225 end;
226 end;
227 Cell := nil;
228 OldCell := Core.Game.CurrentPlayer.View.FocusedCell;
229 with Core.Game do
230 Cell := Map.PosToCell(CurrentPlayer.View.CanvasToCellPos(Point(X, Y)), CurrentPlayer.View );
231 if Assigned(Cell) then begin
232 Core.Game.CurrentPlayer.View.FocusedCell := Cell;
233 StatusBar1.Panels[0].Text := '[' + IntToStr(Cell.Pos.X) + ', ' + IntToStr(Cell.Pos.Y) +
234 '] (' + IntToStr(Cell.MovesFrom.Count) + ', ' + IntToStr(Cell.MovesTo.Count) + ')';
235 end else begin
236 Core.Game.CurrentPlayer.View.FocusedCell := nil;
237 StatusBar1.Panels[0].Text := '';
238 end;
239 CellPos := Core.Game.CurrentPlayer.View.CanvasToCellPos(Point(X, Y));
240 StatusBar1.Panels[2].Text := 'CellPos: ' + IntToStr(CellPos.X) + ', ' + IntToStr(CellPos.Y);
241 if Cell <> OldCell then Redraw;
242 end else StatusBar1.Panels[0].Text := '';
243end;
244
245procedure TFormMain.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
246 Shift: TShiftState; X, Y: Integer);
247begin
248 if (Abs(StartMousePoint.X - X) < 5) and (Abs(StartMousePoint.Y - Y) < 5) then begin
249 if Core.Game.Running and (Core.Game.CurrentPlayer.Mode = pmHuman) then begin
250 Core.Game.CurrentPlayer.View.SelectCell(Point(X, Y), Core.Game.CurrentPlayer);
251 Redraw;
252 end;
253 end;
254 MoveActive := False;
255end;
256
257procedure TFormMain.PaintBox1MouseWheelDown(Sender: TObject;
258 Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
259begin
260 AZoomOut.Execute;
261end;
262
263procedure TFormMain.PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState;
264 MousePos: TPoint; var Handled: Boolean);
265begin
266 AZoomIn.Execute;
267end;
268
269end.
270
Note: See TracBrowser for help on using the repository browser.