source: oup/current/Main.pas@ 86

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