-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTemplatesForm.pas
315 lines (272 loc) · 8.99 KB
/
TemplatesForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
unit TemplatesForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus,
InflatablesList_ItemShop,
InflatablesList_Manager;
type
TfTemplatesForm = class(TForm)
leName: TLabeledEdit;
btnSave: TButton;
lblTemplates: TLabel;
lbTemplates: TListBox;
pmnTemplates: TPopupMenu;
mniTL_Rename: TMenuItem;
N1: TMenuItem;
mniTL_Remove: TMenuItem;
mniTL_Clear: TMenuItem;
N2: TMenuItem;
mniTL_MoveUp: TMenuItem;
mniTL_MoveDown: TMenuItem;
N3: TMenuItem;
mniTL_Export: TMenuItem;
mniTL_Import: TMenuItem;
btnLoad: TButton;
diaExport: TSaveDialog;
diaImport: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure lbTemplatesClick(Sender: TObject);
procedure lbTemplatesDblClick(Sender: TObject);
procedure lbTemplatesMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pmnTemplatesPopup(Sender: TObject);
procedure mniTL_RenameClick(Sender: TObject);
procedure mniTL_RemoveClick(Sender: TObject);
procedure mniTL_ClearClick(Sender: TObject);
procedure mniTL_MoveUpClick(Sender: TObject);
procedure mniTL_MoveDownClick(Sender: TObject);
procedure mniTL_ExportClick(Sender: TObject);
procedure mniTL_ImportClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
private
fILManager: TILManager;
fAsInit: Boolean;
fCurrentShop: TILItemShop;
fSelectedTemplate: Integer;
public
procedure Initialize(ILManager: TILManager);
procedure Finalize;
// current shop can be nil
Function ShowTemplates(CurrentShop: TILItemShop): Integer;
end;
var
fTemplatesForm: TfTemplatesForm;
implementation
uses
PromptForm,
InflatablesList_Utils;
{$R *.dfm}
procedure TfTemplatesForm.Initialize(ILManager: TILManager);
begin
fILManager := ILManager;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.Finalize;
begin
// nothing to do here
end;
//------------------------------------------------------------------------------
Function TfTemplatesForm.ShowTemplates(CurrentShop: TILItemShop): Integer;
var
i: Integer;
begin
fSelectedTemplate := -1;
fCurrentShop := CurrentShop;
btnSave.Enabled := Assigned(fCurrentShop);
If Assigned(fCurrentShop) then
leName.Text := fCurrentShop.Name
else
leName.Text := '';
// fill list of templates
lbTemplates.Items.BeginUpdate;
try
lbTemplates.Items.Clear;
For i := 0 to Pred(fILManager.ShopTemplateCount) do
lbTemplates.Items.Add(fILManager.ShopTemplates[i].Name);
finally
lbTemplates.Items.EndUpdate;
end;
If lbTemplates.Count > 0 then
lbTemplates.ItemIndex := 0
else
lbTemplates.ItemIndex := -1;
lbTemplates.OnClick(nil);
ShowModal;
Result := fSelectedTemplate;
end;
//==============================================================================
procedure TfTemplatesForm.FormCreate(Sender: TObject);
begin
mniTL_MoveUp.ShortCut := ShortCut(VK_UP,[ssShift]);
mniTL_MoveDown.ShortCut := ShortCut(VK_DOWN,[ssShift]);
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.FormShow(Sender: TObject);
begin
lbTemplates.SetFocus;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.btnSaveClick(Sender: TObject);
var
Index: Integer;
begin
If Assigned(fCurrentShop) and not fAsInit then
begin
If Length(leName.Text) > 0 then
begin
Index := fILManager.ShopTemplateAdd(leName.Text,fCurrentShop);
lbTemplates.Items.Add(fILManager.ShopTemplates[Index].Name);
If lbTemplates.ItemIndex < 0 then
begin
lbTemplates.ItemIndex := 0;
lbTemplates.OnClick(nil);
end;
end
else MessageDlg('Empty name not allowed.',mtError,[mbOK],0);
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.lbTemplatesClick(Sender: TObject);
begin
btnLoad.Enabled := lbTemplates.ItemIndex >= 0;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.lbTemplatesDblClick(Sender: TObject);
begin
If lbTemplates.ItemIndex >= 0 then
btnLoad.OnClick(nil);
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.lbTemplatesMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
begin
Index := lbTemplates.ItemAtPos(Point(X,Y),True);
If Index >= 0 then
lbTemplates.ItemIndex := Index
else
lbTemplates.ItemIndex := Pred(lbTemplates.Count);
lbTemplates.OnClick(nil);
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.pmnTemplatesPopup(Sender: TObject);
begin
mniTL_Rename.Enabled := lbTemplates.ItemIndex >= 0;
mniTL_Remove.Enabled := lbTemplates.ItemIndex >= 0;
mniTL_Clear.Enabled := lbTemplates.Count > 0;
mniTL_MoveUp.Enabled := lbTemplates.ItemIndex > 0;
mniTL_MoveDown.Enabled := (lbTemplates.Count > 0) and (lbTemplates.ItemIndex < Pred(lbTemplates.Count));
mniTL_Export.Enabled := lbTemplates.ItemIndex >= 0;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_RenameClick(Sender: TObject);
var
NewName: String;
begin
If lbTemplates.ItemIndex >= 0 then
begin
NewName := lbTemplates.Items[lbTemplates.ItemIndex];
If IL_InputQuery('New template name','Enter new template name:',NewName) then
begin
// following will also change all references to this template
fILManager.ShopTemplateRename(lbTemplates.ItemIndex,NewName);
lbTemplates.Items[lbTemplates.ItemIndex] := NewName;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_RemoveClick(Sender: TObject);
var
Index: Integer;
begin
If lbTemplates.ItemIndex >= 0 then
If MessageDlg(IL_Format('Are you sure you want to remove template "%s"?',
[fILManager.ShopTemplates[lbTemplates.ItemIndex].Name]),
mtConfirmation,[mbYes,mbNo],0) = mrYes then
begin
Index := lbTemplates.ItemIndex;
If lbTemplates.ItemIndex < Pred(lbTemplates.Count) then
lbTemplates.ItemIndex := lbTemplates.ItemIndex + 1
else If lbTemplates.ItemIndex > 0 then
lbTemplates.ItemIndex := lbTemplates.ItemIndex - 1
else
lbTemplates.ItemIndex := -1;
lbTemplates.OnClick(nil);
lbTemplates.Items.Delete(Index);
fILManager.ShopTemplateDelete(Index);
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_ClearClick(Sender: TObject);
begin
If lbTemplates.Count > 0 then
If MessageDlg('Are you sure you want to remove all templates?',
mtConfirmation,[mbYes,mbNo],0) = mrYes then
begin
lbTemplates.Clear;
fILManager.ShopTemplateClear;
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_MoveUpClick(Sender: TObject);
var
Index: Integer;
begin
If lbTemplates.ItemIndex > 0 then
begin
Index := lbTemplates.ItemIndex;
lbTemplates.Items.Exchange(Index,Index - 1);
fILManager.ShopTemplateExchange(Index,Index - 1);
lbTemplates.ItemIndex := Index - 1;
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_MoveDownClick(Sender: TObject);
var
Index: Integer;
begin
If (lbTemplates.Count > 0) and (lbTemplates.ItemIndex < Pred(lbTemplates.Count)) then
begin
Index := lbTemplates.ItemIndex;
lbTemplates.Items.Exchange(Index,Index + 1);
fILManager.ShopTemplateExchange(Index,Index + 1);
lbTemplates.ItemIndex := Index + 1;
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_ExportClick(Sender: TObject);
begin
If lbTemplates.ItemIndex >= 0 then
begin
diaExport.FileName := lbTemplates.Items[lbTemplates.ItemIndex] + '.tpl';
If diaExport.Execute then
fILManager.ShopTemplateExport(diaExport.FileName,lbTemplates.ItemIndex);
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.mniTL_ImportClick(Sender: TObject);
var
Index: Integer;
begin
If diaImport.Execute then
begin
Index := fILManager.ShopTemplateImport(diaImport.FileName);
lbTemplates.Items.Add(fILManager.ShopTemplates[Index].Name);
If lbTemplates.ItemIndex < 0 then
begin
lbTemplates.ItemIndex := 0;
lbTemplates.OnClick(nil);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TfTemplatesForm.btnLoadClick(Sender: TObject);
begin
fSelectedTemplate := lbTemplates.ItemIndex;
Close;
end;
end.