source: branches/delphi/Forms/UFormNew.pas

Last change on this file was 44, checked in by chronos, 10 years ago
  • Added: Test Delphi conversion.
File size: 6.9 KB
Line 
1unit UFormNew;
2
3interface
4
5uses
6 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
7 ComCtrls, Spin, ExtCtrls, ActnList, UGame, System.Actions;
8
9type
10
11 { TFormNew }
12
13 TFormNew = class(TForm)
14 APlayerAdd: TAction;
15 APlayerRemove: TAction;
16 APlayerModify: TAction;
17 ActionList1: TActionList;
18 ButtonCancel: TButton;
19 ButtonOk: TButton;
20 ButtonPlayerAdd: TButton;
21 ButtonPlayerModify: TButton;
22 ButtonPlayerRemove: TButton;
23 CheckBoxCity: TCheckBox;
24 CheckBoxVoid: TCheckBox;
25 ComboBoxGridType: TComboBox;
26 Label1: TLabel;
27 Label2: TLabel;
28 Label3: TLabel;
29 Label4: TLabel;
30 Label5: TLabel;
31 ListView1: TListView;
32 Panel1: TPanel;
33 Panel2: TPanel;
34 RadioGroupGrowAmount: TRadioGroup;
35 RadioGroupGrowCells: TRadioGroup;
36 SpinEditCityPercent: TSpinEdit;
37 SpinEditMapSizeX: TSpinEdit;
38 SpinEditMapSizeY: TSpinEdit;
39 SpinEditVoidPercent: TSpinEdit;
40 Splitter1: TSplitter;
41 TrackBarSizeX: TTrackBar;
42 TrackBarSizeY: TTrackBar;
43 procedure APlayerAddExecute(Sender: TObject);
44 procedure APlayerModifyExecute(Sender: TObject);
45 procedure APlayerRemoveExecute(Sender: TObject);
46 procedure CheckBoxCityChange(Sender: TObject);
47 procedure CheckBoxVoidChange(Sender: TObject);
48 procedure FormCreate(Sender: TObject);
49 procedure FormDestroy(Sender: TObject);
50 procedure FormShow(Sender: TObject);
51 procedure ListView1Data(Sender: TObject; Item: TListItem);
52 procedure ListView1DblClick(Sender: TObject);
53 procedure ListView1KeyPress(Sender: TObject; var Key: char);
54 procedure ListView1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
55 );
56 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
57 Selected: Boolean);
58 procedure SpinEditMapSizeXChange(Sender: TObject);
59 procedure SpinEditMapSizeYChange(Sender: TObject);
60 procedure TrackBarSizeYChange(Sender: TObject);
61 procedure TrackBarSizeXChange(Sender: TObject);
62 private
63 Players: TPlayers;
64 public
65 procedure ReloadView;
66 procedure Load(Game: TGame);
67 procedure Save(Game: TGame);
68 end;
69
70var
71 FormNew: TFormNew;
72
73implementation
74
75{$R *.dfm}
76
77uses
78 UFormPlayer;
79
80{ TFormNew }
81
82procedure TFormNew.ListView1Data(Sender: TObject; Item: TListItem);
83begin
84 if (Item.Index >= 0) and (Item.Index < Players.Count) then
85 with TPlayer(Players[Item.Index]) do begin
86 Item.Caption := Name;
87 Item.Data := Players[Item.Index];
88 Item.SubItems.Add(PlayerModeText[Mode]);
89 Item.SubItems.Add(IntToStr(Color));
90 Item.SubItems.Add(IntToStr(StartUnits));
91 end;
92end;
93
94procedure TFormNew.ListView1DblClick(Sender: TObject);
95begin
96 APlayerModify.Execute;
97end;
98
99procedure TFormNew.ListView1KeyPress(Sender: TObject; var Key: char);
100begin
101 if (Key = #13) then APlayerModify.Execute;
102end;
103
104procedure TFormNew.ListView1KeyUp(Sender: TObject; var Key: Word;
105 Shift: TShiftState);
106begin
107 if Key = 45 then APlayerAdd.Execute;
108 if Key = 46 then APlayerRemove.Execute;
109end;
110
111procedure TFormNew.ListView1SelectItem(Sender: TObject; Item: TListItem;
112 Selected: Boolean);
113begin
114 ReloadView;
115end;
116
117procedure TFormNew.SpinEditMapSizeXChange(Sender: TObject);
118begin
119 TrackBarSizeX.Position := SpinEditMapSizeX.Value;
120end;
121
122procedure TFormNew.SpinEditMapSizeYChange(Sender: TObject);
123begin
124 TrackBarSizeY.Position := SpinEditMapSizeY.Value;
125end;
126
127procedure TFormNew.TrackBarSizeYChange(Sender: TObject);
128begin
129 SpinEditMapSizeY.Value := TrackBarSizeY.Position;
130end;
131
132procedure TFormNew.TrackBarSizeXChange(Sender: TObject);
133begin
134 SpinEditMapSizeX.Value := TrackBarSizeX.Position;
135end;
136
137procedure TFormNew.ReloadView;
138begin
139 ListView1.Items.Count := Players.Count;
140 ListView1.Refresh;
141 ButtonPlayerRemove.Enabled := Assigned(ListView1.Selected);
142 ButtonPlayerAdd.Enabled := Players.Count < MaxPlayerCount;
143 APlayerModify.Enabled := Assigned(ListView1.Selected);
144end;
145
146procedure TFormNew.FormShow(Sender: TObject);
147begin
148 ReloadView;
149end;
150
151procedure TFormNew.FormCreate(Sender: TObject);
152begin
153 Players := TPlayers.Create;
154end;
155
156procedure TFormNew.CheckBoxCityChange(Sender: TObject);
157begin
158 SpinEditCityPercent.Enabled := CheckBoxCity.Checked;
159end;
160
161procedure TFormNew.CheckBoxVoidChange(Sender: TObject);
162begin
163 SpinEditVoidPercent.Enabled := CheckBoxVoid.Checked;
164end;
165
166procedure TFormNew.APlayerModifyExecute(Sender: TObject);
167begin
168 if Assigned(ListView1.Selected) then begin
169 FormPlayer.Load(TPlayer(ListView1.Selected.Data));
170 if FormPlayer.ShowModal = mrOk then begin
171 FormPlayer.Save(TPlayer(ListView1.Selected.Data));
172 ReloadView;
173 end;
174 end;
175end;
176
177procedure TFormNew.APlayerRemoveExecute(Sender: TObject);
178begin
179 if Assigned(ListView1.Selected) then begin
180 Players.Remove(ListView1.Selected.Data);
181 ReloadView;
182 end;
183end;
184
185procedure TFormNew.APlayerAddExecute(Sender: TObject);
186var
187 NewPlayer: TPlayer;
188begin
189 if Players.Count <= (MaxPlayerCount - 1) then begin
190 NewPlayer := TPlayer.Create;
191 NewPlayer.Name := 'Player ' + IntToStr(Players.Count + 1);
192 NewPlayer.Color := PlayerColors[Players.Count];
193 NewPlayer.Mode := pmComputer;
194 Players.Add(NewPlayer);
195 ReloadView;
196 end;
197end;
198
199procedure TFormNew.FormDestroy(Sender: TObject);
200begin
201 Players.Free;
202end;
203
204procedure TFormNew.Load(Game: TGame);
205var
206 I: Integer;
207 NewItem: TListItem;
208begin
209 CheckBoxVoid.Checked := Game.VoidEnabled;
210 SpinEditVoidPercent.Value := Game.VoidPercentage;
211 SpinEditVoidPercent.Enabled := Game.VoidEnabled;
212 for I := 0 to Players.Count - 1 do
213 Players[I] := nil;
214 Players.Count := Game.Players.Count;
215 for I := 0 to Game.Players.Count - 1 do begin
216 Players[I] := TPlayer.Create;
217 TPlayer(Players[I]).Assign(TPlayer(Game.Players[I]));
218 end;
219 CheckBoxCity.Checked := Game.CityEnabled;
220 SpinEditCityPercent.Value := Game.CityPercentage;
221 SpinEditMapSizeX.Value := Game.Map.Size.X;
222 SpinEditMapSizeY.Value := Game.Map.Size.Y;
223 TrackBarSizeX.Position := Game.Map.Size.X;
224 TrackBarSizeY.Position := Game.Map.Size.Y;
225 RadioGroupGrowAmount.ItemIndex := Integer(Game.GrowAmount);
226 RadioGroupGrowCells.ItemIndex := Integer(Game.GrowCells);
227 ComboBoxGridType.ItemIndex := Integer(Game.MapType) - 1;
228end;
229
230procedure TFormNew.Save(Game: TGame);
231var
232 I: Integer;
233begin
234 while Game.Players.Count > Players.Count do
235 Game.Players.Delete(Game.Players.Count - 1);
236 while Game.Players.Count < Players.Count do
237 Game.Players.Add(TPlayer.Create);
238 for I := 0 to Game.Players.Count - 1 do begin
239 TPlayer(Game.Players[I]).Assign(TPlayer(Players[I]));
240 TPlayer(Game.Players[I]).Game := Game;
241 end;
242 Game.VoidEnabled := CheckBoxVoid.Checked;
243 Game.VoidPercentage := SpinEditVoidPercent.Value;
244 Game.CityEnabled := CheckBoxCity.Checked;
245 Game.CityPercentage := SpinEditCityPercent.Value;
246 Game.Map.Size := Point(SpinEditMapSizeX.Value, SpinEditMapSizeY.Value);
247 Game.GrowAmount := TGrowAmount(RadioGroupGrowAmount.ItemIndex);
248 Game.GrowCells := TGrowCells(RadioGroupGrowCells.ItemIndex);
249 Game.MapType := TMapType(ComboBoxGridType.ItemIndex + 1);
250end;
251
252end.
253
Note: See TracBrowser for help on using the repository browser.