UIResourceFrame.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. editorLookup_[editor->GetRootContentLayout()] = editor;
  124. tabcontainer_->SetCurrentPage(tabcontainer_->GetNumPages()-1);
  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_TAB_CHANGED && ev.target == tabcontainer_)
  136. {
  137. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  138. if (widget)
  139. {
  140. if (editorLookup_.Contains(widget))
  141. {
  142. ResourceEditor* editor = editorLookup_[widget];
  143. if (editor != currentResourceEditor_)
  144. {
  145. VariantMap eventData;
  146. eventData[EditorResourceEditorChanged::P_RESOURCEEDITOR] = editor;
  147. SendEvent(E_EDITORRESOURCEEDITORCHANGED, eventData);
  148. currentResourceEditor_ = editor;
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. if (ev.type == EVENT_TYPE_KEY_DOWN || ev.type == EVENT_TYPE_SHORTCUT
  155. || ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  156. {
  157. if (!editors_.Size())
  158. return false;
  159. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  160. if (editorLookup_.Contains(widget))
  161. return editorLookup_[widget]->OnEvent(ev);
  162. }
  163. return false;
  164. }
  165. bool ResourceFrame::IssuesWidgetVisible()
  166. {
  167. TBWidget *child;
  168. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  169. {
  170. if (child == issueswidget_->GetWidgetDelegate())
  171. break;
  172. }
  173. return child != NULL;
  174. }
  175. void ResourceFrame::ShowIssuesWidget(bool show)
  176. {
  177. if (show && ErrorsWidgetVisible())
  178. ShowErrorsWidget(false);
  179. if (show && ConsoleWidgetVisible())
  180. ShowConsoleWidget(false);
  181. TBWidget *child;
  182. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  183. {
  184. if (child == issueswidget_->GetWidgetDelegate())
  185. break;
  186. }
  187. if (show)
  188. {
  189. if (!child)
  190. {
  191. resourceLayout_->AddChild(issueswidget_->GetWidgetDelegate());
  192. }
  193. issueswidget_->UpdateIssues();
  194. }
  195. else
  196. {
  197. if (child)
  198. resourceLayout_->RemoveChild(child);
  199. }
  200. }
  201. bool ResourceFrame::ErrorsWidgetVisible()
  202. {
  203. TBWidget *child;
  204. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  205. {
  206. if (child == errorswidget_->GetWidgetDelegate())
  207. break;
  208. }
  209. return child != NULL;
  210. }
  211. void ResourceFrame::ShowErrorsWidget(bool show)
  212. {
  213. if (show && ConsoleWidgetVisible())
  214. ShowConsoleWidget(false);
  215. if (show && IssuesWidgetVisible())
  216. ShowIssuesWidget(false);
  217. TBWidget *child;
  218. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  219. {
  220. if (child == errorswidget_->GetWidgetDelegate())
  221. break;
  222. }
  223. if (show)
  224. {
  225. if (!child)
  226. {
  227. resourceLayout_->AddChild(errorswidget_->GetWidgetDelegate());
  228. }
  229. errorswidget_->UpdateErrors();
  230. }
  231. else
  232. {
  233. if (child)
  234. resourceLayout_->RemoveChild(child);
  235. }
  236. }
  237. bool ResourceFrame::ConsoleWidgetVisible()
  238. {
  239. TBWidget *child;
  240. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  241. {
  242. if (child == consolewidget_->GetWidgetDelegate())
  243. break;
  244. }
  245. return child != NULL;
  246. }
  247. void ResourceFrame::ShowConsoleWidget(bool show)
  248. {
  249. if (show && ErrorsWidgetVisible())
  250. ShowErrorsWidget(false);
  251. if (show && IssuesWidgetVisible())
  252. ShowIssuesWidget(false);
  253. TBWidget *child;
  254. for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
  255. {
  256. if (child == consolewidget_->GetWidgetDelegate())
  257. break;
  258. }
  259. if (show)
  260. {
  261. if (!child)
  262. {
  263. resourceLayout_->AddChild(consolewidget_->GetWidgetDelegate());
  264. }
  265. }
  266. else
  267. {
  268. if (child)
  269. resourceLayout_->RemoveChild(child);
  270. }
  271. }
  272. void ResourceFrame::SendCurrentEditorEvent(const TBWidgetEvent &ev)
  273. {
  274. TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
  275. if (!widget)
  276. return;
  277. if (editorLookup_.Contains(widget))
  278. editorLookup_[widget]->OnEvent(ev);
  279. }
  280. void ResourceFrame::NavigateToResource(const String& fullpath, int lineNumber, int tokenPos)
  281. {
  282. if (!editors_.Contains(fullpath))
  283. return;
  284. ResourceEditor* editor = editors_[fullpath];
  285. TBWidget* root = tabcontainer_->GetContentRoot();
  286. int i = 0;
  287. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext(), i++)
  288. {
  289. if (editorLookup_.Contains(child))
  290. {
  291. if (editorLookup_[child] == editor)
  292. {
  293. break;
  294. }
  295. }
  296. }
  297. if (i < tabcontainer_->GetNumPages())
  298. {
  299. tabcontainer_->SetCurrentPage(i);
  300. editor->SetFocus();
  301. // this cast could be better
  302. String ext = GetExtension(fullpath);
  303. if (ext == ".js" && lineNumber != -1)
  304. {
  305. JSResourceEditor* jse = (JSResourceEditor*) editor;
  306. jse->GotoLineNumber(lineNumber);
  307. }
  308. else if (ext == ".js" && tokenPos != -1)
  309. {
  310. JSResourceEditor* jse = (JSResourceEditor*) editor;
  311. jse->GotoTokenPos(tokenPos);
  312. }
  313. }
  314. }
  315. bool ResourceFrame::HasUnsavedModifications()
  316. {
  317. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr;
  318. for (itr = editors_.Begin(); itr != editors_.End(); itr++)
  319. {
  320. if (itr->second_->HasUnsavedModifications())
  321. return true;
  322. }
  323. return false;
  324. }
  325. void ResourceFrame::CloseResourceEditor(ResourceEditor* editor, bool navigateToAvailableResource)
  326. {
  327. assert(editors_.Contains(editor->GetFullPath()));
  328. editors_.Erase(editor->GetFullPath());
  329. TBWidget* root = tabcontainer_->GetContentRoot();
  330. bool found = false;
  331. for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext())
  332. {
  333. if (editorLookup_.Contains(child))
  334. {
  335. if (editorLookup_[child] == editor)
  336. {
  337. found = true;
  338. root->RemoveChild(child);
  339. editorLookup_.Erase(child);
  340. break;
  341. }
  342. }
  343. }
  344. assert(found);
  345. tabcontainer_->SetCurrentPage(-1);
  346. if (navigateToAvailableResource)
  347. {
  348. if (editors_.Size())
  349. {
  350. HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr = editors_.End();
  351. itr--;
  352. NavigateToResource(itr->second_->GetFullPath());
  353. }
  354. }
  355. }
  356. void ResourceFrame::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  357. {
  358. //delegate_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  359. delegate_->SetIgnoreInput(true);
  360. //delegate_->SetState(WIDGET_STATE_DISABLED, true);
  361. }
  362. void ResourceFrame::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  363. {
  364. //delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  365. delegate_->SetIgnoreInput(false);
  366. //delegate_->SetState(WIDGET_STATE_DISABLED, false);
  367. }
  368. void ResourceFrame::CloseAllResourceEditors()
  369. {
  370. Vector<SharedPtr<ResourceEditor> > editors = editors_.Values();
  371. for (unsigned i = 0; i < editors.Size(); i++)
  372. editors[i]->Close(false);
  373. }
  374. }