Refactoring to use templates

develop
Anuradha Dissanayake 2012-10-21 14:18:29 +13:00
parent fd60db44ab
commit c5b38a24eb
1 changed files with 316 additions and 260 deletions

@ -20,60 +20,144 @@ using namespace df::enums;
using df::global::gps; using df::global::gps;
void OutputString(int8_t color, int &x, int y, const std::string &text) void OutputString(int8_t color, int &x, int y, const std::string &text)
{ {
Screen::paintString(Screen::Pen(' ', color, 0), x, y, text); Screen::paintString(Screen::Pen(' ', color, 0), x, y, text);
x += text.length(); x += text.length();
} }
struct search_struct
//
// START: Base Search functionality
//
template <class S, class T, class V = void>
struct search_parent
{ {
static string search_string; vector <T*> *sort_list1;
static bool entry_mode; vector <V*> *sort_list2;
int *cursor_pos;
char select_key;
const S *viewscreen;
bool valid;
bool entry_mode;
bool redo_search;
string search_string;
vector <T*> saved_list1;
vector <V*> saved_list2;
void print_search_option(int &x) df::interface_key select_token;
const int ascii_to_enum_offset;
search_parent() : ascii_to_enum_offset(interface_key::STRING_A048 - '0')
{ {
OutputString(12, x, gps->dimy - 2, "s"); reset_all();
OutputString(15, x, gps->dimy - 2, ": Search"); }
if (search_string.length() > 0 || entry_mode)
OutputString(15, x, gps->dimy - 2, ": " + search_string); virtual void init(int *cursor_pos, vector <T*> *sort_list1, vector <V*> *sort_list2 = NULL, char select_key = 's')
if (entry_mode) {
OutputString(12, x, gps->dimy - 2, "_"); 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);
valid = true;
}
void reset_search()
{
entry_mode = false;
search_string = "";
saved_list1.clear();
saved_list2.clear();
}
void reset_all()
{
reset_search();
valid = false;
sort_list1 = NULL;
sort_list2 = NULL;
viewscreen = NULL;
select_key = 's';
}
void clear_search()
{
if (saved_list1.size() > 0)
{
*sort_list1 = saved_list1;
if (sort_list2 != NULL)
*sort_list2 = saved_list2;
}
}
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;
}
sort_list1->clear();
if (sort_list2 != NULL)
sort_list2->clear();
string search_string_l = toLower(search_string);
for (int i = 0; i < saved_list1.size(); i++ )
{
T *element = saved_list1[i];
string desc = toLower(get_element_description(element));
if (desc.find(search_string_l) != string::npos)
{
sort_list1->push_back(element);
if (sort_list2 != NULL)
sort_list2->push_back(saved_list2[i]);
}
}
*cursor_pos = 0;
} }
bool process_input(df::interface_key select_key, set<df::interface_key> *input, bool &string_changed) virtual bool process_input(const set<df::interface_key> *input)
{ {
bool key_processed = true; bool key_processed = true;
string_changed = false;
if (entry_mode) if (entry_mode)
{ {
df::interface_key last_token = *input->rbegin(); df::interface_key last_token = *input->rbegin();
if (last_token >= interface_key::STRING_A032 && last_token <= interface_key::STRING_A126) if (last_token >= interface_key::STRING_A032 && last_token <= interface_key::STRING_A126)
{ {
search_string += 32 + last_token - interface_key::STRING_A032; search_string += last_token - ascii_to_enum_offset;
string_changed = true; do_search();
} }
else if (last_token == interface_key::STRING_A000) else if (last_token == interface_key::STRING_A000)
{ {
if (search_string.length() > 0) if (search_string.length() > 0)
{ {
search_string.erase(search_string.length()-1); search_string.erase(search_string.length()-1);
string_changed = true; do_search();
} }
} }
else if (input->count(interface_key::SELECT) || input->count(interface_key::LEAVESCREEN)) else if (input->count(interface_key::SELECT) || input->count(interface_key::LEAVESCREEN))
{ {
entry_mode = false; entry_mode = false;
} }
else if (input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)) 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))
{ {
entry_mode = false; entry_mode = false;
key_processed = false; key_processed = false;
} }
} }
else if (input->count(select_key)) else if (input->count(select_token))
{ {
entry_mode = true; entry_mode = true;
} }
@ -84,219 +168,190 @@ struct search_struct
return key_processed; return key_processed;
} }
};
string search_struct::search_string = "";
bool search_struct::entry_mode = false;
struct stocks_search : df::viewscreen_storesst, search_struct
{
typedef df::viewscreen_storesst interpose_base;
static vector<df::item* > saved_items;
static void reset_search() virtual string get_element_description(T *element) const = 0;
{ virtual void render () const = 0;
entry_mode = false;
search_string = "";
saved_items.clear();
}
void clear_search() virtual void do_post_update_check()
{ {
if (saved_items.size() > 0) if (redo_search)
{ {
items = saved_items; do_search();
redo_search = false;
} }
} }
void print_search_option(int x) const
DEFINE_VMETHOD_INTERPOSE(void, render, ())
{ {
INTERPOSE_NEXT(render)(); OutputString((entry_mode) ? 4 : 12, x, gps->dimy - 2, string(1, select_key));
OutputString((entry_mode) ? 10 : 15, x, gps->dimy - 2, ": Search");
int x = 1; if (search_string.length() > 0 || entry_mode)
print_search_option(x); OutputString(15, x, gps->dimy - 2, ": " + search_string);
if (entry_mode)
OutputString(10, x, gps->dimy - 2, "_");
} }
};
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{
if (in_group_mode)
{
INTERPOSE_NEXT(feed)(input);
return;
}
template <class T, class V>
struct search_hook : T
{
typedef T interpose_base;
bool string_changed = false; static V module;
if ((input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)) && !in_right_list) DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{
saved_items.clear();
entry_mode = false;
if (search_string.length() > 0)
string_changed = true;
INTERPOSE_NEXT(feed)(input);
}
else
{ {
if (!process_input(interface_key::CUSTOM_S, input, string_changed) && !entry_mode) module.init(this);
if (!module.process_input(input))
{ {
INTERPOSE_NEXT(feed)(input); INTERPOSE_NEXT(feed)(input);
if (in_group_mode) module.do_post_update_check();
{
clear_search();
reset_search();
}
}
}
if (string_changed)
{
if (search_string.length() == 0)
{
clear_search();
return;
} }
if (saved_items.size() == 0 && items.size() > 0)
{
saved_items = items;
} }
items.clear();
for (int i = 0; i < saved_items.size(); i++ ) DEFINE_VMETHOD_INTERPOSE(void, render, ())
{
string search_string_l = toLower(search_string);
string desc = Items::getDescription(saved_items[i], 0, true);
if (desc.find(search_string_l) != string::npos)
{ {
items.push_back(saved_items[i]); module.init(this);
} INTERPOSE_NEXT(render)();
} module.render();
item_cursor = 0;
}
} }
}; };
vector<df::item* > stocks_search::saved_items; template <class T, class V> V search_hook<T, V> ::module;
IMPLEMENT_VMETHOD_INTERPOSE(stocks_search, feed); //
IMPLEMENT_VMETHOD_INTERPOSE(stocks_search, render); // END: Base Search functionality
//
//
struct unitlist_search : df::viewscreen_unitlistst, search_struct // START: Stocks screen search
//
struct stocks_search : search_parent<df::viewscreen_storesst, df::item>
{ {
typedef df::viewscreen_unitlistst interpose_base; virtual void render() const
{
static vector<df::unit*> saved_units; if (!viewscreen->in_group_mode)
static vector<df::job*> saved_jobs; print_search_option(1);
else
{
int x = 1;
OutputString(15, x, gps->dimy - 2, "Tab to enable Search");
}
}
static void reset_search() virtual string get_element_description(df::item *element) const
{ {
entry_mode = false; return Items::getDescription(element, 0, true);
search_string = "";
saved_units.clear();
saved_jobs.clear();
} }
void clear_search() virtual bool process_input(const set<df::interface_key> *input)
{ {
if (saved_units.size() > 0) if (viewscreen->in_group_mode)
return false;
if ((input->count(interface_key::CURSOR_UP) || input->count(interface_key::CURSOR_DOWN)) && !viewscreen->in_right_list)
{ {
units[page] = saved_units; saved_list1.clear();
jobs[page] = saved_jobs; entry_mode = false;
if (search_string.length() > 0)
redo_search = true;
return false;
} }
else
return search_parent::process_input(input) || entry_mode;
return true;
} }
void do_search() virtual void do_post_update_check()
{ {
if (search_string.length() == 0) if (viewscreen->in_group_mode)
{ {
clear_search(); clear_search();
return; reset_search();
} }
else
while(1) search_parent::do_post_update_check();
{
if (saved_units.size() == 0)
{
saved_units = units[page];
saved_jobs = jobs[page];
} }
units[page].clear();
jobs[page].clear();
for (int i = 0; i < saved_units.size(); i++ ) virtual void init(df::viewscreen_storesst *screen)
{ {
df::unit *unit = saved_units[i]; if (!valid)
string search_string_l = toLower(search_string);
string name = toLower(Translation::TranslateName(Units::getVisibleName(unit), false));
if (name.find(search_string_l) != string::npos)
{ {
units[page].push_back(unit); viewscreen = screen;
jobs[page].push_back(saved_jobs[i]); search_parent::init(&screen->item_cursor, &screen->items);
} }
} }
if (units[page].size() > 0) };
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
//
//
// START: Unit screen search
//
struct unitlist_search : search_parent<df::viewscreen_unitlistst, df::unit, df::job>
{
virtual void render() const
{ {
cursor_pos[page] = 0; print_search_option(28);
break;
} }
search_string.erase(search_string.length()-1); virtual string get_element_description(df::unit *element) const
} {
return Translation::TranslateName(Units::getVisibleName(element), false);
} }
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input)) virtual bool process_input(const set<df::interface_key> *input)
{ {
bool string_changed = false; if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT))
if (!process_input(interface_key::CUSTOM_S, input, string_changed))
{ {
if (!entry_mode) if (!entry_mode)
{
if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT))
{ {
clear_search(); clear_search();
reset_search(); reset_search();
return false;
} }
INTERPOSE_NEXT(feed)(input);
} }
} else
else if (string_changed) return search_parent::process_input(input) || entry_mode;
do_search();
return true;
} }
DEFINE_VMETHOD_INTERPOSE(void, render, ()) virtual void init(df::viewscreen_unitlistst *screen)
{ {
INTERPOSE_NEXT(render)(); if (!valid)
if (units[page].size())
{ {
int x = 28; viewscreen = screen;
print_search_option(x); search_parent::init(&screen->cursor_pos[viewscreen->page], &screen->units[viewscreen->page], &screen->jobs[viewscreen->page]);
} }
} }
}; };
vector<df::unit*> unitlist_search::saved_units; typedef search_hook<df::viewscreen_unitlistst, unitlist_search> unitlist_search_hook;
vector<df::job*> unitlist_search::saved_jobs; IMPLEMENT_VMETHOD_INTERPOSE(unitlist_search_hook, feed);
IMPLEMENT_VMETHOD_INTERPOSE(unitlist_search_hook, render);
IMPLEMENT_VMETHOD_INTERPOSE(unitlist_search, feed);
IMPLEMENT_VMETHOD_INTERPOSE(unitlist_search, render);
//
// END: Unit screen search
//
DFHACK_PLUGIN("search"); DFHACK_PLUGIN("search");
@ -304,8 +359,8 @@ DFHACK_PLUGIN("search");
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands) DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{ {
if (!gps || !INTERPOSE_HOOK(unitlist_search, feed).apply() || !INTERPOSE_HOOK(unitlist_search, render).apply() if (!gps || !INTERPOSE_HOOK(unitlist_search_hook, feed).apply() || !INTERPOSE_HOOK(unitlist_search_hook, render).apply()
|| !INTERPOSE_HOOK(stocks_search, feed).apply() || !INTERPOSE_HOOK(stocks_search, render).apply()) || !INTERPOSE_HOOK(stocks_search_hook, feed).apply() || !INTERPOSE_HOOK(stocks_search_hook, render).apply())
out.printerr("Could not insert Search hooks!\n"); out.printerr("Could not insert Search hooks!\n");
return CR_OK; return CR_OK;
@ -313,15 +368,16 @@ DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCom
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{ {
INTERPOSE_HOOK(unitlist_search, feed).remove(); INTERPOSE_HOOK(unitlist_search_hook, feed).remove();
INTERPOSE_HOOK(unitlist_search, render).remove(); INTERPOSE_HOOK(unitlist_search_hook, render).remove();
INTERPOSE_HOOK(stocks_search, feed).remove(); INTERPOSE_HOOK(stocks_search_hook, feed).remove();
INTERPOSE_HOOK(stocks_search, render).remove(); INTERPOSE_HOOK(stocks_search_hook, render).remove();
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event event ) DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event event )
{ {
unitlist_search::reset_search(); unitlist_search_hook::module.reset_all();
stocks_search_hook::module.reset_all();
return CR_OK; return CR_OK;
} }