BsCodeEditor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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<WString> 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(const WString& 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. mActiveEditor->openFile(getSolutionPath(), path, lineNumber);
  52. }
  53. void CodeEditorManager::syncSolution() const
  54. {
  55. if (mActiveEditor == nullptr)
  56. return;
  57. CodeSolutionData slnData;
  58. slnData.name = gEditorApplication().getProjectName();
  59. Vector<UINT32> scriptTypeIds =
  60. {
  61. TID_ScriptCode, TID_PlainText, TID_GpuProgram, TID_GpuProgramInclude
  62. };
  63. Vector<ProjectLibrary::LibraryEntry*> libraryEntries = ProjectLibrary::instance().search(L"*", scriptTypeIds);
  64. // Game project
  65. slnData.projects.push_back(CodeProjectData());
  66. CodeProjectData& gameProject = slnData.projects.back();
  67. gameProject.name = toWString(SCRIPT_GAME_ASSEMBLY);
  68. //// Add references
  69. gameProject.assemblyReferences.push_back(CodeProjectReference{ L"System", Path::BLANK });
  70. gameProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEngine", gApplication().getEngineAssemblyPath() });
  71. // Editor project
  72. slnData.projects.push_back(CodeProjectData());
  73. CodeProjectData& editorProject = slnData.projects.back();
  74. editorProject.name = toWString(SCRIPT_EDITOR_ASSEMBLY);
  75. //// Add references
  76. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"System", Path::BLANK });
  77. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEngine", gApplication().getEngineAssemblyPath() });
  78. editorProject.assemblyReferences.push_back(CodeProjectReference{ L"BansheeEditor", gEditorApplication().getEditorAssemblyPath() });
  79. editorProject.projectReferences.push_back(CodeProjectReference{ gameProject.name, Path::BLANK});
  80. //// Add files for both projects
  81. for (auto& entry : libraryEntries)
  82. {
  83. if (entry->type != ProjectLibrary::LibraryEntryType::File)
  84. continue;
  85. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  86. if (resEntry->meta->getTypeID() == TID_ScriptCode)
  87. {
  88. SPtr<ScriptCodeImportOptions> scriptIO = std::static_pointer_cast<ScriptCodeImportOptions>(resEntry->meta->getImportOptions());
  89. bool isEditorScript = false;
  90. if (scriptIO != nullptr)
  91. isEditorScript = scriptIO->isEditorScript();
  92. if (isEditorScript)
  93. editorProject.codeFiles.push_back(resEntry->path);
  94. else
  95. gameProject.codeFiles.push_back(resEntry->path);
  96. }
  97. else
  98. gameProject.nonCodeFiles.push_back(resEntry->path);
  99. }
  100. mActiveEditor->syncSolution(slnData, getSolutionPath());
  101. }
  102. Path CodeEditorManager::getSolutionPath() const
  103. {
  104. Path path = gEditorApplication().getProjectPath();
  105. path.append(gEditorApplication().getProjectName() + L".sln");
  106. return path;
  107. }
  108. }