Skip to content

Commit

Permalink
set new window parent (Mudlet#3480)
Browse files Browse the repository at this point in the history
* set new window parent

New lua ui function.
setWindow(windowname, name, x, y)

new Geyser function :changeContainer
to change the container and/or even the parent window.

* added guiutils gauges, flyoutlabels, fix for showing hidden children

Added GUIutils gauges
Flyout labels
and before hidden children were shown if added to a container/userwindow
had to slightly change setWindow

* Update GeyserGeyser.lua

* Added suggestions and proably fixed problem with label z axis

* Prevent hidden container to get shown

* Update GeyserGeyser.lua
  • Loading branch information
Edru2 authored Mar 23, 2020
1 parent 807680c commit 70ab757
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4229,6 +4229,60 @@ int TLuaInterpreter::moveWindow(lua_State* L)
return 0;
}

// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#setWindow
int TLuaInterpreter::setWindow(lua_State* L)
{
QString windowname;
QString name;
int n = lua_gettop(L);
int x = 0, y = 0;
bool show = true;

if (lua_type(L, 1) != LUA_TSTRING) {
lua_pushfstring(L, "setWindow: bad argument #1 type (parent windowname as string expected, got %s!)", luaL_typename(L, 1));
return lua_error(L);
} else {
windowname = QString::fromUtf8(lua_tostring(L, 1));
}

if (lua_type(L, 2) != LUA_TSTRING) {
lua_pushfstring(L, "setWindow: bad argument #2 type (element name as string expected, got %s!)", luaL_typename(L, 2));
return lua_error(L);
} else {
name = QString::fromUtf8(lua_tostring(L, 2));
}
if (n > 2) {
if (!lua_isnumber(L, 3)) {
lua_pushfstring(L, "setWindow: bad argument #3 type (x-coordinate as number expected, got %s!)", luaL_typename(L, 3));
return lua_error(L);

} else {
x = lua_tonumber(L, 3);
}
if (!lua_isnumber(L, 4)) {
lua_pushfstring(L, "setWindow: bad argument #4 type (y-coordinate as number expected, got %s!)", luaL_typename(L, 4));
return lua_error(L);
} else {
y = lua_tonumber(L, 4);
}
if (!lua_isboolean(L, 5)) {
lua_pushfstring(L, "setWindow: bad argument #5 type (show element as boolean expected, got %s!)", luaL_typename(L, 5));
return lua_error(L);
} else {
show = lua_toboolean(L, 5);
}
}

Host& host = getHostFromLua(L);
if (auto [success, message] = mudlet::self()->setWindow(&host, windowname, name, x, y, show); !success) {
lua_pushnil(L);
lua_pushfstring(L, message.toUtf8().constData());
return 2;
}
lua_pushboolean(L, true);
return 1;
}

