source: oup/current/Main.pas@ 43

Last change on this file since 43 was 43, checked in by alloc, 18 years ago

DevTree 0.33a.

File size: 15.7 KB
Line 
1unit Main;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, StrUtils, Clipbrd, ExtCtrls, ComCtrls, Menus, Grids,
8 MPHexEditor, ToolWin, ImgList, Tabs,
9 Code_Functions, Data, Code_DataStructures,
10 Helper_LevelDB, Code_Exporters, Settings,
11 Tool_Preview, Tool_TxmpReplace, Tool_BinEdit, Tool_Extractor, Tool_RawEdit,
12 Code_OniDataClass, MDITab, TB2Item, TB2Dock, TB2Toolbar, TB2MDI;
13
14type
15 TForm_Main = class(TForm)
16 saved: TSaveDialog;
17 opend: TOpenDialog;
18 statbar: TStatusBar;
19 MenuImages: TImageList;
20 DockTop: TTBDock;
21 MainMenu: TTBToolbar;
22 menu_main: TTBSubmenuItem;
23 menu_loadfile: TTBItem;
24 menu_sep1: TTBSeparatorItem;
25 menu_settings: TTBItem;
26 menu_sep4: TTBSeparatorItem;
27 menu_exit: TTBItem;
28 menu_convert: TTBSubmenuItem;
29 menu_createdb: TTBItem;
30 menu_createlvl: TTBItem;
31 menu_tools: TTBSubmenuItem;
32 menu_preview: TTBItem;
33 menu_binedit: TTBItem;
34 menu_rawedit: TTBItem;
35 menu_txmpreplace: TTBItem;
36 menu_extractor: TTBItem;
37 menu_filecompare: TTBItem;
38 menu_levelstructedit: TTBItem;
39 menu_windows: TTBSubmenuItem;
40 menu_windows_cascade: TTBItem;
41 menu_windows_tile: TTBItem;
42 menu_windows_closeall: TTBItem;
43 menu_sep3: TTBSeparatorItem;
44 menu_windows_next: TTBItem;
45 menu_windows_previous: TTBItem;
46 menu_sep2: TTBSeparatorItem;
47 menu_About: TTBItem;
48 Toolbar: TTBToolbar;
49 tbOpen: TTBItem;
50 DockLeft: TTBDock;
51 DockRight: TTBDock;
52 DockBottom: TTBDock;
53 MDIToolbar: TTBToolbar;
54 TBControlItem1: TTBControlItem;
55 MDITab: TMDITab;
56 menu_view: TTBSubmenuItem;
57 menu_view_mdibar: TTBItem;
58 menu_view_statusbar: TTBItem;
59 menu_view_toolbar: TTBItem;
60 tb_separator1: TTBSeparatorItem;
61 tb_preview: TTBItem;
62 tb_structure: TTBItem;
63 tb_compare: TTBItem;
64 tb_extractor: TTBItem;
65 tb_txmpreplacer: TTBItem;
66 tb_rawedit: TTBItem;
67 tb_datedit: TTBItem;
68 function TryCloseAll: Boolean;
69 procedure menu_AboutClick(Sender: TObject);
70 procedure menu_settingsClick(Sender: TObject);
71 procedure menu_filecompareClick(Sender: TObject);
72 procedure menu_raweditClick(Sender: TObject);
73 procedure menu_createlvlClick(Sender: TObject);
74 procedure menu_extractorClick(Sender: TObject);
75 procedure menu_createdbClick(Sender: TObject);
76 procedure menu_windows_previousClick(Sender: TObject);
77 procedure menu_windows_nextClick(Sender: TObject);
78 procedure menu_windows_tileClick(Sender: TObject);
79 function open_child(window_context: String): Boolean;
80 procedure menu_windows_closeallClick(Sender: TObject);
81 procedure menu_windows_cascadeClick(Sender: TObject);
82 procedure menu_bineditClick(Sender: TObject);
83 procedure menu_loadfileClick(Sender: TObject);
84 procedure menu_txmpreplaceClick(Sender: TObject);
85 procedure menu_exitClick(Sender: TObject);
86 procedure menu_previewClick(Sender: TObject);
87 procedure FormClose(Sender: TObject; var Action: TCloseAction);
88 procedure FormResize(Sender: TObject);
89 procedure FormCreate(Sender: TObject);
90 procedure UpdateStatBar;
91 procedure menu_view_mdibarClick(Sender: TObject);
92 procedure menu_view_statusbarClick(Sender: TObject);
93 procedure menu_view_toolbarClick(Sender: TObject);
94 procedure MDITabDrawTab(Control: TCustomTabControl; TabIndex: Integer;
95 const Rect: TRect; Active: Boolean);
96 procedure ActivateTools(active: Boolean);
97 procedure MDITabMouseUp(Sender: TObject; Button: TMouseButton;
98 Shift: TShiftState; X, Y: Integer);
99 private
100 public
101 end;
102
103var
104 Form_Main: TForm_Main;
105
106implementation
107
108{$R *.dfm}
109
110
111
112procedure TForm_Main.FormCreate(Sender: TObject);
113begin
114 Self.Caption := 'Oni Un/Packer ' + version;
115 Self.FormResize(Self);
116
117 if FileExists(ExtractFilepath(Application.EXEname) + '\oniunpacker.ini') then
118 begin
119 AssignFile(AppSettingsFile, ExtractFilepath(Application.EXEname) +
120 '\oniunpacker.ini');
121 Reset(AppSettingsFile);
122 Read(AppSettingsFile, AppSettings);
123 CloseFile(AppSettingsFile);
124 end
125 else
126 begin
127 AppSettings.DatPath := 'D:\Spiele\Oni\GameDataFolder';
128 AppSettings.ExtractPath := 'C:\Dokumente und Einstellungen\Administrator\Desktop';
129 AppSettings.FilenumbersAsHex := False;
130 AppSettings.CharSet := DEFAULT_CHARSET;
131 AppSettings.HideUnusedData := False;
132 end;
133
134 if MidStr(ParamStr(1), 1, 3) = 'opf' then
135 begin
136 ShowMessage('Load OPF-File: ' + ParamStr(2));
137 end
138 else if MidStr(ParamStr(1), 1, 4) = 'oldb' then
139 begin
140 if not CreateDataConnection(ParamStr(2), ODB_ADB) then
141 ShowMessage('Error while loading the file:' + CrLf + ParamStr(
142 2) + CrLf + 'Perhaps not an OniUnPacker-LevelDatabase-file?');
143 end
144 else if MidStr(ParamStr(1), 1, 3) = 'dat' then
145 begin
146 if not CreateDataConnection(ParamStr(2), ODB_Dat) then
147 ShowMessage('Error while loading the file:' + CrLf + ParamStr(
148 2) + CrLf + 'Perhaps not an Oni-.dat-file?');
149 end;
150 UpdateStatBar;
151end;
152
153
154
155
156procedure TForm_Main.FormResize(Sender: TObject);
157const
158 MinWidth: Integer = 750;
159 MinHeight: Integer = 500;
160begin
161 if Self.Width < MinWidth then
162 Self.Width := MinWidth;
163 if Self.Height < MinHeight then
164 Self.Height := MinHeight;
165 statbar.Panels.Items[0].Width := Self.Width - 200;
166 MDITab.Width := Self.Width - 20;
167end;
168
169
170
171
172procedure TForm_Main.MDITabDrawTab(Control: TCustomTabControl;
173 TabIndex: Integer; const Rect: TRect; Active: Boolean);
174var
175 x, y: Integer;
176 iconindex: Integer;
177 caption: String;
178begin
179 iconindex := TMDITab(Control).Glyphs[TabIndex];
180 caption := TMDITab(Control).Captions[TabIndex];
181 if active then
182 begin
183 Control.Canvas.Font.Style := Control.Canvas.Font.Style + [fsItalic];
184 y := Rect.Top + 1;
185 end else
186 y := Rect.Top;
187 if iconindex >= 0 then
188 begin
189 TMDITab(Control).Images.Draw(Control.Canvas, Rect.Left + 4, y, iconindex);
190 x := Rect.Left + 26;
191 end else
192 x := Rect.Left + 4;
193 Control.Canvas.TextOut(x, y + 2, caption);
194end;
195
196
197
198
199
200
201procedure TForm_Main.MDITabMouseUp(Sender: TObject; Button: TMouseButton;
202 Shift: TShiftState; X, Y: Integer);
203var
204 pt: TPoint;
205 index: Integer;
206begin
207 pt.X := X;
208 pt.Y := Y;
209 index := MDITab.GetTabAtPos(pt);
210 if (Button = mbRight) and (index >= 0) then
211 MDITab.MDIChildren[index].Close;
212end;
213
214procedure TForm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
215begin
216 AssignFile(AppSettingsFile, ExtractFilepath(Application.EXEname) + '\oniunpacker.ini');
217 if FileExists(ExtractFilepath(Application.EXEname) + '\oniunpacker.ini') then
218 Reset(AppSettingsFile)
219 else
220 Rewrite(AppSettingsFile);
221 Write(AppSettingsFile, AppSettings);
222 CloseFile(AppSettingsFile);
223 Action := caFree;
224end;
225
226
227
228procedure TForm_Main.ActivateTools(active: Boolean);
229begin
230 menu_tools.Enabled := active;
231 tb_preview.Enabled := active;
232 tb_datedit.Enabled := active;
233 tb_rawedit.Enabled := active;
234 tb_txmpreplacer.Enabled := active;
235 tb_extractor.Enabled := active;
236// tb_compare.Enabled := active;
237// tb_structure.Enabled := active;
238end;
239
240procedure TForm_Main.UpdateStatBar;
241begin
242 if Assigned(OniDataConnection) then
243 begin
244 Self.Caption := 'Oni Un/Packer ' + version + ' (' + ExtractFileName(
245 OniDataConnection.FileName) + ')';
246 ActivateTools(True);
247 statbar.Panels.Items[1].Text := 'Files: ' + IntToStr(OniDataConnection.GetFilesCount);
248 statbar.Panels.Items[2].Text :=
249 'Extensions: ' + IntToStr(Length(OniDataConnection.GetExtensionsList));
250 case OniDataConnection.Backend of
251 ODB_Dat:
252 begin
253 statbar.Panels.Items[0].Text := '.dat loaded: ' + OniDataConnection.FileName;
254 end;
255 ODB_ADB:
256 begin
257 statbar.Panels.Items[0].Text := 'OLDB loaded: ' + OniDataConnection.FileName;
258 end;
259 else
260 Self.Caption := 'Oni Un/Packer ' + version;
261 statbar.Panels.Items[0].Text := 'Nothing loaded';
262 statbar.Panels.Items[1].Text := 'Files: -';
263 statbar.Panels.Items[2].Text := 'Extensions: -';
264 ActivateTools(False);
265 end;
266 end
267 else
268 begin
269 Self.Caption := 'Oni Un/Packer ' + version;
270 statbar.Panels.Items[0].Text := 'Nothing loaded';
271 statbar.Panels.Items[1].Text := 'Files: -';
272 statbar.Panels.Items[2].Text := 'Extensions: -';
273 ActivateTools(False);
274 end;
275end;
276
277
278
279
280function TForm_Main.TryCloseAll: Boolean;
281begin
282 menu_windows_closeallClick(Self);
283 Application.ProcessMessages;
284 if MDITab.MDIChildCount = 0 then
285 Result := True
286 else
287 Result := False;
288end;
289
290
291 {#################################}
292 {##### Main-Menu-Handlers #####}
293 {#################################}
294procedure TForm_Main.menu_loadfileClick(Sender: TObject);
295var
296 ext: String;
297begin
298 if TryCloseAll then
299 begin
300 CloseDataConnection;
301 opend.InitialDir := AppSettings.DatPath;
302 opend.Filter := 'Compatible level files|*.dat;*.oldb|Oni level (*.dat)|*.dat|OUP level database (*.oldb)|*.oldb|Any (*.*)|*';
303 if opend.Execute then
304 begin
305 ext := ExtractFileExt(opend.FileName);
306 if ext = '.dat' then
307 if not CreateDataConnection(opend.FileName, ODB_Dat) then
308 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
309 CrLf + 'Perhaps not an Oni-.dat-file?')
310 else if ext = '.oldb' then
311 if not CreateDataConnection(opend.FileName, ODB_ADB) then
312 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
313 CrLf + 'Perhaps not an OniUnPacker-LevelDatabase-file?')
314 else
315 ShowMessage('Incompatible file');
316 AppSettings.DatPath := ExtractFilepath(opend.FileName);
317 end;
318 end;
319 UpdateStatBar;
320end;
321
322
323
324
325procedure TForm_Main.menu_settingsClick(Sender: TObject);
326begin
327 Form_Settings.Visible := True;
328 Self.Enabled := False;
329end;
330
331
332
333
334procedure TForm_Main.menu_exitClick(Sender: TObject);
335begin
336 Self.Close;
337end;
338
339 {####################################}
340 {##### Converters-Menu-Handlers #####}
341 {####################################}
342procedure TForm_Main.menu_createdbClick(Sender: TObject);
343begin
344 if Assigned(OniDataConnection) then
345 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
346 'Do you want to close it to continue?'), PChar('Close file?'),
347 MB_YESNO + MB_ICONQUESTION) = ID_NO then
348 Exit
349 else begin
350 if TryCloseAll then
351 begin
352 CloseDataConnection;
353 UpdateStatBar;
354 end else
355 Exit;
356 end;
357 opend.Filter := 'Oni-Dat-Files|*.dat';
358 saved.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
359 saved.DefaultExt := 'oldb';
360 if opend.Execute then
361 if saved.Execute then
362 Form_LevelDB.CreateDatabase(opend.FileName, saved.FileName);
363end;
364
365
366
367
368procedure TForm_Main.menu_createlvlClick(Sender: TObject);
369begin
370 if Assigned(OniDataConnection) then
371 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
372 'Do you want to close it to continue?'), PChar('Close file?'),
373 MB_YESNO + MB_ICONQUESTION) = ID_NO then
374 Exit
375 else begin
376 if TryCloseAll then
377 begin
378 CloseDataConnection;
379 UpdateStatBar;
380 end else
381 Exit;
382 end;
383 opend.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
384 saved.Filter := 'Oni-Dat-Files|*.dat';
385 saved.DefaultExt := 'dat';
386 if opend.Execute then
387 if saved.Execute then
388 Form_LevelDB.CreateLevel(opend.FileName, saved.FileName);
389end;
390
391 {#################################}
392 {##### Tools-Menu-Handlers #####}
393 {#################################}
394procedure TForm_Main.menu_previewClick(Sender: TObject);
395begin
396 open_child('preview');
397end;
398
399
400procedure TForm_Main.menu_txmpreplaceClick(Sender: TObject);
401begin
402 open_child('txmpreplace');
403end;
404
405
406procedure TForm_Main.menu_bineditClick(Sender: TObject);
407begin
408 open_child('binedit');
409end;
410
411
412procedure TForm_Main.menu_raweditClick(Sender: TObject);
413begin
414 open_child('rawedit');
415end;
416
417
418procedure TForm_Main.menu_extractorClick(Sender: TObject);
419begin
420 open_child('extractor');
421end;
422
423
424procedure TForm_Main.menu_filecompareClick(Sender: TObject);
425begin
426 open_child('compare');
427end;
428
429
430 {#################################}
431 {##### View-Menu-Handlers #####}
432 {#################################}
433procedure TForm_Main.menu_view_mdibarClick(Sender: TObject);
434begin
435 menu_view_mdibar.Checked := not menu_view_mdibar.Checked;
436 mditoolbar.Visible := menu_view_mdibar.Checked;
437end;
438
439
440
441
442procedure TForm_Main.menu_view_statusbarClick(Sender: TObject);
443begin
444 menu_view_statusbar.Checked := not menu_view_statusbar.Checked;
445 statbar.Visible := menu_view_statusbar.Checked;
446end;
447
448
449
450
451procedure TForm_Main.menu_view_toolbarClick(Sender: TObject);
452begin
453 menu_view_toolbar.Checked := not menu_view_toolbar.Checked;
454 Toolbar.Visible := menu_view_toolbar.Checked;
455end;
456
457
458
459 {#################################}
460 {##### Window-Menu-Handlers #####}
461 {#################################}
462procedure TForm_Main.menu_windows_cascadeClick(Sender: TObject);
463begin
464 Self.Cascade;
465end;
466
467
468
469procedure TForm_Main.menu_windows_tileClick(Sender: TObject);
470begin
471 Self.TileMode := tbHorizontal;
472 Self.Tile;
473end;
474
475
476
477procedure TForm_Main.menu_windows_closeallClick(Sender: TObject);
478begin
479 MDITab.CloseAll;
480end;
481
482
483
484procedure TForm_Main.menu_windows_nextClick(Sender: TObject);
485begin
486 if MDIChildCount > 1 then
487 if MDITab.TabIndex = MDITab.MDIChildCount - 1 then
488 MDITab.MDIChildren[0].BringToFront
489 else
490 MDITab.MDIChildren[MDITab.TabIndex + 1].BringToFront;
491end;
492
493
494
495procedure TForm_Main.menu_windows_previousClick(Sender: TObject);
496begin
497 if MDIChildCount > 1 then
498 if MDITab.TabIndex = 0 then
499 MDITab.MDIChildren[MDITab.MDIChildCount - 1].BringToFront
500 else
501 MDITab.MDIChildren[MDITab.TabIndex - 1].BringToFront;
502end;
503
504
505
506
507
508
509procedure TForm_Main.menu_AboutClick(Sender: TObject);
510begin
511 ShowMessage('Will be implemented later ;)');
512end;
513
514
515
516
517function TForm_Main.open_child(window_context: String): Boolean;
518var
519 form: TCustomForm;
520 i: Integer;
521 tag: Integer;
522 iconindex: Integer;
523begin
524 Result := True;
525
526 tag := 1;
527 if MDIChildCount > 0 then
528 for i := 0 to MDIChildCount - 1 do
529 if MDIChildren[i].Tag >= tag then
530 tag := MDIChildren[i].Tag + 1;
531
532 iconindex := -1;
533
534 if window_context = 'binedit' then
535 begin
536 form := TForm_BinEdit.Create(Application);
537 TForm_BinEdit(form).Recreatelist;
538 form.Caption := 'Binary .dat-Editor ' + IntToStr(tag);
539 iconindex := 0;
540 end;
541 if window_context = 'rawedit' then
542 begin
543 form := TForm_RawEdit.Create(Application);
544 TForm_RawEdit(form).Recreatelist;
545 form.Caption := 'Binary .raw-Editor ' + IntToStr(tag);
546 end;
547 if window_context = 'preview' then
548 begin
549 form := TForm_Preview.Create(Application);
550 TForm_Preview(form).Recreatelist;
551 form.Caption := 'Preview-Window ' + IntToStr(tag);
552 end;
553 if window_context = 'txmpreplace' then
554 begin
555 form := TForm_TxmpReplace.Create(Application);
556 TForm_TxmpReplace(form).Recreatelist;
557 form.Caption := 'TXMP Replacer ' + IntToStr(tag);
558 end;
559 if window_context = 'extractor' then
560 begin
561 form := TForm_Extractor.Create(Application);
562 TForm_Extractor(form).Recreatelist;
563 form.Caption := 'Extractor ' + IntToStr(tag);
564 end;
565
566 form.Name := window_context + IntToStr(tag);
567 form.Tag := tag;
568
569 MDITab.AddTab(TForm(form), iconindex);
570end;
571
572end.
Note: See TracBrowser for help on using the repository browser.