From 7ce772ae0ea69ab26009a27d71bd681382ecfac7 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 20 Sep 2012 10:41:03 +0400 Subject: Add an API function that returns the selected building. --- library/LuaApi.cpp | 1 + library/include/modules/Gui.h | 4 +++ library/modules/Gui.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) (limited to 'library') diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d73d14cf..b2d41dc1 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 97e8bd42..b06408f6 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 1662f446..10a20dc2 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( -- cgit v1.2.1