DEADSOFTWARE

gui: implement new settings dialog
[d2df-editor.git] / src / editor / Editor.lpr
index 052fbb8208ff350fd50036d09ce796b92bd4459e..1246516f954c714d0183b4f35976c14c6915ced7 100644 (file)
@@ -3,6 +3,9 @@ program Editor;
 {$INCLUDE ../shared/a_modes.inc}
 
 uses
+  {$IFDEF DARWIN}
+    MacOSAll, CocoaAll,
+  {$ENDIF}
   Forms, Interfaces, Dialogs,
   GL, GLExt, SysUtils,
   e_graphics in '../engine/e_graphics.pas',
@@ -41,7 +44,6 @@ uses
   spectrum in 'spectrum.pas',
   f_saveminimap in 'f_saveminimap.pas' {SaveMiniMapForm},
   f_packmap in 'f_packmap.pas' {PackMapForm},
-  f_maptest in 'f_maptest.pas' {MapTestForm},
   f_choosetype in 'f_choosetype.pas' {ChooseTypeForm},
 {$IFNDEF NOSOUND}
   fmod,
@@ -69,6 +71,27 @@ uses
     LogFileName: AnsiString = '';
     ParamFileIndex: Integer = 1;
 
+{$IFDEF DARWIN}
+  function NSStringToAnsiString (s: NSString): AnsiString;
+    var i: Integer;
+  begin
+    result := '';
+    for i := 0 to s.length - 1 do
+      result := result + AnsiChar(s.characterAtIndex(i));
+  end;
+
+  function GetBundlePath (): AnsiString;
+    var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
+  begin
+    pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
+    pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
+    CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
+    CFRelease(pathRef);
+    CFRelease(pathCFStr);
+    Result := pathStr;
+  end;
+{$ENDIF}
+
   procedure THandlerObject.ExceptionHandler (Sender: TObject; e: Exception);
   begin
     e_WriteStackTrace(e.message);
@@ -97,6 +120,30 @@ uses
           Inc(i);
           CfgFileName := ParamStr(i);
         end;
+      end
+      else if p = '--game-wad' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          GameWad := ParamStr(i);
+        end;
+      end
+      else if p = '--editor-wad' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          EditorWad := ParamStr(i);
+        end;
+      end
+      else if p = '--wads-dir' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          WadsDir := ParamStr(i);
+        end;
       end;
       Inc(i);
     end;
@@ -104,24 +151,60 @@ uses
   end;
 
   procedure CheckParamFiles;
-    var i: Integer; path: AnsiString;
+    var i: Integer;
   begin
     i := ParamFileIndex;
     if i <= ParamCount then
-    begin
-      path := ParamStr(i);
-      if path <> '' then
-        OpenMap(path, '');
-    end;
+      StartMap := ParamStr(i);
   end;
 
-  procedure InitLogs;
+  procedure InitPathes;
+    {$IFDEF DARWIN}
+      var BundlePath, DFPath, DocPath: AnsiString; ns: NSString;
+      var ApplicationSupportDirs, DocumentDirs: NSArray;
+      var count: Integer;
+    {$ELSE}
+      var EditorDir: AnsiString;
+    {$ENDIF}
   begin
-    if LogFileName = '' then
-      LogFileName := 'Editor.log';
+    {$IFDEF DARWIN}
+      BundlePath := GetBundlePath();
+      ApplicationSupportDirs := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+      count := ApplicationSupportDirs.count;
+      ns := ApplicationSupportDirs.objectAtIndex(count - 1);
+      DFPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
+      DocumentDirs := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
+      count := DocumentDirs.count;
+      ns := DocumentDirs.objectAtIndex(count - 1);
+      DocPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
+      GameExeFile := 'Doom 2D Forever.app';
+      CfgFileName := DFPath + DirectorySeparator + 'Editor.cfg';
+      LogFileName := DFPath + DirectorySeparator + 'Editor.log';
+      MapsDir := DocPath + DirectorySeparator + 'Maps';
+      WadsDir := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'wads';
+      GameWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
+      EditorWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
+    {$ELSE}
+      EditorDir := ExtractFilePath(Application.ExeName);
+      {$IFDEF WINDOWS}
+        GameExeFile := 'Doom2DF.exe';
+      {$ELSE}
+        GameExeFile := 'Doom2DF';
+      {$ENDIF}
+      CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
+      LogFileName := EditorDir + DirectorySeparator + 'Editor.log';
+      MapsDir := EditorDir + DirectorySeparator + 'maps';
+      WadsDir := EditorDir + DirectorySeparator + 'wads';
+      GameWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
+      EditorWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
+    {$ENDIF}
+    ForceDirectories(MapsDir);
+    ForceDirectories(WadsDir);
+  end;
 
-    if LogFileName <> '' then
-      e_InitLog(LogFileName, WM_NEWFILE);
+  procedure InitLogs;
+  begin
+    e_InitLog(LogFileName, WM_NEWFILE);
 
     {$IF DECLARED(UseHeapTrace)}
       (* http://wiki.freepascal.org/heaptrc *)
@@ -129,16 +212,27 @@ uses
       //SetHeapTraceOutput('EditorLeaks.log');
       //HaltOnError := False;
     {$ENDIF}
+
+    e_WriteLog('Used file pathes:', MSG_NOTIFY);
+    e_WriteLog('  GameExeFile = ' + GameExeFile, MSG_NOTIFY);
+    e_WriteLog('  CfgFileName = ' + CfgFileName, MSG_NOTIFY);
+    e_WriteLog('  LogFileName = ' + LogFileName, MSG_NOTIFY);
+    e_WriteLog('  MapsDir     = ' + MapsDir, MSG_NOTIFY);
+    e_WriteLog('  WadsDir     = ' + WadsDir, MSG_NOTIFY);
+    e_WriteLog('  GameWad     = ' + GameWad, MSG_NOTIFY);
+    e_WriteLog('  EditorWad   = ' + EditorWad, MSG_NOTIFY);
   end;
 
 begin
   Application.ExceptionDialog := aedOkMessageBox;
   Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
   Application.Initialize();
+  {$IFDEF DARWIN}
+    // Disable icons in menu on OSX by default
+    Application.ShowMenuGlyphs := sbgNever;
+  {$ENDIF}
 
-  EditorDir := ExtractFilePath(Application.ExeName);
-  CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
-
+  InitPathes;
   CheckParamOptions;
   InitLogs;
 
@@ -158,7 +252,6 @@ begin
   Application.CreateForm(TAddSoundForm, AddSoundForm);
   Application.CreateForm(TSaveMiniMapForm, SaveMiniMapForm);
   Application.CreateForm(TPackMapForm, PackMapForm);
-  Application.CreateForm(TMapTestForm, MapTestForm);
   Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
   Application.CreateForm(TSelectLanguageForm, SelectLanguageForm);