UINewProject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicEditor
  4. #include "AtomicEditor.h"
  5. #include <TurboBadger/tb_window.h>
  6. #include <TurboBadger/tb_select.h>
  7. #include <TurboBadger/tb_editfield.h>
  8. #include <Atomic/Core/Context.h>
  9. #include <Atomic/UI/TBUI.h>
  10. #include "Resources/AEResourceOps.h"
  11. #include "AEPreferences.h"
  12. #include "AEEditor.h"
  13. #include "AEEvents.h"
  14. #include "Project/AEProject.h"
  15. #include "Project/ProjectUtils.h"
  16. #include "UINewProject.h"
  17. namespace AtomicEditor
  18. {
  19. UINewProject::UINewProject(Context* context):
  20. UIModalOpWindow(context)
  21. {
  22. Editor* editor = GetSubsystem<Editor>();
  23. Project* project = editor->GetProject();
  24. TBUI* tbui = GetSubsystem<TBUI>();
  25. window_->SetText("New Project");
  26. tbui->LoadResourceFile(window_->GetContentRoot(), "AtomicEditor/editor/ui/newproject.tb.txt");
  27. window_->ResizeToFitContent();
  28. Center();
  29. }
  30. UINewProject::~UINewProject()
  31. {
  32. }
  33. bool UINewProject::Create2DProject(const String& fullpath)
  34. {
  35. Vector<String> elements = fullpath.Split('/');
  36. String projectName = elements.Back();
  37. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  38. #ifdef ATOMIC_PLATFORM_OSX
  39. String templateSourceDir = fileSystem->GetAppBundleResourceFolder();
  40. #else
  41. String templateSourceDir = fileSystem->GetProgramDir();
  42. #endif
  43. templateSourceDir += "/ProjectTemplates/Project2D";
  44. fileSystem->CopyDir(templateSourceDir + "/Resources", fullpath + "/Resources");
  45. File file(context_, fullpath + "/" + projectName + ".atomic", FILE_WRITE);
  46. file.Close();
  47. return true;
  48. }
  49. bool UINewProject::CreateEmptyProject(const String& fullpath)
  50. {
  51. Vector<String> elements = fullpath.Split('/');
  52. String projectName = elements.Back();
  53. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  54. #ifdef ATOMIC_PLATFORM_OSX
  55. String templateSourceDir = fileSystem->GetAppBundleResourceFolder();
  56. #else
  57. String templateSourceDir = fileSystem->GetProgramDir();
  58. #endif
  59. templateSourceDir += "/ProjectTemplates/EmptyProject";
  60. fileSystem->CopyDir(templateSourceDir + "/Resources", fullpath + "/Resources");
  61. File file(context_, fullpath + "/" + projectName + ".atomic", FILE_WRITE);
  62. file.Close();
  63. return true;
  64. }
  65. bool UINewProject::Create3DProject(const String& fullpath)
  66. {
  67. Vector<String> elements = fullpath.Split('/');
  68. String projectName = elements.Back();
  69. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  70. #ifdef ATOMIC_PLATFORM_OSX
  71. String templateSourceDir = fileSystem->GetAppBundleResourceFolder();
  72. #else
  73. String templateSourceDir = fileSystem->GetProgramDir();
  74. #endif
  75. templateSourceDir += "/ProjectTemplates/Project3D";
  76. fileSystem->CopyDir(templateSourceDir + "/Resources", fullpath + "/Resources");
  77. File file(context_, fullpath + "/" + projectName + ".atomic", FILE_WRITE);
  78. file.Close();
  79. return true;
  80. }
  81. bool UINewProject::OnEvent(const TBWidgetEvent &ev)
  82. {
  83. Editor* editor = GetSubsystem<Editor>();
  84. UIModalOps* ops = GetSubsystem<UIModalOps>();
  85. if (ev.type == EVENT_TYPE_CLICK)
  86. {
  87. if (ev.target->GetID() == TBIDC("cancel"))
  88. {
  89. ops->Hide();
  90. return true;
  91. }
  92. int projectType = -1;
  93. if (ev.target->GetID() == TBIDC("project_empty"))
  94. {
  95. projectType = 0;
  96. }
  97. else if (ev.target->GetID() == TBIDC("project_2d"))
  98. {
  99. projectType = 1;
  100. }
  101. else if (ev.target->GetID() == TBIDC("project_3d"))
  102. {
  103. projectType = 2;
  104. }
  105. if (projectType != -1)
  106. {
  107. String projectPath = GetSubsystem<ProjectUtils>()->NewProjectFileDialog();
  108. if (!projectPath.Length())
  109. return true;
  110. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  111. if (!fileSystem->DirExists(projectPath))
  112. {
  113. editor->PostModalInfo("New Project Editor Error", "Project folder does not exist");
  114. return true;
  115. }
  116. Vector<String> results;
  117. fileSystem->ScanDir(results, projectPath, "*", SCAN_DIRS | SCAN_FILES, false);
  118. while (results.Remove(".")) {}
  119. while (results.Remove("..")) {}
  120. if (results.Size())
  121. {
  122. editor->PostModalInfo("New Project Editor Error", "Project folder must be empty.\nPlease create a new folder or select an empty one");
  123. return true;
  124. }
  125. bool result = false;
  126. if (projectType == 0)
  127. {
  128. result = CreateEmptyProject(projectPath);
  129. }
  130. else if (projectType == 1)
  131. {
  132. result = Create2DProject(projectPath);
  133. }
  134. else if (projectType == 2)
  135. {
  136. result = Create3DProject(projectPath);
  137. }
  138. if (!result)
  139. {
  140. editor->PostModalInfo("New Project Editor Error", "Error Creating Project");
  141. return true;
  142. }
  143. Vector<String> elements = projectPath.Split('/');
  144. String projectName = elements.Back();
  145. editor->LoadProject(projectPath + "/" + projectName + ".atomic");
  146. ops->Hide();
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. }