JSResourceEditor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <AtomicWebView/WebViewEvents.h>
  17. #include <AtomicWebView/UIWebView.h>
  18. #include <AtomicWebView/WebClient.h>
  19. #include <AtomicWebView/WebMessageHandler.h>
  20. #include "JSResourceEditor.h"
  21. using namespace tb;
  22. namespace AtomicEditor
  23. {
  24. JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UITabContainer *container) :
  25. ResourceEditor(context, fullpath, container)
  26. {
  27. TBLayout* layout = new TBLayout();
  28. layout->SetLayoutSize(LAYOUT_SIZE_GRAVITY);
  29. layout->SetGravity(WIDGET_GRAVITY_ALL);
  30. layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  31. rootContentWidget_->GetInternalWidget()->AddChild(layout);
  32. TBContainer* c = new TBContainer();
  33. c->SetGravity(WIDGET_GRAVITY_ALL);
  34. layout->AddChild(c);
  35. String url = "file:///Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicEditor/CodeEditor/Editor.html";
  36. webView_ = new UIWebView(context_, url);
  37. webClient_ = webView_->GetWebClient();
  38. messageHandler_ = new WebMessageHandler(context_);
  39. webClient_->AddMessageHandler(messageHandler_);
  40. SubscribeToEvent(webClient_, E_WEBVIEWLOADEND, HANDLER(JSResourceEditor, HandleWebViewLoadEnd));
  41. SubscribeToEvent(messageHandler_, E_WEBMESSAGE, HANDLER(JSResourceEditor, HandleWebMessage));
  42. c->AddChild(webView_->GetInternalWidget());
  43. // FIXME: Set the size at the end of setup, so all children are updated accordingly
  44. // future size changes will be handled automatically
  45. IntRect rect = container_->GetContentRoot()->GetRect();
  46. rootContentWidget_->SetSize(rect.Width(), rect.Height());
  47. }
  48. JSResourceEditor::~JSResourceEditor()
  49. {
  50. }
  51. void JSResourceEditor::HandleWebViewLoadEnd(StringHash eventType, VariantMap& eventData)
  52. {
  53. webClient_->ExecuteJavaScript(ToString("loadCode(\"atomic://resources/%s\");", fullpath_.CString()));
  54. }
  55. void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventData)
  56. {
  57. using namespace WebMessage;
  58. const String& request = eventData[P_REQUEST].GetString();
  59. WebMessageHandler* handler = static_cast<WebMessageHandler*>(eventData[P_HANDLER].GetPtr());
  60. if (request == "change")
  61. {
  62. SetModified(true);
  63. }
  64. else
  65. {
  66. JSONValue jvalue;
  67. if (JSONFile::ParseJSON(request, jvalue, false))
  68. {
  69. String message = jvalue["message"].GetString();
  70. if (message == "saveCode")
  71. {
  72. String code = jvalue["payload"].GetString();
  73. File file(context_, fullpath_, FILE_WRITE);
  74. file.Write((void*) code.CString(), code.Length());
  75. file.Close();
  76. }
  77. }
  78. }
  79. handler->Success();
  80. }
  81. void JSResourceEditor::FormatCode()
  82. {
  83. }
  84. bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
  85. {
  86. if (ev.type == EVENT_TYPE_SHORTCUT)
  87. {
  88. if (ev.ref_id == TBIDC("close"))
  89. {
  90. RequestClose();
  91. }
  92. }
  93. return false;
  94. }
  95. void JSResourceEditor::FindTextClose()
  96. {
  97. }
  98. bool JSResourceEditor::FindText(const String& findText, unsigned flags)
  99. {
  100. return true;
  101. }
  102. void JSResourceEditor::SetFocus()
  103. {
  104. //editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  105. }
  106. void JSResourceEditor::GotoTokenPos(int tokenPos)
  107. {
  108. }
  109. void JSResourceEditor::GotoLineNumber(int lineNumber)
  110. {
  111. }
  112. bool JSResourceEditor::Save()
  113. {
  114. if (!modified_)
  115. return true;
  116. webClient_->ExecuteJavaScript("saveCode();");
  117. SetModified(false);
  118. return true;
  119. }
  120. }