Fix callbacks for scripting

This commit is contained in:
momo5502 2019-01-23 22:27:14 +01:00
parent 2eb8988d73
commit 8e3f2f83c4
6 changed files with 133 additions and 133 deletions

View File

@ -26,17 +26,16 @@ namespace game
chai->add(chaiscript::fun(&entity::on_notify), "onNotify");
chai->add(chaiscript::fun([](const entity& ent, const std::string& event,
const std::function<void(
const std::vector<chaiscript::Boxed_Value>&)>&
std::vector<chaiscript::Boxed_Value>)>&
callback)
{
return ent.on_notify(event, callback, false);
}), "onNotify");
chai->add(chaiscript::fun([context](const std::string& event,
const std::function<void(const entity&,
const std::vector<chaiscript::
Boxed_Value>&)>&
callback)
const std::function<void(
entity, std::vector<chaiscript::Boxed_Value>)>&
callback)
{
generic_event_listener listener;
listener.event = event;
@ -47,10 +46,9 @@ namespace game
}), "onNotify");
chai->add(chaiscript::fun([context](const std::string& event,
const std::function<void(const entity&,
const std::vector<chaiscript::
Boxed_Value>&)>&
callback, const bool is_volatile)
const std::function<void(
entity, std::vector<chaiscript::Boxed_Value>)>&
callback, const bool is_volatile)
{
generic_event_listener listener;
listener.event = event;
@ -68,49 +66,49 @@ namespace game
}), "notify");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1)
{
return ent.notify(event, {a1});
}), "notify");
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1)
{
return ent.notify(event, {a1});
}), "notify");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return ent.notify(event, {a1, a2});
}), "notify");
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return ent.notify(event, {a1, a2});
}), "notify");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return ent.notify(event, {a1, a2, a3});
}), "notify");
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return ent.notify(event, {a1, a2, a3});
}), "notify");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return ent.notify(event, {a1, a2, a3, a4});
}), "notify");
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return ent.notify(event, {a1, a2, a3, a4});
}), "notify");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return ent.notify(event, {a1, a2, a3, a4, a5});
}), "notify");
[](const entity& ent, const std::string& event,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return ent.notify(event, {a1, a2, a3, a4, a5});
}), "notify");
// Instance call
chai->add(chaiscript::fun(&entity::call), "vectorCall");
@ -120,106 +118,106 @@ namespace game
}), "call");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1)
{
return ent.call(function, {a1});
}), "call");
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1)
{
return ent.call(function, {a1});
}), "call");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return ent.call(function, {a1, a2});
}), "call");
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return ent.call(function, {a1, a2});
}), "call");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return ent.call(function, {a1, a2, a3});
}), "call");
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return ent.call(function, {a1, a2, a3});
}), "call");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return ent.call(function, {a1, a2, a3, a4});
}), "call");
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return ent.call(function, {a1, a2, a3, a4});
}), "call");
chai->add(chaiscript::fun(
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return ent.call(function, {a1, a2, a3, a4, a5});
}), "call");
[](const entity& ent, const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return ent.call(function, {a1, a2, a3, a4, a5});
}), "call");
// Global call
chai->add(chaiscript::fun(
[context](const std::string& function,
const std::vector<chaiscript::Boxed_Value>& arguments)
{
return context->get_executer()->call(function, 0, arguments);
}), "vectorCall");
[context](const std::string& function,
const std::vector<chaiscript::Boxed_Value>& arguments)
{
return context->get_executer()->call(function, 0, arguments);
}), "vectorCall");
chai->add(chaiscript::fun([context](const std::string& function)
{
return context->get_executer()->call(function, 0, {});
}), "call");
chai->add(chaiscript::fun(
[context](const std::string& function,
const chaiscript::Boxed_Value& a1)
{
return context->get_executer()->call(function, 0, {a1});
}), "call");
[context](const std::string& function,
const chaiscript::Boxed_Value& a1)
{
return context->get_executer()->call(function, 0, {a1});
}), "call");
chai->add(chaiscript::fun(
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return context->get_executer()->call(function, 0, {a1, a2});
}), "call");
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2)
{
return context->get_executer()->call(function, 0, {a1, a2});
}), "call");
chai->add(chaiscript::fun(
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return context->get_executer()->call(function, 0, {a1, a2, a3});
}), "call");
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3)
{
return context->get_executer()->call(function, 0, {a1, a2, a3});
}), "call");
chai->add(chaiscript::fun(
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return context->get_executer()->call(function, 0, {a1, a2, a3, a4});
}), "call");
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4)
{
return context->get_executer()->call(function, 0, {a1, a2, a3, a4});
}), "call");
chai->add(chaiscript::fun(
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return context->get_executer()->call(function, 0, {a1, a2, a3, a4, a5});
}), "call");
[context](const std::string& function,
const chaiscript::Boxed_Value& a1,
const chaiscript::Boxed_Value& a2,
const chaiscript::Boxed_Value& a3,
const chaiscript::Boxed_Value& a4,
const chaiscript::Boxed_Value& a5)
{
return context->get_executer()->call(function, 0, {a1, a2, a3, a4, a5});
}), "call");
}
void initialize(context* context)

