JSResourceEditor.cpp 9.1 KB

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