JSResourceEditor.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // need to wait until we get an editor load complete message since we could
  76. // still be streaming things in.
  77. }
  78. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  79. {
  80. using namespace WebMessage;
  81. const String& request = eventData[P_REQUEST].GetString();
  82. const String& EDITOR_CHANGE = "editorChange";
  83. const String& EDITOR_SAVE_CODE = "editorSaveCode";
  84. const String& EDITOR_SAVE_FILE = "editorSaveFile";
  85. const String& EDITOR_LOAD_COMPLETE = "editorLoadComplete";
  86. const String& RESOURCES_MARKER = "resources/";
  87. // Full path is the fully qualified path from the root of the filesystem. In order
  88. // to take advantage of the resource caching system, let's trim it down to just the
  89. // path inside the resources directory including the Resources directory so that the casing
  90. // correct.
  91. String normalizedPath = fullpath_.SubstringUTF8(fullpath_.ToLower().Find(RESOURCES_MARKER));
  92. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  93. if (request == EDITOR_CHANGE)
  94. {
  95. SetModified(true);
  96. }
  97. else if (request == EDITOR_LOAD_COMPLETE)
  98. {
  99. // We need to wait until the editor javascript is all required in to call the
  100. // method to load the code. The HandleWebViewLoadEnd event is getting called
  101. // too soon.
  102. webClient_->ExecuteJavaScript(ToString("HOST_loadCode(\"atomic://%s\");", normalizedPath.CString()));
  103. }
  104. else
  105. {
  106. JSONValue jvalue;
  107. if (JSONFile::ParseJSON(request, jvalue, false))
  108. {
  109. String message = jvalue["message"].GetString();
  110. if (message == EDITOR_SAVE_CODE)
  111. {
  112. String code = jvalue["payload"].GetString();
  113. File file(context_, fullpath_, FILE_WRITE);
  114. file.Write((void*) code.CString(), code.Length());
  115. file.Close();
  116. }
  117. else if (message == EDITOR_SAVE_FILE)
  118. {
  119. String code = jvalue["payload"].GetString();
  120. String fn = jvalue["filename"].GetString();
  121. // TODO: determine if we are absolute path or partial path
  122. File file(context_, fn, FILE_WRITE);
  123. file.Write((void*) code.CString(), code.Length());
  124. file.Close();
  125. }
  126. }
  127. }
  128. handler->Success();
  129. }
  130. void JSResourceEditor::FormatCode()
  131. {
  132. //webClient_->ExecuteJavaScript("beautifyCode();");
  133. }
  134. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  135. {
  136. if (ev.type == EVENT_TYPE_SHORTCUT)
  137. {
  138. if (ev.ref_id == TBIDC("close"))
  139. {
  140. RequestClose();
  141. }
  142. }
  143. return false;
  144. }
  145. void JSResourceEditor::FindTextClose()
  146. {
  147. }
  148. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  149. {
  150. return true;
  151. }
  152. void JSResourceEditor::SetFocus()
  153. {
  154. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  155. }
  156. void JSResourceEditor::GotoTokenPos(int tokenPos)
  157. {
  158. }
  159. void JSResourceEditor::GotoLineNumber(int lineNumber)
  160. {
  161. }
  162. bool JSResourceEditor::Save()
  163. {
  164. if (!modified_)
  165. return true;
  166. webClient_->ExecuteJavaScript("HOST_saveCode();");
  167. SetModified(false);
  168. return true;
  169. }
  170. }