// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved // Please see LICENSE.md in repository root for license information // https://github.com/AtomicGameEngine/AtomicEditor #include "AtomicEditor.h" #include #include #include #include #include #include #include #include #include "AEPlayer.h" #include "AEEvents.h" #include "AEEditor.h" #include "Project/AEProject.h" // TODO: Remove dependency #include namespace AtomicEditor { AEPlayer::AEPlayer(Context* context) : Object(context), mode_(AE_PLAYERMODE_WIDGET) { assert(!context->GetSubsystem()); context->RegisterSubsystem(this); Javascript* javascript = context->GetSubsystem(); vm_ = javascript->InstantiateVM("AEPlayerVM"); // only subscribe to errors on our VM SubscribeToEvent(vm_, E_JSERROR, HANDLER(AEPlayer, HandleJSError)); vm_->InitJSContext(); vm_->SetModuleSearchPath("Modules"); duk_eval_string_noresult(vm_->GetJSContext(), "require(\"AtomicGame\"); require (\"AtomicEditor\");"); } AEPlayer::~AEPlayer() { UnsubscribeFromEvent(E_JSERROR); Javascript* javascript = context_->GetSubsystem(); // this can be NULL when exiting during play mode if (javascript) javascript->ShutdownVM("AEPlayerVM"); vm_ = NULL; } void AEPlayer::Invalidate() { context_->RemoveSubsystem(); GetSubsystem()->GetRoot()->RemoveAllChildren(); } void AEPlayer::HandleJSError(StringHash eventType, VariantMap& eventData) { SendEvent(E_PLAYERERROR); AEPlayerError err; using namespace JSError; err.name_ = eventData[P_ERRORNAME].GetString(); err.message_ = eventData[P_ERRORMESSAGE].GetString(); err.filename_ = eventData[P_ERRORFILENAME].GetString(); err.stack_ = eventData[P_ERRORSTACK].GetString(); err.lineNumber_ = eventData[P_ERRORLINENUMBER].GetInt(); errors_.Push(err); } bool AEPlayer::Play(AEPlayerMode mode, const IntRect &rect) { mode_ = mode; duk_context* ctx = vm_->GetJSContext(); String init; init.AppendWithFormat("Atomic.editor.setViewport(%i, %i, %i, %i);", rect.left_, rect.top_, rect.right_, rect.bottom_); duk_eval_string_noresult(vm_->GetJSContext(),init.CString()); Editor* editor = GetSubsystem(); Project* project = editor->GetProject(); String mainPath = project->GetResourcePath(); bool ok = vm_->ExecuteMain(); if (!ok) { SendEvent(E_PLAYERERROR); } return ok; } }