View File

@ -66,7 +66,7 @@ namespace game
}
event_listener_handle entity::on_notify(const std::string& event,
const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>&
const std::function<void(std::vector<chaiscript::Boxed_Value>)>&
callback,
const bool is_volatile)
const

View File

@ -21,8 +21,8 @@ namespace game
entity& operator=(entity&& other) noexcept;
event_listener_handle on_notify(const std::string& event,
const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>& callback,
bool is_volatile) const;
const std::function<void(std::vector<chaiscript::Boxed_Value>)>& callback,
bool is_volatile) const;
unsigned int get_entity_id() const;
game::native::scr_entref_t get_entity_reference() const;

View File

@ -11,12 +11,14 @@ namespace game
chai->add(chaiscript::user_type<event_listener_handle>(), "_event_listener_handle");
chai->add(chaiscript::constructor<event_listener_handle()>(), "_event_listener_handle");
chai->add(chaiscript::constructor<event_listener_handle(const event_listener_handle&)>(), "_event_listener_handle");
chai->add(chaiscript::constructor<event_listener_handle(const event_listener_handle&)>(),
"_event_listener_handle");
chai->add(chaiscript::fun([](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle&
{
return lhs = rhs;
}), "=");
chai->add(chaiscript::fun(
[](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle&
{
return lhs = rhs;
}), "=");
chai->add(chaiscript::fun([this](const event_listener_handle& handle)
{
@ -82,14 +84,14 @@ namespace game
{
listener.id = ++this->current_listener_id_;
this->event_listeners_.add(listener);
return { listener.id };
return {listener.id};
}
event_listener_handle event_handler::add_event_listener(generic_event_listener listener)
{
listener.id = ++this->current_listener_id_;
this->generic_event_listeners_.add(listener);
return { listener.id };
return {listener.id};
}
void event_handler::remove(const event_listener_handle& handle)

View File

@ -20,7 +20,7 @@ namespace game
public:
std::string event = {};
unsigned int entity_id = 0;
std::function<void(const std::vector<chaiscript::Boxed_Value>&)> callback = {};
std::function<void(std::vector<chaiscript::Boxed_Value>)> callback = {};
bool is_volatile = false;
};
@ -28,7 +28,7 @@ namespace game
{
public:
std::string event = {};
std::function<void(const entity&, const std::vector<chaiscript::Boxed_Value>&)> callback = {};
std::function<void(entity, std::vector<chaiscript::Boxed_Value>)> callback = {};
bool is_volatile = false;
};
@ -46,7 +46,7 @@ namespace game
context* context_;
std::atomic_int64_t current_listener_id_ = 0;
utils::concurrent_list<event_listener> event_listeners_;
utils::concurrent_list<event_listener> event_listeners_;
utils::concurrent_list<generic_event_listener> generic_event_listeners_;
void dispatch_to_specific_listeners(event* event, const std::vector<chaiscript::Boxed_Value>& arguments);

View File

@ -21,7 +21,7 @@ public:
html_frame();
virtual ~html_frame();
void initialize(const HWND window);
void initialize(HWND window);
void resize(DWORD width, DWORD height) const;
bool load_url(const std::string& url) const;