JSResourceEditor.cpp 3.4 KB

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