JSResourceEditor.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Container/ArrayPtr.h>
  8. #include <Atomic/UI/UI.h>
  9. #include <Atomic/IO/Log.h>
  10. #include <Atomic/IO/File.h>
  11. #include <Atomic/IO/FileSystem.h>
  12. #include <Atomic/Resource/ResourceCache.h>
  13. #include <Atomic/Resource/JSONFile.h>
  14. #include <Atomic/Core/CoreEvents.h>
  15. #include <AtomicJS/Javascript/JSVM.h>
  16. #include <ToolCore/ToolEnvironment.h>
  17. #include <AtomicWebView/WebViewEvents.h>
  18. #include <AtomicWebView/UIWebView.h>
  19. #include <AtomicWebView/WebClient.h>
  20. #include <AtomicWebView/WebMessageHandler.h>
  21. #include "JSResourceEditor.h"
  22. using namespace tb;
  23. using namespace ToolCore;
  24. namespace AtomicEditor
  25. {
  26. JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
  27. ResourceEditor(context, fullpath, container)
  28. {
  29. TBLayout* layout = new TBLayout();
  30. layout->SetLayoutSize(LAYOUT_SIZE_GRAVITY);
  31. layout->SetGravity(WIDGET_GRAVITY_ALL);
  32. layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  33. rootContentWidget_->GetInternalWidget()->AddChild(layout);
  34. TBContainer* c = new TBContainer();
  35. c->SetGravity(WIDGET_GRAVITY_ALL);
  36. layout->AddChild(c);
  37. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  38. String codeEditorDir = tenv->GetToolDataDir();
  39. codeEditorDir += "CodeEditor/Editor.html";
  40. #ifdef ATOMIC_PLATFORM_OSX
  41. String url = "file://" + codeEditorDir;
  42. #else
  43. String url = "file:///" + codeEditorDir;
  44. #endif
  45. webView_ = new UIWebView(context_, url);
  46. webClient_ = webView_->GetWebClient();
  47. messageHandler_ = new WebMessageHandler(context_);
  48. webClient_->AddMessageHandler(messageHandler_);
  49. SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
  50. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  51. c->AddChild(webView_->GetInternalWidget());
  52. // FIXME: Set the size at the end of setup, so all children are updated accordingly
  53. // future size changes will be handled automatically
  54. IntRect rect = container_->GetContentRoot()->GetRect();
  55. rootContentWidget_->SetSize(rect.Width(), rect.Height());
  56. }
  57. JSResourceEditor::~JSResourceEditor()
  58. {
  59. }
  60. void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
  61. {
  62. webClient_->ExecuteJavaScript(ToString("loadCode(\"atomic://resources/%s\");", fullpath_.CString()));
  63. }
  64. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  65. {
  66. using namespace WebMessage;
  67. const String& request = eventData[P_REQUEST].GetString();
  68. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  69. if (request == "change")
  70. {
  71. SetModified(true);
  72. }
  73. else
  74. {
  75. JSONValue jvalue;
  76. if (JSONFile::ParseJSON(request, jvalue, false))
  77. {
  78. String message = jvalue["message"].GetString();
  79. if (message == "saveCode")
  80. {
  81. String code = jvalue["payload"].GetString();
  82. File file(context_, fullpath_, FILE_WRITE);
  83. file.Write((void*) code.CString(), code.Length());
  84. file.Close();
  85. }
  86. }
  87. }
  88. handler->Success();
  89. }
  90. void JSResourceEditor::FormatCode()
  91. {
  92. //webClient_->ExecuteJavaScript("beautifyCode();");
  93. }
  94. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  95. {
  96. if (ev.type == EVENT_TYPE_SHORTCUT)
  97. {
  98. if (ev.ref_id == TBIDC("close"))
  99. {
  100. RequestClose();
  101. }
  102. }
  103. return false;
  104. }
  105. void JSResourceEditor::FindTextClose()
  106. {
  107. }
  108. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  109. {
  110. return true;
  111. }
  112. void JSResourceEditor::SetFocus()
  113. {
  114. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  115. }
  116. void JSResourceEditor::GotoTokenPos(int tokenPos)
  117. {
  118. }
  119. void JSResourceEditor::GotoLineNumber(int lineNumber)
  120. {
  121. }
  122. bool JSResourceEditor::Save()
  123. {
  124. if (!modified_)
  125. return true;
  126. webClient_->ExecuteJavaScript("saveCode();");
  127. SetModified(false);
  128. return true;
  129. }
  130. }