JSResourceEditor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Container/ArrayPtr.h>
  8. #include <Atomic/UI/UI.h>
  9. #include <Atomic/IO/Log.h>
  10. #include <Atomic/IO/File.h>
  11. #include <Atomic/IO/FileSystem.h>
  12. #include <Atomic/Resource/ResourceCache.h>
  13. #include <Atomic/Resource/JSONFile.h>
  14. #include <Atomic/Core/CoreEvents.h>
  15. #include <AtomicJS/Javascript/JSVM.h>
  16. #include <ToolCore/ToolEnvironment.h>
  17. #include <AtomicWebView/WebViewEvents.h>
  18. #include <AtomicWebView/UIWebView.h>
  19. #include <AtomicWebView/WebClient.h>
  20. #include <AtomicWebView/WebMessageHandler.h>
  21. #include <AtomicWebView/WebTexture2D.h>
  22. #include "JSResourceEditor.h"
  23. using namespace tb;
  24. using namespace ToolCore;
  25. namespace AtomicEditor
  26. {
  27. JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
  28. ResourceEditor(context, fullpath, container)
  29. {
  30. TBLayout* layout = new TBLayout();
  31. layout->SetLayoutSize(LAYOUT_SIZE_GRAVITY);
  32. layout->SetGravity(WIDGET_GRAVITY_ALL);
  33. layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  34. rootContentWidget_->GetInternalWidget()->AddChild(layout);
  35. TBContainer* c = new TBContainer();
  36. c->SetGravity(WIDGET_GRAVITY_ALL);
  37. layout->AddChild(c);
  38. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  39. String codeEditorDir = tenv->GetToolDataDir();
  40. codeEditorDir += "CodeEditor/Editor.html";
  41. #ifdef ATOMIC_PLATFORM_OSX
  42. String url = "file://" + codeEditorDir;
  43. #else
  44. String url = "file:///" + codeEditorDir;
  45. #endif
  46. webView_ = new UIWebView(context_, url);
  47. webClient_ = webView_->GetWebClient();
  48. messageHandler_ = new WebMessageHandler(context_);
  49. webClient_->AddMessageHandler(messageHandler_);
  50. webView_->GetWebTexture2D()->SetClearColor(Color(.23f, .23f, .23f, 1));
  51. SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
  52. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  53. c->AddChild(webView_->GetInternalWidget());
  54. }
  55. JSResourceEditor::~JSResourceEditor()
  56. {
  57. }
  58. void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
  59. {
  60. webClient_->ExecuteJavaScript(ToString("loadCode(\"atomic://resources/%s\");", fullpath_.CString()));
  61. }
  62. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  63. {
  64. using namespace WebMessage;
  65. const String& request = eventData[P_REQUEST].GetString();
  66. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  67. if (request == "change")
  68. {
  69. SetModified(true);
  70. }
  71. else
  72. {
  73. JSONValue jvalue;
  74. if (JSONFile::ParseJSON(request, jvalue, false))
  75. {
  76. String message = jvalue["message"].GetString();
  77. if (message == "saveCode")
  78. {
  79. String code = jvalue["payload"].GetString();
  80. File file(context_, fullpath_, FILE_WRITE);
  81. file.Write((void*) code.CString(), code.Length());
  82. file.Close();
  83. }
  84. }
  85. }
  86. handler->Success();
  87. }
  88. void JSResourceEditor::FormatCode()
  89. {
  90. //webClient_->ExecuteJavaScript("beautifyCode();");
  91. }
  92. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  93. {
  94. if (ev.type == EVENT_TYPE_SHORTCUT)
  95. {
  96. if (ev.ref_id == TBIDC("close"))
  97. {
  98. RequestClose();
  99. }
  100. }
  101. return false;
  102. }
  103. void JSResourceEditor::FindTextClose()
  104. {
  105. }
  106. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  107. {
  108. return true;
  109. }
  110. void JSResourceEditor::SetFocus()
  111. {
  112. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  113. }
  114. void JSResourceEditor::GotoTokenPos(int tokenPos)
  115. {
  116. }
  117. void JSResourceEditor::GotoLineNumber(int lineNumber)
  118. {
  119. }
  120. bool JSResourceEditor::Save()
  121. {
  122. if (!modified_)
  123. return true;
  124. webClient_->ExecuteJavaScript("saveCode();");
  125. SetModified(false);
  126. return true;
  127. }
  128. }