dfwad: detect UTF-8
[d2df-editor.git] / src / editor / f_mapoptions.pas
blob22d74cc5f8d4699f50e8802ae9d3f2cc99b605e6
1 unit f_mapoptions;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 SysUtils, Classes, Forms, Dialogs,
9 Controls, StdCtrls, ComCtrls, Buttons,
10 f_main, utils;
12 type
13 TMapOptionsForm = class (TForm)
14 // Имя карты:
15 LabelName: TLabel;
16 lCharCountName: TLabel;
17 eMapName: TEdit;
18 // Описание карты:
19 LabelDesc: TLabel;
20 lCharCountDescription: TLabel;
21 eMapDescription: TEdit;
22 // Автор карты:
23 LabelAuthor: TLabel;
24 lCharCountAuthor: TLabel;
25 eAuthor: TEdit;
26 // Фон:
27 LabelBack: TLabel;
28 eBack: TEdit;
29 bRemoveBack: TButton;
30 bSelectBack: TButton;
31 // Музыка:
32 LabelMusic: TLabel;
33 eMusic: TEdit;
34 bRemoveMusic: TButton;
35 bSelectMusic: TButton;
37 // Статистика:
38 GBStats: TGroupBox;
39 LabelTexs: TLabel;
40 lTextureCount: TLabel;
41 LabelPanels: TLabel;
42 lPanelCount: TLabel;
43 LabelItems: TLabel;
44 lItemCount: TLabel;
45 LabelMonsters: TLabel;
46 lMonsterCount: TLabel;
47 LabelAreas: TLabel;
48 lAreaCount: TLabel;
49 LabelTriggers: TLabel;
50 lTriggerCount: TLabel;
52 // Размеры:
53 GBSizes: TGroupBox;
54 eMapWidth: TEdit;
55 UpDown1: TUpDown;
56 eMapHeight: TEdit;
57 UpDown2: TUpDown;
58 LabelWidth: TLabel;
59 LabelHeight: TLabel;
60 LabelCurSize: TLabel;
61 lCurrentMapSizes: TLabel;
63 // Навигатор направления смещения
64 sbMoveCenter: TSpeedButton;
65 sbMoveLeft: TSpeedButton;
66 sbMoveRight: TSpeedButton;
67 sbMoveUp: TSpeedButton;
68 sbMoveUpLeft: TSpeedButton;
69 sbMoveUpRight: TSpeedButton;
70 sbMoveDown: TSpeedButton;
71 sbMoveDownLeft: TSpeedButton;
72 sbMoveDownRight: TSpeedButton;
73 LabelMapMove: TLabel;
74 cbSnapping: TCheckBox;
76 // Кнопки:
77 bOK: TButton;
78 bCancel: TButton;
80 procedure FormShow(Sender: TObject);
81 procedure bCancelClick(Sender: TObject);
82 procedure bOKClick(Sender: TObject);
84 procedure eMapNameChange(Sender: TObject);
85 procedure eMapDescriptionChange(Sender: TObject);
86 procedure eAuthorChange(Sender: TObject);
88 procedure bSelectBackClick(Sender: TObject);
89 procedure bSelectMusicClick(Sender: TObject);
90 procedure bRemoveBackClick(Sender: TObject);
91 procedure bRemoveMusicClick(Sender: TObject);
92 procedure eMapSizeKeyPress(Sender: TObject; var Key: Char);
94 private
95 function CalcOffsetX(WidthDiff: Integer): Integer;
96 function CalcOffsetY(HeightDiff: Integer): Integer;
98 public
99 { Public declarations }
100 end;
103 MapOptionsForm: TMapOptionsForm;
105 implementation
107 uses
108 g_map, f_addresource_sky, f_addresource_sound;
110 {$R *.lfm}
112 // Callbacks to receive results from resource choosing dialogs
113 function SetSky: Boolean;
114 begin
115 MapOptionsForm.eBack.Text := AddSkyForm.ResourceName;
116 Result := True;
117 end;
119 function SetMusic: Boolean;
120 begin
121 MapOptionsForm.eMusic.Text := AddSoundForm.ResourceName;
122 Result := True;
123 end;
125 procedure TMapOptionsForm.FormShow(Sender: TObject);
126 var a, b: Integer;
127 begin
128 // General map options
129 eMapName.Text := gMapInfo.Name;
130 eMapDescription.Text := gMapInfo.Description;
131 eAuthor.Text := gMapInfo.Author;
133 eBack.Text := gMapInfo.SkyName;
134 eMusic.Text := gMapInfo.MusicName;
136 eMapWidth.Text := IntToStr(gMapInfo.Width);
137 eMapHeight.Text := IntToStr(gMapInfo.Height);
138 lCurrentMapSizes.Caption := eMapWidth.Text + 'x' + eMapHeight.Text;
140 sbMoveCenter.Down := True;
142 // Map statistics
143 lTextureCount.Caption := IntToStr(MainForm.lbTextureList.Count);
145 b := 0; // Panels
146 if gPanels <> nil then
147 for a := 0 to High(gPanels) do
148 if gPanels[a].PanelType <> 0 then b := b+1;
149 lPanelCount.Caption := IntToStr(b);
151 b := 0; // Items
152 if gItems <> nil then
153 for a := 0 to High(gItems) do
154 if gItems[a].ItemType <> 0 then b := b+1;
155 lItemCount.Caption := IntToStr(b);
157 b := 0; // Areas
158 if gAreas <> nil then
159 for a := 0 to High(gAreas) do
160 if gAreas[a].AreaType <> 0 then b := b+1;
161 lAreaCount.Caption := IntToStr(b);
163 b := 0; // Monsters
164 if gMonsters <> nil then
165 for a := 0 to High(gMonsters) do
166 if gMonsters[a].MonsterType <> 0 then b := b+1;
167 lMonsterCount.Caption := IntToStr(b);
169 b := 0; // Triggers
170 if gTriggers <> nil then
171 for a := 0 to High(gTriggers) do
172 if gTriggers[a].TriggerType <> 0 then
173 b := b + 1;
174 lTriggerCount.Caption := IntToStr(b);
175 end;
177 procedure TMapOptionsForm.bCancelClick(Sender: TObject);
178 begin
179 Close();
180 end;
182 procedure TMapOptionsForm.bOKClick(Sender: TObject);
184 newWidth, newHeight: Integer;
185 begin
186 newWidth := StrToInt(eMapWidth.Text);
187 newHeight := StrToInt(eMapHeight.Text);
189 with gMapInfo do
190 begin
191 Name := eMapName.Text;
192 Description := eMapDescription.Text;
193 Author := eAuthor.Text;
194 SkyName := eBack.Text;
195 MusicName := eMusic.Text;
197 if Width > newWidth then
198 MapOffset.X := 0;
199 if Height > newHeight then
200 MapOffset.Y := 0;
202 ShiftMapObjects( CalcOffsetX(newWidth - Width),
203 CalcOffsetY(newHeight - Height) );
205 Width := newWidth;
206 Height := newHeight;
207 end;
209 LoadSky(gMapInfo.SkyName);
211 MainForm.FormResize(Self);
212 Close();
213 end;
215 // Counters of chars in edit fields
216 procedure TMapOptionsForm.eMapNameChange(Sender: TObject);
217 begin
218 lCharCountName.Caption := Format('%.2d\32', [Length(eMapName.Text)]);
219 end;
221 procedure TMapOptionsForm.eMapDescriptionChange(Sender: TObject);
222 begin
223 lCharCountDescription.Caption := Format('%.3d\256', [Length(eMapDescription.Text)]);
224 end;
226 procedure TMapOptionsForm.eAuthorChange(Sender: TObject);
227 begin
228 lCharCountAuthor.Caption := Format('%.2d\32', [Length(eAuthor.Text)]);
229 end;
231 // Buttons processing
232 procedure TMapOptionsForm.bSelectBackClick(Sender: TObject);
233 begin
234 AddSkyForm.OKFunction := SetSky;
235 AddSkyForm.lbResourcesList.MultiSelect := False;
236 AddSkyForm.SetResource := eBack.Text;
237 AddSkyForm.ShowModal();
238 end;
240 procedure TMapOptionsForm.bSelectMusicClick(Sender: TObject);
241 begin
242 AddSoundForm.OKFunction := SetMusic;
243 AddSoundForm.lbResourcesList.MultiSelect := False;
244 AddSoundForm.SetResource := eMusic.Text;
245 AddSoundForm.ShowModal();
246 end;
248 procedure TMapOptionsForm.bRemoveBackClick(Sender: TObject);
249 begin
250 eBack.Clear();
251 end;
253 procedure TMapOptionsForm.bRemoveMusicClick(Sender: TObject);
254 begin
255 eMusic.Clear();
256 end;
258 // Map width/height edit fields input processor: only digits are allowed
259 procedure TMapOptionsForm.eMapSizeKeyPress( Sender: TObject;
260 var Key: Char );
261 begin
262 if not ( Key in ['0'..'9', #8, #127] ) then // #8 - bs, #127 - del
263 Key := #0;
264 end;
266 // Offsets calculating for shifting map objects
267 function TMapOptionsForm.CalcOffsetX(WidthDiff: Integer): Integer;
268 begin
269 Result := 0;
270 if (sbMoveCenter.Down or
271 sbMoveUp.Down or
272 sbMoveDown.Down) then Result := WidthDiff div 2
273 else
274 if (sbMoveRight.Down or
275 sbMoveUpRight.Down or
276 sbMoveDownRight.Down) then Result := WidthDiff;
278 if cbSnapping.Checked then Result := Trunc(Result / DotStep) * DotStep;
279 end;
281 function TMapOptionsForm.CalcOffsetY(HeightDiff: Integer): Integer;
282 begin
283 Result := 0;
284 if (sbMoveCenter.Down or
285 sbMoveLeft.Down or
286 sbMoveRight.Down) then Result := HeightDiff div 2
287 else
288 if (sbMoveDown.Down or
289 sbMoveDownLeft.Down or
290 sbMoveDownRight.Down) then Result := HeightDiff;
292 if cbSnapping.Checked then Result := Trunc(Result / DotStep) * DotStep;
293 end;
295 end.