UIResourceFrame.cpp 12 KB

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