From c39a33722316ee91f7a42abcd76149788dd19196 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 20 Sep 2012 11:11:20 +0400 Subject: Add unit/item/job/building getter hook vmethods to dfhack_viewscreen. --- LUA_API.rst | 8 ++++++++ Lua API.html | 10 ++++++++++ library/include/modules/Screen.h | 18 ++++++++++++++++++ library/modules/Gui.cpp | 11 +++++++++++ library/modules/Screen.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ library/xml | 2 +- plugins/manipulator.cpp | 7 +++++++ 7 files changed, 96 insertions(+), 1 deletion(-) diff --git a/LUA_API.rst b/LUA_API.rst index 898ab79c..d9b75c48 100644 --- a/LUA_API.rst +++ b/LUA_API.rst @@ -1467,6 +1467,14 @@ Supported callbacks and fields are: If this method is omitted, the screen is dismissed on receival of the ``LEAVESCREEN`` key. +* ``function screen:onGetSelectedUnit()`` +* ``function screen:onGetSelectedItem()`` +* ``function screen:onGetSelectedJob()`` +* ``function screen:onGetSelectedBuilding()`` + + Implement these to provide a return value for the matching + ``dfhack.gui.getSelected...`` function. + Internal API ------------ diff --git a/Lua API.html b/Lua API.html index 97770ff7..0da65b5f 100644 --- a/Lua API.html +++ b/Lua API.html @@ -1611,6 +1611,16 @@ options; if multiple interpretations exist, the table will contain multiple keys

If this method is omitted, the screen is dismissed on receival of the LEAVESCREEN key.

