source: oup/releases/0.33a/Main.pas@ 519

Last change on this file since 519 was 54, checked in by alloc, 18 years ago
File size: 17.4 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 MDITab, TB2Item, TB2Dock, TB2Toolbar, TB2MDI,
10 Data, Functions, DataStructures, OniDataClass, Exporters,
11 Helper_LevelDB, Settings, Template,
12 BinEdit, Extractor, Preview, RawEdit, TxmpReplace;
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 menu_windows_tilevert: TTBItem;
69 tb_meta: TTBItem;
70 menu_meta: TTBItem;
71 TBSeparatorItem1: TTBSeparatorItem;
72 tb_db2dat: TTBItem;
73 tb_dat2db: TTBItem;
74 menu_loaddb: TTBItem;
75 tb_opendb: TTBItem;
76 procedure LoadFile(typedb: Boolean);
77 function TryCloseAll: Boolean;
78 procedure menu_AboutClick(Sender: TObject);
79 procedure menu_settingsClick(Sender: TObject);
80 procedure menu_filecompareClick(Sender: TObject);
81 procedure menu_raweditClick(Sender: TObject);
82 procedure menu_createlvlClick(Sender: TObject);
83 procedure menu_extractorClick(Sender: TObject);
84 procedure menu_createdbClick(Sender: TObject);
85 procedure menu_windows_previousClick(Sender: TObject);
86 procedure menu_windows_nextClick(Sender: TObject);
87 procedure menu_windows_tileClick(Sender: TObject);
88 function open_child(window_context: String; fileid: Integer): TForm_ToolTemplate;
89 procedure menu_windows_closeallClick(Sender: TObject);
90 procedure menu_windows_cascadeClick(Sender: TObject);
91 procedure menu_bineditClick(Sender: TObject);
92 procedure menu_loadfileClick(Sender: TObject);
93 procedure menu_txmpreplaceClick(Sender: TObject);
94 procedure menu_exitClick(Sender: TObject);
95 procedure menu_previewClick(Sender: TObject);
96 procedure FormClose(Sender: TObject; var Action: TCloseAction);
97 procedure FormResize(Sender: TObject);
98 procedure FormCreate(Sender: TObject);
99 procedure UpdateStatBar;
100 procedure menu_view_mdibarClick(Sender: TObject);
101 procedure menu_view_statusbarClick(Sender: TObject);
102 procedure menu_view_toolbarClick(Sender: TObject);
103 procedure MDITabDrawTab(Control: TCustomTabControl; TabIndex: Integer;
104 const Rect: TRect; Active: Boolean);
105 procedure ActivateTools(active: Boolean);
106 procedure MDITabMouseUp(Sender: TObject; Button: TMouseButton;
107 Shift: TShiftState; X, Y: Integer);
108 procedure menu_windows_tilevertClick(Sender: TObject);
109 procedure menu_metaClick(Sender: TObject);
110 procedure ToolbarDockChanged(Sender: TObject);
111 procedure menu_loaddbClick(Sender: TObject);
112 private
113 public
114 end;
115
116var
117 Form_Main: TForm_Main;
118
119implementation
120
121{$R *.dfm}
122
123
124
125procedure TForm_Main.FormCreate(Sender: TObject);
126begin
127 Self.Caption := 'Oni Un/Packer ' + version;
128 Self.FormResize(Self);
129
130 if FileExists(ExtractFilepath(Application.EXEname) + '\oniunpacker.ini') then
131 begin
132 AssignFile(AppSettingsFile, ExtractFilepath(Application.EXEname) +
133 '\oniunpacker.ini');
134 Reset(AppSettingsFile);
135 Read(AppSettingsFile, AppSettings);
136 CloseFile(AppSettingsFile);
137 end
138 else
139 begin
140 AppSettings.DatPath := 'D:\Spiele\Oni\GameDataFolder';
141 AppSettings.ExtractPath := 'C:\Dokumente und Einstellungen\Administrator\Desktop';
142 AppSettings.FilenumbersAsHex := False;
143 AppSettings.CharSet := DEFAULT_CHARSET;
144 AppSettings.HideUnusedData := False;
145 end;
146
147 if MidStr(ParamStr(1), 1, 3) = 'opf' then
148 begin
149 ShowMessage('Load OPF-File: ' + ParamStr(2));
150 end
151 else if MidStr(ParamStr(1), 1, 4) = 'oldb' then
152 begin
153 if not CreateDataConnection(ParamStr(2), ODB_ADB) then
154 ShowMessage('Error while loading the file:' + CrLf + ParamStr(
155 2) + CrLf + 'Perhaps not an OniUnPacker-LevelDatabase-file?');
156 end
157 else if MidStr(ParamStr(1), 1, 3) = 'dat' then
158 begin
159 if not CreateDataConnection(ParamStr(2), ODB_Dat) then
160 ShowMessage('Error while loading the file:' + CrLf + ParamStr(
161 2) + CrLf + 'Perhaps not an Oni-.dat-file?');
162 end;
163 UpdateStatBar;
164end;
165
166
167
168
169procedure TForm_Main.FormResize(Sender: TObject);
170const
171 MinWidth: Integer = 750;
172 MinHeight: Integer = 500;
173begin
174 if Self.Width < MinWidth then
175 Self.Width := MinWidth;
176 if Self.Height < MinHeight then
177 Self.Height := MinHeight;
178 statbar.Panels.Items[0].Width := Self.Width - 200;
179 MDITab.Width := Self.Width - 20;
180end;
181
182
183
184
185procedure TForm_Main.MDITabDrawTab(Control: TCustomTabControl;
186 TabIndex: Integer; const Rect: TRect; Active: Boolean);
187var
188 x, y: Integer;
189 iconindex: Integer;
190 caption: String;
191begin
192 iconindex := TMDITab(Control).Glyphs[TabIndex];
193 caption := TMDITab(Control).Captions[TabIndex];
194 if active then
195 begin
196 Control.Canvas.Font.Style := Control.Canvas.Font.Style + [fsItalic];
197 y := Rect.Top + 1;
198 end else
199 y := Rect.Top;
200 if iconindex >= 0 then
201 begin
202 TMDITab(Control).Images.Draw(Control.Canvas, Rect.Left + 4, y, iconindex);
203 x := Rect.Left + 26;
204 end else
205 x := Rect.Left + 4;
206 Control.Canvas.TextOut(x, y + 2, caption);
207end;
208
209
210
211
212
213
214procedure TForm_Main.MDITabMouseUp(Sender: TObject; Button: TMouseButton;
215 Shift: TShiftState; X, Y: Integer);
216var
217 pt: TPoint;
218 index: Integer;
219begin
220 pt.X := X;
221 pt.Y := Y;
222 index := MDITab.GetTabAtPos(pt);
223 if (Button = mbRight) and (index >= 0) then
224 MDITab.MDIChildren[index].Close;
225end;
226
227procedure TForm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
228begin
229 AssignFile(AppSettingsFile, ExtractFilepath(Application.EXEname) + '\oniunpacker.ini');
230 if FileExists(ExtractFilepath(Application.EXEname) + '\oniunpacker.ini') then
231 Reset(AppSettingsFile)
232 else
233 Rewrite(AppSettingsFile);
234 Write(AppSettingsFile, AppSettings);
235 CloseFile(AppSettingsFile);
236 Action := caFree;
237end;
238
239
240
241procedure TForm_Main.ActivateTools(active: Boolean);
242begin
243 menu_tools.Enabled := active;
244 tb_preview.Enabled := active;
245 tb_datedit.Enabled := active;
246 tb_rawedit.Enabled := active;
247 tb_txmpreplacer.Enabled := active;
248 tb_extractor.Enabled := active;
249// tb_compare.Enabled := active;
250// tb_structure.Enabled := active;
251end;
252
253procedure TForm_Main.UpdateStatBar;
254begin
255 if Assigned(OniDataConnection) then
256 begin
257 Self.Caption := 'Oni Un/Packer ' + version + ' (' + ExtractFileName(
258 OniDataConnection.FileName) + ')';
259 ActivateTools(True);
260 statbar.Panels.Items[1].Text := 'Files: ' + IntToStr(OniDataConnection.GetFilesCount);
261 statbar.Panels.Items[2].Text :=
262 'Extensions: ' + IntToStr(Length(OniDataConnection.GetExtensionsList));
263 case OniDataConnection.Backend of
264 ODB_Dat:
265 begin
266 statbar.Panels.Items[0].Text := '.dat loaded: ' + OniDataConnection.FileName;
267 end;
268 ODB_ADB:
269 begin
270 statbar.Panels.Items[0].Text := 'OLDB loaded: ' + OniDataConnection.FileName;
271 end;
272 else
273 Self.Caption := 'Oni Un/Packer ' + version;
274 statbar.Panels.Items[0].Text := 'Nothing loaded';
275 statbar.Panels.Items[1].Text := 'Files: -';
276 statbar.Panels.Items[2].Text := 'Extensions: -';
277 ActivateTools(False);
278 end;
279 end
280 else
281 begin
282 Self.Caption := 'Oni Un/Packer ' + version;
283 statbar.Panels.Items[0].Text := 'Nothing loaded';
284 statbar.Panels.Items[1].Text := 'Files: -';
285 statbar.Panels.Items[2].Text := 'Extensions: -';
286 ActivateTools(False);
287 end;
288end;
289
290
291
292
293procedure TForm_Main.ToolbarDockChanged(Sender: TObject);
294var
295 toolbar: TTBToolbar;
296 position: TTBDockPosition;
297 mode: TTBItemDisplayMode;
298 i: Integer;
299 isEnabled: Boolean;
300begin
301 toolbar := TTBToolbar(Sender);
302 if toolbar.Floating then
303 mode := nbdmImageAndText
304 else begin
305 position := toolbar.CurrentDock.Position;
306 if position in [dpLeft, dpRight] then
307 mode := nbdmDefault
308 else
309 mode := nbdmImageAndText;
310 end;
311 for i := 0 to toolbar.Items.Count - 1 do
312 toolbar.Items.Items[i].DisplayMode := mode;
313end;
314
315function TForm_Main.TryCloseAll: Boolean;
316begin
317 menu_windows_closeallClick(Self);
318 Application.ProcessMessages;
319 if MDITab.MDIChildCount = 0 then
320 Result := True
321 else
322 Result := False;
323end;
324
325
326
327procedure TForm_Main.LoadFile(typedb: Boolean);
328var
329 ext: String;
330begin
331 if TryCloseAll then
332 begin
333 CloseDataConnection;
334 opend.InitialDir := AppSettings.DatPath;
335 opend.Filter := 'Compatible level files|*.dat;*.oldb|Oni level (*.dat)|*.dat|OUP level database (*.oldb)|*.oldb|Any (*.*)|*';
336 if typedb then
337 opend.FilterIndex := 3
338 else
339 opend.FilterIndex := 2;
340 if opend.Execute then
341 begin
342 ext := ExtractFileExt(opend.FileName);
343 if ext = '.dat' then
344 begin
345 if not CreateDataConnection(opend.FileName, ODB_Dat) then
346 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
347 CrLf + 'Perhaps not an Oni-.dat-file?');
348 end else if ext = '.oldb' then
349 begin
350 if not CreateDataConnection(opend.FileName, ODB_ADB) then
351 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
352 CrLf + 'Perhaps not an OniUnPacker-LevelDatabase-file?');
353 end else
354 ShowMessage('Incompatible file');
355 AppSettings.DatPath := ExtractFilepath(opend.FileName);
356 end;
357 end;
358 UpdateStatBar;
359end;
360
361
362 {#################################}
363 {##### Main-Menu-Handlers #####}
364 {#################################}
365procedure TForm_Main.menu_loaddbClick(Sender: TObject);
366begin
367 LoadFile(True);
368end;
369
370procedure TForm_Main.menu_loadfileClick(Sender: TObject);
371begin
372 LoadFile(False);
373end;
374
375
376
377
378procedure TForm_Main.menu_settingsClick(Sender: TObject);
379begin
380 Form_Settings.Visible := True;
381 Self.Enabled := False;
382end;
383
384
385
386
387procedure TForm_Main.menu_exitClick(Sender: TObject);
388begin
389 Self.Close;
390end;
391
392 {####################################}
393 {##### Converters-Menu-Handlers #####}
394 {####################################}
395procedure TForm_Main.menu_createdbClick(Sender: TObject);
396begin
397 if Assigned(OniDataConnection) then
398 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
399 'Do you want to close it to continue?'), PChar('Close file?'),
400 MB_YESNO + MB_ICONQUESTION) = ID_NO then
401 Exit
402 else begin
403 if TryCloseAll then
404 begin
405 CloseDataConnection;
406 UpdateStatBar;
407 end else
408 Exit;
409 end;
410 opend.Filter := 'Oni-Dat-Files|*.dat';
411 saved.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
412 saved.DefaultExt := 'oldb';
413 if opend.Execute then
414 if saved.Execute then
415 Form_LevelDB.CreateDatabase(opend.FileName, saved.FileName);
416end;
417
418
419
420
421procedure TForm_Main.menu_createlvlClick(Sender: TObject);
422begin
423 if Assigned(OniDataConnection) then
424 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
425 'Do you want to close it to continue?'), PChar('Close file?'),
426 MB_YESNO + MB_ICONQUESTION) = ID_NO then
427 Exit
428 else begin
429 if TryCloseAll then
430 begin
431 CloseDataConnection;
432 UpdateStatBar;
433 end else
434 Exit;
435 end;
436 opend.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
437 saved.Filter := 'Oni-Dat-Files|*.dat';
438 saved.DefaultExt := 'dat';
439 if opend.Execute then
440 if saved.Execute then
441 Form_LevelDB.CreateLevel(opend.FileName, saved.FileName);
442end;
443
444 {#################################}
445 {##### Tools-Menu-Handlers #####}
446 {#################################}
447procedure TForm_Main.menu_previewClick(Sender: TObject);
448begin
449 open_child('preview', -1);
450end;
451
452
453procedure TForm_Main.menu_txmpreplaceClick(Sender: TObject);
454begin
455 open_child('txmpreplace', -1);
456end;
457
458
459procedure TForm_Main.menu_bineditClick(Sender: TObject);
460begin
461 open_child('binedit', -1);
462end;
463
464
465procedure TForm_Main.menu_raweditClick(Sender: TObject);
466begin
467 open_child('rawedit', -1);
468end;
469
470
471procedure TForm_Main.menu_extractorClick(Sender: TObject);
472begin
473 open_child('extractor', -1);
474end;
475
476
477procedure TForm_Main.menu_metaClick(Sender: TObject);
478begin
479 ShowMessage('TBD');
480end;
481
482
483procedure TForm_Main.menu_filecompareClick(Sender: TObject);
484begin
485 open_child('compare', -1);
486end;
487
488
489 {#################################}
490 {##### View-Menu-Handlers #####}
491 {#################################}
492procedure TForm_Main.menu_view_mdibarClick(Sender: TObject);
493begin
494 menu_view_mdibar.Checked := not menu_view_mdibar.Checked;
495 mditoolbar.Visible := menu_view_mdibar.Checked;
496end;
497
498
499
500
501procedure TForm_Main.menu_view_statusbarClick(Sender: TObject);
502begin
503 menu_view_statusbar.Checked := not menu_view_statusbar.Checked;
504 statbar.Visible := menu_view_statusbar.Checked;
505end;
506
507
508
509
510procedure TForm_Main.menu_view_toolbarClick(Sender: TObject);
511begin
512 menu_view_toolbar.Checked := not menu_view_toolbar.Checked;
513 Toolbar.Visible := menu_view_toolbar.Checked;
514end;
515
516
517
518 {#################################}
519 {##### Window-Menu-Handlers #####}
520 {#################################}
521procedure TForm_Main.menu_windows_cascadeClick(Sender: TObject);
522begin
523 Self.Cascade;
524end;
525
526
527
528procedure TForm_Main.menu_windows_tileClick(Sender: TObject);
529begin
530 Self.TileMode := tbHorizontal;
531 Self.Tile;
532end;
533
534procedure TForm_Main.menu_windows_tilevertClick(Sender: TObject);
535begin
536 Self.TileMode := tbVertical;
537 Self.Tile;
538end;
539
540
541procedure TForm_Main.menu_windows_closeallClick(Sender: TObject);
542begin
543 MDITab.CloseAll;
544end;
545
546
547
548procedure TForm_Main.menu_windows_nextClick(Sender: TObject);
549begin
550 if MDIChildCount > 1 then
551 if MDITab.TabIndex = MDITab.MDIChildCount - 1 then
552 MDITab.MDIChildren[0].BringToFront
553 else
554 MDITab.MDIChildren[MDITab.TabIndex + 1].BringToFront;
555end;
556
557
558
559procedure TForm_Main.menu_windows_previousClick(Sender: TObject);
560begin
561 if MDIChildCount > 1 then
562 if MDITab.TabIndex = 0 then
563 MDITab.MDIChildren[MDITab.MDIChildCount - 1].BringToFront
564 else
565 MDITab.MDIChildren[MDITab.TabIndex - 1].BringToFront;
566end;
567
568
569
570
571
572
573procedure TForm_Main.menu_AboutClick(Sender: TObject);
574begin
575 ShowMessage('Will be implemented later ;)');
576end;
577
578
579
580
581function TForm_Main.open_child(window_context: String; fileid: Integer): TForm_ToolTemplate;
582var
583 toolform: TForm_ToolTemplate;
584 i: Integer;
585 tag: Integer;
586 iconindex: Integer;
587begin
588 Result := nil;
589
590 tag := 1;
591 if MDIChildCount > 0 then
592 for i := 0 to MDIChildCount - 1 do
593 if MDIChildren[i].Tag >= tag then
594 tag := MDIChildren[i].Tag + 1;
595
596 iconindex := -1;
597
598 if window_context = 'binedit' then
599 begin
600 toolform := TForm_BinEdit.Create(Self);
601 toolform.Caption := 'Binary .dat-Editor ' + IntToStr(tag);
602 iconindex := 5;
603 end;
604 if window_context = 'extractor' then
605 begin
606 toolform := TForm_Extractor.Create(Self);
607 toolform.Caption := 'Extractor ' + IntToStr(tag);
608 iconindex := 8;
609 end;
610 if window_context = 'preview' then
611 begin
612 toolform := TForm_Preview.Create(Self);
613 toolform.Caption := 'Preview-Window ' + IntToStr(tag);
614 iconindex := 4;
615 end;
616 if window_context = 'rawedit' then
617 begin
618 toolform := TForm_RawEdit.Create(Self);
619 toolform.Caption := 'Binary .raw-Editor ' + IntToStr(tag);
620 iconindex := 6;
621 end;
622 if window_context = 'txmpreplace' then
623 begin
624 toolform := TForm_TxmpReplace.Create(Application);
625 toolform.Caption := 'TXMP Replacer ' + IntToStr(tag);
626 iconindex := 7;
627 end;
628
629 if Assigned(toolform) then
630 begin
631 toolform.Name := window_context + IntToStr(tag);
632 toolform.Tag := tag;
633 MDITab.AddTab(TForm(toolform), iconindex);
634 if fileid > -1 then
635 toolform.SelectFileID(fileid);
636 Result := toolform;
637 end;
638end;
639
640end.
Note: See TracBrowser for help on using the repository browser.