JSResourceEditor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Container/ArrayPtr.h>
  23. #include <Atomic/UI/UI.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/Resource/ResourceCache.h>
  28. #include <Atomic/Resource/JSONFile.h>
  29. #include <Atomic/Core/CoreEvents.h>
  30. #include <AtomicJS/Javascript/JSVM.h>
  31. #include <ToolCore/ToolEnvironment.h>
  32. #include <AtomicWebView/WebViewEvents.h>
  33. #include <AtomicWebView/UIWebView.h>
  34. #include <AtomicWebView/WebClient.h>
  35. #include <AtomicWebView/WebMessageHandler.h>
  36. #include <AtomicWebView/WebTexture2D.h>
  37. #include "JSResourceEditor.h"
  38. using namespace tb;
  39. using namespace ToolCore;
  40. namespace AtomicEditor
  41. {
  42. JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
  43. ResourceEditor(context, fullpath, container)
  44. {
  45. TBLayout* layout = new TBLayout();
  46. layout->SetLayoutSize(LAYOUT_SIZE_GRAVITY);
  47. layout->SetGravity(WIDGET_GRAVITY_ALL);
  48. layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  49. rootContentWidget_->GetInternalWidget()->AddChild(layout);
  50. TBContainer* c = new TBContainer();
  51. c->SetGravity(WIDGET_GRAVITY_ALL);
  52. layout->AddChild(c);
  53. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  54. String codeEditorDir = tenv->GetToolDataDir();
  55. codeEditorDir += "CodeEditor/Editor.html";
  56. #ifdef ATOMIC_PLATFORM_OSX
  57. String url = "file://" + codeEditorDir;
  58. #else
  59. String url = "file:///" + codeEditorDir;
  60. #endif
  61. webView_ = new UIWebView(context_, url);
  62. webClient_ = webView_->GetWebClient();
  63. messageHandler_ = new WebMessageHandler(context_);
  64. webClient_->AddMessageHandler(messageHandler_);
  65. webView_->GetWebTexture2D()->SetClearColor(Color(.23f, .23f, .23f, 1));
  66. SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
  67. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  68. c->AddChild(webView_->GetInternalWidget());
  69. }
  70. JSResourceEditor::~JSResourceEditor()
  71. {
  72. }
  73. void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
  74. {
  75. webClient_->ExecuteJavaScript(ToString("loadCode(\"atomic://resources/%s\");", fullpath_.CString()));
  76. }
  77. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  78. {
  79. using namespace WebMessage;
  80. const String& request = eventData[P_REQUEST].GetString();
  81. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  82. if (request == "change")
  83. {
  84. SetModified(true);
  85. }
  86. else
  87. {
  88. JSONValue jvalue;
  89. if (JSONFile::ParseJSON(request, jvalue, false))
  90. {
  91. String message = jvalue["message"].GetString();
  92. if (message == "saveCode")
  93. {
  94. String code = jvalue["payload"].GetString();
  95. File file(context_, fullpath_, FILE_WRITE);
  96. file.Write((void*) code.CString(), code.Length());
  97. file.Close();
  98. }
  99. }
  100. }
  101. handler->Success();
  102. }
  103. void JSResourceEditor::FormatCode()
  104. {
  105. //webClient_->ExecuteJavaScript("beautifyCode();");
  106. }
  107. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  108. {
  109. if (ev.type == EVENT_TYPE_SHORTCUT)
  110. {
  111. if (ev.ref_id == TBIDC("close"))
  112. {
  113. RequestClose();
  114. }
  115. }
  116. return false;
  117. }
  118. void JSResourceEditor::FindTextClose()
  119. {
  120. }
  121. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  122. {
  123. return true;
  124. }
  125. void JSResourceEditor::SetFocus()
  126. {
  127. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  128. }
  129. void JSResourceEditor::GotoTokenPos(int tokenPos)
  130. {
  131. }
  132. void JSResourceEditor::GotoLineNumber(int lineNumber)
  133. {
  134. }
  135. bool JSResourceEditor::Save()
  136. {
  137. if (!modified_)
  138. return true;
  139. webClient_->ExecuteJavaScript("saveCode();");
  140. SetModified(false);
  141. return true;
  142. }
  143. }