Skip to content

Commit

Permalink
Improved setBackgroundColor to work with main window (Mudlet#1696)
Browse files Browse the repository at this point in the history
* Improved setBackgroundColour to work with main window

Improved setBackgroundColour to work with main window, applied the improved error message style, and made the alpha argument optional (since it does not work on miniconsoles).
  • Loading branch information
vadi2 authored Jun 9, 2018
1 parent 0e70258 commit 03c14b4
Showing 1 changed file with 86 additions and 36 deletions.
122 changes: 86 additions & 36 deletions src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3067,51 +3067,101 @@ int TLuaInterpreter::setMainWindowSize(lua_State* L)
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#setBackgroundColor
int TLuaInterpreter::setBackgroundColor(lua_State* L)
{
string luaSendText = "";
if (!lua_isstring(L, 1)) {
lua_pushstring(L, "setBackgroundColor: wrong argument type");
lua_error(L);
return 1;
} else {
luaSendText = lua_tostring(L, 1);
}
double x1;
if (!lua_isnumber(L, 2)) {
lua_pushstring(L, "setBackgroundColor: wrong argument type");
lua_error(L);
return 1;
Host* pHost = &getHostFromLua(L);
QString windowName;
int r, g, b, alpha;

auto validRange = [](int number) {
return number >= 0 and number <= 255;
};

int s = 1;
if (lua_isstring(L, s) && !lua_isnumber(L, s)) {
windowName = QString::fromUtf8(lua_tostring(L, s));

if (!lua_isnumber(L, ++s)) {
lua_pushfstring(L, "setBackgroundColor: bad argument #%d type (red value 0-255 as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
} else {
r = static_cast<int>(lua_tonumber(L, s));

if (!validRange(r)) {
lua_pushnil(L);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d value (red value needs to be between 0-255, got %d!)", s, r);
return 2;
}
}
} else if (lua_isnumber(L, s)) {
r = static_cast<int>(lua_tonumber(L, s));

if (!validRange(r)) {
lua_pushnil(L);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d value (red value needs to be between 0-255, got %d!)", s, r);
return 2;
}
} else {
x1 = lua_tonumber(L, 2);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d type (window name as string, or red value 0-255 as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
}
double y1;
if (!lua_isnumber(L, 3)) {
lua_pushstring(L, "setBackgroundColor: wrong argument type");
lua_error(L);
return 1;

if (!lua_isnumber(L, ++s)) {
lua_pushfstring(L, "setBackgroundColor: bad argument #%d type (green value 0-255 as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
} else {
y1 = lua_tonumber(L, 3);
g = static_cast<int>(lua_tonumber(L, s));

if (!validRange(g)) {
lua_pushnil(L);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d value (green value needs to be between 0-255, got %d!)", s, g);
return 2;
}
}
double x2;
if (!lua_isnumber(L, 4)) {
lua_pushstring(L, "setBackgroundColor: wrong argument type");
lua_error(L);
return 1;

if (!lua_isnumber(L, ++s)) {
lua_pushfstring(L, "setBackgroundColor: bad argument #%d type (blue value 0-255 as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
} else {
x2 = lua_tonumber(L, 4);
b = static_cast<int>(lua_tonumber(L, s));

if (!validRange(b)) {
lua_pushnil(L);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d value (blue value needs to be between 0-255, got %d!)", s, b);
return 2;
}
}
double y2;
if (!lua_isnumber(L, 5)) {
lua_pushstring(L, "setBackgroundColor: wrong argument type");
lua_error(L);
return 1;

// if we get nothing for the alpha value, assume it is 255. If we get a non-number value, complain.
if (lua_gettop(L) <= s) {
alpha = 255;
} else if (!lua_isnumber(L, ++s)) {
lua_pushfstring(L, "setBackgroundColor: bad argument #%d type (optional alpha value 0-255 as number expected, got %s!)", s, luaL_typename(L, s));
return lua_error(L);
} else {
y2 = lua_tonumber(L, 5);
alpha = static_cast<int>(lua_tonumber(L, s));

if (!validRange(alpha)) {
lua_pushnil(L);
lua_pushfstring(L, "setBackgroundColor: bad argument #%d value (alpha value needs to be between 0-255, got %d!)", s, alpha);
return 2;
}
}
Host& host = getHostFromLua(L);
QString text(luaSendText.c_str());
mudlet::self()->setBackgroundColor(&host, text, static_cast<int>(x1), static_cast<int>(y1), static_cast<int>(x2), static_cast<int>(y2));

return 0;
if (windowName.isEmpty() || windowName.compare(QStringLiteral("main"), Qt::CaseSensitive) == 0) {
if (mudlet::self()->mConsoleMap.contains(pHost)) {
pHost->mBgColor.setRgb(r, g, b);
pHost->mpConsole->setConsoleBgColor(r, g, b);
} else {
lua_pushnil(L);
lua_pushstring(L, "could not find the main window");
return 2;
}
} else if (!mudlet::self()->setBackgroundColor(pHost, windowName, r, g, b, alpha)) {
lua_pushnil(L);
lua_pushfstring(L, R"(window "%s" not found)", windowName.toUtf8().constData());
return 2;
}
lua_pushboolean(L, true);
return 1;
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#calcFontSize
Expand Down

0 comments on commit 03c14b4

Please sign in to comment.