lua binder improvments
This commit is contained in:
parent
b9e5a4e463
commit
0cb5facd7a
|
@ -17,3 +17,4 @@ Module
|
||||||
require 'dispatcher'
|
require 'dispatcher'
|
||||||
require 'widget'
|
require 'widget'
|
||||||
require 'effects'
|
require 'effects'
|
||||||
|
require 'settings'
|
||||||
|
|
|
@ -16,11 +16,3 @@ end
|
||||||
function string:trim()
|
function string:trim()
|
||||||
return self:match('^%s*(.*%S)') or ''
|
return self:match('^%s*(.*%S)') or ''
|
||||||
end
|
end
|
||||||
|
|
||||||
function toboolean(str)
|
|
||||||
str = str:trim()
|
|
||||||
if str == '1' or str == 'true' then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
|
@ -68,3 +68,11 @@ function resolvepath(filePath, depth)
|
||||||
return filePath
|
return filePath
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function toboolean(str)
|
||||||
|
str = str:trim():lower()
|
||||||
|
if str == '1' or str == 'true' then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
|
@ -54,3 +54,25 @@ bool ConfigManager::save()
|
||||||
}
|
}
|
||||||
return doc->save(m_fileName);
|
return doc->save(m_fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ConfigManager::exists(const std::string& key)
|
||||||
|
{
|
||||||
|
return m_confsMap.find(key) != m_confsMap.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::set(const std::string& key, const std::string& value)
|
||||||
|
{
|
||||||
|
m_confsMap[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ConfigManager::get(const std::string& key)
|
||||||
|
{
|
||||||
|
return m_confsMap[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::remove(const std::string& key)
|
||||||
|
{
|
||||||
|
auto it = m_confsMap.find(key);
|
||||||
|
if(it != m_confsMap.end())
|
||||||
|
m_confsMap.erase(it);
|
||||||
|
}
|
||||||
|
|
|
@ -31,9 +31,10 @@ public:
|
||||||
bool load(const std::string& file);
|
bool load(const std::string& file);
|
||||||
bool save();
|
bool save();
|
||||||
|
|
||||||
bool exists(const std::string& key) { return m_confsMap.find(key) != m_confsMap.end(); }
|
bool exists(const std::string& key);
|
||||||
void set(const std::string& key, const std::string& value) { m_confsMap[key] = value; }
|
void set(const std::string& key, const std::string& value);
|
||||||
std::string get(const std::string& key) { return m_confsMap[key]; }
|
std::string get(const std::string& key);
|
||||||
|
void remove(const std::string& key);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_fileName;
|
std::string m_fileName;
|
||||||
|
|
|
@ -31,11 +31,29 @@
|
||||||
#include <framework/graphics/graphics.h>
|
#include <framework/graphics/graphics.h>
|
||||||
#include <framework/platform/platformwindow.h>
|
#include <framework/platform/platformwindow.h>
|
||||||
|
|
||||||
|
class LuaRect : public LuaObject, public Rect {
|
||||||
|
public:
|
||||||
|
LuaRect() : TRect() { }
|
||||||
|
};
|
||||||
|
|
||||||
void Application::registerLuaFunctions()
|
void Application::registerLuaFunctions()
|
||||||
{
|
{
|
||||||
|
// globals
|
||||||
|
g_lua.registerStaticClass("Rect");
|
||||||
|
g_lua.bindClassStaticFunction("Rect", "create", []{ return std::shared_ptr<LuaRect>(new LuaRect); });
|
||||||
|
g_lua.bindClassMemberFunction<LuaRect>("Rect", "resize", (void (Rect::*)(int,int)) &Rect::resize);
|
||||||
|
|
||||||
|
/*
|
||||||
|
g_lua.bindGlobalFunction("torect", [](const std::string& str) { return Fw::unsafeCast<Rect>(str); });
|
||||||
|
g_lua.bindGlobalFunction("topoint", [](const std::string& str) { return Fw::unsafeCast<Point>(str); });
|
||||||
|
g_lua.bindGlobalFunction("tocolor", [](const std::string& str) { return Fw::unsafeCast<Color>(str); });
|
||||||
|
g_lua.bindGlobalFunction("tosize", [](const std::string& str) { return Fw::unsafeCast<Size>(str); });
|
||||||
|
g_lua.bindGlobalFunction("toboolean", [](const std::string& str) { return Fw::unsafeCast<bool>(str); });
|
||||||
|
*/
|
||||||
|
|
||||||
// UIWidget
|
// UIWidget
|
||||||
g_lua.registerClass<UIWidget>();
|
g_lua.registerClass<UIWidget>();
|
||||||
g_lua.bindClassStaticFunction<UIWidget>("create", &UIWidget::create<UIWidget>);
|
g_lua.bindClassStaticFunction<UIWidget>("create", []{ return UIWidgetPtr(new UIWidget); } );
|
||||||
g_lua.bindClassMemberFunction<UIWidget>("destroy", &UIWidget::destroy);
|
g_lua.bindClassMemberFunction<UIWidget>("destroy", &UIWidget::destroy);
|
||||||
g_lua.bindClassMemberFunction<UIWidget>("setVisible", &UIWidget::setVisible);
|
g_lua.bindClassMemberFunction<UIWidget>("setVisible", &UIWidget::setVisible);
|
||||||
g_lua.bindClassMemberFunction<UIWidget>("setEnabled", &UIWidget::setEnabled);
|
g_lua.bindClassMemberFunction<UIWidget>("setEnabled", &UIWidget::setEnabled);
|
||||||
|
@ -219,7 +237,7 @@ void Application::registerLuaFunctions()
|
||||||
// UIFrameCounter
|
// UIFrameCounter
|
||||||
g_lua.registerClass<UIFrameCounter, UIWidget>();
|
g_lua.registerClass<UIFrameCounter, UIWidget>();
|
||||||
g_lua.bindClassStaticFunction<UIFrameCounter>("create", &UIWidget::create<UIFrameCounter>);
|
g_lua.bindClassStaticFunction<UIFrameCounter>("create", &UIWidget::create<UIFrameCounter>);
|
||||||
g_lua.bindClassMemberFunction("getFrameCount", &UIFrameCounter::getFrameCount);
|
g_lua.bindClassMemberFunction<UIFrameCounter>("getFrameCount", &UIFrameCounter::getFrameCount);
|
||||||
|
|
||||||
// Protocol
|
// Protocol
|
||||||
g_lua.registerClass<Protocol>();
|
g_lua.registerClass<Protocol>();
|
||||||
|
@ -230,6 +248,7 @@ void Application::registerLuaFunctions()
|
||||||
g_lua.bindClassStaticFunction("g_configs", "get", std::bind(&ConfigManager::get, &g_configs, _1));
|
g_lua.bindClassStaticFunction("g_configs", "get", std::bind(&ConfigManager::get, &g_configs, _1));
|
||||||
g_lua.bindClassStaticFunction("g_configs", "exists", std::bind(&ConfigManager::exists, &g_configs, _1));
|
g_lua.bindClassStaticFunction("g_configs", "exists", std::bind(&ConfigManager::exists, &g_configs, _1));
|
||||||
|
|
||||||
|
|
||||||
g_lua.registerStaticClass("g_window");
|
g_lua.registerStaticClass("g_window");
|
||||||
g_lua.bindClassStaticFunction("g_window", "show", std::bind(&PlatformWindow::show, &g_window));
|
g_lua.bindClassStaticFunction("g_window", "show", std::bind(&PlatformWindow::show, &g_window));
|
||||||
g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window));
|
g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window));
|
||||||
|
|
|
@ -113,6 +113,27 @@ namespace luabinder
|
||||||
Tuple>(f);
|
Tuple>(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specialization for lambdas
|
||||||
|
template<typename F>
|
||||||
|
struct bind_lambda_fun;
|
||||||
|
|
||||||
|
template<typename Lambda, typename Ret, typename... Args>
|
||||||
|
struct bind_lambda_fun<Ret(Lambda::*)(Args...) const> {
|
||||||
|
static LuaCppFunction call(const Lambda& f) {
|
||||||
|
typedef typename std::tuple<typename remove_const_ref<Args>::type...> Tuple;
|
||||||
|
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||||
|
decltype(f),
|
||||||
|
Tuple>(f);
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Lambda>
|
||||||
|
typename std::enable_if<std::is_class<Lambda>::value, LuaCppFunction>::type bind_fun(const Lambda& f) {
|
||||||
|
typedef decltype(&Lambda::operator()) F;
|
||||||
|
return bind_lambda_fun<F>::call(f);
|
||||||
|
}
|
||||||
|
|
||||||
/// Bind a customized functions
|
/// Bind a customized functions
|
||||||
template<>
|
template<>
|
||||||
inline
|
inline
|
||||||
|
@ -207,29 +228,29 @@ namespace luabinder
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bind member functions
|
/// Bind member functions
|
||||||
template<typename Ret, typename Obj, typename... Args>
|
template<typename C, typename Ret, class FC, typename... Args>
|
||||||
LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...)) {
|
LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...)) {
|
||||||
auto mf = std::mem_fn(f);
|
auto mf = std::mem_fn(f);
|
||||||
typedef typename std::tuple<std::shared_ptr<Obj>, typename remove_const_ref<Args>::type...> Tuple;
|
typedef typename std::tuple<std::shared_ptr<C>, typename remove_const_ref<Args>::type...> Tuple;
|
||||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||||
decltype(mf),
|
decltype(mf),
|
||||||
Tuple>(mf);
|
Tuple>(mf);
|
||||||
}
|
}
|
||||||
template<typename Ret, typename Obj, typename... Args>
|
template<typename C, typename Ret, class FC, typename... Args>
|
||||||
LuaCppFunction bind_mem_fun(Ret (Obj::*f)(Args...) const) {
|
LuaCppFunction bind_mem_fun(Ret (FC::*f)(Args...) const) {
|
||||||
auto mf = std::mem_fn(f);
|
auto mf = std::mem_fn(f);
|
||||||
typedef typename std::tuple<std::shared_ptr<Obj>, typename remove_const_ref<Args>::type...> Tuple;
|
typedef typename std::tuple<std::shared_ptr<C>, typename remove_const_ref<Args>::type...> Tuple;
|
||||||
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
return bind_fun_specializer<typename remove_const_ref<Ret>::type,
|
||||||
decltype(mf),
|
decltype(mf),
|
||||||
Tuple>(mf);
|
Tuple>(mf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bind customized member functions
|
/// Bind customized member functions
|
||||||
template<typename Obj>
|
template<typename C>
|
||||||
LuaCppFunction bind_mem_fun(int (Obj::*f)(LuaInterface*)) {
|
LuaCppFunction bind_mem_fun(int (C::*f)(LuaInterface*)) {
|
||||||
auto mf = std::mem_fn(f);
|
auto mf = std::mem_fn(f);
|
||||||
return [=](LuaInterface* lua) {
|
return [=](LuaInterface* lua) {
|
||||||
auto obj = lua->castValue<std::shared_ptr<Obj>>(1);
|
auto obj = lua->castValue<std::shared_ptr<C>>(1);
|
||||||
lua->remove(1);
|
lua->remove(1);
|
||||||
return mf(obj, lua);
|
return mf(obj, lua);
|
||||||
};
|
};
|
||||||
|
|
|
@ -90,25 +90,25 @@ public:
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function);
|
void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function);
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberFunction(const std::string& functionName, F C::*function);
|
void bindClassMemberFunction(const std::string& functionName, F FC::*function);
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function);
|
void bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function);
|
||||||
|
|
||||||
template<class C, typename F1, typename F2>
|
template<class C, typename F1, typename F2, class FC>
|
||||||
void bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction);
|
void bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
|
||||||
template<class C, typename F1, typename F2>
|
template<class C, typename F1, typename F2, class FC>
|
||||||
void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction);
|
void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberGetField(const std::string& fieldName, F C::*getFunction);
|
void bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction);
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction);
|
void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction);
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberSetField(const std::string& fieldName, F C::*setFunction);
|
void bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction);
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction);
|
void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction);
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void bindGlobalFunction(const std::string& functionName, const F& function);
|
void bindGlobalFunction(const std::string& functionName, const F& function);
|
||||||
|
@ -336,40 +336,40 @@ void LuaInterface::bindClassStaticFunction(const std::string& className, const s
|
||||||
registerClassStaticFunction(className, functionName, luabinder::bind_fun(function));
|
registerClassStaticFunction(className, functionName, luabinder::bind_fun(function));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberFunction(const std::string& functionName, F C::*function) {
|
void LuaInterface::bindClassMemberFunction(const std::string& functionName, F FC::*function) {
|
||||||
registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun(function));
|
registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun<C>(function));
|
||||||
}
|
}
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F C::*function) {
|
void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function) {
|
||||||
registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun(function));
|
registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun<C>(function));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class C, typename F1, typename F2>
|
template<class C, typename F1, typename F2, class FC>
|
||||||
void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) {
|
void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
|
||||||
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction));
|
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
|
||||||
}
|
}
|
||||||
template<class C, typename F1, typename F2>
|
template<class C, typename F1, typename F2, class FC>
|
||||||
void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 C::*getFunction, F2 C::*setFunction) {
|
void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
|
||||||
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), luabinder::bind_mem_fun(setFunction));
|
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F C::*getFunction) {
|
void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction) {
|
||||||
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction());
|
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
|
||||||
}
|
}
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F C::*getFunction) {
|
void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction) {
|
||||||
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun(getFunction), LuaCppFunction());
|
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F C::*setFunction) {
|
void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction) {
|
||||||
registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction));
|
registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
|
||||||
}
|
}
|
||||||
template<class C, typename F>
|
template<class C, typename F, class FC>
|
||||||
void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F C::*setFunction) {
|
void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction) {
|
||||||
registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun(setFunction));
|
registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
|
|
|
@ -35,8 +35,8 @@ void OTClient::registerLuaFunctions()
|
||||||
|
|
||||||
g_lua.registerClass<ProtocolLogin, Protocol>();
|
g_lua.registerClass<ProtocolLogin, Protocol>();
|
||||||
g_lua.bindClassStaticFunction<ProtocolLogin>("create", &ProtocolLogin::create);
|
g_lua.bindClassStaticFunction<ProtocolLogin>("create", &ProtocolLogin::create);
|
||||||
g_lua.bindClassMemberFunction("login", &ProtocolLogin::login);
|
g_lua.bindClassMemberFunction<ProtocolLogin>("login", &ProtocolLogin::login);
|
||||||
g_lua.bindClassMemberFunction("cancelLogin", &ProtocolLogin::cancelLogin);
|
g_lua.bindClassMemberFunction<ProtocolLogin>("cancelLogin", &ProtocolLogin::cancelLogin);
|
||||||
|
|
||||||
g_lua.registerClass<ProtocolGame, Protocol>();
|
g_lua.registerClass<ProtocolGame, Protocol>();
|
||||||
|
|
||||||
|
@ -53,9 +53,9 @@ void OTClient::registerLuaFunctions()
|
||||||
g_lua.bindClassMemberFunction<Thing>("asLocalPlayer", &Thing::asLocalPlayer);
|
g_lua.bindClassMemberFunction<Thing>("asLocalPlayer", &Thing::asLocalPlayer);
|
||||||
|
|
||||||
g_lua.registerClass<Creature, Thing>();
|
g_lua.registerClass<Creature, Thing>();
|
||||||
g_lua.bindClassMemberFunction("getName", &Creature::getName);
|
g_lua.bindClassMemberFunction<Creature>("getName", &Creature::getName);
|
||||||
g_lua.bindClassMemberFunction("setOutfit", &Creature::setOutfit);
|
g_lua.bindClassMemberFunction<Creature>("setOutfit", &Creature::setOutfit);
|
||||||
g_lua.bindClassMemberFunction("getOutfit", &Creature::getOutfit);
|
g_lua.bindClassMemberFunction<Creature>("getOutfit", &Creature::getOutfit);
|
||||||
|
|
||||||
g_lua.registerClass<Player, Creature>();
|
g_lua.registerClass<Player, Creature>();
|
||||||
g_lua.registerClass<LocalPlayer, Player>();
|
g_lua.registerClass<LocalPlayer, Player>();
|
||||||
|
|
|
@ -56,7 +56,7 @@ for line in io.lines(cppclassheader) do
|
||||||
bindline = bindline .. '>();'
|
bindline = bindline .. '>();'
|
||||||
print(bindline)
|
print(bindline)
|
||||||
|
|
||||||
bindline = 'g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", &' .. cppclassname .. '::create);'
|
bindline = 'g_lua.bindClassStaticFunction<' .. cppclassname .. '>("create", []{ return ' .. cppclassname .. 'Ptr(new ' .. cppclassname .. '); });'
|
||||||
print(bindline)
|
print(bindline)
|
||||||
end
|
end
|
||||||
elseif classfound then
|
elseif classfound then
|
||||||
|
|
Loading…
Reference in New Issue