Engine handlers onActive/onInactive and a function self:isActive()

dont-compose-content
Petr Mikheev 4 years ago
parent d5cda61855
commit de349962d1

@ -33,6 +33,7 @@ namespace MWLua
selfAPI[sol::meta_function::to_string] = [](SelfObject& self) { return "openmw.self[" + self.toString() + "]"; };
selfAPI["object"] = sol::readonly_property([](SelfObject& self) -> LObject { return LObject(self); });
selfAPI["controls"] = sol::readonly_property([](SelfObject& self) { return &self.mControls; });
selfAPI["isActive"] = [](SelfObject& self) { return &self.mIsActive; };
selfAPI["setDirectControl"] = [](SelfObject& self, bool v) { self.mControls.controlledFromLua = v; };
selfAPI["setEquipment"] = [manager=context.mLuaManager](const SelfObject& obj, sol::table equipment)
{
@ -87,6 +88,18 @@ namespace MWLua
{
mData.mControls.controlledFromLua = false;
this->addPackage("openmw.self", sol::make_object(lua->sol(), &mData));
registerEngineHandlers({&mOnActiveHandlers, &mOnInactiveHandlers});
}
void LocalScripts::becomeActive()
{
mData.mIsActive = true;
callEngineHandlers(mOnActiveHandlers);
}
void LocalScripts::becomeInactive()
{
mData.mIsActive = false;
callEngineHandlers(mOnInactiveHandlers);
}
}

@ -26,12 +26,19 @@ namespace MWLua
struct SelfObject : public LObject
{
SelfObject(const LObject& obj) : LObject(obj) {}
SelfObject(const LObject& obj) : LObject(obj), mIsActive(false) {}
MWBase::LuaManager::ActorControls mControls;
bool mIsActive;
};
void becomeActive();
void becomeInactive();
protected:
LocalScripts(LuaUtil::LuaState* lua, const LObject& obj);
SelfObject mData;
private:
EngineHandlerList mOnActiveHandlers{"onActive"};
EngineHandlerList mOnInactiveHandlers{"onInactive"};
};
}

@ -44,7 +44,9 @@ namespace MWLua
localContext.mSerializer = mLocalSerializer.get();
initObjectBindingsForGlobalScripts(context);
initCellBindingsForGlobalScripts(context);
initObjectBindingsForLocalScripts(localContext);
initCellBindingsForLocalScripts(localContext);
LocalScripts::initializeSelfPackage(localContext);
mLua.addCommonPackage("openmw.async", getAsyncPackageInitializer(context));
@ -158,6 +160,13 @@ namespace MWLua
}
mKeyPressEvents.clear();
for (LocalScripts* localScripts : mObjectInactiveEvents)
localScripts->becomeInactive();
for (LocalScripts* localScripts : mObjectActiveEvents)
localScripts->becomeActive();
mObjectActiveEvents.clear();
mObjectInactiveEvents.clear();
for (ObjectId id : mActorAddedEvents)
mGlobalScripts.actorActive(GObject(id, mWorldView.getObjectRegistry()));
mActorAddedEvents.clear();
@ -190,6 +199,8 @@ namespace MWLua
mGlobalEvents.clear();
mKeyPressEvents.clear();
mActorAddedEvents.clear();
mObjectActiveEvents.clear();
mObjectInactiveEvents.clear();
mPlayerChanged = false;
mPlayerScripts = nullptr;
mWorldView.clear();
@ -207,7 +218,10 @@ namespace MWLua
LocalScripts* localScripts = ptr.getRefData().getLuaScripts();
if (localScripts)
{
mActiveLocalScripts.insert(localScripts);
mObjectActiveEvents.push_back(localScripts);
}
if (ptr.getClass().isActor() && ptr != mPlayer)
mActorAddedEvents.push_back(getId(ptr));
@ -233,7 +247,10 @@ namespace MWLua
mWorldView.objectRemovedFromScene(ptr);
LocalScripts* localScripts = ptr.getRefData().getLuaScripts();
if (localScripts)
{
mActiveLocalScripts.erase(localScripts);
mObjectInactiveEvents.push_back(localScripts);
}
// TODO: call mWorldView.objectUnloaded if object is unloaded from memory (does it ever happen?) and ptr becomes invalid.
}
@ -242,7 +259,7 @@ namespace MWLua
{
mKeyPressEvents.push_back(arg.keysym);
}
const MWBase::LuaManager::ActorControls* LuaManager::getActorControls(const MWWorld::Ptr& ptr) const
{
LocalScripts* localScripts = ptr.getRefData().getLuaScripts();

@ -94,6 +94,8 @@ namespace MWLua
std::vector<SDL_Keysym> mKeyPressEvents;
std::vector<ObjectId> mActorAddedEvents;
std::vector<LocalScripts*> mObjectActiveEvents;
std::vector<LocalScripts*> mObjectInactiveEvents;
// Queued actions that should be done in main thread. Processed by applyQueuedChanges().
std::vector<std::unique_ptr<Action>> mActionQueue;

Loading…
Cancel
Save