dfhack/plugins/search.cpp

579 lines
14 KiB
C++

// Dwarf Manipulator - a Therapist-style labor editor
#include <modules/Screen.h>
#include <modules/Translation.h>
#include <modules/Units.h>
#include <MiscUtils.h>
#include <VTableInterpose.h>
#include "df/viewscreen_petst.h"
#include "df/viewscreen_storesst.h"
2012-10-21 02:32:01 -06:00
#include "df/viewscreen_tradegoodsst.h"
#include "df/viewscreen_unitlistst.h"
#include "df/interface_key.h"
using std::set;
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
using df::global::gps;
void OutputString(int8_t color, int &x, int y, const std::string &text)
{
Screen::paintString(Screen::Pen(' ', color, 0), x, y, text);
x += text.length();
}
2012-10-20 19:18:29 -06:00
//
// START: Base Search functionality
//
template <class S, class T, class V = void*>
2012-10-21 02:32:01 -06:00
class search_parent
{
2012-10-21 02:32:01 -06:00
public:
void reset_all()
{
reset_search();
valid = false;
sort_list1 = NULL;
sort_list2 = NULL;
viewscreen = NULL;
select_key = 's';
track_secondary_values = false;
2012-10-21 02:32:01 -06:00
}
virtual bool process_input(set<df::interface_key> *input)
{
if (lock != NULL && lock != this)
return false;
if (!should_check_input(input))
return false;
bool key_processed = true;
if (entry_mode)
{
df::interface_key last_token = *input->rbegin();
if (last_token >= interface_key::STRING_A032 && last_token <= interface_key::STRING_A126)
{
search_string += last_token - ascii_to_enum_offset;
do_search();
}
else if (last_token == interface_key::STRING_A000)
{
if (search_string.length() > 0)
{
search_string.erase(search_string.length()-1);
do_search();
}
}
else if (input->count(interface_key::SELECT) || input->count(interface_key::LEAVESCREEN))
{
end_entry_mode();
}
else if (input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)
|| input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT))
{
end_entry_mode();
key_processed = false;
}
}
else if (input->count(select_token))
{
start_entry_mode();
}
else if (input->count((df::interface_key) (select_token + shift_offset)))
{
clear_search();
}
else
{
key_processed = false;
}
return key_processed || entry_mode;
}
virtual void do_post_update_check()
{
if (redo_search)
{
do_search();
redo_search = false;
}
}
static search_parent<S,T,V> *lock;
protected:
2012-10-20 19:18:29 -06:00
const S *viewscreen;
vector <T> saved_list1, reference_list;
vector <V> saved_list2;
vector <int> saved_indexes;
2012-10-20 19:18:29 -06:00
bool valid;
bool redo_search;
bool track_secondary_values;
2012-10-20 19:18:29 -06:00
string search_string;
2012-10-21 02:32:01 -06:00
search_parent() : ascii_to_enum_offset(interface_key::STRING_A048 - '0'), shift_offset('A' - 'a')
2012-10-20 19:18:29 -06:00
{
reset_all();
}
virtual void init(int *cursor_pos, vector <T> *sort_list1, vector <V> *sort_list2 = NULL, char select_key = 's')
2012-10-20 19:18:29 -06:00
{
this->cursor_pos = cursor_pos;
this->sort_list1 = sort_list1;
this->sort_list2 = sort_list2;
this->select_key = select_key;
select_token = (df::interface_key) (ascii_to_enum_offset + select_key);
track_secondary_values = false;
2012-10-20 19:18:29 -06:00
valid = true;
}
2012-10-21 02:32:01 -06:00
bool is_entry_mode()
{
return entry_mode;
}
void start_entry_mode()
{
entry_mode = true;
lock = this;
}
2012-10-20 19:18:29 -06:00
2012-10-21 02:32:01 -06:00
void end_entry_mode()
2012-10-20 19:18:29 -06:00
{
entry_mode = false;
2012-10-21 02:32:01 -06:00
lock = NULL;
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
void reset_search()
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
end_entry_mode();
search_string = "";
saved_list1.clear();
saved_list2.clear();
reference_list.clear();
saved_indexes.clear();
}
void update_secondary_values()
{
if (sort_list2 != NULL && track_secondary_values)
{
bool list_has_been_sorted = (sort_list1->size() == reference_list.size()
&& *sort_list1 != reference_list);
for (int i = 0; i < saved_indexes.size(); i++)
{
int adjusted_item_index = i;
if (list_has_been_sorted)
{
for (int j = 0; j < sort_list1->size(); j++)
{
if ((*sort_list1)[j] == reference_list[i])
{
adjusted_item_index = j;
break;
}
}
}
saved_list2[saved_indexes[i]] = (*sort_list2)[adjusted_item_index];
}
saved_indexes.clear();
}
}
//Used to work out if filtered list has been sorted after filtering
void store_reference_values()
{
if (track_secondary_values)
reference_list = *sort_list1;
2012-10-20 19:18:29 -06:00
}
void clear_search()
{
if (saved_list1.size() > 0)
{
*sort_list1 = saved_list1;
if (sort_list2 != NULL)
{
update_secondary_values();
2012-10-20 19:18:29 -06:00
*sort_list2 = saved_list2;
}
2012-10-20 19:18:29 -06:00
}
store_reference_values();
2012-10-21 02:32:01 -06:00
search_string = "";
2012-10-20 19:18:29 -06:00
}
void do_search()
{
if (search_string.length() == 0)
{
clear_search();
return;
}
if (saved_list1.size() == 0)
{
saved_list1 = *sort_list1;
if (sort_list2 != NULL)
saved_list2 = *sort_list2;
}
else
update_secondary_values();
2012-10-20 19:18:29 -06:00
sort_list1->clear();
if (sort_list2 != NULL)
{
2012-10-20 19:18:29 -06:00
sort_list2->clear();
saved_indexes.clear();
}
2012-10-20 19:18:29 -06:00
string search_string_l = toLower(search_string);
for (int i = 0; i < saved_list1.size(); i++ )
{
T element = saved_list1[i];
2012-10-20 19:18:29 -06:00
string desc = toLower(get_element_description(element));
if (desc.find(search_string_l) != string::npos)
{
sort_list1->push_back(element);
if (sort_list2 != NULL)
{
2012-10-20 19:18:29 -06:00
sort_list2->push_back(saved_list2[i]);
if (track_secondary_values)
saved_indexes.push_back(i);
}
2012-10-20 19:18:29 -06:00
}
}
store_reference_values();
2012-10-20 19:18:29 -06:00
*cursor_pos = 0;
}
2012-10-21 02:32:01 -06:00
virtual bool should_check_input(set<df::interface_key> *input)
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
return true;
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
void print_search_option(int x, int y = -1) const
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
if (y == -1)
y = gps->dimy - 2;
2012-10-20 19:18:29 -06:00
2012-10-21 02:32:01 -06:00
OutputString((entry_mode) ? 4 : 12, x, y, string(1, select_key));
OutputString((entry_mode) ? 10 : 15, x, y, ": Search");
2012-10-20 19:18:29 -06:00
if (search_string.length() > 0 || entry_mode)
2012-10-21 02:32:01 -06:00
OutputString(15, x, y, ": " + search_string);
2012-10-20 19:18:29 -06:00
if (entry_mode)
2012-10-21 02:32:01 -06:00
OutputString(10, x, y, "_");
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
virtual string get_element_description(T element) const = 0;
2012-10-21 02:32:01 -06:00
virtual void render () const = 0;
private:
vector <T> *sort_list1;
vector <V> *sort_list2;
2012-10-21 02:32:01 -06:00
int *cursor_pos;
char select_key;
bool entry_mode;
df::interface_key select_token;
const int ascii_to_enum_offset;
const int shift_offset;
};
2012-10-21 02:32:01 -06:00
template <class S, class T, class V> search_parent<S,T,V> *search_parent<S,T,V> ::lock = NULL;
2012-10-21 02:32:01 -06:00
template <class T, class V, typename D = void>
2012-10-20 19:18:29 -06:00
struct search_hook : T
{
2012-10-20 19:18:29 -06:00
typedef T interpose_base;
static V module;
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{
module.init(this);
if (!module.process_input(input))
{
INTERPOSE_NEXT(feed)(input);
module.do_post_update_check();
}
}
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
module.init(this);
INTERPOSE_NEXT(render)();
module.render();
}
};
2012-10-21 02:32:01 -06:00
template <class T, class V, typename D> V search_hook<T, V, D> ::module;
2012-10-20 19:18:29 -06:00
//
// END: Base Search functionality
//
2012-10-20 19:18:29 -06:00
//
// START: Stocks screen search
//
class stocks_search : public search_parent<df::viewscreen_storesst, df::item*>
{
2012-10-21 02:32:01 -06:00
public:
2012-10-20 19:18:29 -06:00
virtual void render() const
{
if (!viewscreen->in_group_mode)
print_search_option(1);
else
{
int x = 1;
OutputString(15, x, gps->dimy - 2, "Tab to enable Search");
}
}
virtual void do_post_update_check()
{
if (viewscreen->in_group_mode)
{
clear_search();
reset_search();
}
else
search_parent::do_post_update_check();
}
virtual void init(df::viewscreen_storesst *screen)
{
if (!valid)
{
viewscreen = screen;
search_parent::init(&screen->item_cursor, &screen->items);
}
}
2012-10-21 02:32:01 -06:00
private:
virtual string get_element_description(df::item *element) const
{
return Items::getDescription(element, 0, true);
}
virtual bool should_check_input(set<df::interface_key> *input)
{
if (viewscreen->in_group_mode)
return false;
if ((input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)) && !viewscreen->in_right_list)
{
saved_list1.clear();
end_entry_mode();
if (search_string.length() > 0)
redo_search = true;
return false;
}
return true;
}
};
2012-10-20 19:18:29 -06:00
typedef search_hook<df::viewscreen_storesst, stocks_search> stocks_search_hook;
IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, render);
//
// END: Stocks screen search
//
2012-10-20 19:18:29 -06:00
//
// START: Unit screen search
//
class unitlist_search : public search_parent<df::viewscreen_unitlistst, df::unit*, df::job*>
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
public:
2012-10-20 19:18:29 -06:00
virtual void render() const
{
print_search_option(28);
}
2012-10-21 02:32:01 -06:00
virtual void init(df::viewscreen_unitlistst *screen)
{
if (!valid)
{
viewscreen = screen;
search_parent::init(&screen->cursor_pos[viewscreen->page], &screen->units[viewscreen->page], &screen->jobs[viewscreen->page]);
}
}
private:
2012-10-20 19:18:29 -06:00
virtual string get_element_description(df::unit *element) const
{
string desc = Translation::TranslateName(Units::getVisibleName(element), false);
if (viewscreen->page == 1)
desc += Units::getProfessionName(element);
return desc;
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
virtual bool should_check_input(set<df::interface_key> *input)
2012-10-20 19:18:29 -06:00
{
if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT) || input->count(interface_key::CUSTOM_L))
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
if (!is_entry_mode())
2012-10-20 19:18:29 -06:00
{
clear_search();
2012-10-21 06:09:10 -06:00
reset_all();
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
else
input->clear();
return false;
2012-10-20 19:18:29 -06:00
}
return true;
}
2012-10-21 02:32:01 -06:00
};
typedef search_hook<df::viewscreen_unitlistst, unitlist_search> unitlist_search_hook;
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, feed, 100);
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, render, 100);
2012-10-21 02:32:01 -06:00
//
// END: Unit screen search
//
//
// TODO: Animals screen search
//
//
// END: Animals screen search
//
2012-10-21 02:32:01 -06:00
//
// START: Trade screen search
//
class trade_search_base : public search_parent<df::viewscreen_tradegoodsst, df::item*, char>
2012-10-21 02:32:01 -06:00
{
private:
virtual string get_element_description(df::item *element) const
{
return Items::getDescription(element, 0, true);
}
};
class trade_search_merc : public trade_search_base
{
public:
virtual void render() const
{
print_search_option(2, 26);
}
virtual void init(df::viewscreen_tradegoodsst *screen)
2012-10-20 19:18:29 -06:00
{
if (!valid)
{
viewscreen = screen;
search_parent::init(&screen->trader_cursor, &screen->trader_items, &screen->trader_selected, 'q');
track_secondary_values = true;
2012-10-20 19:18:29 -06:00
}
}
};
2012-10-21 02:32:01 -06:00
typedef search_hook<df::viewscreen_tradegoodsst, trade_search_merc, int> trade_search_merc_hook;
IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, render);
class trade_search_fort : public trade_search_base
{
public:
virtual void render() const
{
print_search_option(42, 26);
}
virtual void init(df::viewscreen_tradegoodsst *screen)
{
if (!valid)
{
viewscreen = screen;
search_parent::init(&screen->broker_cursor, &screen->broker_items, &screen->broker_selected, 'w');
track_secondary_values = true;
2012-10-21 02:32:01 -06:00
}
}
};
typedef search_hook<df::viewscreen_tradegoodsst, trade_search_fort, char> trade_search_fort_hook;
IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, render);
2012-10-20 19:18:29 -06:00
//
2012-10-21 02:32:01 -06:00
// END: Trade screen search
2012-10-20 19:18:29 -06:00
//
DFHACK_PLUGIN("search");
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{
2012-10-20 19:18:29 -06:00
if (!gps || !INTERPOSE_HOOK(unitlist_search_hook, feed).apply() || !INTERPOSE_HOOK(unitlist_search_hook, render).apply()
2012-10-21 02:32:01 -06:00
|| !INTERPOSE_HOOK(trade_search_merc_hook, feed).apply() || !INTERPOSE_HOOK(trade_search_merc_hook, render).apply()
|| !INTERPOSE_HOOK(trade_search_fort_hook, feed).apply() || !INTERPOSE_HOOK(trade_search_fort_hook, render).apply()
2012-10-20 19:18:29 -06:00
|| !INTERPOSE_HOOK(stocks_search_hook, feed).apply() || !INTERPOSE_HOOK(stocks_search_hook, render).apply())
out.printerr("Could not insert Search hooks!\n");
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
2012-10-20 19:18:29 -06:00
INTERPOSE_HOOK(unitlist_search_hook, feed).remove();
INTERPOSE_HOOK(unitlist_search_hook, render).remove();
2012-10-21 02:32:01 -06:00
INTERPOSE_HOOK(trade_search_merc_hook, feed).remove();
INTERPOSE_HOOK(trade_search_merc_hook, render).remove();
INTERPOSE_HOOK(trade_search_fort_hook, feed).remove();
INTERPOSE_HOOK(trade_search_fort_hook, render).remove();
2012-10-20 19:18:29 -06:00
INTERPOSE_HOOK(stocks_search_hook, feed).remove();
INTERPOSE_HOOK(stocks_search_hook, render).remove();
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event event )
{
2012-10-20 19:18:29 -06:00
unitlist_search_hook::module.reset_all();
2012-10-21 02:32:01 -06:00
trade_search_merc_hook::module.reset_all();
trade_search_fort_hook::module.reset_all();
2012-10-20 19:18:29 -06:00
stocks_search_hook::module.reset_all();
return CR_OK;
}