mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
lua console and some changes
This commit is contained in:
@@ -10,51 +10,71 @@ void LuaInterface::registerFunctions()
|
||||
// UIWidget
|
||||
g_lua.registerClass<UIWidget>();
|
||||
g_lua.bindClassStaticFunction<UIWidget>("create", &UIWidget::create);
|
||||
|
||||
g_lua.bindClassMemberFunction("destroy", &UIWidget::destroy);
|
||||
g_lua.bindClassMemberFunction("addChild", &UIWidget::addChild);
|
||||
|
||||
g_lua.bindClassMemberField<UIWidget>("id", &UIWidget::getId, &UIWidget::setId);
|
||||
g_lua.bindClassMemberField<UIWidget>("enabled", &UIWidget::isEnabled, &UIWidget::setEnabled);
|
||||
g_lua.bindClassMemberField<UIWidget>("visible", &UIWidget::isVisible, &UIWidget::setVisible);
|
||||
g_lua.bindClassMemberField<UIWidget>("width", &UIWidget::getWidth, &UIWidget::setWidth);
|
||||
g_lua.bindClassMemberField<UIWidget>("height", &UIWidget::getHeight, &UIWidget::setHeight);
|
||||
g_lua.bindClassMemberField<UIWidget>("parent", &UIWidget::getParent, &UIWidget::setParent);
|
||||
g_lua.bindClassMemberField<UIWidget>("color", &UIWidget::getColor, &UIWidget::setColor);
|
||||
g_lua.bindClassMemberField<UIWidget>("opacity", &UIWidget::getOpacity, &UIWidget::setOpacity);
|
||||
g_lua.bindClassMemberField<UIWidget>("marginTop", &UIWidget::getMarginTop, &UIWidget::setMarginTop);
|
||||
g_lua.bindClassMemberField<UIWidget>("marginBottom", &UIWidget::getMarginBottom, &UIWidget::setMarginBottom);
|
||||
g_lua.bindClassMemberField<UIWidget>("marginLeft", &UIWidget::getMarginLeft, &UIWidget::setMarginLeft);
|
||||
g_lua.bindClassMemberField<UIWidget>("marginRight", &UIWidget::getMarginRight, &UIWidget::setMarginRight);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getId", &UIWidget::getId);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setId", &UIWidget::setId);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isEnabled", &UIWidget::isEnabled);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setEnabled", &UIWidget::setEnabled);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isVisible", &UIWidget::isVisible);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setVisible", &UIWidget::setVisible);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getWidth", &UIWidget::getWidth);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setWidth", &UIWidget::setWidth);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getHeight", &UIWidget::getHeight);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setHeight", &UIWidget::setHeight);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getParent", &UIWidget::getParent);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setParent", &UIWidget::setParent);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getBackgroundColor", &UIWidget::getBackgroundColor);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setBackgroundColor", &UIWidget::setBackgroundColor);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getForegroundColor", &UIWidget::getForegroundColor);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setForegroundColor", &UIWidget::setForegroundColor);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getOpacity", &UIWidget::getOpacity);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setOpacity", &UIWidget::setOpacity);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setStyle", &UIWidget::setStyle);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getMarginTop", &UIWidget::getMarginTop);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setMarginTop", &UIWidget::setMarginTop);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getMarginBottom", &UIWidget::getMarginBottom);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setMarginBottom", &UIWidget::setMarginBottom);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getMarginLeft", &UIWidget::getMarginLeft);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setMarginLeft", &UIWidget::setMarginLeft);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getMarginRight", &UIWidget::getMarginRight);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setMarginRight", &UIWidget::setMarginRight);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hide", &UIWidget::hide);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("show", &UIWidget::show);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("fill", &UIWidget::fill);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("centerIn", &UIWidget::centerIn);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("addAnchor", &UIWidget::addAnchor);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getChild", &UIWidget::getChildById);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("insertChild", &UIWidget::insertChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("removeChild", &UIWidget::removeChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("lock", &UIWidget::lock);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hide", &UIWidget::hide);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("show", &UIWidget::show);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("lockChild", &UIWidget::lockChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("destroy", &UIWidget::destroy);
|
||||
|
||||
|
||||
// UILabel
|
||||
g_lua.registerClass<UILabel, UIWidget>();
|
||||
g_lua.bindClassStaticFunction<UILabel>("create", &UILabel::create);
|
||||
g_lua.bindClassMemberField("text", &UILabel::getText, &UILabel::setText);
|
||||
g_lua.bindClassMemberFunction<UILabel>("getText", &UILabel::getText);
|
||||
g_lua.bindClassMemberFunction<UILabel>("setText", &UILabel::setText);
|
||||
g_lua.bindClassMemberFunction("resizeToText", &UILabel::resizeToText);
|
||||
|
||||
// UIButton
|
||||
g_lua.registerClass<UIButton, UIWidget>();
|
||||
g_lua.bindClassStaticFunction<UIButton>("create", &UIButton::create);
|
||||
g_lua.bindClassMemberField("text", &UIButton::getText, &UIButton::setText);
|
||||
g_lua.bindClassMemberFunction<UIButton>("getText", &UIButton::getText);
|
||||
g_lua.bindClassMemberFunction<UIButton>("setText", &UIButton::setText);
|
||||
|
||||
// UILineEdit
|
||||
g_lua.registerClass<UILineEdit, UIWidget>();
|
||||
g_lua.bindClassStaticFunction<UILineEdit>("create", &UILineEdit::create);
|
||||
g_lua.bindClassMemberField("text", &UILineEdit::getText, &UILineEdit::setText);
|
||||
g_lua.bindClassMemberFunction<UILineEdit>("getText", &UILineEdit::getText);
|
||||
g_lua.bindClassMemberFunction<UILineEdit>("setText", &UILineEdit::setText);
|
||||
g_lua.bindClassMemberFunction<UILineEdit>("clearText", &UILineEdit::clearText);
|
||||
|
||||
// UIWindow
|
||||
g_lua.registerClass<UIWindow, UIWidget>();
|
||||
g_lua.bindClassStaticFunction<UIWindow>("create", &UIWindow::create);
|
||||
g_lua.bindClassMemberField("title", &UIWindow::getTitle, &UIWindow::setTitle);
|
||||
g_lua.bindClassMemberFunction<UIWindow>("getTitle", &UIWindow::getTitle);
|
||||
g_lua.bindClassMemberFunction<UIWindow>("setTitle", &UIWindow::setTitle);
|
||||
|
||||
// Protocol
|
||||
g_lua.registerClass<Protocol>();
|
||||
@@ -64,6 +84,12 @@ void LuaInterface::registerFunctions()
|
||||
g_lua.bindClassStaticFunction<Configs>("set", std::bind(&Configs::set, &g_configs, _1, _2));
|
||||
g_lua.bindClassStaticFunction<Configs>("get", std::bind(&Configs::get, &g_configs, _1));
|
||||
|
||||
// Logger
|
||||
g_lua.registerClass<Logger>();
|
||||
g_lua.bindClassStaticFunction<Logger>("log", std::bind(&Logger::log, &g_logger, _1, _2));
|
||||
g_lua.bindClassStaticFunction<Logger>("fireOldMessages", std::bind(&Logger::fireOldMessages, &g_logger));
|
||||
g_lua.bindClassStaticFunction<Logger>("setOnLog", std::bind(&Logger::setOnLog, &g_logger, _1));
|
||||
|
||||
// global functions
|
||||
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
|
||||
g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1));
|
||||
|
@@ -422,7 +422,7 @@ int LuaInterface::protectedCall(int numArgs, int requestedResults)
|
||||
throw LuaException("attempt to call a non function value", 0);
|
||||
}
|
||||
} catch(LuaException &e) {
|
||||
logError(e.what());
|
||||
logError("protected lua call failed: ", e.what());
|
||||
}
|
||||
|
||||
// pushes nil values if needed
|
||||
@@ -457,7 +457,7 @@ int LuaInterface::luaScriptLoader(lua_State* L)
|
||||
g_lua.loadScript(fileName);
|
||||
return 1;
|
||||
} catch(LuaException& e) {
|
||||
logError("ERROR: failed to load script file '", fileName, "' :'", e.what());
|
||||
logError("failed to load script file '", fileName, "' :'", e.what());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -489,7 +489,7 @@ int LuaInterface::luaCppFunctionCallback(lua_State* L)
|
||||
numRets = (*(funcPtr->get()))(&g_lua);
|
||||
assert(numRets == g_lua.stackSize());
|
||||
} catch(LuaException &e) {
|
||||
logError(e.what());
|
||||
logError("lua cpp callback failed: ", e.what());
|
||||
}
|
||||
|
||||
return numRets;
|
||||
|
@@ -7,10 +7,10 @@ LuaObject::LuaObject() : m_fieldsTableRef(-1)
|
||||
|
||||
LuaObject::~LuaObject()
|
||||
{
|
||||
luaReleaseFieldsTable();
|
||||
releaseLuaFieldsTable();
|
||||
}
|
||||
|
||||
void LuaObject::luaReleaseFieldsTable()
|
||||
void LuaObject::releaseLuaFieldsTable()
|
||||
{
|
||||
if(m_fieldsTableRef != -1)
|
||||
g_lua.unref(m_fieldsTableRef);
|
||||
|
@@ -15,6 +15,8 @@ public:
|
||||
/// @return the number of results
|
||||
template<typename... T>
|
||||
int callLuaField(const std::string& field, const T&... args);
|
||||
template<typename R, typename... T>
|
||||
R callLuaField(const std::string& field, const T&... args);
|
||||
|
||||
/// Sets a field in this lua object
|
||||
template<typename T>
|
||||
@@ -25,7 +27,7 @@ public:
|
||||
T getLuaField(const std::string& key);
|
||||
|
||||
/// Release fields table reference
|
||||
void luaReleaseFieldsTable();
|
||||
void releaseLuaFieldsTable();
|
||||
|
||||
/// Sets a field from this lua object, the value must be on the stack
|
||||
void luaSetField(const std::string& key);
|
||||
@@ -61,12 +63,28 @@ int LuaObject::callLuaField(const std::string& field, const T&... args) {
|
||||
g_lua.pushObject(asLuaObject());
|
||||
g_lua.getField(field);
|
||||
|
||||
// the first argument is always this object (self)
|
||||
g_lua.insert(-2);
|
||||
g_lua.polymorphicPush(args...);
|
||||
return g_lua.protectedCall(1 + sizeof...(args));
|
||||
if(!g_lua.isNil()) {
|
||||
// the first argument is always this object (self)
|
||||
g_lua.insert(-2);
|
||||
g_lua.polymorphicPush(args...);
|
||||
return g_lua.protectedCall(1 + sizeof...(args));
|
||||
} else {
|
||||
g_lua.pop(2);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename R, typename... T>
|
||||
R LuaObject::callLuaField(const std::string& field, const T&... args) {
|
||||
R result;
|
||||
int rets = callLuaField(field, args...);
|
||||
if(rets > 0) {
|
||||
assert(rets == 1);
|
||||
result = g_lua.polymorphicPop<R>();
|
||||
} else
|
||||
return 0;
|
||||
result = R();
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -81,4 +99,4 @@ T LuaObject::getLuaField(const std::string& key) {
|
||||
return g_lua.polymorphicPop<T>();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
@@ -42,6 +42,20 @@ bool luavalue_cast(int index, double& d)
|
||||
return true;
|
||||
}
|
||||
|
||||
// size_t
|
||||
void push_luavalue(std::size_t s)
|
||||
{
|
||||
push_luavalue((double)s);
|
||||
}
|
||||
|
||||
bool luavalue_cast(int index, std::size_t& s)
|
||||
{
|
||||
double d;
|
||||
bool ret = luavalue_cast(index, d);
|
||||
s = d;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// string
|
||||
void push_luavalue(const char* cstr)
|
||||
{
|
||||
|
@@ -18,6 +18,10 @@ bool luavalue_cast(int index, int& i);
|
||||
void push_luavalue(double d);
|
||||
bool luavalue_cast(int index, double& d);
|
||||
|
||||
// size_t
|
||||
void push_luavalue(std::size_t s);
|
||||
bool luavalue_cast(int index, std::size_t& s);
|
||||
|
||||
// string
|
||||
void push_luavalue(const char* cstr);
|
||||
void push_luavalue(const std::string& str);
|
||||
@@ -131,7 +135,7 @@ bool luavalue_cast(int index, std::function<void(Args...)>& func) {
|
||||
"did you forget to hold a reference for that function?", 0);
|
||||
}
|
||||
} catch(std::exception& e) {
|
||||
logError(e.what());
|
||||
logError("lua function callback failed: ", e.what());
|
||||
}
|
||||
};
|
||||
return true;
|
||||
@@ -165,7 +169,7 @@ luavalue_cast(int index, std::function<Ret(Args...)>& func) {
|
||||
"did you forget to hold a reference for that function?", 0);
|
||||
}
|
||||
} catch(std::exception& e) {
|
||||
logError(e.what());
|
||||
logError("lua function callback failed: ", e.what());
|
||||
}
|
||||
return Ret();
|
||||
};
|
||||
|
Reference in New Issue
Block a user