From 7ce772ae0ea69ab26009a27d71bd681382ecfac7 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 20 Sep 2012 10:41:03 +0400 Subject: [PATCH] Add an API function that returns the selected building. --- LUA_API.rst | 4 +++ Lua API.html | 3 ++ library/LuaApi.cpp | 1 + library/include/modules/Gui.h | 4 +++ library/modules/Gui.cpp | 58 +++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+) diff --git a/LUA_API.rst b/LUA_API.rst index c5f9a1c58..898ab79c0 100644 --- a/LUA_API.rst +++ b/LUA_API.rst @@ -777,6 +777,10 @@ Gui module last case, the highlighted *contained item* is returned, not the container itself. +* ``dfhack.gui.getSelectedBuilding([silent])`` + + Returns the building selected via *'q'*, *'t'*, *'k'* or *'i'*. + * ``dfhack.gui.showAnnouncement(text,color[,is_bright])`` Adds a regular announcement with given text, color, and brightness. diff --git a/Lua API.html b/Lua API.html index 07f038bc4..97770ff77 100644 --- a/Lua API.html +++ b/Lua API.html @@ -1032,6 +1032,9 @@ a full-screen item view of a container. Note that in the last case, the highlighted contained item is returned, not the container itself.

+
  • dfhack.gui.getSelectedBuilding([silent])

    +

    Returns the building selected via 'q', 't', 'k' or 'i'.

    +
  • dfhack.gui.showAnnouncement(text,color[,is_bright])

    Adds a regular announcement with given text, color, and brightness. The is_bright boolean actually seems to invert the brightness.

    diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d73d14cf9..b2d41dc19 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -760,6 +760,7 @@ static const LuaWrapper::FunctionReg dfhack_gui_module[] = { WRAPM(Gui, getSelectedJob), WRAPM(Gui, getSelectedUnit), WRAPM(Gui, getSelectedItem), + WRAPM(Gui, getSelectedBuilding), WRAPM(Gui, showAnnouncement), WRAPM(Gui, showZoomAnnouncement), WRAPM(Gui, showPopupAnnouncement), diff --git a/library/include/modules/Gui.h b/library/include/modules/Gui.h index 97e8bd422..b06408f68 100644 --- a/library/include/modules/Gui.h +++ b/library/include/modules/Gui.h @@ -91,6 +91,10 @@ namespace DFHack DFHACK_EXPORT bool any_item_hotkey(df::viewscreen *top); DFHACK_EXPORT df::item *getSelectedItem(color_ostream &out, bool quiet = false); + // A building is selected via 'q', 't' or 'i' (civzone) + DFHACK_EXPORT bool any_building_hotkey(df::viewscreen *top); + DFHACK_EXPORT df::building *getSelectedBuilding(color_ostream &out, bool quiet = false); + // Show a plain announcement, or a titan-style popup message DFHACK_EXPORT void showAnnouncement(std::string message, int color = 7, bool bright = true); DFHACK_EXPORT void showZoomAnnouncement(df::announcement_type type, df::coord pos, std::string message, int color = 7, bool bright = true); diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 1662f4467..10a20dc2e 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -933,6 +933,64 @@ df::item *Gui::getSelectedItem(color_ostream &out, bool quiet) return item; } +static df::building *getAnyBuilding(df::viewscreen *top) +{ + using namespace ui_sidebar_mode; + using df::global::ui; + using df::global::ui_look_list; + using df::global::ui_look_cursor; + using df::global::world; + using df::global::ui_sidebar_menus; + + if (!Gui::dwarfmode_hotkey(top)) + return NULL; + + switch (ui->main.mode) { + case LookAround: + { + if (!ui_look_list || !ui_look_cursor) + return NULL; + + auto item = vector_get(ui_look_list->items, *ui_look_cursor); + if (item && item->type == df::ui_look_list::T_items::Building) + return item->building; + else + return NULL; + } + case QueryBuilding: + case BuildingItems: + { + return world->selected_building; + } + case Zones: + case ZonesPenInfo: + case ZonesPitInfo: + case ZonesHospitalInfo: + { + if (ui_sidebar_menus) + return ui_sidebar_menus->zone.selected; + return NULL; + } + default: + return NULL; + } +} + +bool Gui::any_building_hotkey(df::viewscreen *top) +{ + return getAnyBuilding(top) != NULL; +} + +df::building *Gui::getSelectedBuilding(color_ostream &out, bool quiet) +{ + df::building *building = getAnyBuilding(Core::getTopViewscreen()); + + if (!building && !quiet) + out.printerr("No building is selected in the UI.\n"); + + return building; +} + // static void doShowAnnouncement(