UIResourceFrame.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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/SceneResourceEditor.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 == ".xml" || ext == ".txt")
  90. {
  91. //SceneResourceEditor* sre = new SceneResourceEditor(context_, fullpath, tabcontainer_);
  92. TextResourceEditor* tre = new TextResourceEditor(context_, fullpath, tabcontainer_);
  93. editor = tre;
  94. }
  95. else if (ext == ".mdl")
  96. {
  97. ModelResourceEditor* mre = new ModelResourceEditor(context_, fullpath, tabcontainer_);
  98. editor = mre;
  99. }
  100. else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".ogg")
  101. {
  102. FileSystem* fs = GetSubsystem<FileSystem>();
  103. fs->SystemOpen(fullpath);
  104. }
  105. else if (ext == ".tmx")
  106. {
  107. ExternalTooling* tooling = GetSubsystem<ExternalTooling>();
  108. tooling->LaunchOrOpen("AtomicTiled", fullpath);
  109. }
  110. if (editor)
  111. {
  112. // We have a new editor so send a stop playing if we are playing
  113. if (GetSubsystem<Editor>()->IsPlayingProject())
  114. {
  115. SendEvent(E_EDITORPLAYSTOP);
  116. }
  117. editors_[fullpath] = editor;
  118. tabcontainer_->SetCurrentPage(tabcontainer_->GetNumPages()-1);
  119. editorLookup_[tabcontainer_->GetCurrentPageWidget()] = editor;
  120. }
  121. }
  122. void ResourceFrame::FocusCurrentTab()
  123. {
  124. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  125. if (widget && editorLookup_.Contains(widget))
  126. return editorLookup_[widget]->SetFocus();
  127. }
  128. bool ResourceFrame::OnEvent(const TBWidgetEvent &ev)
  129. {
  130. if (ev.type == EVENT_TYPE_KEY_DOWN || ev.type == EVENT_TYPE_SHORTCUT
  131. || ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  132. {
  133. if (!editors_.Size())
  134. return false;
  135. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  136. if (editorLookup_.Contains(widget))
  137. return editorLookup_[widget]->OnEvent(ev);
  138. }
  139. return false;
  140. }
  141. bool ResourceFrame::IssuesWidgetVisible()
  142. {
  143. TBWidget *child;
  144. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  145. {
  146. if (child == issueswidget_->GetWidgetDelegate())
  147. break;
  148. }
  149. return child != NULL;
  150. }
  151. void ResourceFrame::ShowIssuesWidget(bool show)
  152. {
  153. if (show && ErrorsWidgetVisible())
  154. ShowErrorsWidget(false);
  155. if (show && ConsoleWidgetVisible())
  156. ShowConsoleWidget(false);
  157. TBWidget *child;
  158. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  159. {
  160. if (child == issueswidget_->GetWidgetDelegate())
  161. break;
  162. }
  163. if (show)
  164. {
  165. if (!child)
  166. {
  167. resourceLayout_->AddChild(issueswidget_->GetWidgetDelegate());
  168. }
  169. issueswidget_->UpdateIssues();
  170. }
  171. else
  172. {
  173. if (child)
  174. resourceLayout_->RemoveChild(child);
  175. }
  176. }
  177. bool ResourceFrame::ErrorsWidgetVisible()
  178. {
  179. TBWidget *child;
  180. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  181. {
  182. if (child == errorswidget_->GetWidgetDelegate())
  183. break;
  184. }
  185. return child != NULL;
  186. }
  187. void ResourceFrame::ShowErrorsWidget(bool show)
  188. {
  189. if (show && ConsoleWidgetVisible())
  190. ShowConsoleWidget(false);
  191. if (show && IssuesWidgetVisible())
  192. ShowIssuesWidget(false);
  193. TBWidget *child;
  194. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  195. {
  196. if (child == errorswidget_->GetWidgetDelegate())
  197. break;
  198. }
  199. if (show)
  200. {
  201. if (!child)
  202. {
  203. resourceLayout_->AddChild(errorswidget_->GetWidgetDelegate());
  204. }
  205. errorswidget_->UpdateErrors();
  206. }
  207. else
  208. {
  209. if (child)
  210. resourceLayout_->RemoveChild(child);
  211. }
  212. }
  213. bool ResourceFrame::ConsoleWidgetVisible()
  214. {
  215. TBWidget *child;
  216. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  217. {
  218. if (child == consolewidget_->GetWidgetDelegate())
  219. break;
  220. }
  221. return child != NULL;
  222. }
  223. void ResourceFrame::ShowConsoleWidget(bool show)
  224. {
  225. if (show && ErrorsWidgetVisible())
  226. ShowErrorsWidget(false);
  227. if (show && IssuesWidgetVisible())
  228. ShowIssuesWidget(false);
  229. TBWidget *child;
  230. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  231. {
  232. if (child == consolewidget_->GetWidgetDelegate())
  233. break;
  234. }
  235. if (show)
  236. {
  237. if (!child)
  238. {
  239. resourceLayout_->AddChild(consolewidget_->GetWidgetDelegate());
  240. }
  241. }
  242. else
  243. {
  244. if (child)
  245. resourceLayout_->RemoveChild(child);
  246. }
  247. }
  248. void ResourceFrame::NavigateToResource(const String& fullpath, int lineNumber, int tokenPos)
  249. {
  250. if (!editors_.Contains(fullpath))
  251. return;
  252. ResourceEditor* editor = editors_[fullpath];
  253. TBWidget* root = tabcontainer_->GetContentRoot();
  254. int i = 0;
  255. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext(), i++)
  256. {
  257. if (editorLookup_.Contains(child))
  258. {
  259. if (editorLookup_[child] == editor)
  260. {
  261. break;
  262. }
  263. }
  264. }
  265. if (i < tabcontainer_->GetNumPages())
  266. {
  267. tabcontainer_->SetCurrentPage(i);
  268. editor->SetFocus();
  269. // this cast could be better
  270. String ext = GetExtension(fullpath);
  271. if (ext == ".js" && lineNumber != -1)
  272. {
  273. JSResourceEditor* jse = (JSResourceEditor*) editor;
  274. jse->GotoLineNumber(lineNumber);
  275. }
  276. else if (ext == ".js" && tokenPos != -1)
  277. {
  278. JSResourceEditor* jse = (JSResourceEditor*) editor;
  279. jse->GotoTokenPos(tokenPos);
  280. }
  281. }
  282. }
  283. bool ResourceFrame::HasUnsavedModifications()
  284. {
  285. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr;
  286. for (itr = editors_.Begin(); itr != editors_.End(); itr++)
  287. {
  288. if (itr->second_->HasUnsavedModifications())
  289. return true;
  290. }
  291. return false;
  292. }
  293. void ResourceFrame::CloseResourceEditor(ResourceEditor* editor, bool navigateToAvailableResource)
  294. {
  295. assert(editors_.Contains(editor->GetFullPath()));
  296. editors_.Erase(editor->GetFullPath());
  297. TBWidget* root = tabcontainer_->GetContentRoot();
  298. bool found = false;
  299. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext())
  300. {
  301. if (editorLookup_.Contains(child))
  302. {
  303. if (editorLookup_[child] == editor)
  304. {
  305. found = true;
  306. root->RemoveChild(child);
  307. editorLookup_.Erase(child);
  308. break;
  309. }
  310. }
  311. }
  312. assert(found);
  313. tabcontainer_->SetCurrentPage(-1);
  314. if (navigateToAvailableResource)
  315. {
  316. if (editors_.Size())
  317. {
  318. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr = editors_.End();
  319. itr--;
  320. NavigateToResource(itr->second_->GetFullPath());
  321. }
  322. }
  323. }
  324. void ResourceFrame::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  325. {
  326. delegate_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  327. delegate_->SetIgnoreInput(true);
  328. delegate_->SetState(WIDGET_STATE_DISABLED, true);
  329. }
  330. void ResourceFrame::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  331. {
  332. delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  333. delegate_->SetIgnoreInput(false);
  334. delegate_->SetState(WIDGET_STATE_DISABLED, false);
  335. }
  336. void ResourceFrame::CloseAllResourceEditors()
  337. {
  338. Vector<SharedPtr<ResourceEditor> > editors = editors_.Values();
  339. for (unsigned i = 0; i < editors.Size(); i++)
  340. editors[i]->Close(false);
  341. }
  342. }