BsCodeEditor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "BsCodeEditor.h"
  2. #include "BsEditorApplication.h"
  3. #include "BsProjectLibrary.h"
  4. #include "BsProjectResourceMeta.h"
  5. #include "BsScriptCodeImportOptions.h"
  6. #if BS_PLATFORM == BS_PLATFORM_WIN32
  7. #include "Win32/BsVSCodeEditor.h"
  8. #else
  9. static_assert("Make sure to add implementations for other platforms.");
  10. #endif
  11. namespace BansheeEngine
  12. {
  13. CodeEditorManager::CodeEditorManager()
  14. :mActiveEditor(nullptr)
  15. {
  16. #if BS_PLATFORM == BS_PLATFORM_WIN32
  17. VSCodeEditorFactory* vsCodeEditorFactory = bs_new<VSCodeEditorFactory>();
  18. Vector<CodeEditorType> vsEditors = vsCodeEditorFactory->getAvailableEditors();
  19. for(auto& editor : vsEditors)
  20. {
  21. mFactoryPerEditor[editor] = vsCodeEditorFactory;
  22. mEditors.push_back(editor);
  23. }
  24. mFactories.push_back(vsCodeEditorFactory);
  25. #else
  26. static_assert("Make sure to add implementations for other platforms.");
  27. #endif
  28. }
  29. CodeEditorManager::~CodeEditorManager()
  30. {
  31. for (auto& factory : mFactories)
  32. bs_delete(factory);
  33. if (mActiveEditor != nullptr)
  34. bs_delete(mActiveEditor);
  35. }
  36. void CodeEditorManager::setActive(CodeEditorType editor)
  37. {
  38. if (mActiveEditor != nullptr)
  39. {
  40. bs_delete(mActiveEditor);
  41. mActiveEditor = nullptr;
  42. }
  43. auto findIter = mFactoryPerEditor.find(editor);
  44. if (findIter == mFactoryPerEditor.end())
  45. return;
  46. mActiveEditor = findIter->second->create(editor);
  47. }
  48. void CodeEditorManager::openFile(const Path& path, UINT32 lineNumber) const
  49. {
  50. if (mActiveEditor == nullptr)
  51. return;
  52. Path filePath = path;
  53. if (!path.isAbsolute())
  54. filePath.makeAbsolute(gEditorApplication().getProjectPath());
  55. mActiveEditor->openFile(getSolutionPath(), filePath, lineNumber);
  56. }
  57. void CodeEditorManager::syncSolution() const
  58. {
  59. if (mActiveEditor == nullptr)
  60. return;
  61. CodeSolutionData slnData;
  62. slnData.name = gEditorApplication().getProjectName();
  63. Vector<UINT32> scriptTypeIds =
  64. {
  65. TID_ScriptCode, TID_PlainText, TID_GpuProgram, TID_GpuProgramInclude
  66. };
  67. Vector<ProjectLibrary::LibraryEntry*> libraryEntries = ProjectLibrary::instance().search(L"*", scriptTypeIds);
  68. slnData.projects.push_back(CodeProjectData());
  69. slnData.projects.push_back(CodeProjectData());
  70. // Game project
  71. CodeProjectData& gameProject = slnData.projects[0];
  72. gameProject.name = toWString(SCRIPT_GAME_ASSEMBLY);
  73. //// Add references
  74. gameProject.assemblyReferences.push_back(CodeProjectReference{ L"System", Path::BLANK });
  75. gameProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEngine", gApplication().getEngineAssemblyPath() });
  76. // Editor project
  77. CodeProjectData& editorProject = slnData.projects[1];
  78. editorProject.name = toWString(SCRIPT_EDITOR_ASSEMBLY);
  79. //// Add references
  80. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"System", Path::BLANK });
  81. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEngine", gApplication().getEngineAssemblyPath() });
  82. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEditor", gEditorApplication().getEditorAssemblyPath() });
  83. editorProject.projectReferences.push_back(CodeProjectReference{ gameProject.name, Path::BLANK });
  84. //// Add files for both projects
  85. for (auto& entry : libraryEntries)
  86. {
  87. if (entry->type != ProjectLibrary::LibraryEntryType::File)
  88. continue;
  89. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  90. if (resEntry->meta->getTypeID() == TID_ScriptCode)
  91. {
  92. SPtr<ScriptCodeImportOptions> scriptIO = std::static_pointer_cast<ScriptCodeImportOptions>(resEntry->meta->getImportOptions());
  93. bool isEditorScript = false;
  94. if (scriptIO != nullptr)
  95. isEditorScript = scriptIO->isEditorScript();
  96. if (isEditorScript)
  97. editorProject.codeFiles.push_back(resEntry->path);
  98. else
  99. gameProject.codeFiles.push_back(resEntry->path);
  100. }
  101. else
  102. gameProject.nonCodeFiles.push_back(resEntry->path);
  103. }
  104. mActiveEditor->syncSolution(slnData, gEditorApplication().getProjectPath());
  105. }
  106. Path CodeEditorManager::getSolutionPath() const
  107. {
  108. Path path = gEditorApplication().getProjectPath();
  109. path.append(gEditorApplication().getProjectName() + L".sln");
  110. return path;
  111. }
  112. }