dfhack/plugins/search.cpp

1827 lines
43 KiB
C++

#include <modules/Screen.h>
#include <modules/Translation.h>
#include <modules/Units.h>
#include <MiscUtils.h>
#include <VTableInterpose.h>
#include "df/viewscreen_announcelistst.h"
#include "df/viewscreen_petst.h"
#include "df/viewscreen_storesst.h"
#include "df/viewscreen_layer_stockpilest.h"
#include "df/viewscreen_layer_militaryst.h"
#include "df/viewscreen_layer_noblelistst.h"
2014-05-04 04:04:03 -06:00
#include "df/viewscreen_layer_workshop_profilest.h"
2012-10-21 02:32:01 -06:00
#include "df/viewscreen_tradegoodsst.h"
#include "df/viewscreen_unitlistst.h"
#include "df/viewscreen_buildinglistst.h"
#include "df/viewscreen_joblistst.h"
#include "df/interface_key.h"
#include "df/interfacest.h"
#include "df/layer_object_listst.h"
#include "df/job.h"
#include "df/report.h"
#include "modules/Job.h"
#include "df/global_objects.h"
#include "df/viewscreen_dwarfmodest.h"
#include "modules/Gui.h"
#include "df/unit.h"
#include "df/misc_trait_type.h"
#include "df/unit_misc_trait.h"
using namespace std;
using std::set;
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("search");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(gps);
REQUIRE_GLOBAL(gview);
REQUIRE_GLOBAL(ui);
2015-01-05 17:36:06 -07:00
REQUIRE_GLOBAL(ui_building_assign_units);
REQUIRE_GLOBAL(ui_building_in_assign);
REQUIRE_GLOBAL(ui_building_item_cursor);
2012-10-23 02:20:51 -06:00
/*
Search Plugin
A plugin that adds a "Search" hotkey to some screens (Units, Trade and Stocks)
that allows filtering of the list items by a typed query.
Works by manipulating the vector(s) that the list based viewscreens use to store
their items. When a search is started the plugin saves the original vectors and
with each keystroke creates a new filtered vector off the saves for the screen
to use.
*/
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();
}
void make_text_dim(int x1, int x2, int y)
{
for (int x = x1; x <= x2; x++)
{
Screen::Pen pen = Screen::readTile(x,y);
if (pen.valid())
{
if (pen.fg != 0)
{
if (pen.fg == 7)
pen.adjust(0,true);
else
pen.bold = 0;
}
Screen::paintTile(pen,x,y);
}
}
}
static bool is_live_screen(const df::viewscreen *screen)
{
for (df::viewscreen *cur = &gview->view; cur; cur = cur->child)
if (cur == screen)
return true;
return false;
}
static string get_unit_description(df::unit *unit)
{
string desc;
auto name = Units::getVisibleName(unit);
if (name->has_name)
desc = Translation::TranslateName(name, false);
desc += ", " + Units::getProfessionName(unit); // Check animal type too
return desc;
}
2012-10-20 19:18:29 -06:00
//
// START: Generic Search functionality
2012-10-20 19:18:29 -06:00
//
2012-10-23 02:20:51 -06:00
template <class S, class T>
class search_generic
{
2012-10-21 02:32:01 -06:00
public:
bool init(S *screen)
{
if (screen != viewscreen && !reset_on_change())
return false;
if (!can_init(screen))
{
if (is_valid())
{
clear_search();
reset_all();
}
return false;
}
if (!is_valid())
{
this->viewscreen = screen;
this->cursor_pos = get_viewscreen_cursor();
this->primary_list = get_primary_list();
this->select_key = get_search_select_key();
select_token = Screen::charToKey(select_key);
shift_select_token = Screen::charToKey(select_key + 'A' - 'a');
valid = true;
do_post_init();
}
return true;
}
2012-10-23 02:20:51 -06:00
// Called each time you enter or leave a searchable screen. Resets everything.
virtual void reset_all()
2012-10-21 02:32:01 -06:00
{
reset_search();
valid = false;
primary_list = NULL;
2012-10-21 02:32:01 -06:00
viewscreen = NULL;
select_key = 's';
}
bool reset_on_change()
{
if (valid && is_live_screen(viewscreen))
return false;
reset_all();
return true;
}
bool is_valid()
{
return valid;
}
2012-10-23 02:20:51 -06:00
// A new keystroke is received in a searchable screen
2012-10-21 02:32:01 -06:00
virtual bool process_input(set<df::interface_key> *input)
{
2012-10-23 02:20:51 -06:00
// If the page has two search options (Trade screen), only allow one to operate
// at a time
2012-10-21 02:32:01 -06:00
if (lock != NULL && lock != this)
return false;
2012-10-23 02:20:51 -06:00
// Allows custom preprocessing for each screen
2012-10-21 02:32:01 -06:00
if (!should_check_input(input))
return false;
bool key_processed = true;
if (entry_mode)
{
2012-10-23 02:20:51 -06:00
// Query typing mode
if (input->empty())
{
return false;
}
2012-10-21 02:32:01 -06:00
df::interface_key last_token = *input->rbegin();
int charcode = Screen::keyToChar(last_token);
if (charcode >= 32 && charcode <= 126)
2012-10-21 02:32:01 -06:00
{
2012-10-23 02:20:51 -06:00
// Standard character
search_string += char(charcode);
2012-10-21 02:32:01 -06:00
do_search();
}
else if (last_token == interface_key::STRING_A000)
{
2012-10-23 02:20:51 -06:00
// Backspace
2012-10-21 02:32:01 -06:00
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))
{
2012-10-23 02:20:51 -06:00
// ENTER or ESC: leave typing mode
2012-10-21 02:32:01 -06:00
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))
{
2012-10-23 02:20:51 -06:00
// Arrow key pressed. Leave entry mode and allow screen to process key
2012-10-21 02:32:01 -06:00
end_entry_mode();
key_processed = false;
}
}
2012-10-23 02:20:51 -06:00
// Not in query typing mode
2012-10-21 02:32:01 -06:00
else if (input->count(select_token))
{
2012-10-23 02:20:51 -06:00
// Hotkey pressed, enter typing mode
2012-10-21 02:32:01 -06:00
start_entry_mode();
}
else if (input->count(shift_select_token))
2012-10-21 02:32:01 -06:00
{
2012-10-23 02:20:51 -06:00
// Shift + Hotkey pressed, clear query
2012-10-21 02:32:01 -06:00
clear_search();
}
else
{
2012-10-23 02:20:51 -06:00
// Not a key for us, pass it on to the screen
2012-10-21 02:32:01 -06:00
key_processed = false;
}
2012-10-23 02:20:51 -06:00
return key_processed || entry_mode; // Only pass unrecognized keys down if not in typing mode
2012-10-21 02:32:01 -06:00
}
// Called after a keystroke has been processed
virtual void do_post_input_feed()
2012-10-21 02:32:01 -06:00
{
}
static search_generic<S, T> *lock;
2012-10-21 02:32:01 -06:00
protected:
virtual string get_element_description(T element) const = 0;
virtual void render() const = 0;
virtual int32_t *get_viewscreen_cursor() = 0;
virtual vector<T> *get_primary_list() = 0;
2012-10-20 19:18:29 -06:00
search_generic()
2012-10-20 19:18:29 -06:00
{
reset_all();
}
virtual bool can_init(S *screen)
{
return true;
}
virtual void do_post_init()
2012-10-20 19:18:29 -06:00
{
2012-10-20 19:18:29 -06:00
}
2012-10-21 02:32:01 -06:00
bool in_entry_mode()
2012-10-21 02:32:01 -06:00
{
return entry_mode;
}
void start_entry_mode()
{
entry_mode = true;
lock = this;
}
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
}
virtual char get_search_select_key()
{
return 's';
}
virtual 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();
}
// Shortcut to clear the search immediately
virtual void clear_search()
{
if (saved_list1.size() > 0)
{
*primary_list = saved_list1;
saved_list1.clear();
}
search_string = "";
}
virtual void save_original_values()
{
saved_list1 = *primary_list;
}
virtual void do_pre_incremental_search()
{
}
virtual void clear_viewscreen_vectors()
{
primary_list->clear();
}
virtual void add_to_filtered_list(size_t i)
{
primary_list->push_back(saved_list1[i]);
2012-10-20 19:18:29 -06:00
}
virtual void do_post_search()
2012-10-20 19:18:29 -06:00
{
2012-10-20 19:18:29 -06:00
}
2013-01-05 22:14:25 -07:00
virtual bool is_valid_for_search(size_t index)
{
return true;
}
virtual bool force_in_search(size_t index)
{
return false;
}
2012-10-23 02:20:51 -06:00
// The actual sort
virtual void do_search()
2012-10-20 19:18:29 -06:00
{
if (search_string.length() == 0)
{
clear_search();
return;
}
if (saved_list1.size() == 0)
2012-10-23 02:20:51 -06:00
// On first run, save the original list
save_original_values();
else
do_pre_incremental_search();
clear_viewscreen_vectors();
2012-10-20 19:18:29 -06:00
string search_string_l = toLower(search_string);
for (size_t i = 0; i < saved_list1.size(); i++ )
2012-10-20 19:18:29 -06:00
{
if (force_in_search(i))
{
add_to_filtered_list(i);
continue;
}
2013-01-05 22:14:25 -07:00
if (!is_valid_for_search(i))
continue;
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)
{
add_to_filtered_list(i);
2012-10-20 19:18:29 -06:00
}
}
do_post_search();
if (cursor_pos)
*cursor_pos = 0;
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
{
2012-10-21 02:32:01 -06:00
return true;
2012-10-20 19:18:29 -06:00
}
2012-10-23 02:20:51 -06:00
// Display hotkey message
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-11-23 18:18:56 -07:00
auto dim = Screen::getWindowSize();
2012-10-21 02:32:01 -06:00
if (y == -1)
2012-11-23 18:18:56 -07:00
y = dim.y - 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
S *viewscreen;
vector <T> saved_list1, reference_list, *primary_list;
//bool redo_search;
string search_string;
2012-10-21 02:32:01 -06:00
protected:
2012-10-21 02:32:01 -06:00
int *cursor_pos;
char select_key;
bool valid;
2012-10-21 02:32:01 -06:00
bool entry_mode;
df::interface_key select_token;
df::interface_key shift_select_token;
};
template <class S, class T> search_generic<S, T> *search_generic<S, T> ::lock = NULL;
2012-10-20 19:18:29 -06:00
// Search class helper for layered screens
template <class S, class T, int LIST_ID>
class layered_search : public search_generic<S, T>
{
protected:
virtual bool can_init(S *screen)
2012-10-20 19:18:29 -06:00
{
auto list = getLayerList(screen);
2014-05-04 04:04:03 -06:00
if (!is_list_valid(screen) || !list || !list->active)
return false;
2012-10-20 19:18:29 -06:00
return true;
2012-10-20 19:18:29 -06:00
}
virtual bool is_list_valid(S*)
{
return true;
}
virtual void do_search()
2012-10-20 19:18:29 -06:00
{
search_generic<S,T>::do_search();
auto list = getLayerList(this->viewscreen);
list->num_entries = this->get_primary_list()->size();
}
int32_t *get_viewscreen_cursor()
{
auto list = getLayerList(this->viewscreen);
return &list->cursor;
2012-10-20 19:18:29 -06:00
}
virtual void clear_search()
{
search_generic<S,T>::clear_search();
if (is_list_valid(this->viewscreen))
{
auto list = getLayerList(this->viewscreen);
list->num_entries = this->get_primary_list()->size();
}
}
private:
static df::layer_object_listst *getLayerList(const df::viewscreen_layer *layer)
{
return virtual_cast<df::layer_object_listst>(vector_get(layer->layer_objects, LIST_ID));
}
};
// Parent class for screens that have more than one primary list to synchronise
template < class S, class T, class PARENT = search_generic<S,T> >
class search_multicolumn_modifiable_generic : public PARENT
{
protected:
vector <T> reference_list;
vector <int> saved_indexes;
bool read_only;
2012-10-21 02:32:01 -06:00
virtual void update_saved_secondary_list_item(size_t i, size_t j) = 0;
virtual void save_secondary_values() = 0;
virtual void clear_secondary_viewscreen_vectors() = 0;
virtual void add_to_filtered_secondary_lists(size_t i) = 0;
virtual void clear_secondary_saved_lists() = 0;
virtual void reset_secondary_viewscreen_vectors() = 0;
virtual void restore_secondary_values() = 0;
virtual void do_post_init()
2012-10-20 19:18:29 -06:00
{
// If true, secondary list isn't modifiable so don't bother synchronising values
read_only = false;
2012-10-20 19:18:29 -06:00
}
void reset_all()
2012-10-20 19:18:29 -06:00
{
PARENT::reset_all();
reference_list.clear();
saved_indexes.clear();
reset_secondary_viewscreen_vectors();
2012-10-20 19:18:29 -06:00
}
void reset_search()
2012-10-20 19:18:29 -06:00
{
PARENT::reset_search();
reference_list.clear();
saved_indexes.clear();
clear_secondary_saved_lists();
}
virtual void clear_search()
{
if (this->saved_list1.size() > 0)
2012-10-20 19:18:29 -06:00
{
do_pre_incremental_search();
restore_secondary_values();
2012-10-20 19:18:29 -06:00
}
clear_secondary_saved_lists();
PARENT::clear_search();
do_post_search();
2012-10-20 19:18:29 -06:00
}
virtual bool is_match(T &a, T &b) = 0;
2012-10-21 02:32:01 -06:00
virtual bool is_match(vector<T> &a, vector<T> &b) = 0;
void do_pre_incremental_search()
2012-10-21 02:32:01 -06:00
{
PARENT::do_pre_incremental_search();
if (read_only)
return;
2012-10-21 02:32:01 -06:00
bool list_has_been_sorted = (this->primary_list->size() == reference_list.size()
&& !is_match(*this->primary_list, reference_list));
2012-10-21 02:32:01 -06:00
for (size_t i = 0; i < saved_indexes.size(); i++)
2012-10-21 02:32:01 -06:00
{
int adjusted_item_index = i;
if (list_has_been_sorted)
{
for (size_t j = 0; j < this->primary_list->size(); j++)
{
if (is_match((*this->primary_list)[j], reference_list[i]))
{
adjusted_item_index = j;
break;
}
}
}
2012-10-21 02:32:01 -06:00
update_saved_secondary_list_item(saved_indexes[i], adjusted_item_index);
2012-10-21 02:32:01 -06:00
}
saved_indexes.clear();
}
2012-10-21 02:32:01 -06:00
void clear_viewscreen_vectors()
{
search_generic<S,T>::clear_viewscreen_vectors();
saved_indexes.clear();
clear_secondary_viewscreen_vectors();
2012-10-21 02:32:01 -06:00
}
void add_to_filtered_list(size_t i)
{
search_generic<S,T>::add_to_filtered_list(i);
add_to_filtered_secondary_lists(i);
if (!read_only)
saved_indexes.push_back(i); // Used to map filtered indexes back to original, if needed
}
virtual void do_post_search()
{
if (!read_only)
reference_list = *this->primary_list;
}
2012-10-20 19:18:29 -06:00
void save_original_values()
{
search_generic<S,T>::save_original_values();
save_secondary_values();
}
};
// This basic match function is separated out from the generic multi column class, because the
// pets screen, which uses a union in its primary list, will cause a compile failure is this
// match function exists in the generic class
template < class S, class T, class PARENT = search_generic<S,T> >
class search_multicolumn_modifiable : public search_multicolumn_modifiable_generic<S, T, PARENT>
{
bool is_match(T &a, T &b)
{
return a == b;
}
bool is_match(vector<T> &a, vector<T> &b)
{
return a == b;
}
};
// General class for screens that have only one secondary list to keep in sync
template < class S, class T, class V, class PARENT = search_generic<S,T> >
class search_twocolumn_modifiable : public search_multicolumn_modifiable<S, T, PARENT>
2012-10-20 19:18:29 -06:00
{
2012-10-21 02:32:01 -06:00
public:
protected:
virtual vector<V> * get_secondary_list() = 0;
2012-10-21 02:32:01 -06:00
virtual void do_post_init()
2012-10-20 19:18:29 -06:00
{
search_multicolumn_modifiable<S, T, PARENT>::do_post_init();
secondary_list = get_secondary_list();
2012-10-20 19:18:29 -06:00
}
void save_secondary_values()
2012-10-21 02:32:01 -06:00
{
saved_secondary_list = *secondary_list;
}
void reset_secondary_viewscreen_vectors()
{
secondary_list = NULL;
2012-10-21 02:32:01 -06:00
}
virtual void update_saved_secondary_list_item(size_t i, size_t j)
2012-10-20 19:18:29 -06:00
{
saved_secondary_list[i] = (*secondary_list)[j];
}
void clear_secondary_viewscreen_vectors()
{
secondary_list->clear();
2012-10-20 19:18:29 -06:00
}
void add_to_filtered_secondary_lists(size_t i)
2012-10-20 19:18:29 -06:00
{
secondary_list->push_back(saved_secondary_list[i]);
}
2012-10-21 02:32:01 -06:00
void clear_secondary_saved_lists()
{
saved_secondary_list.clear();
}
2012-10-20 19:18:29 -06:00
void restore_secondary_values()
{
*secondary_list = saved_secondary_list;
2012-10-20 19:18:29 -06:00
}
vector<V> *secondary_list, saved_secondary_list;
2012-10-21 02:32:01 -06:00
};
// Parent struct for the hooks, use optional param D to generate multiple search classes in the same
// viewscreen but different static modules
template <class T, class V, int D = 0>
struct generic_search_hook : T
2012-10-21 02:32:01 -06:00
{
typedef T interpose_base;
2012-10-21 02:32:01 -06:00
static V module;
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{
if (!module.init(this))
{
INTERPOSE_NEXT(feed)(input);
return;
}
if (!module.process_input(input))
{
INTERPOSE_NEXT(feed)(input);
module.do_post_input_feed();
}
}
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
bool ok = module.init(this);
INTERPOSE_NEXT(render)();
if (ok)
module.render();
}
};
template <class T, class V, int D> V generic_search_hook<T, V, D> ::module;
// Hook definition helpers
#define IMPLEMENT_HOOKS_WITH_ID(screen, module, id, prio) \
typedef generic_search_hook<screen, module, id> module##_hook; \
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(module##_hook, feed, prio); \
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(module##_hook, render, prio)
#define IMPLEMENT_HOOKS(screen, module) \
typedef generic_search_hook<screen, module> module##_hook; \
template<> IMPLEMENT_VMETHOD_INTERPOSE(module##_hook, feed); \
template<> IMPLEMENT_VMETHOD_INTERPOSE(module##_hook, render)
#define IMPLEMENT_HOOKS_PRIO(screen, module, prio) \
typedef generic_search_hook<screen, module> module##_hook; \
2014-05-04 04:04:03 -06:00
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(module##_hook, feed, prio); \
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(module##_hook, render, prio)
//
// END: Generic Search functionality
//
//
// START: Animal screen search
//
typedef df::viewscreen_petst::T_animal T_animal;
typedef df::viewscreen_petst::T_mode T_mode;
class pets_search : public search_multicolumn_modifiable_generic<df::viewscreen_petst, T_animal>
{
public:
void render() const
{
if (viewscreen->mode == T_mode::List)
print_search_option(25, 4);
}
private:
int32_t *get_viewscreen_cursor()
{
return &viewscreen->cursor;
}
vector<T_animal> *get_primary_list()
{
return &viewscreen->animal;
}
virtual void do_post_init()
{
is_vermin = &viewscreen->is_vermin;
is_tame = &viewscreen->is_tame;
is_adopting = &viewscreen->is_adopting;
}
string get_element_description(df::viewscreen_petst::T_animal element) const
{
return get_unit_description(element.unit);
}
bool should_check_input()
{
return viewscreen->mode == T_mode::List;
}
2013-01-05 22:14:25 -07:00
bool is_valid_for_search(size_t i)
{
return is_vermin_s[i] == 0;
}
void save_secondary_values()
{
is_vermin_s = *is_vermin;
is_tame_s = *is_tame;
is_adopting_s = *is_adopting;
}
void reset_secondary_viewscreen_vectors()
{
is_vermin = NULL;
is_tame = NULL;
is_adopting = NULL;
}
void update_saved_secondary_list_item(size_t i, size_t j)
{
is_vermin_s[i] = (*is_vermin)[j];
is_tame_s[i] = (*is_tame)[j];
is_adopting_s[i] = (*is_adopting)[j];
}
void clear_secondary_viewscreen_vectors()
{
is_vermin->clear();
is_tame->clear();
is_adopting->clear();
}
void add_to_filtered_secondary_lists(size_t i)
{
is_vermin->push_back(is_vermin_s[i]);
is_tame->push_back(is_tame_s[i]);
is_adopting->push_back(is_adopting_s[i]);
}
void clear_secondary_saved_lists()
{
is_vermin_s.clear();
is_tame_s.clear();
is_adopting_s.clear();
}
void restore_secondary_values()
{
*is_vermin = is_vermin_s;
*is_tame = is_tame_s;
*is_adopting = is_adopting_s;
}
bool is_match(T_animal &a, T_animal &b)
{
return a.unit == b.unit;
}
bool is_match(vector<T_animal> &a, vector<T_animal> &b)
{
for (size_t i = 0; i < a.size(); i++)
{
if (!is_match(a[i], b[i]))
return false;
}
return true;
}
std::vector<char > *is_vermin, is_vermin_s;
std::vector<char > *is_tame, is_tame_s;
std::vector<char > *is_adopting, is_adopting_s;
};
IMPLEMENT_HOOKS(df::viewscreen_petst, pets_search);
//
// END: Animal screen search
//
//
// START: Stocks screen search
//
typedef search_generic<df::viewscreen_storesst, df::item*> stocks_search_base;
class stocks_search : public stocks_search_base
{
public:
void render() const
{
if (!viewscreen->in_group_mode)
print_search_option(2);
else
{
auto dim = Screen::getWindowSize();
int x = 2, y = dim.y - 2;
OutputString(15, x, y, "Tab to enable Search");
}
}
bool process_input(set<df::interface_key> *input)
{
if (viewscreen->in_group_mode)
return false;
redo_search = false;
if ((input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)) && !viewscreen->in_right_list)
{
// Redo search if category changes
saved_list1.clear();
end_entry_mode();
if (search_string.length() > 0)
redo_search = true;
return false;
}
return stocks_search_base::process_input(input);
}
virtual void do_post_input_feed()
{
if (viewscreen->in_group_mode)
{
// Disable search if item lists are grouped
clear_search();
reset_search();
}
else if (redo_search)
{
do_search();
redo_search = false;
}
}
private:
int32_t *get_viewscreen_cursor()
{
return &viewscreen->item_cursor;
}
virtual vector<df::item*> *get_primary_list()
{
return &viewscreen->items;
}
private:
string get_element_description(df::item *element) const
{
return Items::getDescription(element, 0, true);
}
bool redo_search;
};
2014-05-04 04:04:03 -06:00
IMPLEMENT_HOOKS_PRIO(df::viewscreen_storesst, stocks_search, 100);
//
// END: Stocks screen search
//
//
// START: Unit screen search
//
typedef search_twocolumn_modifiable<df::viewscreen_unitlistst, df::unit*, df::job*> unitlist_search_base;
class unitlist_search : public unitlist_search_base
{
public:
void render() const
{
print_search_option(28);
}
private:
void do_post_init()
{
unitlist_search_base::do_post_init();
read_only = true;
}
static string get_non_work_description(df::unit *unit)
{
for (auto p = unit->status.misc_traits.begin(); p < unit->status.misc_traits.end(); p++)
{
if ((*p)->id == misc_trait_type::Migrant || (*p)->id == misc_trait_type::OnBreak)
{
int i = (*p)->value;
return ".on break";
}
}
if (unit->profession == profession::BABY ||
unit->profession == profession::CHILD ||
unit->profession == profession::DRUNK)
{
return "";
}
if (ENUM_ATTR(profession, military, unit->profession))
return ".military";
return ".idle.no job";
}
string get_element_description(df::unit *unit) const
{
string desc = get_unit_description(unit);
if (!unit->job.current_job)
{
desc += get_non_work_description(unit);
}
return desc;
}
2015-01-08 14:59:37 -07:00
bool should_check_input(set<df::interface_key> *input)
{
2015-01-08 14:59:37 -07:00
if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT) ||
(!in_entry_mode() && input->count(interface_key::UNITVIEW_PRF_PROF)))
{
if (!in_entry_mode())
{
// Changing screens, reset search
clear_search();
reset_all();
}
else
input->clear(); // Ignore cursor keys when typing
return false;
}
return true;
}
char get_search_select_key()
{
return 'q';
}
2015-01-08 14:59:37 -07:00
vector<df::job*> *get_secondary_list()
{
return &viewscreen->jobs[viewscreen->page];
}
2015-01-08 14:59:37 -07:00
int32_t *get_viewscreen_cursor()
{
return &viewscreen->cursor_pos[viewscreen->page];
}
2015-01-08 14:59:37 -07:00
vector<df::unit*> *get_primary_list()
{
return &viewscreen->units[viewscreen->page];
}
};
2013-01-16 07:54:35 -07:00
typedef generic_search_hook<df::viewscreen_unitlistst, unitlist_search> unitlist_search_hook;
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, feed, 100);
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, render, 100);
//
// END: Unit screen search
//
//
// START: Trade screen search
//
class trade_search_base : public search_twocolumn_modifiable<df::viewscreen_tradegoodsst, df::item*, char>
{
private:
string get_element_description(df::item *element) const
{
return Items::getDescription(element, 0, true);
}
bool should_check_input(set<df::interface_key> *input)
{
if (in_entry_mode())
return true;
if (input->count(interface_key::TRADE_TRADE) ||
input->count(interface_key::TRADE_OFFER) ||
input->count(interface_key::TRADE_SEIZE))
{
// Block the keys if were searching
if (!search_string.empty())
2014-05-04 04:04:03 -06:00
{
input->clear();
2014-05-04 04:04:03 -06:00
}
return false;
}
else if (input->count(interface_key::CUSTOM_ALT_C))
{
clear_search_for_trade();
return true;
}
return true;
}
void clear_search_for_trade()
{
// Trying to trade, reset search
clear_search();
reset_all();
}
};
class trade_search_merc : public trade_search_base
{
public:
virtual void render() const
{
if (viewscreen->counteroffer.size() > 0)
{
// The merchant is proposing a counteroffer.
// Not only is there nothing to search,
// but the native hotkeys are where we normally write.
return;
}
2015-01-08 14:59:37 -07:00
print_search_option(2, -1);
if (!search_string.empty())
{
2014-05-04 04:04:03 -06:00
int32_t x = 2;
int32_t y = gps->dimy - 3;
make_text_dim(2, gps->dimx-2, y);
OutputString(COLOR_LIGHTRED, x, y, string(1, select_key + 'A' - 'a'));
OutputString(COLOR_WHITE, x, y, ": Clear search to trade ");
}
}
private:
vector<char> *get_secondary_list()
{
return &viewscreen->trader_selected;
}
int32_t *get_viewscreen_cursor()
{
return &viewscreen->trader_cursor;
}
vector<df::item*> *get_primary_list()
{
return &viewscreen->trader_items;
}
2015-01-08 14:59:37 -07:00
char get_search_select_key()
{
return 'q';
}
};
IMPLEMENT_HOOKS_WITH_ID(df::viewscreen_tradegoodsst, trade_search_merc, 1, 100);
class trade_search_fort : public trade_search_base
{
public:
virtual void render() const
{
if (viewscreen->counteroffer.size() > 0)
{
// The merchant is proposing a counteroffer.
// Not only is there nothing to search,
// but the native hotkeys are where we normally write.
return;
}
2015-01-08 14:59:37 -07:00
int32_t x = gps->dimx / 2 + 2;
print_search_option(x, -1);
if (!search_string.empty())
{
2014-05-04 04:04:03 -06:00
int32_t y = gps->dimy - 3;
make_text_dim(2, gps->dimx-2, y);
OutputString(COLOR_LIGHTRED, x, y, string(1, select_key + 'A' - 'a'));
OutputString(COLOR_WHITE, x, y, ": Clear search to trade ");
}
}
private:
vector<char> *get_secondary_list()
{
return &viewscreen->broker_selected;
}
int32_t *get_viewscreen_cursor()
{
return &viewscreen->broker_cursor;
}
vector<df::item*> *get_primary_list()
{
return &viewscreen->broker_items;
}
2015-01-08 14:59:37 -07:00
char get_search_select_key()
{
return 'w';
}
};
2012-11-30 02:44:05 -07:00
IMPLEMENT_HOOKS_WITH_ID(df::viewscreen_tradegoodsst, trade_search_fort, 2, 100);
//
// END: Trade screen search
//
//
// START: Stockpile screen search
//
typedef layered_search<df::viewscreen_layer_stockpilest, string *, 2> stocks_layer;
class stockpile_search : public search_twocolumn_modifiable<df::viewscreen_layer_stockpilest, string *, bool *, stocks_layer>
{
public:
void update_saved_secondary_list_item(size_t i, size_t j)
{
*saved_secondary_list[i] = *(*secondary_list)[j];
}
string get_element_description(string *element) const
{
return *element;
}
void render() const
{
print_search_option(51, 23);
}
2015-01-08 14:59:37 -07:00
vector<string *> *get_primary_list()
{
return &viewscreen->item_names;
2012-11-30 02:44:05 -07:00
}
2015-01-08 14:59:37 -07:00
vector<bool *> *get_secondary_list()
{
return &viewscreen->item_status;
}
2012-10-21 02:32:01 -06:00
};
IMPLEMENT_HOOKS(df::viewscreen_layer_stockpilest, stockpile_search);
2012-10-21 02:32:01 -06:00
//
// END: Stockpile screen search
//
//
// START: Military screen search
//
typedef layered_search<df::viewscreen_layer_militaryst, df::unit *, 2> military_search_base;
class military_search : public military_search_base
2012-10-21 02:32:01 -06:00
{
public:
string get_element_description(df::unit *element) const
2012-10-21 02:32:01 -06:00
{
return get_unit_description(element);
2012-10-21 02:32:01 -06:00
}
void render() const
2012-10-20 19:18:29 -06:00
{
print_search_option(52, 22);
2012-10-21 02:32:01 -06:00
}
char get_search_select_key()
2012-10-20 19:18:29 -06:00
{
return 'q';
}
// When not on the positions page, this list is used for something
// else entirely, so screwing with it seriously breaks stuff.
bool is_list_valid(df::viewscreen_layer_militaryst *screen)
{
if (screen->page != df::viewscreen_layer_militaryst::Positions)
return false;
return true;
}
2015-01-08 14:59:37 -07:00
vector<df::unit *> *get_primary_list()
{
return &viewscreen->positions.candidates;
}
bool should_check_input(set<df::interface_key> *input)
{
if (input->count(interface_key::SELECT) && !in_entry_mode() && !search_string.empty())
2012-10-20 19:18:29 -06:00
{
// About to make an assignment, so restore original list (it will be changed by the game)
int32_t *cursor = get_viewscreen_cursor();
df::unit *selected_unit = get_primary_list()->at(*cursor);
clear_search();
2015-01-08 14:59:37 -07:00
for (*cursor = 0; *cursor < get_primary_list()->size(); (*cursor)++)
{
if (get_primary_list()->at(*cursor) == selected_unit)
break;
}
reset_all();
2012-10-20 19:18:29 -06:00
}
return true;
2012-10-20 19:18:29 -06:00
}
};
IMPLEMENT_HOOKS_PRIO(df::viewscreen_layer_militaryst, military_search, 100);
2012-10-21 02:32:01 -06:00
//
// END: Military screen search
//
2012-10-21 02:32:01 -06:00
//
// START: Room list search
//
static map< df::building_type, vector<string> > room_quality_names;
static int32_t room_value_bounds[] = {1, 100, 250, 500, 1000, 1500, 2500, 10000};
typedef search_twocolumn_modifiable<df::viewscreen_buildinglistst, df::building*, int32_t> roomlist_search_base;
class roomlist_search : public roomlist_search_base
2012-10-21 02:32:01 -06:00
{
public:
void render() const
2012-10-21 02:32:01 -06:00
{
print_search_option(2, 23);
2012-10-21 02:32:01 -06:00
}
private:
void do_post_init()
2012-10-21 02:32:01 -06:00
{
roomlist_search_base::do_post_init();
read_only = true;
}
string get_element_description(df::building *bld) const
{
bool is_ownable_room = (bld->is_room && room_quality_names.find(bld->getType()) != room_quality_names.end());
string desc;
desc.reserve(100);
if (bld->owner)
desc += get_unit_description(bld->owner);
else if (is_ownable_room)
desc += "no owner";
desc += ".";
if (is_ownable_room)
{
int32_t value = bld->getRoomValue(NULL);
vector<string> *names = &room_quality_names[bld->getType()];
string *room_name = &names->at(0);
for (int i = 1; i < 8; i++)
{
if (room_value_bounds[i] > value)
break;
room_name = &names->at(i);
}
desc += *room_name;
}
else
2012-10-21 02:32:01 -06:00
{
string name;
bld->getName(&name);
if (!name.empty())
{
desc += name;
}
2012-10-21 02:32:01 -06:00
}
2015-01-08 14:59:37 -07:00
return desc;
}
2015-01-08 14:59:37 -07:00
vector<int32_t> *get_secondary_list()
{
return &viewscreen->room_value;
}
2015-01-08 14:59:37 -07:00
int32_t *get_viewscreen_cursor()
{
return &viewscreen->cursor;
}
2015-01-08 14:59:37 -07:00
vector<df::building*> *get_primary_list()
{
return &viewscreen->buildings;
2012-10-21 02:32:01 -06:00
}
};
IMPLEMENT_HOOKS(df::viewscreen_buildinglistst, roomlist_search);
2012-10-20 19:18:29 -06:00
//
// END: Room list search
2012-10-20 19:18:29 -06:00
//
//
// START: Announcement list search
//
2015-01-08 14:59:25 -07:00
class announcement_search : public search_generic<df::viewscreen_announcelistst, df::report*>
{
public:
void render() const
{
print_search_option(2, gps->dimy - 3);
}
private:
int32_t *get_viewscreen_cursor()
{
return &viewscreen->sel_idx;
}
virtual vector<df::report *> *get_primary_list()
{
return &viewscreen->reports;
}
private:
string get_element_description(df::report *element) const
{
return element->text;
}
};
2015-01-08 14:59:25 -07:00
IMPLEMENT_HOOKS(df::viewscreen_announcelistst, announcement_search);
//
// END: Announcement list search
//
//
// START: Nobles search list
//
typedef df::viewscreen_layer_noblelistst::T_candidates T_candidates;
typedef layered_search<df::viewscreen_layer_noblelistst, T_candidates *, 1> nobles_search_base;
class nobles_search : public nobles_search_base
{
public:
string get_element_description(T_candidates *element) const
{
if (!element->unit)
return "";
return get_unit_description(element->unit);
}
void render() const
{
print_search_option(2, 23);
}
bool force_in_search(size_t index)
{
return index == 0; // Leave Vacant
}
bool can_init(df::viewscreen_layer_noblelistst *screen)
{
if (screen->mode != df::viewscreen_layer_noblelistst::Appoint)
return false;
return nobles_search_base::can_init(screen);
}
2015-01-08 14:59:37 -07:00
vector<T_candidates *> *get_primary_list()
{
return &viewscreen->candidates;
}
};
IMPLEMENT_HOOKS(df::viewscreen_layer_noblelistst, nobles_search);
//
// END: Nobles search list
//
2014-05-04 04:04:03 -06:00
//
// START: Workshop profiles search list
//
typedef layered_search<df::viewscreen_layer_workshop_profilest, df::unit*, 0> profiles_search_base;
class profiles_search : public profiles_search_base
{
public:
string get_element_description(df::unit *element) const
{
return get_unit_description(element);
}
void render() const
{
print_search_option(2, 23);
}
2015-01-08 14:59:37 -07:00
vector<df::unit *> *get_primary_list()
2014-05-04 04:04:03 -06:00
{
return &viewscreen->workers;
}
};
IMPLEMENT_HOOKS(df::viewscreen_layer_workshop_profilest, profiles_search);
//
// END: Workshop profiles search list
//
//
// START: Job list search
//
void get_job_details(string &desc, df::job *job)
{
string job_name = ENUM_KEY_STR(job_type,job->job_type);
for (size_t i = 0; i < job_name.length(); i++)
{
char c = job_name[i];
if (c >= 'A' && c <= 'Z')
desc += " ";
desc += c;
}
desc += ".";
2015-01-08 14:59:37 -07:00
df::item_type itype = ENUM_ATTR(job_type, item, job->job_type);
MaterialInfo mat(job);
if (itype == item_type::FOOD)
mat.decode(-1);
if (mat.isValid() || job->material_category.whole)
{
desc += mat.toString();
desc += ".";
if (job->material_category.whole)
{
desc += bitfield_to_string(job->material_category);
desc += ".";
}
}
if (!job->reaction_name.empty())
{
for (size_t i = 0; i < job->reaction_name.length(); i++)
{
if (job->reaction_name[i] == '_')
desc += " ";
else
desc += job->reaction_name[i];
}
desc += ".";
}
if (job->flags.bits.suspend)
desc += "suspended.";
}
typedef search_twocolumn_modifiable<df::viewscreen_joblistst, df::job*, df::unit*> joblist_search_base;
class joblist_search : public joblist_search_base
{
public:
void render() const
{
print_search_option(2);
}
private:
void do_post_init()
{
joblist_search_base::do_post_init();
read_only = true;
}
string get_element_description(df::job *element) const
{
if (!element)
return "no job.idle";
string desc;
desc.reserve(100);
get_job_details(desc, element);
df::unit *worker = DFHack::Job::getWorker(element);
if (worker)
desc += get_unit_description(worker);
else
desc += "Inactive";
return desc;
}
char get_search_select_key()
{
return 'q';
}
2015-01-08 14:59:37 -07:00
vector<df::unit*> *get_secondary_list()
{
return &viewscreen->units;
}
2015-01-08 14:59:37 -07:00
int32_t *get_viewscreen_cursor()
{
return &viewscreen->cursor_pos;
}
2015-01-08 14:59:37 -07:00
vector<df::job*> *get_primary_list()
{
return &viewscreen->jobs;
}
};
IMPLEMENT_HOOKS(df::viewscreen_joblistst, joblist_search);
//
// END: Job list search
//
//
// START: Burrow assignment search
//
typedef search_twocolumn_modifiable<df::viewscreen_dwarfmodest, df::unit*, bool> burrow_search_base;
class burrow_search : public burrow_search_base
{
public:
bool can_init(df::viewscreen_dwarfmodest *screen)
{
if (ui->main.mode == df::ui_sidebar_mode::Burrows && ui->burrows.in_add_units_mode)
{
return burrow_search_base::can_init(screen);
}
return false;
}
string get_element_description(df::unit *element) const
{
return get_unit_description(element);
}
void render() const
{
auto dims = Gui::getDwarfmodeViewDims();
int left_margin = dims.menu_x1 + 1;
int x = left_margin;
int y = 23;
print_search_option(x, y);
}
2015-01-08 14:59:37 -07:00
vector<df::unit *> *get_primary_list()
{
return &ui->burrows.list_units;
}
2015-01-08 14:59:37 -07:00
vector<bool> *get_secondary_list()
{
return &ui->burrows.sel_units;
}
2015-01-08 14:59:37 -07:00
virtual int32_t * get_viewscreen_cursor()
{
return &ui->burrows.unit_cursor_pos;
}
bool should_check_input(set<df::interface_key> *input)
{
if (input->count(interface_key::SECONDSCROLL_UP) || input->count(interface_key::SECONDSCROLL_DOWN)
|| input->count(interface_key::SECONDSCROLL_PAGEUP) || input->count(interface_key::SECONDSCROLL_PAGEDOWN))
{
end_entry_mode();
return false;
}
return true;
}
};
IMPLEMENT_HOOKS(df::viewscreen_dwarfmodest, burrow_search);
//
// END: Burrow assignment search
//
2015-01-05 17:36:06 -07:00
//
// START: Room assignment search
//
typedef search_generic<df::viewscreen_dwarfmodest, df::unit*> room_assign_search_base;
class room_assign_search : public room_assign_search_base
{
public:
bool can_init(df::viewscreen_dwarfmodest *screen)
{
if (ui->main.mode == df::ui_sidebar_mode::QueryBuilding && *ui_building_in_assign)
{
return room_assign_search_base::can_init(screen);
}
return false;
}
string get_element_description(df::unit *element) const
{
return element ? get_unit_description(element) : "Nobody";
}
void render() const
{
auto dims = Gui::getDwarfmodeViewDims();
int left_margin = dims.menu_x1 + 1;
int x = left_margin;
int y = 19;
print_search_option(x, y);
}
vector<df::unit *> *get_primary_list()
{
return ui_building_assign_units;
}
virtual int32_t * get_viewscreen_cursor()
{
return ui_building_item_cursor;
}
bool should_check_input(set<df::interface_key> *input)
{
if (input->count(interface_key::SECONDSCROLL_UP) || input->count(interface_key::SECONDSCROLL_DOWN)
|| input->count(interface_key::SECONDSCROLL_PAGEUP) || input->count(interface_key::SECONDSCROLL_PAGEDOWN))
{
end_entry_mode();
return false;
}
return true;
}
};
IMPLEMENT_HOOKS(df::viewscreen_dwarfmodest, room_assign_search);
//
// END: Room assignment search
//
#define SEARCH_HOOKS \
HOOK_ACTION(unitlist_search_hook) \
HOOK_ACTION(roomlist_search_hook) \
HOOK_ACTION(trade_search_merc_hook) \
HOOK_ACTION(trade_search_fort_hook) \
HOOK_ACTION(stocks_search_hook) \
HOOK_ACTION(pets_search_hook) \
HOOK_ACTION(military_search_hook) \
HOOK_ACTION(nobles_search_hook) \
2014-05-04 04:04:03 -06:00
HOOK_ACTION(profiles_search_hook) \
2015-01-08 14:59:25 -07:00
HOOK_ACTION(announcement_search_hook) \
HOOK_ACTION(joblist_search_hook) \
HOOK_ACTION(burrow_search_hook) \
2015-01-05 17:36:06 -07:00
HOOK_ACTION(stockpile_search_hook) \
HOOK_ACTION(room_assign_search_hook)
DFhackCExport command_result plugin_enable ( color_ostream &out, bool enable)
{
if (!gps || !gview)
return CR_FAILURE;
if (is_enabled != enable)
{
#define HOOK_ACTION(hook) \
!INTERPOSE_HOOK(hook, feed).apply(enable) || \
!INTERPOSE_HOOK(hook, render).apply(enable) ||
if (SEARCH_HOOKS 0)
return CR_FAILURE;
is_enabled = enable;
}
return CR_OK;
}
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{
#undef HOOK_ACTION
const string a[] = {"Meager Quarters", "Modest Quarters", "Quarters", "Decent Quarters", "Fine Quarters", "Great Bedroom", "Grand Bedroom", "Royal Bedroom"};
room_quality_names[df::building_type::Bed] = vector<string>(a, a + 8);
const string b[] = {"Meager Dining Room", "Modest Dining Room", "Dining Room", "Decent Dining Room", "Fine Dining Room", "Great Dining Room", "Grand Dining Room", "Royal Dining Room"};
room_quality_names[df::building_type::Table] = vector<string>(b, b + 8);
const string c[] = {"Meager Office", "Modest Office", "Office", "Decent Office", "Splendid Office", "Throne Room", "Opulent Throne Room", "Royal Throne Room"};
room_quality_names[df::building_type::Chair] = vector<string>(c, c + 8);
const string d[] = {"Grave", "Servants Burial Chamber", "Burial Chamber", "Tomb", "Fine Tomb", "Mausoleum", "Grand Mausoleum", "Royal Mausoleum"};
room_quality_names[df::building_type::Coffin] = vector<string>(d, d + 8);
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
#define HOOK_ACTION(hook) \
INTERPOSE_HOOK(hook, feed).remove(); \
INTERPOSE_HOOK(hook, render).remove();
SEARCH_HOOKS
#undef HOOK_ACTION
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event event )
{
#define HOOK_ACTION(hook) hook::module.reset_on_change();
switch (event) {
case SC_VIEWSCREEN_CHANGED:
SEARCH_HOOKS
break;
default:
break;
}
2012-10-20 19:18:29 -06:00
return CR_OK;
#undef HOOK_ACTION
}
#undef IMPLEMENT_HOOKS
#undef SEARCH_HOOKS