[r1]: trunk / Forms / Template.pas Maximize Restore History

Download this file

Template.pas    345 lines (306 with data), 9.6 kB

  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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{
Apophysis Copyright (C) 2001-2004 Mark Townsend
Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
unit Template;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Translation,
Dialogs, StdCtrls, ComCtrls, ImgList, ControlPoint, cmap, RenderingInterface, Main,
Global, Adjust;
type
TTemplateForm = class(TForm)
TemplateList: TListView;
btnCancel: TButton;
btnOK: TButton;
UsedThumbnails: TImageList;
Files: TListBox;
lblFile: TLabel;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TemplateListChange(Sender: TObject; Item: TListItem;
Change: TItemChange);
procedure btnOKClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
TemplateForm: TTemplateForm;
const
blankFlameXML1 = '<flame name="Blank Flame" version="Apophysis" size="1500 1000" center="0 0" background="0 0 0">';
blankFlameXML2 = '<xform weight="0.5" color="0" linear3D="1" coefs="1 0 0 1 0 0" />';
blankFlameXML3 = '<palette count="256" format="RGB">';
procedure ListTemplateByFileName(filename:string);
implementation
{$R *.dfm}
function LoadUserTemplates2(mask:string): integer;
var
FindResult: integer;
SearchRec : TSearchRec;
Path : string;
begin
Path:=AppPath + templatePath + '\';
result := 0;
FindResult := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec);
while FindResult = 0 do
begin
ListTemplateByFileName(Path + SearchRec.Name);
result := result + 1;
FindResult := FindNext(SearchRec);
end;
{ free memory }
FindClose(SearchRec);
end;
function LoadUserTemplates:integer;
begin
LoadUserTemplates2('*.flame');
LoadUserTemplates2('*.template');
Result := 0; // make RTL happy
end;
function BlankXML:string;
var
i:integer;
s:string;
const
break = ' ';
begin
s:=blankFlameXML1 + break + blankFlameXML2 + break + blankFlameXML3 + break;
for i:=1 to 256 do begin
s := s + '000000';
if (i mod 32 = 0) then s := s + break;
end;
s := s + '</palette></flame>';
Result := s;
end;
procedure DropBlank();
var
flameXML: string;
cp: TControlPoint;
bm: TBitmap;
cmap: TColorMap;
zoom: double;
center: array[0..1] of double;
brightness, gamma, vibrancy: double;
Render: TRenderer;
ListItem: TListItem;
index: integer;
begin
cp := TControlPoint.Create;
Render := TRenderer.Create;
bm := TBitmap.Create;
cp.Clear;
flameXML := BlankXML;
MainForm.ParseXML(cp, PCHAR(flameXML), true);
cp.AdjustScale(TemplateForm.UsedThumbnails.Width, TemplateForm.UsedThumbnails.Height);
//Clipboard.SetTextBuf(PChar(Trim(flameXML)));
// start preview
cp.Width := TemplateForm.UsedThumbnails.Width;
cp.Height := TemplateForm.UsedThumbnails.Height;
cp.spatial_oversample := 1;
cp.spatial_filter_radius := 0.1;
cp.sample_density := 3;
// Render.Compatibility := compatibility;
try
Render.SetCP(cp);
Render.Render;
finally
BM.Assign(Render.GetImage);
cp.Free;
Render.free;
end;
// Thumbnails
TemplateForm.UsedThumbnails.Add(bm, nil);
ListItem := TemplateForm.TemplateList.Items.Add;
ListItem.Caption := 'Blank Flame';
ListItem.ImageIndex := 0;
TemplateForm.Files.Items.Add('n/a');
//end preview
//
Application.ProcessMessages;
end;
procedure DropListItem(FileName: string; FlameName: string);
var
flameXML: string;
cp: TControlPoint;
bm: TBitmap;
cmap: TColorMap;
zoom: double;
center: array[0..1] of double;
brightness, gamma, vibrancy: double;
Render: TRenderer;
ListItem: TListItem;
index: integer;
begin
cp := TControlPoint.Create;
Render := TRenderer.Create;
bm := TBitmap.Create;
cp.Clear;
flameXML := LoadXMLFlameText(filename, FlameName);
MainForm.ParseXML(cp, PCHAR(flameXML), true);
cp.AdjustScale(TemplateForm.UsedThumbnails.Width, TemplateForm.UsedThumbnails.Height);
//Clipboard.SetTextBuf(PChar(Trim(flameXML)));
// start preview
cp.Width := TemplateForm.UsedThumbnails.Width;
cp.Height := TemplateForm.UsedThumbnails.Height;
cp.spatial_oversample := 1;
cp.spatial_filter_radius := 0.1;
cp.sample_density := 3;
// Render.Compatibility := compatibility;
try
Render.SetCP(cp);
Render.Render;
finally
BM.Assign(Render.GetImage);
cp.Free;
Render.free;
end;
// Thumbnails
TemplateForm.UsedThumbnails.Add(bm, nil);
ListItem := TemplateForm.TemplateList.Items.Add;
ListItem.Caption := FlameName;
ListItem.ImageIndex := TemplateForm.TemplateList.Items.Count - 1;
TemplateForm.Files.Items.Add(FileName);
//end preview
//
Application.ProcessMessages;
end;
procedure ListTemplateByFileName(filename:string);
{ List .flame file }
var
sel:integer;
i, p, img: integer;
Title: string;
ListItem: TListItem;
FStrings: TStringList;
bm: TBitmap;
begin
sel := 0;
if not FileExists(FileName) then exit;
FStrings := TStringList.Create;
FStrings.LoadFromFile(FileName);
try
if (Pos('<flame ', Lowercase(FStrings.Text)) <> 0) then
begin
for i := 0 to FStrings.Count - 1 do
begin
p := Pos('<flame ', LowerCase(FStrings[i]));
if (p <> 0) then
begin
MainForm.ListXMLScanner.LoadFromBuffer(PAnsiChar(AnsiString(FSTrings[i])));
MainForm.ListXMLScanner.Execute;
if Length(pname) = 0 then
Title := '*untitled ' + ptime
else
Title := Trim(pname);
if Title <> '' then
begin { Otherwise bad format }
//ListItem := MainForm.ListView.Items.Add;
//Listitem.Caption := Title;
DropListItem(FileName, Title);
end;
end;
end;
end;
finally
FStrings.Free;
end;
end;
procedure ListTemplate;
var
i:integer;
bm:TBitmap;
begin
TemplateForm.TemplateList.Items.BeginUpdate;
TemplateForm.TemplateList.Items.Clear;
TemplateForm.UsedThumbnails.Clear;
// hmmm...
(*for i := 0 to TemplateForm.UsedThumbnails.Count - 1 do
begin
TemplateForm.UsedThumbnails.GetBitmap(i, bm);
bm.Free;
end; *)
DropBlank;
ListTemplateByFileName(AppPath + templateFileName);
LoadUserTemplates;
TemplateForm.TemplateList.Items.EndUpdate;
TemplateForm.TemplateList.Selected := TemplateForm.TemplateList.Items[0];
end;
procedure TTemplateForm.FormCreate(Sender: TObject);
begin
self.Caption := TextByKey('template-title');
btnOK.Caption := TextByKey('common-ok');
btnCancel.Caption := TextByKey('common-cancel');
end;
procedure TTemplateForm.TemplateListChange(Sender: TObject;
Item: TListItem; Change: TItemChange);
var
fn : string;
begin
if (TemplateList.Selected = nil) then begin
btnOK.Enabled := false;
end else begin
if (TemplateList.Selected.Index >= 0) then begin
btnOK.Enabled := true;
if (TemplateList.Selected.Index > 0) then begin
fn := ChangeFileExt(ExtractFileName(Files.Items[TemplateList.Selected.Index]), '');
if (LowerCase(fn) <> 'default') then lblFile.Caption := 'Template file: ' + fn
else lblFile.Caption := '';
end else begin
lblFile.Caption := '';
end;
end else begin
btnOK.Enabled := false;
end;
end;
end;
procedure TTemplateForm.btnOKClick(Sender: TObject);
var
flameXML:string;
fn:string;
ci:integer;
begin
fn:=Files.Items[TemplateList.Selected.Index];
if (TemplateList.Selected.Index = 0) then flameXML := BlankXML
else flameXML := LoadXMLFlameText(fn, TemplateList.Selected.Caption);
MainForm.UpdateUndo;
MainForm.StopThread;
MainForm.InvokeLoadXML(flameXML);
Transforms := MainCp.TrianglesFromCP(MainTriangles);
MainForm.Statusbar.Panels[3].Text := MainCp.name;
{if ResizeOnLoad then}
MainForm.ResizeImage;
MainForm.RedrawTimer.Enabled := True;
Application.ProcessMessages;
MainForm.UpdateWindows;
ci := Random(256); //Random(NRCMAPS);
GetCMap(ci, 1, MainCp.cmap);
MainCp.cmapIndex := ci;
AdjustForm.TemplateRandomizeGradient;
btnCancelClick(sender);
end;
procedure TTemplateForm.btnCancelClick(Sender: TObject);
begin
Close();
end;
procedure TTemplateForm.FormShow(Sender: TObject);
begin
ListTemplate;
end;
end.