+
  • function screen:onGetSelectedUnit()

    +
  • +
  • function screen:onGetSelectedItem()

    +
  • +
  • function screen:onGetSelectedJob()

    +
  • +
  • function screen:onGetSelectedBuilding()

    +

    Implement these to provide a return value for the matching +dfhack.gui.getSelected... function.

    +
  • diff --git a/library/include/modules/Screen.h b/library/include/modules/Screen.h index 4f47205f..fdad6c8a 100644 --- a/library/include/modules/Screen.h +++ b/library/include/modules/Screen.h @@ -33,6 +33,14 @@ distribution. #include "df/graphic.h" #include "df/viewscreen.h" +namespace df +{ + struct job; + struct item; + struct unit; + struct building; +} + /** * \defgroup grp_screen utilities for painting to the screen * @ingroup grp_screen @@ -134,6 +142,7 @@ namespace DFHack virtual ~dfhack_viewscreen(); static bool is_instance(df::viewscreen *screen); + static dfhack_viewscreen *try_cast(df::viewscreen *screen); virtual void logic(); virtual void render(); @@ -146,6 +155,10 @@ namespace DFHack virtual std::string getFocusString() = 0; virtual void onShow() {}; virtual void onDismiss() {}; + virtual df::unit *getSelectedUnit() { return NULL; } + virtual df::item *getSelectedItem() { return NULL; } + virtual df::job *getSelectedJob() { return NULL; } + virtual df::building *getSelectedBuilding() { return NULL; } }; class DFHACK_EXPORT dfhack_lua_viewscreen : public dfhack_viewscreen { @@ -178,5 +191,10 @@ namespace DFHack virtual void onShow(); virtual void onDismiss(); + + virtual df::unit *getSelectedUnit(); + virtual df::item *getSelectedItem(); + virtual df::job *getSelectedJob(); + virtual df::building *getSelectedBuilding(); }; } diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 10a20dc2..bc7deedc 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -691,6 +691,8 @@ df::job *Gui::getSelectedJob(color_ostream &out, bool quiet) return job; } + else if (auto dfscreen = dfhack_viewscreen::try_cast(top)) + return dfscreen->getSelectedJob(); else return getSelectedWorkshopJob(out, quiet); } @@ -781,6 +783,9 @@ static df::unit *getAnyUnit(df::viewscreen *top) return NULL; } + if (auto dfscreen = dfhack_viewscreen::try_cast(top)) + return dfscreen->getSelectedUnit(); + if (!Gui::dwarfmode_hotkey(top)) return NULL; @@ -875,6 +880,9 @@ static df::item *getAnyItem(df::viewscreen *top) return NULL; } + if (auto dfscreen = dfhack_viewscreen::try_cast(top)) + return dfscreen->getSelectedItem(); + if (!Gui::dwarfmode_hotkey(top)) return NULL; @@ -942,6 +950,9 @@ static df::building *getAnyBuilding(df::viewscreen *top) using df::global::world; using df::global::ui_sidebar_menus; + if (auto dfscreen = dfhack_viewscreen::try_cast(top)) + return dfscreen->getSelectedBuilding(); + if (!Gui::dwarfmode_hotkey(top)) return NULL; diff --git a/library/modules/Screen.cpp b/library/modules/Screen.cpp index 9f258fe0..8057d17a 100644 --- a/library/modules/Screen.cpp +++ b/library/modules/Screen.cpp @@ -50,6 +50,10 @@ using namespace DFHack; #include "df/tile_page.h" #include "df/interfacest.h" #include "df/enabler.h" +#include "df/unit.h" +#include "df/item.h" +#include "df/job.h" +#include "df/building.h" using namespace df::enums; using df::global::init; @@ -322,6 +326,11 @@ bool dfhack_viewscreen::is_instance(df::viewscreen *screen) return dfhack_screens.count(screen) != 0; } +dfhack_viewscreen *dfhack_viewscreen::try_cast(df::viewscreen *screen) +{ + return is_instance(screen) ? static_cast(screen) : NULL; +} + void dfhack_viewscreen::check_resize() { auto size = Screen::getWindowSize(); @@ -637,3 +646,35 @@ void dfhack_lua_viewscreen::onDismiss() lua_pushstring(Lua::Core::State, "onDismiss"); safe_call_lua(do_notify, 1, 0); } + +df::unit *dfhack_lua_viewscreen::getSelectedUnit() +{ + Lua::StackUnwinder frame(Lua::Core::State); + lua_pushstring(Lua::Core::State, "onGetSelectedUnit"); + safe_call_lua(do_notify, 1, 1); + return Lua::GetDFObject(Lua::Core::State, -1); +} + +df::item *dfhack_lua_viewscreen::getSelectedItem() +{ + Lua::StackUnwinder frame(Lua::Core::State); + lua_pushstring(Lua::Core::State, "onGetSelectedItem"); + safe_call_lua(do_notify, 1, 1); + return Lua::GetDFObject(Lua::Core::State, -1); +} + +df::job *dfhack_lua_viewscreen::getSelectedJob() +{ + Lua::StackUnwinder frame(Lua::Core::State); + lua_pushstring(Lua::Core::State, "onGetSelectedJob"); + safe_call_lua(do_notify, 1, 1); + return Lua::GetDFObject(Lua::Core::State, -1); +} + +df::building *dfhack_lua_viewscreen::getSelectedBuilding() +{ + Lua::StackUnwinder frame(Lua::Core::State); + lua_pushstring(Lua::Core::State, "onGetSelectedBuilding"); + safe_call_lua(do_notify, 1, 1); + return Lua::GetDFObject(Lua::Core::State, -1); +} diff --git a/library/xml b/library/xml index 8a78bfa2..eb4af94c 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 8a78bfa218817765b0a80431e0cf25435ffb2179 +Subproject commit eb4af94c7e737d99c194381749ac6743e146913e diff --git a/plugins/manipulator.cpp b/plugins/manipulator.cpp index 2b3fc86f..033ad8b8 100644 --- a/plugins/manipulator.cpp +++ b/plugins/manipulator.cpp @@ -338,6 +338,8 @@ public: std::string getFocusString() { return "unitlabors"; } + df::unit *getSelectedUnit(); + viewscreen_unitlaborsst(vector &src, int cursor_pos); ~viewscreen_unitlaborsst() { }; @@ -986,6 +988,11 @@ void viewscreen_unitlaborsst::render() } } +df::unit *viewscreen_unitlaborsst::getSelectedUnit() +{ + return units[sel_row]->unit; +} + struct unitlist_hook : df::viewscreen_unitlistst { typedef df::viewscreen_unitlistst interpose_base; -- cgit v1.2.1