source: oup/current/Main.pas@ 82

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