DEADSOFTWARE

a78e6009a44a3dd6d6c165eadb50f7a7c3299155
[d2df-editor.git] / src / editor / f_savemap.pas
1 unit f_savemap;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 LCLIntf, LCLType, LMessages, SysUtils, Variants, Classes,
9 Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, utils;
11 type
12 TSaveMapForm = class (TForm)
13 lbMapList: TListBox;
14 Panel1: TPanel;
15 bOK: TButton;
16 bCancel: TButton;
17 Panel2: TPanel;
18 eMapName: TEdit;
20 procedure GetMaps(FileName: String; placeName: Boolean; ArchiveFormat: String);
21 procedure FormActivate(Sender: TObject);
22 procedure eMapNameChange(Sender: TObject);
23 procedure lbMapListClick(Sender: TObject);
24 procedure bOKClick(Sender: TObject);
26 private
27 { Private declarations }
28 public
29 { Public declarations }
30 end;
32 var
33 SaveMapForm: TSaveMapForm;
35 implementation
37 uses
38 BinEditor, MAPREADER, WADEDITOR, WADSTRUCT, MAPSTRUCT, g_language;
40 {$R *.lfm}
42 procedure TSaveMapForm.FormActivate(Sender: TObject);
43 begin
44 bOK.Enabled := (eMapName.Text <> '');
45 eMapName.SetFocus();
46 end;
48 procedure TSaveMapForm.eMapNameChange(Sender: TObject);
49 begin
50 if eMapName.Text <> '' then
51 bOK.Enabled := True
52 else
53 bOK.Enabled := False;
54 end;
56 procedure TSaveMapForm.lbMapListClick(Sender: TObject);
57 begin
58 if lbMapList.ItemIndex > -1 then
59 eMapName.Text := lbMapList.Items[lbMapList.ItemIndex];
60 end;
62 procedure TSaveMapForm.bOKClick(Sender: TObject);
63 var
64 a: Integer;
65 ok: Boolean;
67 begin
68 ok := True;
69 for a := 0 to lbMapList.Count-1 do
70 if eMapName.Text = lbMapList.Items[a] then
71 begin
72 ok := Application.MessageBox(PChar(Format(MsgMsgMapExists,
73 [eMapName.Text])),
74 PChar(MsgMsgSaveMap),
75 MB_ICONQUESTION or MB_YESNO or MB_DEFBUTTON1) = mrYes;
76 if not ok then
77 Exit;
78 Break;
79 end;
81 if ok then
82 SaveMapForm.ModalResult := mrOk
83 else
84 SaveMapForm.ModalResult := mrCancel;
85 end;
87 procedure TSaveMapForm.GetMaps(FileName: String; placeName: Boolean; ArchiveFormat: String);
88 var
89 WAD: TWADEditor;
90 a, max_num, j: Integer;
91 ResList: SArray;
92 Data: Pointer;
93 Len: Integer;
94 Sign: Array [0..2] of Char;
95 nm: String;
97 begin
98 lbMapList.Items.Clear();
99 max_num := 1;
101 if ArchiveFormat = '' then
102 begin
103 // format not specified -> try open automatically and append to it (or create new default)
104 WAD := gWADEditorFactory.OpenFile(FileName);
105 if WAD = nil then
106 WAD := gWADEditorFactory.CreateDefaultEditor();
107 end
108 else
109 begin
110 // format specified -> appned using exactly this format (overwrite if not compatible)
111 WAD := gWADEditorFactory.CreateEditor(ArchiveFormat);
112 if WAD.ReadFile(FileName) = False then
113 WAD.FreeWAD();
114 end;
116 ResList := WAD.GetResourcesList('');
118 if ResList <> nil then
119 for a := 0 to High(ResList) do
120 begin
121 if not WAD.GetResource('', ResList[a], Data, Len) then
122 Continue;
124 CopyMemory(@Sign[0], Data, 3);
125 FreeMem(Data);
127 if Sign = MAP_SIGNATURE then
128 begin
129 nm := win2utf(ResList[a]);
130 lbMapList.Items.Add(nm);
132 if placeName then
133 begin
134 nm := UpperCase(nm);
135 if (nm[1] = 'M') and
136 (nm[2] = 'A') and
137 (nm[3] = 'P') then
138 begin
139 nm := Trim(Copy(nm, 4, Length(nm)-3));
140 j := StrToIntDef(nm, 0);
141 if j >= max_num then
142 max_num := j + 1;
143 end;
144 end;
145 end;
147 Sign := '';
148 end;
150 WAD.Free();
152 if placeName then
153 begin
154 nm := IntToStr(max_num);
155 if Length(nm) < 2 then
156 nm := '0' + nm;
157 nm := 'MAP' + nm;
158 eMapName.Text := nm;
159 end
160 else
161 eMapName.Text := '';
162 end;
164 end.