JSResourceEditor.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, const String &editorUrl) :
  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. webView_ = new UIWebView(context_, editorUrl);
  57. webClient_ = webView_->GetWebClient();
  58. messageHandler_ = new WebMessageHandler(context_);
  59. webClient_->AddMessageHandler(messageHandler_);
  60. webView_->GetWebTexture2D()->SetClearColor(Color(.23f, .23f, .23f, 1));
  61. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  62. SubscribeToEvent(E_RENAMERESOURCENOTIFICATION, HANDLER(JSResourceEditor, HandleRenameResourceNotification));
  63. c->AddChild(webView_->GetInternalWidget());
  64. }
  65. JSResourceEditor::~JSResourceEditor()
  66. {
  67. }
  68. String getNormalizedPath(const String& path)
  69. {
  70. // Full path is the fully qualified path from the root of the filesystem. In order
  71. // to take advantage of the resource caching system, let's trim it down to just the
  72. // path inside the resources directory including the Resources directory so that the casing
  73. // is correct.
  74. const String& RESOURCES_MARKER = "resources/";
  75. return path.SubstringUTF8(path.ToLower().Find(RESOURCES_MARKER));
  76. }
  77. // This needs to stay in place until these properties are accessible from the .ts side
  78. void JSResourceEditor::HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData)
  79. {
  80. using namespace RenameResourceNotification;
  81. const String& newPath = eventData[P_NEWRESOURCEPATH].GetString();
  82. const String& path = eventData[P_RESOURCEPATH].GetString();
  83. webClient_->ExecuteJavaScript(ToString("HOST_resourceRenamed(\"%s\",\"%s\");", getNormalizedPath(path).CString(), getNormalizedPath(newPath).CString()));
  84. if (fullpath_.Compare(path) == 0) {
  85. fullpath_ = newPath;
  86. SetModified(modified_);
  87. }
  88. }
  89. // This needs to stay in place until there is a way for the .ts side to provide back a "success" method
  90. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  91. {
  92. using namespace WebMessage;
  93. const String& request = eventData[P_REQUEST].GetString();
  94. const String& EDITOR_CHANGE = "editorChange";
  95. const String& EDITOR_SAVE_CODE = "editorSaveCode";
  96. const String& EDITOR_SAVE_FILE = "editorSaveFile";
  97. const String& EDITOR_GET_USER_PREFS = "editorGetUserPrefs";
  98. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  99. // All messages come in as a JSON string with a "message" property describing what the message is
  100. JSONValue jvalue;
  101. if (JSONFile::ParseJSON(request, jvalue, false))
  102. {
  103. String message = jvalue["message"].GetString();
  104. if (message == EDITOR_CHANGE) {
  105. SetModified(true);
  106. }
  107. else if (message == EDITOR_SAVE_CODE)
  108. {
  109. String code = jvalue["payload"].GetString();
  110. File file(context_, fullpath_, FILE_WRITE);
  111. file.Write((void*) code.CString(), code.Length());
  112. file.Close();
  113. }
  114. else if (message == EDITOR_SAVE_FILE)
  115. {
  116. // filename coming in should be a fully qualified path
  117. String code = jvalue["payload"].GetString();
  118. String fn = jvalue["filename"].GetString();
  119. // NOTE: We only want to be able save into the resource directory, so check to see if the file coming in
  120. // should live in the resource directory and also for safety check that there is no funky path navigation
  121. // going on such as my/resource/../../../out.file
  122. ToolSystem* tsys = GetSubsystem<ToolSystem>();
  123. if (fn.Find(tsys->GetProject()->GetResourcePath(), 0, false) != String::NPOS
  124. && fn.Find("..", 0) == String::NPOS )
  125. {
  126. File file(context_, fn, FILE_WRITE);
  127. file.Write((void*) code.CString(), code.Length());
  128. file.Close();
  129. } else {
  130. LOGWARNING("Ignoring attempt to write file: " + fn);
  131. }
  132. }
  133. else if (message == EDITOR_GET_USER_PREFS)
  134. {
  135. ToolSystem* tsys = GetSubsystem<ToolSystem>();
  136. Project* proj = tsys->GetProject();
  137. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  138. if (fileSystem->FileExists(proj->GetUserPrefsFullPath()))
  139. {
  140. webClient_->ExecuteJavaScript(ToString("HOST_loadPreferences(\"atomic://%s\");", proj->GetUserPrefsFullPath().CString()));
  141. }
  142. }
  143. }
  144. handler->Success();
  145. }
  146. void JSResourceEditor::FormatCode()
  147. {
  148. //webClient_->ExecuteJavaScript("beautifyCode();");
  149. }
  150. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  151. {
  152. if (ev.type == EVENT_TYPE_SHORTCUT)
  153. {
  154. if (ev.ref_id == TBIDC("close"))
  155. {
  156. RequestClose();
  157. }
  158. }
  159. return false;
  160. }
  161. void JSResourceEditor::FindTextClose()
  162. {
  163. }
  164. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  165. {
  166. return true;
  167. }
  168. void JSResourceEditor::SetFocus()
  169. {
  170. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  171. }
  172. void JSResourceEditor::GotoTokenPos(int tokenPos)
  173. {
  174. }
  175. void JSResourceEditor::GotoLineNumber(int lineNumber)
  176. {
  177. }
  178. bool JSResourceEditor::Save()
  179. {
  180. if (!modified_)
  181. return true;
  182. webClient_->ExecuteJavaScript("HOST_saveCode();");
  183. SetModified(false);
  184. return true;
  185. }
  186. }