1 | unit TxmpReplace;
|
---|
2 | interface
|
---|
3 | uses
|
---|
4 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
5 | Dialogs, Template, StdCtrls, ExtCtrls,
|
---|
6 | Functions, Data, OniImgClass, Menus, Buttons;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TForm_TxmpReplace = class(TForm_ToolTemplate)
|
---|
10 | group_options: TGroupBox;
|
---|
11 | btn_replace: TButton;
|
---|
12 | check_transparency: TCheckBox;
|
---|
13 | check_fading: TCheckBox;
|
---|
14 | panel_txmppreview: TPanel;
|
---|
15 | btn_save: TButton;
|
---|
16 | image_txmppreview: TImage;
|
---|
17 | splitter_txmp: TSplitter;
|
---|
18 | group_bmpselect: TGroupBox;
|
---|
19 | image_bmppreview: TImage;
|
---|
20 | panel_load: TPanel;
|
---|
21 | btn_load: TButton;
|
---|
22 | opend: TOpenDialog;
|
---|
23 | saved: TSaveDialog;
|
---|
24 | procedure SelectFile(fileinfo: TFileInfo);
|
---|
25 | procedure FormCreate(Sender: TObject);
|
---|
26 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
27 | procedure btn_saveClick(Sender: TObject);
|
---|
28 | procedure btn_loadClick(Sender: TObject);
|
---|
29 | procedure btn_replaceClick(Sender: TObject);
|
---|
30 | private
|
---|
31 | OniImage_Old: TOniImage;
|
---|
32 | OniImage_New: TOniImage;
|
---|
33 | old_size: Integer;
|
---|
34 | fileid: Integer;
|
---|
35 | public
|
---|
36 | end;
|
---|
37 |
|
---|
38 | var
|
---|
39 | Form_TxmpReplace: TForm_TxmpReplace;
|
---|
40 |
|
---|
41 | implementation
|
---|
42 | {$R *.dfm}
|
---|
43 | uses Main, OniDataClass;
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
|
---|
48 | var
|
---|
49 | Data: Tdata;
|
---|
50 | mem: TMemoryStream;
|
---|
51 | fadingbyte, depthbyte, storebyte: Byte;
|
---|
52 | begin
|
---|
53 | fileid := fileinfo.ID;
|
---|
54 | Connection.LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte);
|
---|
55 | Connection.LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte);
|
---|
56 | Connection.LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte);
|
---|
57 | check_fading.Checked := (fadingbyte and $01) > 0;
|
---|
58 | check_transparency.Checked := (depthbyte and $04) > 0;
|
---|
59 |
|
---|
60 | OniImage_Old.LoadFromTXMP(connection, fileid);
|
---|
61 | old_size := OniImage_Old.GetImageDataSize((fadingbyte and $01) > 0);
|
---|
62 | Data := OniImage_Old.GetAsBMP;
|
---|
63 | mem := TMemoryStream.Create;
|
---|
64 | mem.Write(Data[0], Length(Data));
|
---|
65 | mem.Seek(0, soFromBeginning);
|
---|
66 | image_txmppreview.Picture.Bitmap.LoadFromStream(mem);
|
---|
67 | mem.Free;
|
---|
68 |
|
---|
69 | group_bmpselect.Enabled := True;
|
---|
70 | end;
|
---|
71 |
|
---|
72 |
|
---|
73 | procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject);
|
---|
74 | var
|
---|
75 | mem: TMemoryStream;
|
---|
76 | tempd: Tdata;
|
---|
77 | begin
|
---|
78 | if opend.Execute then
|
---|
79 | begin
|
---|
80 | OniImage_New.LoadFromBMP(opend.FileName);
|
---|
81 | tempd := OniImage_New.GetAsBMP;
|
---|
82 | mem := TMemoryStream.Create;
|
---|
83 | mem.Write(tempd[0], Length(tempd));
|
---|
84 | mem.Seek(0, soFromBeginning);
|
---|
85 | image_bmppreview.Picture.Bitmap.LoadFromStream(mem);
|
---|
86 | mem.Free;
|
---|
87 | group_options.Enabled := True;
|
---|
88 | end;
|
---|
89 | end;
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject);
|
---|
95 | var
|
---|
96 | newsize: LongWord;
|
---|
97 | old_rawaddr, new_rawaddr: LongWord;
|
---|
98 | oldfading: Byte;
|
---|
99 | tempd: Tdata;
|
---|
100 |
|
---|
101 | datbyte: Word;
|
---|
102 | begin
|
---|
103 | if filelist.ItemIndex >= 0 then
|
---|
104 | begin
|
---|
105 | Connection.LoadDatFilePart(fileid, $88, 1, @oldfading);
|
---|
106 | if Connection.OSisMac then
|
---|
107 | Connection.UpdateDatFilePart(fileid, $A0, 4, @old_rawaddr)
|
---|
108 | else
|
---|
109 | Connection.LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
|
---|
110 |
|
---|
111 | if (OniImage_Old.Width <> OniImage_New.Width) or
|
---|
112 | (OniImage_Old.Height <> OniImage_New.Height) then
|
---|
113 | begin
|
---|
114 | if MessageBox(Self.Handle, PChar(
|
---|
115 | 'Current image and new image have different size' + CrLf +
|
---|
116 | '(Current: ' + IntToStr(OniImage_Old.Width) +
|
---|
117 | 'x' + IntToStr(OniImage_Old.Height) + ' - New: ' +
|
---|
118 | IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) + ')' + CrLf +
|
---|
119 | 'Replace anyways?'),
|
---|
120 | PChar(filelist.Items.Strings[filelist.ItemIndex]),
|
---|
121 | MB_YESNO) = idNo then
|
---|
122 | Exit;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | if check_fading.Checked then
|
---|
126 | if not OniImage_New.GetMipMappedImage(tempd) then
|
---|
127 | if MessageBox(Self.Handle,
|
---|
128 | PChar('Can not create a MipMapped-image (probably because of a wrong dimension).' +
|
---|
129 | #13 + #10 + 'Do you want to continue without MipMapping?'), PChar('Warning'),
|
---|
130 | MB_YESNO) = ID_YES then
|
---|
131 | check_fading.Checked := False
|
---|
132 | else
|
---|
133 | Exit;
|
---|
134 |
|
---|
135 | if not check_fading.Checked then
|
---|
136 | tempd := OniImage_New.GetAsData;
|
---|
137 |
|
---|
138 | newsize := OniImage_New.GetImageDataSize(check_fading.Checked);
|
---|
139 |
|
---|
140 | if (newsize > old_size) and (Connection.Backend = ODB_Dat) then
|
---|
141 | new_rawaddr := Connection.AppendRawFile(
|
---|
142 | Connection.OSisMac, Length(tempd), tempd)
|
---|
143 | else
|
---|
144 | begin
|
---|
145 | new_rawaddr := old_rawaddr;
|
---|
146 | Connection.UpdateRawFile(fileid, $9C, Length(tempd), tempd);
|
---|
147 | end;
|
---|
148 |
|
---|
149 | datbyte := $00;
|
---|
150 | if check_fading.Checked then
|
---|
151 | datbyte := datbyte or $01;
|
---|
152 | Connection.UpdateDatFilePart(fileid, $88, 1, @datbyte);
|
---|
153 | datbyte := $10;
|
---|
154 | if check_transparency.Checked then
|
---|
155 | datbyte := datbyte or $04;
|
---|
156 | Connection.UpdateDatFilePart(fileid, $89, 1, @datbyte);
|
---|
157 | Connection.UpdateDatFilePart(fileid, $8C, 2, @OniImage_New.Width);
|
---|
158 | Connection.UpdateDatFilePart(fileid, $8E, 2, @OniImage_New.Height);
|
---|
159 | datbyte := $08;
|
---|
160 | Connection.UpdateDatFilePart(fileid, $90, 1, @datbyte);
|
---|
161 | if Connection.OSisMac then
|
---|
162 | Connection.UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
|
---|
163 | else
|
---|
164 | Connection.UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr);
|
---|
165 |
|
---|
166 | ShowMessage('TXMP-image replaced');
|
---|
167 | end;
|
---|
168 | end;
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | procedure TForm_TxmpReplace.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
174 | begin
|
---|
175 | OniImage_Old.Free;
|
---|
176 | OniImage_New.Free;
|
---|
177 | inherited;
|
---|
178 | end;
|
---|
179 |
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | procedure TForm_TxmpReplace.FormCreate(Sender: TObject);
|
---|
184 | begin
|
---|
185 | inherited;
|
---|
186 | OniImage_Old := TOniImage.Create;
|
---|
187 | OniImage_New := TOniImage.Create;
|
---|
188 | Self.AllowedExts := 'TXMP';
|
---|
189 | Self.OnNewFileSelected := SelectFile;
|
---|
190 | end;
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 | procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject);
|
---|
196 | begin
|
---|
197 | if saved.Execute then
|
---|
198 | OniImage_Old.WriteToBMP(saved.FileName);
|
---|
199 | end;
|
---|
200 |
|
---|
201 | begin
|
---|
202 | AddToolListEntry('txmpreplace', 'TXMP Replacer', 'TXMP');
|
---|
203 | end.
|
---|