AEEditorApp.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Core/StringUtils.h>
  8. #include <Atomic/Engine/Engine.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Input/Input.h>
  11. #include <Atomic/Resource/ResourceCache.h>
  12. #include <Atomic/Graphics/Graphics.h>
  13. #include <Atomic/Atomic3D/AnimatedModel.h>
  14. #include <Atomic/UI/UI.h>
  15. #include <AtomicJS/Javascript/Javascript.h>
  16. #include <ToolCore/ToolEnvironment.h>
  17. #include <ToolCore/License/LicenseEvents.h>
  18. #include <ToolCore/License/LicenseSystem.h>
  19. #include "../EditorMode/AEEditorMode.h"
  20. #include "AEEditorApp.h"
  21. using namespace ToolCore;
  22. namespace AtomicEditor
  23. {
  24. extern void jsapi_init_editor(JSVM* vm);
  25. AEEditorApp::AEEditorApp(Context* context) :
  26. AEEditorCommon(context)
  27. {
  28. }
  29. void AEEditorApp::Start()
  30. {
  31. AEEditorCommon::Start();
  32. context_->RegisterSubsystem(new EditorMode(context_));
  33. vm_->SetModuleSearchPaths("AtomicEditor/JavaScript;AtomicEditor/EditorScripts;AtomicEditor/EditorScripts/AtomicEditor");
  34. // Do not create bone structure by default when in the editor
  35. // this can be toggled temporarily, for example to setup an animation preview
  36. AnimatedModel::SetBoneCreationEnabled(false);
  37. // move UI initialization to JS
  38. UI* ui = GetSubsystem<UI>();
  39. ui->Initialize("AtomicEditor/resources/language/lng_en.tb.txt");
  40. SubscribeToEvent(E_JSERROR, HANDLER(AEEditorApp, HandleJSError));
  41. jsapi_init_editor(vm_);
  42. duk_get_global_string(vm_->GetJSContext(), "require");
  43. duk_push_string(vm_->GetJSContext(), "main");
  44. if (duk_pcall(vm_->GetJSContext(), 1) != 0)
  45. {
  46. vm_->SendJSErrorEvent();
  47. ErrorExit("Error executing main.js");
  48. }
  49. GetSubsystem<LicenseSystem>()->Initialize();
  50. }
  51. void AEEditorApp::Setup()
  52. {
  53. context_->SetEditorContext(true);
  54. AEEditorCommon::Setup();
  55. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  56. engineParameters_["WindowTitle"] = "AtomicEditor";
  57. engineParameters_["WindowResizable"] = true;
  58. engineParameters_["FullScreen"] = false;
  59. engineParameters_["LogLevel"] = LOG_DEBUG;
  60. FileSystem* filesystem = GetSubsystem<FileSystem>();
  61. engineParameters_["LogName"] = filesystem->GetAppPreferencesDir("AtomicEditor", "Logs") + "AtomicEditor.log";
  62. #ifdef ATOMIC_PLATFORM_OSX
  63. engineParameters_["WindowIcon"] = "Images/AtomicLogo32.png";
  64. #endif
  65. #ifdef ATOMIC_DEV_BUILD
  66. engineParameters_["ResourcePrefixPath"] = "";
  67. String resourcePaths = env->GetCoreDataDir() + ";" + env->GetEditorDataDir();
  68. // for dev builds, add the compile editor scripts from artifacts
  69. resourcePaths += ";" + env->GetRootSourceDir() + "Artifacts/Build/Resources/EditorData/";
  70. engineParameters_["ResourcePaths"] = resourcePaths;
  71. #else
  72. #ifdef ATOMIC_PLATFORM_OSX
  73. engineParameters_["ResourcePrefixPath"] = "../Resources";
  74. #else
  75. engineParameters_["ResourcePrefixPath"] = filesystem->GetProgramDir() + "Resources";
  76. #endif
  77. engineParameters_["ResourcePaths"] = "CoreData;EditorData";
  78. #endif // ATOMIC_DEV_BUILD
  79. String prefsPath = filesystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
  80. prefsPath += "prefs.json";
  81. JSONValue editorWindow;
  82. if (ReadPreferences(prefsPath, editorWindow, "editorWindow"))
  83. {
  84. if (editorWindow.IsObject())
  85. {
  86. engineParameters_["WindowPositionX"] = editorWindow.Get("x").GetUInt();
  87. engineParameters_["WindowPositionY"] = editorWindow.Get("y").GetUInt();
  88. engineParameters_["WindowWidth"] = editorWindow.Get("width").GetUInt();
  89. engineParameters_["WindowHeight"] = editorWindow.Get("height").GetUInt();
  90. }
  91. }
  92. }
  93. void AEEditorApp::Stop()
  94. {
  95. AEEditorCommon::Stop();
  96. }
  97. void AEEditorApp::HandleJSError(StringHash eventType, VariantMap& eventData)
  98. {
  99. using namespace JSError;
  100. //String errName = eventData[P_ERRORNAME].GetString();
  101. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  102. String errFilename = eventData[P_ERRORFILENAME].GetString();
  103. //String errStack = eventData[P_ERRORSTACK].GetString();
  104. int errLineNumber = vm_->GetRealLineNumber(errFilename, eventData[P_ERRORLINENUMBER].GetInt());
  105. String errorString = ToString("%s - %s - Line: %i", errFilename.CString(), errMessage.CString(), errLineNumber);
  106. ErrorExit(errorString);
  107. }
  108. }