mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 14:59:54 +00:00
Fix LuaUtil::Callback
This commit is contained in:
parent
be100749f8
commit
d16fa553c8
6 changed files with 16 additions and 9 deletions
|
@ -6,6 +6,9 @@ namespace sol
|
|||
{
|
||||
template <>
|
||||
struct is_automagical<MWLua::AsyncPackageId> : std::false_type {};
|
||||
|
||||
template <>
|
||||
struct is_automagical<LuaUtil::Callback> : std::false_type {};
|
||||
}
|
||||
|
||||
namespace MWLua
|
||||
|
@ -50,11 +53,15 @@ namespace MWLua
|
|||
asyncId.mContainer->setupUnsavableTimer(
|
||||
TimerType::GAME_TIME, world->getGameTime() + delay, asyncId.mScriptId, std::move(callback));
|
||||
};
|
||||
api["callback"] = [](const AsyncPackageId& asyncId, sol::function fn)
|
||||
api["callback"] = [](const AsyncPackageId& asyncId, sol::function fn) -> LuaUtil::Callback
|
||||
{
|
||||
return LuaUtil::Callback{std::move(fn), asyncId.mHiddenData};
|
||||
};
|
||||
|
||||
sol::usertype<LuaUtil::Callback> callbackType = context.mLua->sol().new_usertype<LuaUtil::Callback>("Callback");
|
||||
callbackType[sol::meta_function::call] =
|
||||
[](const LuaUtil::Callback& callback, sol::variadic_args va) { return callback.call(sol::as_args(va)); };
|
||||
|
||||
auto initializer = [](sol::table hiddenData)
|
||||
{
|
||||
LuaUtil::ScriptsContainer::ScriptId id = hiddenData[LuaUtil::ScriptsContainer::sScriptIdKey];
|
||||
|
|
|
@ -169,7 +169,7 @@ namespace MWLua
|
|||
|
||||
// Run queued callbacks
|
||||
for (CallbackWithData& c : mQueuedCallbacks)
|
||||
c.mCallback(c.mArg);
|
||||
c.mCallback.call(c.mArg);
|
||||
mQueuedCallbacks.clear();
|
||||
|
||||
// Engine handlers in local scripts
|
||||
|
|
|
@ -447,16 +447,16 @@ return {
|
|||
callback.mHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptsContainer::ScriptId{nullptr, 0};
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
callback(1.5);
|
||||
callback.call(1.5);
|
||||
EXPECT_EQ(internal::GetCapturedStdout(), "1.5\n");
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
callback(1.5, 2.5);
|
||||
callback.call(1.5, 2.5);
|
||||
EXPECT_EQ(internal::GetCapturedStdout(), "1.5\t2.5\n");
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
callback.mHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = sol::nil;
|
||||
callback(1.5, 2.5);
|
||||
callback.call(1.5, 2.5);
|
||||
EXPECT_EQ(internal::GetCapturedStdout(), "Ignored callback to the removed script some_script.lua\n");
|
||||
}
|
||||
|
||||
|
|
|
@ -252,7 +252,7 @@ namespace LuaUtil
|
|||
bool isValid() const { return mHiddenData[ScriptsContainer::sScriptIdKey] != sol::nil; }
|
||||
|
||||
template <typename... Args>
|
||||
sol::object operator()(Args&&... args) const
|
||||
sol::object call(Args&&... args) const
|
||||
{
|
||||
if (isValid())
|
||||
return LuaUtil::call(mFunc, std::forward<Args>(args)...);
|
||||
|
@ -265,7 +265,7 @@ namespace LuaUtil
|
|||
template <typename... Args>
|
||||
void tryCall(Args&&... args) const
|
||||
{
|
||||
try { (*this)(std::forward<Args>(args)...); }
|
||||
try { this->call(std::forward<Args>(args)...); }
|
||||
catch (std::exception& e)
|
||||
{
|
||||
Log(Debug::Error) << "Error in callback: " << e.what();
|
||||
|
|
|
@ -340,7 +340,7 @@ namespace LuaUi
|
|||
{
|
||||
auto it = mCallbacks.find(name);
|
||||
if (it != mCallbacks.end())
|
||||
it->second(argument, mLayout);
|
||||
it->second.call(argument, mLayout);
|
||||
}
|
||||
|
||||
void WidgetExtension::keyPress(MyGUI::Widget*, MyGUI::KeyCode code, MyGUI::Char ch)
|
||||
|
|
|
@ -111,7 +111,7 @@ namespace LuaUi
|
|||
auto it = w->mCallbacks.find(name);
|
||||
if (it != w->mCallbacks.end())
|
||||
{
|
||||
sol::object res = it->second(argumentFactory(w), w->mLayout);
|
||||
sol::object res = it->second.call(argumentFactory(w), w->mLayout);
|
||||
shouldPropagate = res.is<bool>() && res.as<bool>();
|
||||
}
|
||||
if (w->mParent && w->mPropagateEvents && shouldPropagate)
|
||||
|
|
Loading…
Reference in a new issue