UIResourceFrame.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/Container/ArrayPtr.h>
  6. #include <Atomic/UI/TBUI.h>
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Resource/ResourceCache.h>
  11. #include <Atomic/Core/CoreEvents.h>
  12. #include <Atomic/Javascript/JSEvents.h>
  13. #include "UIResourceFrame.h"
  14. #include "../Editors/JSResourceEditor.h"
  15. #include "../Editors/SceneEditor3D/SceneEditor3D.h"
  16. #include "../Editors/ModelResourceEditor.h"
  17. #include "../Editors/TextResourceEditor.h"
  18. #include "../AEEvents.h"
  19. #include "../AEEditor.h"
  20. #include "UIIssuesWidget.h"
  21. #include "UIErrorsWidget.h"
  22. #include "UIConsoleWidget.h"
  23. #include "../Tools/External/AEExternalTooling.h"
  24. using namespace tb;
  25. namespace AtomicEditor
  26. {
  27. ResourceFrame::ResourceFrame(Context* context) :
  28. AEWidget(context),
  29. tabcontainer_(0),
  30. resourceLayout_(0)
  31. {
  32. TBUI* tbui = GetSubsystem<TBUI>();
  33. tbui->LoadResourceFile(delegate_, "AtomicEditor/editor/ui/resourceframe.tb.txt");
  34. tabcontainer_ = delegate_->GetWidgetByIDAndType<TBTabContainer>(TBIDC("tabcontainer"));
  35. assert(tabcontainer_);
  36. resourceLayout_ = delegate_->GetWidgetByIDAndType<TBLayout>(TBIDC("resourcelayout"));
  37. assert(resourceLayout_);
  38. delegate_->SetGravity(WIDGET_GRAVITY_ALL);
  39. //delegate_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  40. issueswidget_ = new IssuesWidget(context_);
  41. errorswidget_ = new ErrorsWidget(context_);
  42. consolewidget_ = new ConsoleWidget(context_);
  43. SubscribeToEvent(E_FINDTEXT, HANDLER(ResourceFrame, HandleFindText));
  44. SubscribeToEvent(E_FINDTEXTCLOSE, HANDLER(ResourceFrame, HandleFindTextClose));
  45. SubscribeToEvent(E_EDITORPLAYSTARTED, HANDLER(ResourceFrame, HandlePlayStarted));
  46. SubscribeToEvent(E_EDITORPLAYSTOPPED, HANDLER(ResourceFrame, HandlePlayStopped));
  47. }
  48. ResourceFrame::~ResourceFrame()
  49. {
  50. }
  51. void ResourceFrame::HandleFindText(StringHash eventType, VariantMap& eventData)
  52. {
  53. using namespace FindText;
  54. if (!editors_.Size())
  55. return;
  56. int page = tabcontainer_->GetCurrentPage();
  57. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  58. if (editorLookup_.Contains(widget))
  59. editorLookup_[widget]->FindText(eventData[P_TEXT].ToString(),
  60. (unsigned) eventData[P_FLAGS].GetInt());
  61. }
  62. void ResourceFrame::HandleFindTextClose(StringHash eventType, VariantMap& eventData)
  63. {
  64. if (!editors_.Size())
  65. return;
  66. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  67. if (editorLookup_.Contains(widget))
  68. return editorLookup_[widget]->FindTextClose();
  69. }
  70. void ResourceFrame::EditResource(const String& fullpath)
  71. {
  72. if (editors_.Contains(fullpath))
  73. {
  74. NavigateToResource(fullpath);
  75. if (GetSubsystem<Editor>()->IsPlayingProject())
  76. {
  77. SendEvent(E_EDITORPLAYSTOP);
  78. }
  79. return;
  80. }
  81. delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  82. String ext = GetExtension(fullpath);
  83. ResourceEditor* editor = NULL;
  84. if (ext == ".js")
  85. {
  86. JSResourceEditor* jse = new JSResourceEditor(context_, fullpath, tabcontainer_);
  87. editor = jse;
  88. }
  89. else if (ext == ".scene")
  90. {
  91. SceneEditor3D* sre = new SceneEditor3D(context_, fullpath, tabcontainer_);
  92. editor = sre;
  93. }
  94. else if (ext == ".xml" || ext == ".txt")
  95. {
  96. //SceneResourceEditor* sre = new SceneResourceEditor(context_, fullpath, tabcontainer_);
  97. TextResourceEditor* tre = new TextResourceEditor(context_, fullpath, tabcontainer_);
  98. editor = tre;
  99. }
  100. else if (ext == ".mdl")
  101. {
  102. ModelResourceEditor* mre = new ModelResourceEditor(context_, fullpath, tabcontainer_);
  103. editor = mre;
  104. }
  105. else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".ogg")
  106. {
  107. FileSystem* fs = GetSubsystem<FileSystem>();
  108. fs->SystemOpen(fullpath);
  109. }
  110. else if (ext == ".tmx")
  111. {
  112. ExternalTooling* tooling = GetSubsystem<ExternalTooling>();
  113. tooling->LaunchOrOpen("AtomicTiled", fullpath);
  114. }
  115. if (editor)
  116. {
  117. // We have a new editor so send a stop playing if we are playing
  118. if (GetSubsystem<Editor>()->IsPlayingProject())
  119. {
  120. SendEvent(E_EDITORPLAYSTOP);
  121. }
  122. editors_[fullpath] = editor;
  123. tabcontainer_->SetCurrentPage(tabcontainer_->GetNumPages()-1);
  124. editorLookup_[tabcontainer_->GetCurrentPageWidget()] = editor;
  125. }
  126. }
  127. void ResourceFrame::FocusCurrentTab()
  128. {
  129. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  130. if (widget && editorLookup_.Contains(widget))
  131. return editorLookup_[widget]->SetFocus();
  132. }
  133. bool ResourceFrame::OnEvent(const TBWidgetEvent &ev)
  134. {
  135. if (ev.type == EVENT_TYPE_KEY_DOWN || ev.type == EVENT_TYPE_SHORTCUT
  136. || ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  137. {
  138. if (!editors_.Size())
  139. return false;
  140. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  141. if (editorLookup_.Contains(widget))
  142. return editorLookup_[widget]->OnEvent(ev);
  143. }
  144. return false;
  145. }
  146. bool ResourceFrame::IssuesWidgetVisible()
  147. {
  148. TBWidget *child;
  149. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  150. {
  151. if (child == issueswidget_->GetWidgetDelegate())
  152. break;
  153. }
  154. return child != NULL;
  155. }
  156. void ResourceFrame::ShowIssuesWidget(bool show)
  157. {
  158. if (show && ErrorsWidgetVisible())
  159. ShowErrorsWidget(false);
  160. if (show && ConsoleWidgetVisible())
  161. ShowConsoleWidget(false);
  162. TBWidget *child;
  163. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  164. {
  165. if (child == issueswidget_->GetWidgetDelegate())
  166. break;
  167. }
  168. if (show)
  169. {
  170. if (!child)
  171. {
  172. resourceLayout_->AddChild(issueswidget_->GetWidgetDelegate());
  173. }
  174. issueswidget_->UpdateIssues();
  175. }
  176. else
  177. {
  178. if (child)
  179. resourceLayout_->RemoveChild(child);
  180. }
  181. }
  182. bool ResourceFrame::ErrorsWidgetVisible()
  183. {
  184. TBWidget *child;
  185. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  186. {
  187. if (child == errorswidget_->GetWidgetDelegate())
  188. break;
  189. }
  190. return child != NULL;
  191. }
  192. void ResourceFrame::ShowErrorsWidget(bool show)
  193. {
  194. if (show && ConsoleWidgetVisible())
  195. ShowConsoleWidget(false);
  196. if (show && IssuesWidgetVisible())
  197. ShowIssuesWidget(false);
  198. TBWidget *child;
  199. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  200. {
  201. if (child == errorswidget_->GetWidgetDelegate())
  202. break;
  203. }
  204. if (show)
  205. {
  206. if (!child)
  207. {
  208. resourceLayout_->AddChild(errorswidget_->GetWidgetDelegate());
  209. }
  210. errorswidget_->UpdateErrors();
  211. }
  212. else
  213. {
  214. if (child)
  215. resourceLayout_->RemoveChild(child);
  216. }
  217. }
  218. bool ResourceFrame::ConsoleWidgetVisible()
  219. {
  220. TBWidget *child;
  221. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  222. {
  223. if (child == consolewidget_->GetWidgetDelegate())
  224. break;
  225. }
  226. return child != NULL;
  227. }
  228. void ResourceFrame::ShowConsoleWidget(bool show)
  229. {
  230. if (show && ErrorsWidgetVisible())
  231. ShowErrorsWidget(false);
  232. if (show && IssuesWidgetVisible())
  233. ShowIssuesWidget(false);
  234. TBWidget *child;
  235. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  236. {
  237. if (child == consolewidget_->GetWidgetDelegate())
  238. break;
  239. }
  240. if (show)
  241. {
  242. if (!child)
  243. {
  244. resourceLayout_->AddChild(consolewidget_->GetWidgetDelegate());
  245. }
  246. }
  247. else
  248. {
  249. if (child)
  250. resourceLayout_->RemoveChild(child);
  251. }
  252. }
  253. void ResourceFrame::NavigateToResource(const String& fullpath, int lineNumber, int tokenPos)
  254. {
  255. if (!editors_.Contains(fullpath))
  256. return;
  257. ResourceEditor* editor = editors_[fullpath];
  258. TBWidget* root = tabcontainer_->GetContentRoot();
  259. int i = 0;
  260. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext(), i++)
  261. {
  262. if (editorLookup_.Contains(child))
  263. {
  264. if (editorLookup_[child] == editor)
  265. {
  266. break;
  267. }
  268. }
  269. }
  270. if (i < tabcontainer_->GetNumPages())
  271. {
  272. tabcontainer_->SetCurrentPage(i);
  273. editor->SetFocus();
  274. // this cast could be better
  275. String ext = GetExtension(fullpath);
  276. if (ext == ".js" && lineNumber != -1)
  277. {
  278. JSResourceEditor* jse = (JSResourceEditor*) editor;
  279. jse->GotoLineNumber(lineNumber);
  280. }
  281. else if (ext == ".js" && tokenPos != -1)
  282. {
  283. JSResourceEditor* jse = (JSResourceEditor*) editor;
  284. jse->GotoTokenPos(tokenPos);
  285. }
  286. }
  287. }
  288. bool ResourceFrame::HasUnsavedModifications()
  289. {
  290. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr;
  291. for (itr = editors_.Begin(); itr != editors_.End(); itr++)
  292. {
  293. if (itr->second_->HasUnsavedModifications())
  294. return true;
  295. }
  296. return false;
  297. }
  298. void ResourceFrame::CloseResourceEditor(ResourceEditor* editor, bool navigateToAvailableResource)
  299. {
  300. assert(editors_.Contains(editor->GetFullPath()));
  301. editors_.Erase(editor->GetFullPath());
  302. TBWidget* root = tabcontainer_->GetContentRoot();
  303. bool found = false;
  304. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext())
  305. {
  306. if (editorLookup_.Contains(child))
  307. {
  308. if (editorLookup_[child] == editor)
  309. {
  310. found = true;
  311. root->RemoveChild(child);
  312. editorLookup_.Erase(child);
  313. break;
  314. }
  315. }
  316. }
  317. assert(found);
  318. tabcontainer_->SetCurrentPage(-1);
  319. if (navigateToAvailableResource)
  320. {
  321. if (editors_.Size())
  322. {
  323. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr = editors_.End();
  324. itr--;
  325. NavigateToResource(itr->second_->GetFullPath());
  326. }
  327. }
  328. }
  329. void ResourceFrame::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  330. {
  331. delegate_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  332. delegate_->SetIgnoreInput(true);
  333. delegate_->SetState(WIDGET_STATE_DISABLED, true);
  334. }
  335. void ResourceFrame::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  336. {
  337. delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  338. delegate_->SetIgnoreInput(false);
  339. delegate_->SetState(WIDGET_STATE_DISABLED, false);
  340. }
  341. void ResourceFrame::CloseAllResourceEditors()
  342. {
  343. Vector<SharedPtr<ResourceEditor> > editors = editors_.Values();
  344. for (unsigned i = 0; i < editors.Size(); i++)
  345. editors[i]->Close(false);
  346. }
  347. }