JSResourceEditor.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/Resource/ResourceEvents.h>
  30. #include <Atomic/Core/CoreEvents.h>
  31. #include <AtomicJS/Javascript/JSVM.h>
  32. #include <ToolCore/ToolEnvironment.h>
  33. #include <AtomicWebView/WebViewEvents.h>
  34. #include <AtomicWebView/UIWebView.h>
  35. #include <AtomicWebView/WebClient.h>
  36. #include <AtomicWebView/WebMessageHandler.h>
  37. #include <AtomicWebView/WebTexture2D.h>
  38. #include "JSResourceEditor.h"
  39. using namespace tb;
  40. using namespace ToolCore;
  41. namespace AtomicEditor
  42. {
  43. JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
  44. ResourceEditor(context, fullpath, container)
  45. {
  46. TBLayout* layout = new TBLayout();
  47. layout->SetLayoutSize(LAYOUT_SIZE_GRAVITY);
  48. layout->SetGravity(WIDGET_GRAVITY_ALL);
  49. layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  50. rootContentWidget_->GetInternalWidget()->AddChild(layout);
  51. TBContainer* c = new TBContainer();
  52. c->SetGravity(WIDGET_GRAVITY_ALL);
  53. layout->AddChild(c);
  54. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  55. String codeEditorDir = tenv->GetToolDataDir();
  56. codeEditorDir += "CodeEditor/Editor.html";
  57. #ifdef ATOMIC_PLATFORM_OSX
  58. String url = "file://" + codeEditorDir;
  59. #else
  60. String url = "file:///" + codeEditorDir;
  61. #endif
  62. webView_ = new UIWebView(context_, url);
  63. webClient_ = webView_->GetWebClient();
  64. messageHandler_ = new WebMessageHandler(context_);
  65. webClient_->AddMessageHandler(messageHandler_);
  66. webView_->GetWebTexture2D()->SetClearColor(Color(.23f, .23f, .23f, 1));
  67. SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
  68. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  69. SubscribeToEvent(E_RENAMERESOURCENOTIFICATION, HANDLER(JSResourceEditor, HandleRenameResourceNotification));
  70. SubscribeToEvent(E_DELETERESOURCENOTIFICATION, HANDLER(JSResourceEditor, HandleDeleteResourceNotification));
  71. SubscribeToEvent(E_PROJECTUNLOADEDNOTIFICATION, HANDLER(JSResourceEditor, HandleProjectUnloadedNotification));
  72. c->AddChild(webView_->GetInternalWidget());
  73. }
  74. JSResourceEditor::~JSResourceEditor()
  75. {
  76. }
  77. String getNormalizedPath(const String& path)
  78. {
  79. // Full path is the fully qualified path from the root of the filesystem. In order
  80. // to take advantage of the resource caching system, let's trim it down to just the
  81. // path inside the resources directory including the Resources directory so that the casing
  82. // is correct.
  83. const String& RESOURCES_MARKER = "resources/";
  84. return path.SubstringUTF8(path.ToLower().Find(RESOURCES_MARKER));
  85. }
  86. void JSResourceEditor::HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData)
  87. {
  88. using namespace RenameResourceNotification;
  89. const String& newPath = eventData[P_NEWRESOURCEPATH].GetString();
  90. const String& path = eventData[P_RESOURCEPATH].GetString();
  91. webClient_->ExecuteJavaScript(ToString("HOST_resourceRenamed(\"%s\",\"%s\");", getNormalizedPath(path).CString(), getNormalizedPath(newPath).CString()));
  92. if (fullpath_.Compare(path) == 0) {
  93. fullpath_ = newPath;
  94. SetModified(modified_);
  95. }
  96. }
  97. void JSResourceEditor::HandleDeleteResourceNotification(StringHash eventType, VariantMap& eventData)
  98. {
  99. using namespace DeleteResourceNotification;
  100. const String& path = eventData[P_RESOURCEPATH].GetString();
  101. webClient_->ExecuteJavaScript(ToString("HOST_resourceDeleted(\"%s\");", getNormalizedPath(path).CString()));
  102. }
  103. void JSResourceEditor::HandleProjectUnloadedNotification(StringHash eventType, VariantMap& eventData)
  104. {
  105. webClient_->ExecuteJavaScript("HOST_projectUnloaded();");
  106. }
  107. void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
  108. {
  109. // need to wait until we get an editor load complete message since we could
  110. // still be streaming things in.
  111. }
  112. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  113. {
  114. using namespace WebMessage;
  115. const String& request = eventData[P_REQUEST].GetString();
  116. const String& EDITOR_CHANGE = "editorChange";
  117. const String& EDITOR_SAVE_CODE = "editorSaveCode";
  118. const String& EDITOR_SAVE_FILE = "editorSaveFile";
  119. const String& EDITOR_LOAD_COMPLETE = "editorLoadComplete";
  120. String normalizedPath = getNormalizedPath(fullpath_);
  121. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  122. if (request == EDITOR_CHANGE)
  123. {
  124. SetModified(true);
  125. }
  126. else if (request == EDITOR_LOAD_COMPLETE)
  127. {
  128. // We need to wait until the editor javascript is all required in to call the
  129. // method to load the code. The HandleWebViewLoadEnd event is getting called
  130. // too soon.
  131. webClient_->ExecuteJavaScript(ToString("HOST_loadCode(\"atomic://%s\");", normalizedPath.CString()));
  132. }
  133. else
  134. {
  135. JSONValue jvalue;
  136. if (JSONFile::ParseJSON(request, jvalue, false))
  137. {
  138. String message = jvalue["message"].GetString();
  139. if (message == EDITOR_SAVE_CODE)
  140. {
  141. String code = jvalue["payload"].GetString();
  142. File file(context_, fullpath_, FILE_WRITE);
  143. file.Write((void*) code.CString(), code.Length());
  144. file.Close();
  145. }
  146. else if (message == EDITOR_SAVE_FILE)
  147. {
  148. String code = jvalue["payload"].GetString();
  149. String fn = jvalue["filename"].GetString();
  150. // TODO: determine if we are absolute path or partial path
  151. File file(context_, fn, FILE_WRITE);
  152. file.Write((void*) code.CString(), code.Length());
  153. file.Close();
  154. }
  155. }
  156. }
  157. handler->Success();
  158. }
  159. void JSResourceEditor::FormatCode()
  160. {
  161. //webClient_->ExecuteJavaScript("beautifyCode();");
  162. }
  163. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  164. {
  165. if (ev.type == EVENT_TYPE_SHORTCUT)
  166. {
  167. if (ev.ref_id == TBIDC("close"))
  168. {
  169. RequestClose();
  170. }
  171. }
  172. return false;
  173. }
  174. void JSResourceEditor::FindTextClose()
  175. {
  176. }
  177. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  178. {
  179. return true;
  180. }
  181. void JSResourceEditor::SetFocus()
  182. {
  183. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  184. }
  185. void JSResourceEditor::GotoTokenPos(int tokenPos)
  186. {
  187. }
  188. void JSResourceEditor::GotoLineNumber(int lineNumber)
  189. {
  190. }
  191. bool JSResourceEditor::Save()
  192. {
  193. if (!modified_)
  194. return true;
  195. webClient_->ExecuteJavaScript("HOST_saveCode();");
  196. SetModified(false);
  197. return true;
  198. }
  199. }