Add getSelectedPlant() and related functions

Currently only works with the center tile of multi-tile trees
develop
lethosor 2017-05-05 14:45:46 -04:00
parent a527091172
commit d3c496cc2b
7 changed files with 74 additions and 4 deletions

@ -921,6 +921,10 @@ Gui module
Returns the building selected via :kbd:`q`, :kbd:`t`, :kbd:`k` or :kbd:`i`.
* ``dfhack.gui.getSelectedPlant([silent])``
Returns the plant selected via :kbd:`k`.
* ``dfhack.gui.writeToGamelog(text)``
Writes a string to :file:`gamelog.txt` without doing an announcement.

@ -1453,6 +1453,7 @@ static const LuaWrapper::FunctionReg dfhack_gui_module[] = {
WRAPM(Gui, getSelectedUnit),
WRAPM(Gui, getSelectedItem),
WRAPM(Gui, getSelectedBuilding),
WRAPM(Gui, getSelectedPlant),
WRAPM(Gui, writeToGamelog),
WRAPM(Gui, makeAnnouncement),
WRAPM(Gui, addCombatReport),

@ -45,6 +45,7 @@ namespace df {
struct job;
struct unit;
struct item;
struct plant;
};
/**
@ -104,6 +105,11 @@ namespace DFHack
DFHACK_EXPORT df::building *getAnyBuilding(df::viewscreen *top);
DFHACK_EXPORT df::building *getSelectedBuilding(color_ostream &out, bool quiet = false);
// A plant is selected, e.g. via 'k'
DFHACK_EXPORT bool any_plant_hotkey(df::viewscreen *top);
DFHACK_EXPORT df::plant *getAnyPlant(df::viewscreen *top);
DFHACK_EXPORT df::plant *getSelectedPlant(color_ostream &out, bool quiet = false);
// Low-level API that gives full control over announcements and reports
DFHACK_EXPORT void writeToGamelog(std::string message);

@ -44,6 +44,7 @@ namespace df
struct item;
struct unit;
struct building;
struct plant;
}
/**
@ -326,10 +327,11 @@ 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; }
virtual df::unit *getSelectedUnit() { return nullptr; }
virtual df::item *getSelectedItem() { return nullptr; }
virtual df::job *getSelectedJob() { return nullptr; }
virtual df::building *getSelectedBuilding() { return nullptr; }
virtual df::plant *getSelectedPlant() { return nullptr; }
};
class DFHACK_EXPORT dfhack_lua_viewscreen : public dfhack_viewscreen {
@ -369,5 +371,6 @@ namespace DFHack
virtual df::item *getSelectedItem();
virtual df::job *getSelectedJob();
virtual df::building *getSelectedBuilding();
virtual df::plant *getSelectedPlant();
};
}

@ -635,6 +635,8 @@ function df_shortcut_var(k)
return dfhack.gui.getSelectedWorkshopJob()
elseif k == 'unit' then
return dfhack.gui.getSelectedUnit()
elseif k == 'plant' then
return dfhack.gui.getSelectedPlant()
else
for g in pairs(df.global) do
if g == k then

@ -92,6 +92,7 @@ using namespace DFHack;
#include "df/game_mode.h"
#include "df/unit.h"
#include "df/occupation.h"
#include "df/plant.h"
using namespace df::enums;
using df::global::gview;
@ -1120,6 +1121,50 @@ df::building *Gui::getSelectedBuilding(color_ostream &out, bool quiet)
return building;
}
df::plant *Gui::getAnyPlant(df::viewscreen *top)
{
using df::global::cursor;
using df::global::ui;
using df::global::world;
if (auto dfscreen = dfhack_viewscreen::try_cast(top))
return dfscreen->getSelectedPlant();
if (Gui::dwarfmode_hotkey(top))
{
if (!cursor || !ui || !world)
return nullptr;
if (ui->main.mode == ui_sidebar_mode::LookAround)
{
for (df::plant *plant : world->plants.all)
{
if (plant->pos.x == cursor->x && plant->pos.y == cursor->y && plant->pos.z == cursor->z)
{
return plant;
}
}
}
}
return nullptr;
}
bool Gui::any_plant_hotkey(df::viewscreen *top)
{
return getAnyPlant(top) != nullptr;
}
df::plant *Gui::getSelectedPlant(color_ostream &out, bool quiet)
{
df::plant *plant = getAnyPlant(Core::getTopViewscreen());
if (!plant && !quiet)
out.printerr("No plant is selected in the UI.\n");
return plant;
}
//
DFHACK_EXPORT void Gui::writeToGamelog(std::string message)

@ -57,6 +57,7 @@ using namespace DFHack;
#include "df/job.h"
#include "df/building.h"
#include "df/renderer.h"
#include "df/plant.h"
using namespace df::enums;
using df::global::init;
@ -934,3 +935,11 @@ df::building *dfhack_lua_viewscreen::getSelectedBuilding()
safe_call_lua(do_notify, 1, 1);
return Lua::GetDFObject<df::building>(Lua::Core::State, -1);
}
df::plant *dfhack_lua_viewscreen::getSelectedPlant()
{
Lua::StackUnwinder frame(Lua::Core::State);
lua_pushstring(Lua::Core::State, "onGetSelectedPlant");
safe_call_lua(do_notify, 1, 1);
return Lua::GetDFObject<df::plant>(Lua::Core::State, -1);
}