DEADSOFTWARE

Added SFS support (resource wads only) (#4)
[d2df-editor.git] / src / editor / f_addresource.pas
1 unit f_addresource;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes,
9 Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
10 ExtDlgs, e_Log, e_textures, WADEDITOR;
12 type
13 TOKFunction = function: Boolean;
15 TAddResourceForm = class (TForm)
16 LabelWADs: TLabel;
17 cbWADList: TComboBox;
18 LabelSections: TLabel;
19 cbSectionsList: TComboBox;
20 lbResourcesList: TListBox;
21 bOK: TButton;
22 bCancel: TButton;
24 procedure FormActivate(Sender: TObject);
25 procedure bOKClick(Sender: TObject);
26 procedure cbWADListChange(Sender: TObject);
27 procedure cbSectionsListChange(Sender: TObject);
28 procedure lbResourcesListClick(Sender: TObject);
30 protected
31 FResourceName: String;
32 FFullResourceName: String;
33 FResourceSelected: Boolean;
34 FOKFunction: TOKFunction;
36 public
37 property ResourceName: String read FResourceName;
38 property FullResourceName: String read FFullResourceName;
39 property OKFunction: TOKFunction read FOKFunction write FOKFunction;
40 end;
42 var
43 AddResourceForm: TAddResourceForm;
45 implementation
47 uses
48 f_main, WADSTRUCT, g_language, utils, sfs;
50 {$R *.lfm}
52 const
53 STANDART_WAD = 'Standart.wad';
55 procedure TAddResourceForm.FormActivate(Sender: TObject);
56 var
57 SR: TSearchRec;
58 begin
59 cbWADList.Clear();
60 cbSectionsList.Clear();
61 lbResourcesList.Clear();
63 FResourceName := '';
64 FFullResourceName := '';
65 FResourceSelected := False;
67 ChDir(EditorDir);
68 if FindFirst(EditorDir + 'wads/*.*', faAnyFile, SR) = 0 then
69 repeat
70 if (SR.name <> '.') and (SR.name <> '..') then
71 cbWADList.Items.Add(SR.Name);
72 until FindNext(SR) <> 0;
73 FindClose(SR);
75 // "Standart.wad" в начало списка:
76 if cbWADList.Items.IndexOf(STANDART_WAD) > 0 then
77 begin
78 cbWADList.Items.Delete(cbWADList.Items.IndexOf(STANDART_WAD));
79 cbWADList.Items.Insert(0, STANDART_WAD);
80 end;
82 // WAD карты:
83 if OpenedMap <> '' then
84 cbWADList.Items.Add(_lc[I_WAD_SPECIAL_MAP]);
85 end;
87 procedure TAddResourceForm.bOKClick(Sender: TObject);
88 begin
89 if FResourceName = '' then
90 begin
91 MessageBox(0, PChar(_lc[I_MSG_CHOOSE_RES]),
92 PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
93 Exit;
94 end;
96 if @FOKFunction <> nil then
97 begin
98 if FOKFunction() then
99 Close();
100 end
101 else
102 Close();
103 end;
105 procedure TAddResourceForm.cbWADListChange(Sender: TObject);
106 var
107 wad: TSFSFileList;
108 i: Integer;
109 FileName, Section, sn, rn: String;
110 begin
111 if cbWADList.Text <> _lc[I_WAD_SPECIAL_MAP] then
112 FileName := EditorDir + 'wads/' + cbWADList.Text (* Resource wad *)
113 else
114 g_ProcessResourceStr(OpenedMap, FileName, sn, rn); (* Map wad *)
116 cbSectionsList.Clear();
117 lbResourcesList.Clear();
119 wad := SFSFileList(FileName);
120 if wad <> nil then
121 begin
122 for i := 0 to wad.Count - 1 do
123 begin
124 Section := win2utf(Copy(wad.Files[i].path, 1, Length(wad.Files[i].path) - 1));
125 if cbSectionsList.Items.IndexOf(Section) = -1 then
126 cbSectionsList.Items.Add(Section)
127 end;
128 wad.Destroy
129 end;
131 (* Update resource list (see below) *)
132 cbSectionsListChange(Sender)
133 end;
135 procedure TAddResourceForm.cbSectionsListChange(Sender: TObject);
136 var
137 wad: TSFSFileList;
138 i: Integer;
139 FileName, Section, SectionName, sn, rn: String;
140 begin
141 if cbWADList.Text <> _lc[I_WAD_SPECIAL_MAP] then
142 FileName := EditorDir + 'wads/' + cbWADList.Text (* Resource wad *)
143 else
144 g_ProcessResourceStr(OpenedMap, FileName, sn, rn); (* Map wad *)
146 SectionName := cbSectionsList.Text;
147 lbResourcesList.Clear();
149 wad := SFSFileList(FileName);
150 if wad <> nil then
151 begin
152 for i := 0 to wad.Count - 1 do
153 begin
154 Section := win2utf(Copy(wad.Files[i].path, 1, Length(wad.Files[i].path) - 1));
155 if Section = SectionName then
156 lbResourcesList.Items.Add(win2utf(wad.Files[i].name))
157 end;
158 wad.Destroy
159 end;
160 end;
162 procedure TAddResourceForm.lbResourcesListClick(Sender: TObject);
163 var
164 FileName, fn: String;
165 begin
166 FResourceSelected := (lbResourcesList.SelCount > 0) or (lbResourcesList.ItemIndex > -1);
167 if not FResourceSelected then
168 begin
169 FResourceName := '';
170 FFullResourceName := '';
171 Exit;
172 end;
174 if cbWADList.Text[1] <> '<' then
175 FileName := cbWADList.Text
176 else
177 FileName := '';
179 FResourceName := FileName + ':' + cbSectionsList.Text + '\' + lbResourcesList.Items[lbResourcesList.ItemIndex];
181 g_ProcessResourceStr(OpenedMap, @fn, nil, nil);
182 if FileName <> '' then
183 FFullResourceName := EditorDir + 'wads/' + FResourceName
184 else
185 FFullResourceName := fn + FResourceName
186 end;
188 end.