1 | unit UFormMain;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
|
---|
7 | UGame, Menus, ActnList, ComCtrls, types, dateutils, System.Actions,
|
---|
8 | Vcl.ToolWin;
|
---|
9 |
|
---|
10 | const
|
---|
11 | ZoomFactor = 1.5;
|
---|
12 |
|
---|
13 | type
|
---|
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 |
|
---|
76 | var
|
---|
77 | FormMain: TFormMain;
|
---|
78 |
|
---|
79 | implementation
|
---|
80 |
|
---|
81 | {$R *.dfm}
|
---|
82 |
|
---|
83 | uses
|
---|
84 | UFormNew, UFormMove, UCore;
|
---|
85 |
|
---|
86 | resourcestring
|
---|
87 | STurn = 'turn';
|
---|
88 |
|
---|
89 | { TFormMain }
|
---|
90 |
|
---|
91 | procedure TFormMain.PaintBox1Paint(Sender: TObject);
|
---|
92 | begin
|
---|
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;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TFormMain.PaintBox1Resize(Sender: TObject);
|
---|
102 | begin
|
---|
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;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TFormMain.Timer1Timer(Sender: TObject);
|
---|
110 | var
|
---|
111 | NewCaption: string;
|
---|
112 | DrawStart: TDateTime;
|
---|
113 | begin
|
---|
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;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TFormMain.Redraw;
|
---|
128 | begin
|
---|
129 | RedrawPending := True;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TFormMain.FormActivate(Sender: TObject);
|
---|
133 | begin
|
---|
134 | if not Core.Initialized then begin
|
---|
135 | Core.Init;
|
---|
136 | end;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | procedure TFormMain.AZoomAllExecute(Sender: TObject);
|
---|
140 | var
|
---|
141 | Factor: TFloatPoint;
|
---|
142 | MapRect: TRect;
|
---|
143 | begin
|
---|
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;
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TFormMain.AZoomInExecute(Sender: TObject);
|
---|
159 | begin
|
---|
160 | with Core.Game.CurrentPlayer do begin
|
---|
161 | View.Zoom := View.Zoom * ZoomFactor;
|
---|
162 | end;
|
---|
163 | Redraw;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TFormMain.AZoomOutExecute(Sender: TObject);
|
---|
167 | var
|
---|
168 | D: TPoint;
|
---|
169 | begin
|
---|
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;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
183 | begin
|
---|
184 | Core.Game.Running := False;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TFormMain.FormShow(Sender: TObject);
|
---|
188 | begin
|
---|
189 | Redraw;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TFormMain.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
|
---|
193 | Shift: TShiftState; X, Y: Integer);
|
---|
194 | begin
|
---|
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;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | procedure TFormMain.PaintBox1MouseLeave(Sender: TObject);
|
---|
205 | begin
|
---|
206 | MoveActive := False;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | procedure TFormMain.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
|
---|
210 | Y: Integer);
|
---|
211 | var
|
---|
212 | Cell: TCell;
|
---|
213 | OldCell: TCell;
|
---|
214 | CellPos: TPoint;
|
---|
215 | begin
|
---|
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 := '';
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TFormMain.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
|
---|
246 | Shift: TShiftState; X, Y: Integer);
|
---|
247 | begin
|
---|
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;
|
---|
255 | end;
|
---|
256 |
|
---|
257 | procedure TFormMain.PaintBox1MouseWheelDown(Sender: TObject;
|
---|
258 | Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
|
---|
259 | begin
|
---|
260 | AZoomOut.Execute;
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TFormMain.PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState;
|
---|
264 | MousePos: TPoint; var Handled: Boolean);
|
---|
265 | begin
|
---|
266 | AZoomIn.Execute;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | end.
|
---|
270 |
|
---|