From abe027c940ec3efcea7955d45708d2c7e8916295 Mon Sep 17 00:00:00 2001 From: Anuradha Dissanayake Date: Fri, 30 Nov 2012 22:44:05 +1300 Subject: [PATCH 1/2] Copy changes from ag fork --- plugins/search.cpp | 154 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 120 insertions(+), 34 deletions(-) diff --git a/plugins/search.cpp b/plugins/search.cpp index fdc788955..a14397fba 100644 --- a/plugins/search.cpp +++ b/plugins/search.cpp @@ -10,6 +10,7 @@ #include "df/viewscreen_tradegoodsst.h" #include "df/viewscreen_unitlistst.h" #include "df/interface_key.h" +#include "df/interfacest.h" using std::set; using std::vector; @@ -19,6 +20,7 @@ using namespace DFHack; using namespace df::enums; using df::global::gps; +using df::global::gview; /* Search Plugin @@ -39,6 +41,14 @@ void OutputString(int8_t color, int &x, int y, const std::string &text) x += text.length(); } +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; +} + // // START: Base Search functionality // @@ -60,6 +70,15 @@ public: track_secondary_values = false; } + bool reset_on_change() + { + if (valid && is_live_screen(viewscreen)) + return false; + + reset_all(); + return true; + } + // A new keystroke is received in a searchable screen virtual bool process_input(set *input) { @@ -206,12 +225,12 @@ protected: bool list_has_been_sorted = (sort_list1->size() == reference_list.size() && *sort_list1 != reference_list); - for (int i = 0; i < saved_indexes.size(); i++) + for (size_t 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++) + for (size_t j = 0; j < sort_list1->size(); j++) { if ((*sort_list1)[j] == reference_list[i]) { @@ -245,6 +264,9 @@ protected: update_secondary_values(); *sort_list2 = saved_list2; } + + saved_list1.clear(); + saved_list2.clear(); } store_reference_values(); search_string = ""; @@ -278,7 +300,7 @@ protected: } string search_string_l = toLower(search_string); - for (int i = 0; i < saved_list1.size(); i++ ) + for (size_t i = 0; i < saved_list1.size(); i++ ) { T element = saved_list1[i]; string desc = toLower(get_element_description(element)); @@ -307,8 +329,9 @@ protected: // Display hotkey message void print_search_option(int x, int y = -1) const { + auto dim = Screen::getWindowSize(); if (y == -1) - y = gps->dimy - 2; + y = dim.y - 2; OutputString((entry_mode) ? 4 : 12, x, y, string(1, select_key)); OutputString((entry_mode) ? 10 : 15, x, y, ": Search"); @@ -346,7 +369,12 @@ struct search_hook : T DEFINE_VMETHOD_INTERPOSE(void, feed, (set *input)) { - module.init(this); + if (!module.init(this)) + { + INTERPOSE_NEXT(feed)(input); + return; + } + if (!module.process_input(input)) { INTERPOSE_NEXT(feed)(input); @@ -357,9 +385,10 @@ struct search_hook : T DEFINE_VMETHOD_INTERPOSE(void, render, ()) { - module.init(this); + bool ok = module.init(this); INTERPOSE_NEXT(render)(); - module.render(); + if (ok) + module.render(); } }; @@ -382,11 +411,12 @@ public: virtual void render() const { if (!viewscreen->in_group_mode) - print_search_option(1); + print_search_option(2); else { - int x = 1; - OutputString(15, x, gps->dimy - 2, "Tab to enable Search"); + auto dim = Screen::getWindowSize(); + int x = 2, y = dim.y - 2; + OutputString(15, x, y, "Tab to enable Search"); } } @@ -402,13 +432,18 @@ public: search_parent::do_post_update_check(); } - virtual void init(df::viewscreen_storesst *screen) + bool init(df::viewscreen_storesst *screen) { + if (screen != viewscreen && !reset_on_change()) + return false; + if (!valid) { viewscreen = screen; search_parent::init(&screen->item_cursor, &screen->items); } + + return true; } @@ -440,8 +475,8 @@ private: typedef search_hook stocks_search_hook; -IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, feed); -IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, render); +template<> IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, feed); +template<> IMPLEMENT_VMETHOD_INTERPOSE(stocks_search_hook, render); // // END: Stocks screen search @@ -461,28 +496,33 @@ public: print_search_option(28); } - virtual void init(df::viewscreen_unitlistst *screen) + bool init(df::viewscreen_unitlistst *screen) { + if (screen != viewscreen && !reset_on_change()) + return false; + if (!valid) { viewscreen = screen; search_parent::init(&screen->cursor_pos[viewscreen->page], &screen->units[viewscreen->page], &screen->jobs[viewscreen->page]); } + + return true; } private: 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); // Check animal type too + desc += ", " + Units::getProfessionName(element); // Check animal type too return desc; } virtual bool should_check_input(set *input) { - if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT) || input->count(interface_key::CUSTOM_L)) + if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT) || + (!is_entry_mode() && input->count(interface_key::UNITVIEW_PRF_PROF))) { if (!is_entry_mode()) { @@ -502,8 +542,8 @@ private: }; typedef search_hook unitlist_search_hook; -IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, feed, 100); -IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, render, 100); +template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, feed, 100); +template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(unitlist_search_hook, render, 100); // // END: Unit screen search @@ -529,6 +569,29 @@ private: { return Items::getDescription(element, 0, true); } + + virtual bool should_check_input(set *input) + { + if (is_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()) + input->clear(); + + // Trying to trade, reset search + clear_search(); + reset_all(); + + return false; + } + + return true; + } }; @@ -540,20 +603,25 @@ public: print_search_option(2, 26); } - virtual void init(df::viewscreen_tradegoodsst *screen) + bool init(df::viewscreen_tradegoodsst *screen) { + if (screen != viewscreen && !reset_on_change()) + return false; + if (!valid) { viewscreen = screen; search_parent::init(&screen->trader_cursor, &screen->trader_items, &screen->trader_selected, 'q'); track_secondary_values = true; } + + return true; } }; typedef search_hook trade_search_merc_hook; -IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, feed); -IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, render); +template<> IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, feed); +template<> IMPLEMENT_VMETHOD_INTERPOSE(trade_search_merc_hook, render); class trade_search_fort : public trade_search_base @@ -564,20 +632,25 @@ public: print_search_option(42, 26); } - virtual void init(df::viewscreen_tradegoodsst *screen) + bool init(df::viewscreen_tradegoodsst *screen) { + if (screen != viewscreen && !reset_on_change()) + return false; + if (!valid) { viewscreen = screen; search_parent::init(&screen->broker_cursor, &screen->broker_items, &screen->broker_selected, 'w'); track_secondary_values = true; } + + return true; } }; typedef search_hook trade_search_fort_hook; -IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, feed); -IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, render); +template<> IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, feed); +template<> IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, render); // // END: Trade screen search @@ -589,10 +662,15 @@ DFHACK_PLUGIN("search"); DFhackCExport command_result plugin_init ( color_ostream &out, vector &commands) { - if (!gps || !INTERPOSE_HOOK(unitlist_search_hook, feed).apply() || !INTERPOSE_HOOK(unitlist_search_hook, render).apply() - || !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() - || !INTERPOSE_HOOK(stocks_search_hook, feed).apply() || !INTERPOSE_HOOK(stocks_search_hook, render).apply()) + if (!gps || !gview || + !INTERPOSE_HOOK(unitlist_search_hook, feed).apply() || + !INTERPOSE_HOOK(unitlist_search_hook, render).apply() || + !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() || + !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; @@ -613,9 +691,17 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_onstatechange ( color_ostream &out, state_change_event event ) { - unitlist_search_hook::module.reset_all(); - trade_search_merc_hook::module.reset_all(); - trade_search_fort_hook::module.reset_all(); - stocks_search_hook::module.reset_all(); + switch (event) { + case SC_VIEWSCREEN_CHANGED: + unitlist_search_hook::module.reset_on_change(); + trade_search_merc_hook::module.reset_on_change(); + trade_search_fort_hook::module.reset_on_change(); + stocks_search_hook::module.reset_on_change(); + break; + + default: + break; + } + return CR_OK; -} \ No newline at end of file +} From 210c1650ecb19a9407779b37ff334f28dca859a7 Mon Sep 17 00:00:00 2001 From: Anuradha Dissanayake Date: Sat, 1 Dec 2012 01:01:04 +1300 Subject: [PATCH 2/2] Add stockpile screen searching capability --- plugins/search.cpp | 122 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 13 deletions(-) diff --git a/plugins/search.cpp b/plugins/search.cpp index a14397fba..cf1798dcf 100644 --- a/plugins/search.cpp +++ b/plugins/search.cpp @@ -7,10 +7,12 @@ //#include "df/viewscreen_petst.h" #include "df/viewscreen_storesst.h" +#include "df/viewscreen_layer_stockpilest.h" #include "df/viewscreen_tradegoodsst.h" #include "df/viewscreen_unitlistst.h" #include "df/interface_key.h" #include "df/interfacest.h" +#include "df/layer_object_listst.h" using std::set; using std::vector; @@ -79,6 +81,11 @@ public: return true; } + bool is_valid() + { + return valid; + } + // A new keystroke is received in a searchable screen virtual bool process_input(set *input) { @@ -164,9 +171,9 @@ protected: const S *viewscreen; vector saved_list1, reference_list; vector saved_list2; + vector *sort_list2; vector saved_indexes; - bool valid; bool redo_search; bool track_secondary_values; string search_string; @@ -240,12 +247,17 @@ protected: } } - saved_list2[saved_indexes[i]] = (*sort_list2)[adjusted_item_index]; + update_saved_secondary_list_item(saved_indexes[i], adjusted_item_index); } saved_indexes.clear(); } } + virtual void update_saved_secondary_list_item(size_t i, size_t j) + { + saved_list2[i] = (*sort_list2)[j]; + } + // Store a copy of filtered list, used later to work out if filtered list has been sorted after filtering void store_reference_values() { @@ -254,7 +266,7 @@ protected: } // Shortcut to clear the search immediately - void clear_search() + virtual void clear_search() { if (saved_list1.size() > 0) { @@ -273,7 +285,7 @@ protected: } // The actual sort - void do_search() + virtual void do_search() { if (search_string.length() == 0) { @@ -318,7 +330,8 @@ protected: store_reference_values(); //Keep a copy, in case user sorts new list - *cursor_pos = 0; + if (cursor_pos) + *cursor_pos = 0; } virtual bool should_check_input(set *input) @@ -346,10 +359,9 @@ protected: private: vector *sort_list1; - vector *sort_list2; int *cursor_pos; char select_key; - + bool valid; bool entry_mode; df::interface_key select_token; @@ -359,7 +371,8 @@ private: }; template search_parent *search_parent ::lock = NULL; -// Parent struct for the hooks +// Parent struct for the hooks, use optional param D to generate multiple classes with same T & V +// but different static modules template struct search_hook : T { @@ -437,7 +450,7 @@ public: if (screen != viewscreen && !reset_on_change()) return false; - if (!valid) + if (!is_valid()) { viewscreen = screen; search_parent::init(&screen->item_cursor, &screen->items); @@ -501,7 +514,7 @@ public: if (screen != viewscreen && !reset_on_change()) return false; - if (!valid) + if (!is_valid()) { viewscreen = screen; search_parent::init(&screen->cursor_pos[viewscreen->page], &screen->units[viewscreen->page], &screen->jobs[viewscreen->page]); @@ -608,7 +621,7 @@ public: if (screen != viewscreen && !reset_on_change()) return false; - if (!valid) + if (!is_valid()) { viewscreen = screen; search_parent::init(&screen->trader_cursor, &screen->trader_items, &screen->trader_selected, 'q'); @@ -637,7 +650,7 @@ public: if (screen != viewscreen && !reset_on_change()) return false; - if (!valid) + if (!is_valid()) { viewscreen = screen; search_parent::init(&screen->broker_cursor, &screen->broker_items, &screen->broker_selected, 'w'); @@ -657,6 +670,84 @@ template<> IMPLEMENT_VMETHOD_INTERPOSE(trade_search_fort_hook, render); // +// +// START: Stockpile screen search +// + +class stockpile_search : public search_parent +{ +public: + void update_saved_secondary_list_item(size_t i, size_t j) + { + *saved_list2[i] = *(*sort_list2)[j]; + } + + string get_element_description(string *element) const + { + return *element; + } + + void render() const + { + print_search_option(2, 25); + } + + static df::layer_object_listst *getLayerList(const df::viewscreen_layer *layer, int idx) + { + return virtual_cast(vector_get(layer->layer_objects,idx)); + } + + bool init(df::viewscreen_layer_stockpilest *screen) + { + if (screen != viewscreen && !reset_on_change()) + return false; + + auto list3 = getLayerList(screen, 2); + if (!list3->active) + { + if (is_valid()) + { + clear_search(); + reset_all(); + } + + return false; + } + + if (!is_valid()) + { + viewscreen = screen; + search_parent::init(&list3->cursor, &screen->item_names, &screen->item_status); + track_secondary_values = true; + } + + return true; + } + + void do_search() + { + search_parent::do_search(); + auto list3 = getLayerList(viewscreen, 2); + list3->num_entries = viewscreen->item_names.size(); + } + + void clear_search() + { + search_parent::clear_search(); + auto list3 = getLayerList(viewscreen, 2); + list3->num_entries = viewscreen->item_names.size(); + } +}; + +typedef search_hook stockpile_search_hook; +template<> IMPLEMENT_VMETHOD_INTERPOSE(stockpile_search_hook, feed); +template<> IMPLEMENT_VMETHOD_INTERPOSE(stockpile_search_hook, render); + +// +// END: Stockpile screen search +// + + DFHACK_PLUGIN("search"); @@ -670,7 +761,9 @@ DFhackCExport command_result plugin_init ( color_ostream &out, vector