source: oup/current/Main.pas@ 49

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