diff --git a/TODO.txt b/TODO.txt index b2872c8..6dbdbdc 100644 --- a/TODO.txt +++ b/TODO.txt @@ -9,9 +9,6 @@ Refactoring and is good style anyway. * StateManager / State needs to be completely rewritten: It's way to complicated without providing any benefits and, worse, lowers flexibility. -* Maybe replace jd.connect with a Lua function <-> luabind::object() converter - so that jd.connect() is not necessary anymore and - fooComponent:onBar(f) can be used instead. * Get rid of LuaComponent::bindLuaPart(): Maybe luabind::adopt() is now capable of replacing it. * Exceptions: Use boost::exception/error_info system or at least a proper diff --git a/base.jd/lua/lib/evt.lua b/base.jd/lua/lib/evt.lua index eeacd08..c22e6eb 100644 --- a/base.jd/lua/lib/evt.lua +++ b/base.jd/lua/lib/evt.lua @@ -8,6 +8,14 @@ local tabutil = require 'tabutil' local util = require 'util' local oo = require 'oo' +-- Compatibility +function jd.connect(component, event, callback) + -- When we get event="foo", we need to call the components onFoo function, + -- i.e. the first letter becomes uppercase. + local event = event:gsub("^%l", string.upper) + return component["on" .. event](component, callback) +end + local M = { } do diff --git a/src/compsys/BasicMetaComponent.hpp b/src/compsys/BasicMetaComponent.hpp index ca9e805..52d57d1 100644 --- a/src/compsys/BasicMetaComponent.hpp +++ b/src/compsys/BasicMetaComponent.hpp @@ -27,24 +27,6 @@ class BasicMetaComponent: public MetaComponent { } }; -#define JD_EVT_METACOMPONENT(c, bc) \ - namespace { \ - class c##Meta: public bc { \ - public: \ - virtual ssig::ConnectionBase* connectEvent( \ - lua_State*, \ - Component*, \ - std::string const& name) const; \ - }; \ - } // anonymous namespace - - #define JD_BASIC_COMPONENT_IMPL(c) JD_COMPONENT_IMPL(c, BasicMetaComponent) -#define JD_BASIC_EVT_COMPONENT_IMPL(c) \ - JD_EVT_METACOMPONENT(c, BasicMetaComponent) \ - JD_COMPONENT_IMPL(c, c##Meta) - - - #endif diff --git a/src/compsys/MetaComponent.cpp b/src/compsys/MetaComponent.cpp index ed657aa..a7c7ab2 100644 --- a/src/compsys/MetaComponent.cpp +++ b/src/compsys/MetaComponent.cpp @@ -49,21 +49,6 @@ static luabind::object Component_parent(Component const& c, lua_State* L) return luabind::object(); } -static ssig::ConnectionBase* connectEvent( - lua_State* L, - Component& sender, - std::string const& eventname, - luabind::object const& receiver) -{ - receiver.push(L); - ssig::ConnectionBase* result = sender.metaComponent().connectEvent( - L, &sender, eventname); - lua_pop(L, 1); - if (!result) - throw luaU::Error("Event \"" + eventname + "\" not found!"); - return result; -} - class wrap_Component: public Component, public luabind::wrap_base { public: wrap_Component(lua_State* L, Entity& parent, std::string const& classname) @@ -145,7 +130,6 @@ static void init(LuaVm& vm) .def(tostring(const_self)) .LHISREFVALID2(Component), def("registerComponent", ®isterMetaComponent), - def("connect", &connectEvent, adopt(result)), class_("ConnectionBase") .def(constructor<>()) .def("disconnect", &ssig::ConnectionBase::disconnect) @@ -200,13 +184,3 @@ void LuaMetaComponent::castDown(lua_State* L, Component* c) const luabind::object o(L, c->ref()); o.push(L); } - -ssig::ConnectionBase* LuaMetaComponent::connectEvent(lua_State* L, Component* c, std::string const& name) const -{ - using namespace luabind; - object receiver(from_stack(L, -1)); - object callback = globals(L)[jd::moduleName]["callback_connectLuaEvent"]; - if (type(callback) != LUA_TFUNCTION) - throw std::runtime_error("no callback for event connection defined (must be a plain function)"); - return object_cast(callback(c, name, receiver)[adopt(result)]); -} diff --git a/src/compsys/MetaComponent.hpp b/src/compsys/MetaComponent.hpp index f119cf1..38e4ce6 100644 --- a/src/compsys/MetaComponent.hpp +++ b/src/compsys/MetaComponent.hpp @@ -13,7 +13,6 @@ struct lua_State; -namespace ssig { class ConnectionBase; } class Component; class InvalidMetaComponentName: public std::runtime_error { @@ -34,13 +33,6 @@ class InvalidMetaComponentName: public std::runtime_error { assert("not scriptable!" && false); throw std::runtime_error("castDown() is not implemented."); } - - // receiver is on top of stack; return value disconnects automatically on deletion - virtual ssig::ConnectionBase* connectEvent(lua_State*, Component*, std::string const& name) const - { - (void)name; - return nullptr; - } }; // Two MetaComponent instances are equal if and only if they have the same @@ -58,8 +50,6 @@ class LuaMetaComponent: public MetaComponent { std::string const& name() const override; void castDown(lua_State* L, Component* c) const override; - ssig::ConnectionBase* connectEvent( - lua_State* L, Component* c, std::string const& name) const override; private: std::string const m_name; diff --git a/src/luaexport/LuaEventHelpers.hpp b/src/luaexport/LuaEventHelpers.hpp index 754f1b0..b3c7b6a 100644 --- a/src/luaexport/LuaEventHelpers.hpp +++ b/src/luaexport/LuaEventHelpers.hpp @@ -9,41 +9,6 @@ #include #include -#define LUA_EVENT_HELPERS_MAX_ARGS SSIG_MAX_ARGS - -#define JD_EVENT_ENTRY(ename, r, ...) \ - if (name == BOOST_STRINGIZE(ename)) \ - return makeConnection(cc->connect_##ename(boost::bind( \ - LuaFunction(recv), __VA_ARGS__))); \ - else { } - -#define JD_EVENT_ENTRY0(ename, r) \ - if (name == BOOST_STRINGIZE(ename)) \ - return makeConnection(cc->connect_##ename(boost::bind( \ - &luabind::call_function, recv))); \ - else { } - -#define JD_EVENT_TABLE_BEGIN(ct) \ - ssig::ConnectionBase* ct##Meta::connectEvent( \ - lua_State* L, \ - Component* c, \ - std::string const& name) const \ - { \ - ct* cc = c->as(); \ - luabind::object recv(luabind::from_stack(L, -1)); \ - using boost::ref; using boost::cref; - -#define JD_EVENT_TABLE_END return nullptr; } - -#define JD_EVENT(name, cname) def("on" #cname, &LHCURCLASS::connect_##name) - -template -ssig::ConnectionBase* makeConnection( - ssig::Connection const& con) -{ - return new ssig::ScopedConnection(con); -} - namespace luabind { template struct default_converter> @@ -51,9 +16,16 @@ namespace luabind { { void to(lua_State* L, ssig::Connection const& value) { - luabind::object o(L, makeConnection(value), luabind::adopt(luabind::result)); + luabind::object o(L, makeConnection(value), + luabind::adopt(luabind::result)); o.push(L); } + private: + ssig::ConnectionBase* makeConnection( + ssig::Connection const& con) + { + return new ssig::ScopedConnection(con); + } }; template diff --git a/src/luaexport/LuaExportHelpers.cpp b/src/luaexport/LuaExportHelpers.cpp deleted file mode 100644 index a1e7cfe..0000000 --- a/src/luaexport/LuaExportHelpers.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// Part of the Jade Engine -- Copyright (c) Christian Neumüller 2012--2013 -// This file is subject to the terms of the BSD 2-Clause License. -// See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause - -#include "ExportThis.hpp" - -void exportEnum(int idx, ExportedEnumValue const* entries) -{ -}