AEPlayer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/Core/Context.h>
  6. #include <Atomic/IO/FileSystem.h>
  7. #include <Atomic/Input/Input.h>
  8. #include <Atomic/Resource/ResourceCache.h>
  9. #include <Atomic/Javascript/Javascript.h>
  10. #include <Atomic/Javascript/JSVM.h>
  11. #include <Atomic/Javascript/JSEvents.h>
  12. #include <Atomic/UI/TBUI.h>
  13. #include <Atomic/UI/UI.h>
  14. #include "AEPlayer.h"
  15. #include "AEEvents.h"
  16. #include "AEEditor.h"
  17. #include "Project/AEProject.h"
  18. #include "UIPlayer.h"
  19. #include "UI/Modal/UIModalOps.h"
  20. // TODO: Remove dependency
  21. #include <Duktape/duktape.h>
  22. namespace AtomicEditor
  23. {
  24. static int js_atomiceditor_SetView(duk_context* ctx)
  25. {
  26. JSVM* vm = JSVM::GetJSVM(ctx);
  27. AEPlayer* player = vm->GetSubsystem<AEPlayer>();
  28. Scene* scene = js_to_class_instance<Scene>(ctx, 0, 0);
  29. Camera* camera = js_to_class_instance<Camera>(ctx, 1, 0);
  30. player->GetUIPlayer()->SetView(scene, camera);
  31. return 0;
  32. }
  33. AEPlayer::AEPlayer(Context* context) :
  34. Object(context),
  35. mode_(AE_PLAYERMODE_WIDGET)
  36. {
  37. SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(AEPlayer, HandleEditorShutdown));
  38. assert(!context->GetSubsystem<AEPlayer>());
  39. context->RegisterSubsystem(this);
  40. Javascript* javascript = context->GetSubsystem<Javascript>();
  41. vm_ = javascript->InstantiateVM("AEPlayerVM");
  42. // only subscribe to errors on our VM
  43. SubscribeToEvent(vm_, E_JSERROR, HANDLER(AEPlayer, HandleJSError));
  44. vm_->InitJSContext();
  45. if (errors_.Size())
  46. return;
  47. vm_->SetModuleSearchPath("Modules");
  48. duk_eval_string_noresult(vm_->GetJSContext(), "require(\"AtomicGame\"); require (\"AtomicEditor\");");
  49. }
  50. AEPlayer::~AEPlayer()
  51. {
  52. UnsubscribeFromEvent(E_JSERROR);
  53. Javascript* javascript = context_->GetSubsystem<Javascript>();
  54. // this can be NULL when exiting during play mode
  55. if (javascript)
  56. javascript->ShutdownVM("AEPlayerVM");
  57. vm_ = NULL;
  58. GetSubsystem<Input>()->SetTouchEmulation(false);
  59. }
  60. void AEPlayer::Invalidate()
  61. {
  62. UIModalOps* ops = GetSubsystem<UIModalOps>();
  63. ops->Hide();
  64. context_->RemoveSubsystem<AEPlayer>();
  65. GetSubsystem<UI>()->GetRoot()->RemoveAllChildren();
  66. }
  67. void AEPlayer::HandleJSError(StringHash eventType, VariantMap& eventData)
  68. {
  69. SendEvent(E_PLAYERERROR);
  70. AEPlayerError err;
  71. using namespace JSError;
  72. err.name_ = eventData[P_ERRORNAME].GetString();
  73. err.message_ = eventData[P_ERRORMESSAGE].GetString();
  74. err.filename_ = eventData[P_ERRORFILENAME].GetString();
  75. err.stack_ = eventData[P_ERRORSTACK].GetString();
  76. err.lineNumber_ = eventData[P_ERRORLINENUMBER].GetInt();
  77. errors_.Push(err);
  78. }
  79. bool AEPlayer::Play(AEPlayerMode mode, const IntRect &rect)
  80. {
  81. if (errors_.Size())
  82. return false;
  83. mode_ = mode;
  84. UIModalOps* ops = GetSubsystem<UIModalOps>();
  85. ops->ShowPlayer();
  86. duk_context* ctx = vm_->GetJSContext();
  87. duk_get_global_string(ctx, "Atomic");
  88. duk_get_prop_string(ctx, -1, "editor");
  89. duk_push_c_function(ctx, js_atomiceditor_SetView, 2);
  90. duk_put_prop_string(ctx, -2, "setView");
  91. duk_pop_2(ctx);
  92. bool ok = vm_->ExecuteMain();
  93. if (!ok)
  94. {
  95. SendEvent(E_PLAYERERROR);
  96. }
  97. return ok;
  98. }
  99. void AEPlayer::SetUIPlayer(UIPlayer* uiPlayer)
  100. {
  101. uiPlayer_ = uiPlayer;
  102. }
  103. void AEPlayer::HandleEditorShutdown(StringHash eventType, VariantMap& eventData)
  104. {
  105. context_->RemoveSubsystem(GetType());
  106. }
  107. }