source: oup/current/Main.pas@ 84

Last change on this file since 84 was 84, checked in by alloc, 18 years ago
File size: 19.2 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 begin
232 hint := 'Window: ' + MDITab.MDIChildren[tabIndex].Caption + #13+#10 +
233 'Selected File: ';
234 if TForm_ToolTemplate(MDITab.MDIChildren[tabIndex]).SelectedFile.ID >= 0 then
235 hint := hint + TForm_ToolTemplate(MDITab.MDIChildren[tabIndex]).SelectedFile.FileName
236 else
237 hint := hint + 'None';
238 end
239 else
240 hint := 'Window: ' + MDITab.MDIChildren[tabIndex].Caption;
241 if hint <> MDITab.Hint then
242 begin
243 MDITab.Hint := hint;
244 MDITab.ShowHint := True;
245 end;
246 end
247 else
248 begin
249 MDITab.ShowHint := False;
250 MDITab.Hint := '';
251 end;
252end;
253
254procedure TForm_Main.MDITabMouseUp(Sender: TObject; Button: TMouseButton;
255 Shift: TShiftState; X, Y: Integer);
256var
257 pt: TPoint;
258 tabIndex: Integer;
259 tabRect: TRect;
260 tabX, tabY, tabWidth: Integer;
261begin
262 pt.X := X;
263 pt.Y := Y;
264 tabIndex := MDITab.GetTabAtPos(pt);
265 if (Button = mbRight) and (tabIndex >= 0) then
266 MDITab.MDIChildren[tabIndex].Close;
267
268 if (Button = mbLeft) and (tabIndex >= 0) then
269 begin
270 tabrect := MDITab.TabRect(tabIndex);
271 tabX := X - tabrect.Left;
272 tabY := Y - tabrect.Top;
273 tabwidth := tabrect.Right - tabrect.Left;
274 if (tabY >= 4) and (tabY <= 17) then
275 if (tabX >= tabwidth - 19) and (tabX <= tabwidth - 7) then
276 MDITab.MDIChildren[tabIndex].Close;
277 end;
278end;
279
280
281
282
283procedure TForm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
284begin
285 AssignFile(AppSettingsFile, ExtractFilepath(Application.EXEname) + '\oniunpacker.ini');
286 if FileExists(ExtractFilepath(Application.EXEname) + '\oniunpacker.ini') then
287 Reset(AppSettingsFile)
288 else
289 Rewrite(AppSettingsFile);
290 Write(AppSettingsFile, AppSettings);
291 CloseFile(AppSettingsFile);
292 Action := caFree;
293end;
294
295
296
297procedure TForm_Main.ActivateTools(active: Boolean);
298begin
299 menu_tools.Enabled := active;
300 tb_preview.Enabled := active;
301 tb_datedit.Enabled := active;
302 tb_rawedit.Enabled := active;
303 tb_txmpreplacer.Enabled := active;
304 tb_extractor.Enabled := active;
305// tb_compare.Enabled := active;
306// tb_structure.Enabled := active;
307end;
308
309procedure TForm_Main.UpdateStatBar;
310begin
311 if Assigned(OniDataConnection) then
312 begin
313 Self.Caption := 'Oni Un/Packer ' + version + ' (' + ExtractFileName(
314 OniDataConnection.FileName) + ')';
315 ActivateTools(True);
316 statbar.Panels.Items[1].Text := 'Files: ' + IntToStr(OniDataConnection.GetFilesCount);
317 statbar.Panels.Items[2].Text :=
318 'Extensions: ' + IntToStr(Length(OniDataConnection.GetExtensionsList));
319 case OniDataConnection.Backend of
320 ODB_Dat:
321 begin
322 statbar.Panels.Items[0].Text := '.dat loaded: ' + OniDataConnection.FileName;
323 end;
324 ODB_ADB:
325 begin
326 statbar.Panels.Items[0].Text := 'OLDB loaded: ' + OniDataConnection.FileName;
327 end;
328 else
329 Self.Caption := 'Oni Un/Packer ' + version;
330 statbar.Panels.Items[0].Text := 'Nothing loaded';
331 statbar.Panels.Items[1].Text := 'Files: -';
332 statbar.Panels.Items[2].Text := 'Extensions: -';
333 ActivateTools(False);
334 end;
335 end
336 else
337 begin
338 Self.Caption := 'Oni Un/Packer ' + version;
339 statbar.Panels.Items[0].Text := 'Nothing loaded';
340 statbar.Panels.Items[1].Text := 'Files: -';
341 statbar.Panels.Items[2].Text := 'Extensions: -';
342 ActivateTools(False);
343 end;
344end;
345
346
347
348
349procedure TForm_Main.ToolbarDockChanged(Sender: TObject);
350var
351 toolbar: TTBToolbar;
352 position: TTBDockPosition;
353 mode: TTBItemDisplayMode;
354 i: Integer;
355 isEnabled: Boolean;
356begin
357 toolbar := TTBToolbar(Sender);
358 if toolbar.Floating then
359 mode := nbdmImageAndText
360 else begin
361 position := toolbar.CurrentDock.Position;
362 if position in [dpLeft, dpRight] then
363 mode := nbdmDefault
364 else
365 mode := nbdmImageAndText;
366 end;
367 for i := 0 to toolbar.Items.Count - 1 do
368 toolbar.Items.Items[i].DisplayMode := mode;
369end;
370
371function TForm_Main.TryCloseAll: Boolean;
372begin
373 menu_windows_closeallClick(Self);
374 Application.ProcessMessages;
375 if MDITab.MDIChildCount = 0 then
376 Result := True
377 else
378 Result := False;
379end;
380
381
382
383procedure TForm_Main.LoadFile(typedb: Boolean);
384var
385 ext: String;
386begin
387 if TryCloseAll then
388 begin
389 CloseDataConnection;
390 opend.InitialDir := AppSettings.DatPath;
391 opend.Filter := 'Compatible level files|*.dat;*.oldb|Oni level (*.dat)|*.dat|OUP level database (*.oldb)|*.oldb|Any (*.*)|*';
392 if typedb then
393 opend.FilterIndex := 3
394 else
395 opend.FilterIndex := 2;
396 if opend.Execute then
397 begin
398 ext := ExtractFileExt(opend.FileName);
399 if ext = '.dat' then
400 begin
401 if not CreateDataConnection(opend.FileName, ODB_Dat) then
402 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
403 CrLf + 'Perhaps not an Oni-.dat-file?');
404 end else if ext = '.oldb' then
405 begin
406 if not CreateDataConnection(opend.FileName, ODB_ADB) then
407 ShowMessage('Error while loading the file:' + CrLf + opend.FileName +
408 CrLf + 'Perhaps not an OniUnPacker-LevelDatabase-file?');
409 end else
410 ShowMessage('Incompatible file');
411 AppSettings.DatPath := ExtractFilepath(opend.FileName);
412 end;
413 end;
414 UpdateStatBar;
415end;
416
417
418 {#################################}
419 {##### Main-Menu-Handlers #####}
420 {#################################}
421procedure TForm_Main.menu_loaddbClick(Sender: TObject);
422begin
423 LoadFile(True);
424end;
425
426procedure TForm_Main.menu_loadfileClick(Sender: TObject);
427begin
428 LoadFile(False);
429end;
430
431
432
433
434procedure TForm_Main.menu_settingsClick(Sender: TObject);
435begin
436 Form_Settings.Visible := True;
437 Self.Enabled := False;
438end;
439
440
441
442
443procedure TForm_Main.menu_exitClick(Sender: TObject);
444begin
445 Self.Close;
446end;
447
448 {####################################}
449 {##### Converters-Menu-Handlers #####}
450 {####################################}
451procedure TForm_Main.menu_createdbClick(Sender: TObject);
452begin
453 if Assigned(OniDataConnection) then
454 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
455 'Do you want to close it to continue?'), PChar('Close file?'),
456 MB_YESNO + MB_ICONQUESTION) = ID_NO then
457 Exit
458 else begin
459 if TryCloseAll then
460 begin
461 CloseDataConnection;
462 UpdateStatBar;
463 end else
464 Exit;
465 end;
466 opend.Filter := 'Oni-Dat-Files|*.dat';
467 saved.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
468 saved.DefaultExt := 'oldb';
469 if opend.Execute then
470 if saved.Execute then
471 Form_LevelDB.CreateDatabase(opend.FileName, saved.FileName);
472end;
473
474
475
476
477procedure TForm_Main.menu_createlvlClick(Sender: TObject);
478begin
479 if Assigned(OniDataConnection) then
480 if MessageBox(Self.Handle, PChar('You have currently opened a level-file. ' +
481 'Do you want to close it to continue?'), PChar('Close file?'),
482 MB_YESNO + MB_ICONQUESTION) = ID_NO then
483 Exit
484 else begin
485 if TryCloseAll then
486 begin
487 CloseDataConnection;
488 UpdateStatBar;
489 end else
490 Exit;
491 end;
492 opend.Filter := 'OUP-Level-DB (*.oldb)|*.oldb';
493 saved.Filter := 'Oni-Dat-Files|*.dat';
494 saved.DefaultExt := 'dat';
495 if opend.Execute then
496 if saved.Execute then
497 Form_LevelDB.CreateLevel(opend.FileName, saved.FileName);
498end;
499
500 {#################################}
501 {##### Tools-Menu-Handlers #####}
502 {#################################}
503procedure TForm_Main.menu_previewClick(Sender: TObject);
504begin
505 open_child('preview', -1);
506end;
507
508
509procedure TForm_Main.menu_txmpreplaceClick(Sender: TObject);
510begin
511 open_child('txmpreplace', -1);
512end;
513
514
515procedure TForm_Main.menu_bineditClick(Sender: TObject);
516begin
517 open_child('binedit', -1);
518end;
519
520
521procedure TForm_Main.menu_raweditClick(Sender: TObject);
522begin
523 open_child('rawedit', -1);
524end;
525
526
527procedure TForm_Main.menu_extractorClick(Sender: TObject);
528begin
529 open_child('extractor', -1);
530end;
531
532
533procedure TForm_Main.menu_metaClick(Sender: TObject);
534begin
535 ShowMessage('TBD');
536end;
537
538
539procedure TForm_Main.menu_filecompareClick(Sender: TObject);
540begin
541 open_child('compare', -1);
542end;
543
544
545 {#################################}
546 {##### View-Menu-Handlers #####}
547 {#################################}
548procedure TForm_Main.menu_view_mdibarClick(Sender: TObject);
549begin
550 menu_view_mdibar.Checked := not menu_view_mdibar.Checked;
551 mditoolbar.Visible := menu_view_mdibar.Checked;
552end;
553
554
555
556
557procedure TForm_Main.menu_view_statusbarClick(Sender: TObject);
558begin
559 menu_view_statusbar.Checked := not menu_view_statusbar.Checked;
560 statbar.Visible := menu_view_statusbar.Checked;
561end;
562
563
564
565
566procedure TForm_Main.menu_view_toolbarClick(Sender: TObject);
567begin
568 menu_view_toolbar.Checked := not menu_view_toolbar.Checked;
569 Toolbar.Visible := menu_view_toolbar.Checked;
570end;
571
572
573
574 {#################################}
575 {##### Window-Menu-Handlers #####}
576 {#################################}
577procedure TForm_Main.menu_windows_cascadeClick(Sender: TObject);
578begin
579 Self.Cascade;
580end;
581
582
583
584procedure TForm_Main.menu_windows_tileClick(Sender: TObject);
585begin
586 Self.TileMode := tbHorizontal;
587 Self.Tile;
588end;
589
590procedure TForm_Main.menu_windows_tilevertClick(Sender: TObject);
591begin
592 Self.TileMode := tbVertical;
593 Self.Tile;
594end;
595
596
597procedure TForm_Main.menu_windows_closeallClick(Sender: TObject);
598begin
599 MDITab.CloseAll;
600end;
601
602
603
604procedure TForm_Main.menu_windows_nextClick(Sender: TObject);
605begin
606 if MDIChildCount > 1 then
607 if MDITab.TabIndex = MDITab.MDIChildCount - 1 then
608 MDITab.MDIChildren[0].BringToFront
609 else
610 MDITab.MDIChildren[MDITab.TabIndex + 1].BringToFront;
611end;
612
613
614
615procedure TForm_Main.menu_windows_previousClick(Sender: TObject);
616begin
617 if MDIChildCount > 1 then
618 if MDITab.TabIndex = 0 then
619 MDITab.MDIChildren[MDITab.MDIChildCount - 1].BringToFront
620 else
621 MDITab.MDIChildren[MDITab.TabIndex - 1].BringToFront;
622end;
623
624
625
626
627
628
629procedure TForm_Main.menu_AboutClick(Sender: TObject);
630begin
631 ShowMessage('Will be implemented later ;)');
632end;
633
634
635
636
637function TForm_Main.open_child(window_context: String; fileid: Integer): TForm_ToolTemplate;
638var
639 toolform: TForm_ToolTemplate;
640 i: Integer;
641 tag: Integer;
642 caption_end: String;
643 iconindex: Integer;
644begin
645 Result := nil;
646
647 tag := 1;
648 if MDIChildCount > 0 then
649 for i := 0 to MDIChildCount - 1 do
650 if MDIChildren[i].Tag >= tag then
651 tag := MDIChildren[i].Tag + 1;
652
653 iconindex := -1;
654 caption_end := IntToStr(tag) + ' ';
655
656 if window_context = 'binedit' then
657 begin
658 toolform := TForm_BinEdit.Create(Self);
659 toolform.Caption := 'Binary .dat-Editor ' + caption_end;
660 iconindex := 5;
661 end;
662 if window_context = 'extractor' then
663 begin
664 toolform := TForm_Extractor.Create(Self);
665 toolform.Caption := 'Extractor ' + caption_end;
666 iconindex := 8;
667 end;
668 if window_context = 'preview' then
669 begin
670 toolform := TForm_Preview.Create(Self);
671 toolform.Caption := 'Preview-Window ' + caption_end;
672 iconindex := 4;
673 end;
674 if window_context = 'rawedit' then
675 begin
676 toolform := TForm_RawEdit.Create(Self);
677 toolform.Caption := 'Binary .raw-Editor ' + caption_end;
678 iconindex := 6;
679 end;
680 if window_context = 'txmpreplace' then
681 begin
682 toolform := TForm_TxmpReplace.Create(Application);
683 toolform.Caption := 'TXMP Replacer ' + caption_end;
684 iconindex := 7;
685 end;
686
687 if Assigned(toolform) then
688 begin
689 toolform.Name := window_context + IntToStr(tag);
690 toolform.Tag := tag;
691 MDITab.AddTab(TForm(toolform), iconindex);
692 toolform.Caption := AnsiReplaceStr(toolform.Caption, ' ', '');
693 if fileid > -1 then
694 toolform.SelectFileID(fileid);
695 Result := toolform;
696 end;
697end;
698
699end.
Note: See TracBrowser for help on using the repository browser.