Add unit/item/job/building getter hook vmethods to dfhack_viewscreen.

develop
Alexander Gavrilov 2012-09-20 11:11:20 +04:00
parent 7ce772ae0e
commit c39a337223
7 changed files with 96 additions and 1 deletions

@ -1467,6 +1467,14 @@ Supported callbacks and fields are:
If this method is omitted, the screen is dismissed on receival of the ``LEAVESCREEN`` key. 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 Internal API
------------ ------------

@ -1611,6 +1611,16 @@ options; if multiple interpretations exist, the table will contain multiple keys
</dl> </dl>
<p>If this method is omitted, the screen is dismissed on receival of the <tt class="docutils literal">LEAVESCREEN</tt> key.</p> <p>If this method is omitted, the screen is dismissed on receival of the <tt class="docutils literal">LEAVESCREEN</tt> key.</p>
</li> </li>
<li><p class="first"><tt class="docutils literal">function screen:onGetSelectedUnit()</tt></p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onGetSelectedItem()</tt></p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onGetSelectedJob()</tt></p>
</li>
<li><p class="first"><tt class="docutils literal">function screen:onGetSelectedBuilding()</tt></p>
<p>Implement these to provide a return value for the matching
<tt class="docutils literal"><span class="pre">dfhack.gui.getSelected...</span></tt> function.</p>
</li>
</ul> </ul>
</div> </div>
<div class="section" id="internal-api"> <div class="section" id="internal-api">

@ -33,6 +33,14 @@ distribution.
#include "df/graphic.h" #include "df/graphic.h"
#include "df/viewscreen.h" #include "df/viewscreen.h"
namespace df
{
struct job;
struct item;
struct unit;
struct building;
}
/** /**
* \defgroup grp_screen utilities for painting to the screen * \defgroup grp_screen utilities for painting to the screen
* @ingroup grp_screen * @ingroup grp_screen
@ -134,6 +142,7 @@ namespace DFHack
virtual ~dfhack_viewscreen(); virtual ~dfhack_viewscreen();
static bool is_instance(df::viewscreen *screen); static bool is_instance(df::viewscreen *screen);
static dfhack_viewscreen *try_cast(df::viewscreen *screen);
virtual void logic(); virtual void logic();
virtual void render(); virtual void render();
@ -146,6 +155,10 @@ namespace DFHack
virtual std::string getFocusString() = 0; virtual std::string getFocusString() = 0;
virtual void onShow() {}; virtual void onShow() {};
virtual void onDismiss() {}; 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 { class DFHACK_EXPORT dfhack_lua_viewscreen : public dfhack_viewscreen {
@ -178,5 +191,10 @@ namespace DFHack
virtual void onShow(); virtual void onShow();
virtual void onDismiss(); virtual void onDismiss();
virtual df::unit *getSelectedUnit();
virtual df::item *getSelectedItem();
virtual df::job *getSelectedJob();
virtual df::building *getSelectedBuilding();
}; };
} }

@ -691,6 +691,8 @@ df::job *Gui::getSelectedJob(color_ostream &out, bool quiet)
return job; return job;
} }
else if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedJob();
else else
return getSelectedWorkshopJob(out, quiet); return getSelectedWorkshopJob(out, quiet);
} }
@ -781,6 +783,9 @@ static df::unit *getAnyUnit(df::viewscreen *top)
return NULL; return NULL;
} }
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedUnit();
if (!Gui::dwarfmode_hotkey(top)) if (!Gui::dwarfmode_hotkey(top))
return NULL; return NULL;
@ -875,6 +880,9 @@ static df::item *getAnyItem(df::viewscreen *top)
return NULL; return NULL;
} }
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedItem();
if (!Gui::dwarfmode_hotkey(top)) if (!Gui::dwarfmode_hotkey(top))
return NULL; return NULL;
@ -942,6 +950,9 @@ static df::building *getAnyBuilding(df::viewscreen *top)
using df::global::world; using df::global::world;
using df::global::ui_sidebar_menus; using df::global::ui_sidebar_menus;
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedBuilding();
if (!Gui::dwarfmode_hotkey(top)) if (!Gui::dwarfmode_hotkey(top))
return NULL; return NULL;

@ -50,6 +50,10 @@ using namespace DFHack;
#include "df/tile_page.h" #include "df/tile_page.h"
#include "df/interfacest.h" #include "df/interfacest.h"
#include "df/enabler.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 namespace df::enums;
using df::global::init; using df::global::init;
@ -322,6 +326,11 @@ bool dfhack_viewscreen::is_instance(df::viewscreen *screen)
return dfhack_screens.count(screen) != 0; return dfhack_screens.count(screen) != 0;
} }
dfhack_viewscreen *dfhack_viewscreen::try_cast(df::viewscreen *screen)
{
return is_instance(screen) ? static_cast<dfhack_viewscreen*>(screen) : NULL;
}
void dfhack_viewscreen::check_resize() void dfhack_viewscreen::check_resize()
{ {
auto size = Screen::getWindowSize(); auto size = Screen::getWindowSize();
@ -637,3 +646,35 @@ void dfhack_lua_viewscreen::onDismiss()
lua_pushstring(Lua::Core::State, "onDismiss"); lua_pushstring(Lua::Core::State, "onDismiss");
safe_call_lua(do_notify, 1, 0); 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<df::unit>(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<df::item>(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<df::job>(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<df::building>(Lua::Core::State, -1);
}

@ -1 +1 @@
Subproject commit 8a78bfa218817765b0a80431e0cf25435ffb2179 Subproject commit eb4af94c7e737d99c194381749ac6743e146913e

@ -338,6 +338,8 @@ public:
std::string getFocusString() { return "unitlabors"; } std::string getFocusString() { return "unitlabors"; }
df::unit *getSelectedUnit();
viewscreen_unitlaborsst(vector<df::unit*> &src, int cursor_pos); viewscreen_unitlaborsst(vector<df::unit*> &src, int cursor_pos);
~viewscreen_unitlaborsst() { }; ~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 struct unitlist_hook : df::viewscreen_unitlistst
{ {
typedef df::viewscreen_unitlistst interpose_base; typedef df::viewscreen_unitlistst interpose_base;