mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-30 03:32:36 +00:00
Add types.Player.sendMenuEvent
This commit is contained in:
parent
1490f6f082
commit
dafe858cb4
@ -13,6 +13,7 @@
|
||||
|
||||
#include "globalscripts.hpp"
|
||||
#include "localscripts.hpp"
|
||||
#include "menuscripts.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
@ -23,6 +24,7 @@ namespace MWLua
|
||||
mLocalEventBatch.clear();
|
||||
mNewGlobalEventBatch.clear();
|
||||
mNewLocalEventBatch.clear();
|
||||
mMenuEvents.clear();
|
||||
}
|
||||
|
||||
void LuaEvents::finalizeEventBatch()
|
||||
@ -51,6 +53,13 @@ namespace MWLua
|
||||
mLocalEventBatch.clear();
|
||||
}
|
||||
|
||||
void LuaEvents::callMenuEventHandlers()
|
||||
{
|
||||
for (const Global& e : mMenuEvents)
|
||||
mMenuScripts.receiveEvent(e.mEventName, e.mEventData);
|
||||
mMenuEvents.clear();
|
||||
}
|
||||
|
||||
template <typename Event>
|
||||
static void saveEvent(ESM::ESMWriter& esm, ESM::RefNum dest, const Event& event)
|
||||
{
|
||||
|
@ -23,12 +23,14 @@ namespace MWLua
|
||||
{
|
||||
|
||||
class GlobalScripts;
|
||||
class MenuScripts;
|
||||
|
||||
class LuaEvents
|
||||
{
|
||||
public:
|
||||
explicit LuaEvents(GlobalScripts& globalScripts)
|
||||
explicit LuaEvents(GlobalScripts& globalScripts, MenuScripts& menuScripts)
|
||||
: mGlobalScripts(globalScripts)
|
||||
, mMenuScripts(menuScripts)
|
||||
{
|
||||
}
|
||||
|
||||
@ -45,11 +47,13 @@ namespace MWLua
|
||||
};
|
||||
|
||||
void addGlobalEvent(Global event) { mNewGlobalEventBatch.push_back(std::move(event)); }
|
||||
void addMenuEvent(Global event) { mMenuEvents.push_back(std::move(event)); }
|
||||
void addLocalEvent(Local event) { mNewLocalEventBatch.push_back(std::move(event)); }
|
||||
|
||||
void clear();
|
||||
void finalizeEventBatch();
|
||||
void callEventHandlers();
|
||||
void callMenuEventHandlers();
|
||||
|
||||
void load(lua_State* lua, ESM::ESMReader& esm, const std::map<int, int>& contentFileMapping,
|
||||
const LuaUtil::UserdataSerializer* serializer);
|
||||
@ -57,10 +61,12 @@ namespace MWLua
|
||||
|
||||
private:
|
||||
GlobalScripts& mGlobalScripts;
|
||||
MenuScripts& mMenuScripts;
|
||||
std::vector<Global> mNewGlobalEventBatch;
|
||||
std::vector<Local> mNewLocalEventBatch;
|
||||
std::vector<Global> mGlobalEventBatch;
|
||||
std::vector<Local> mLocalEventBatch;
|
||||
std::vector<Global> mMenuEvents;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -229,6 +229,7 @@ namespace MWLua
|
||||
playerScripts->processInputEvent(event);
|
||||
}
|
||||
mInputEvents.clear();
|
||||
mLuaEvents.callMenuEventHandlers();
|
||||
mMenuScripts.update(0);
|
||||
if (playerScripts)
|
||||
playerScripts->onFrame(MWBase::Environment::get().getWorld()->getTimeManager()->isPaused()
|
||||
|
@ -172,7 +172,7 @@ namespace MWLua
|
||||
|
||||
MWWorld::Ptr mPlayer;
|
||||
|
||||
LuaEvents mLuaEvents{ mGlobalScripts };
|
||||
LuaEvents mLuaEvents{ mGlobalScripts, mMenuScripts };
|
||||
EngineEvents mEngineEvents{ mGlobalScripts };
|
||||
std::vector<MWBase::LuaManager::InputEvent> mInputEvents;
|
||||
|
||||
|
@ -35,14 +35,18 @@ namespace sol
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
static void verifyPlayer(const Object& player)
|
||||
{
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player!");
|
||||
}
|
||||
|
||||
void addPlayerQuestBindings(sol::table& player, const Context& context)
|
||||
{
|
||||
MWBase::Journal* const journal = MWBase::Environment::get().getJournal();
|
||||
|
||||
player["quests"] = [](const Object& player) {
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player!");
|
||||
verifyPlayer(player);
|
||||
bool allowChanges = dynamic_cast<const GObject*>(&player) != nullptr
|
||||
|| dynamic_cast<const SelfObject*>(&player) != nullptr;
|
||||
return Quests{ .mMutable = allowChanges };
|
||||
@ -134,28 +138,28 @@ namespace MWLua
|
||||
|
||||
MWBase::InputManager* input = MWBase::Environment::get().getInputManager();
|
||||
player["getControlSwitch"] = [input](const Object& player, std::string_view key) {
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player.");
|
||||
verifyPlayer(player);
|
||||
return input->getControlSwitch(key);
|
||||
};
|
||||
player["setControlSwitch"] = [input](const Object& player, std::string_view key, bool v) {
|
||||
verifyPlayer(player);
|
||||
if (dynamic_cast<const LObject*>(&player) && !dynamic_cast<const SelfObject*>(&player))
|
||||
throw std::runtime_error("Only player and global scripts can toggle control switches.");
|
||||
input->toggleControlSwitch(key, v);
|
||||
};
|
||||
player["isTeleportingEnabled"] = [](const Object& player) -> bool {
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player.");
|
||||
verifyPlayer(player);
|
||||
return MWBase::Environment::get().getWorld()->isTeleportingEnabled();
|
||||
};
|
||||
player["setTeleportingEnabled"] = [](const Object& player, bool state) {
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player.");
|
||||
verifyPlayer(player);
|
||||
if (dynamic_cast<const LObject*>(&player) && !dynamic_cast<const SelfObject*>(&player))
|
||||
throw std::runtime_error("Only player and global scripts can toggle teleportation.");
|
||||
MWBase::Environment::get().getWorld()->enableTeleporting(state);
|
||||
};
|
||||
player["setControlSwitch"] = [input](const Object& player, std::string_view key, bool v) {
|
||||
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
throw std::runtime_error("The argument must be a player.");
|
||||
if (dynamic_cast<const LObject*>(&player) && !dynamic_cast<const SelfObject*>(&player))
|
||||
throw std::runtime_error("Only player and global scripts can toggle control switches.");
|
||||
input->toggleControlSwitch(key, v);
|
||||
player["sendMenuEvent"] = [context](const Object& player, std::string eventName, const sol::object& eventData) {
|
||||
verifyPlayer(player);
|
||||
context.mLuaEvents->addMenuEvent({ std::move(eventName), LuaUtil::serialize(eventData) });
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user