source: oup/current/Main.pas@ 83

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