int TLuaInterpreter::openMapWidget(lua_State* L)
{
int n = lua_gettop(L);
Expand Down Expand Up @@ -16037,6 +16091,7 @@ void TLuaInterpreter::initLuaGlobals()
lua_register(pGlobalLua, "setLabelOnLeave", TLuaInterpreter::setLabelOnLeave);
lua_register(pGlobalLua, "getImageSize", TLuaInterpreter::getImageSize);
lua_register(pGlobalLua, "moveWindow", TLuaInterpreter::moveWindow);
lua_register(pGlobalLua, "setWindow", TLuaInterpreter::setWindow);
lua_register(pGlobalLua, "openMapWidget", TLuaInterpreter::openMapWidget);
lua_register(pGlobalLua, "closeMapWidget", TLuaInterpreter::closeMapWidget);
lua_register(pGlobalLua, "setTextFormat", TLuaInterpreter::setTextFormat);
Expand Down
1 change: 1 addition & 0 deletions src/TLuaInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ class TLuaInterpreter : public QThread
static int deleteLabel(lua_State*);
static int setLabelToolTip(lua_State*);
static int moveWindow(lua_State*);
static int setWindow(lua_State*);
static int openMapWidget(lua_State*);
static int closeMapWidget(lua_State*);
static int setTextFormat(lua_State*);
Expand Down
14 changes: 14 additions & 0 deletions src/mudlet-lua/lua/GUIUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ function showGauge(gaugeName)
showWindow(gaugeName .. "_text")
end

--- @see createGauge
function setGaugeWindow(windowName, gaugeName, x, y, show)
windowName = windowName or "main"
x = x or 0
y = y or 0
show = show or true
assert(gaugesTable[gaugeName], "setGaugeWindow: no such gauge exists.")
setWindow(windowName, gaugeName .. "_back", x, y, show)
setWindow(windowName, gaugeName .. "_front", x, y, show)
setWindow(windowName, gaugeName .. "_text", x, y, show)
-- save new values in table
gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

--- Set the text on a custom gauge.
---
Expand Down
76 changes: 75 additions & 1 deletion src/mudlet-lua/lua/geyser/GeyserGeyser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ function Geyser:add (window, cons)
if not self.defer_updates then
window:reposition()
end
window:show()
if not (window.hidden or window.auto_hidden) then
window:show()
end
end

--- Removes a window from the list that it manages
Expand All @@ -85,3 +87,75 @@ function Geyser:remove (window)
index = table.index_of(self.windows, window.name) or 0
table.remove(self.windows, index)
end


--- Removes a window from the parent it is in and puts it in a new one
-- This is only used internally.
-- @param window The new parents windowname
local function setMyWindow(self, windowname)
windowname = windowname or "main"
local name
name = self.name
if self.type == "mapper" then
name = self.type
end

-- Change containerwindow for nested Labels
if self.type == "label" and self.nestedLabels then
for k,v in ipairs(self.nestedLabels) do
if windowname ~= "main" then
v:changeContainer(Geyser.windowList[windowname.."Container"].windowList[windowname])
else
v:changeContainer(Geyser)
end
end
closeAllLevels(self)
end

-- Prevent hidden children to get visible
if self.hidden or self.auto_hidden then
setWindow(windowname, name, 0, 0, false)
else
setWindow(windowname, name, 0, 0, true)
end
end


--- Removes all containers windows from the parent they are in and puts them in a new one
-- This is only used internally
-- @param window The new parents windowname
local function setContainerWindow(self, windowname)
self.windowname = windowname
--Iterate through windows has a given order and prevents problems with z-coordinate
for k,v in ipairs(self.windows) do
setMyWindow(self.windowList[v], windowname)
setContainerWindow(self.windowList[v], windowname)
end
end

--- Removes a window from the container that it manages
-- @param container The new container the window will be set in
function Geyser:changeContainer (container)
--Change container to Geyser if "main" is given
if type(container) == "string" and container:lower() == "main" then
container = Geyser
end
--only a container has a windowList
if not container or not container.windowList or self == container then
return nil, "didn't get a valid container"
end
--Nothing to change
if self.container == container then
return nil, "nothing to change. "..self.name.." is already in this container"
end
--If there is no windowname then windowname is "main"
local windowname = container.windowname
windowname = windowname or "main"

self.container:remove(self)
if self.windowname ~= windowname then
setMyWindow(self, windowname)
setContainerWindow(self, windowname)
end
container:add(self)
end
48 changes: 48 additions & 0 deletions src/mudlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,54 @@ bool mudlet::moveWindow(Host* pHost, const QString& name, int x1, int y1)
return false;
}

std::pair<bool, QString> mudlet::setWindow(Host* pHost, const QString& windowname, const QString& name, int x1, int y1, bool show)
{
if (!pHost || !pHost->mpConsole) {
return {false, QString()};
}

auto pL = pHost->mpConsole->mLabelMap.value(name);
auto pC = pHost->mpConsole->mSubConsoleMap.value(name);
auto pD = pHost->mpConsole->mDockWidgetMap.value(windowname);
auto pW = pHost->mpConsole->mpMainFrame;
auto pM = pHost->mpConsole->mpMapper;

if (!pD && windowname.toLower() != QLatin1String("main")) {
return {false, QStringLiteral("Window \"%1\" not found.").arg(windowname)};
}

if (pD) {
pW = pD->widget();
}

if (pL) {
pL->setParent(pW);
pL->move(x1, y1);
if (show) {
pL->show();
}
return {true, QString()};
} else if (pC) {
pC->setParent(pW);
pC->move(x1, y1);
pC->mOldX = x1;
pC->mOldY = y1;
if (show) {
pC->show();
}
return {true, QString()};
} else if (pM && name.toLower() == QLatin1String("mapper")) {
pM->setParent(pW);
pM->move(x1, y1);
if (show) {
pM->show();
}
return {true, QString()};
}

return {false, QStringLiteral("Element \"%1\" not found.").arg(name)};
}

std::pair<bool, QString> mudlet::openMapWidget(Host* pHost, const QString& area, int x, int y, int width, int height)
{
if (!pHost || !pHost->mpConsole) {
Expand Down
1 change: 1 addition & 0 deletions src/mudlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class mudlet : public QMainWindow, public Ui::main_window
bool setLabelOnEnter(Host*, const QString&, const QString&, const TEvent&);
bool setLabelOnLeave(Host*, const QString&, const QString&, const TEvent&);
bool moveWindow(Host*, const QString& name, int, int);
std::pair<bool, QString> setWindow(Host* pHost, const QString& windowname, const QString& name, int x1, int y1, bool show);
std::pair<bool, QString> openMapWidget(Host* pHost, const QString& area, int x, int y, int width, int height);
std::pair<bool, QString> closeMapWidget(Host* pHost);
void deleteLine(Host*, const QString& name);
Expand Down

0 comments on commit 70ab757

Please sign in to comment.