From b983c7c587190feb2d0db6ea63393d39a06cea91 Mon Sep 17 00:00:00 2001 From: OzoneH3 Date: Sun, 24 Feb 2013 10:22:25 +0100 Subject: [PATCH] List Items: Filter multiple items, seperate with ',' --- game.cpp | 36 ++++++++++++++++++++++++++++++++---- game.h | 2 ++ output.cpp | 47 ----------------------------------------------- 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/game.cpp b/game.cpp index 8c6c782fb39ed..2cd57df1f1e10 100644 --- a/game.cpp +++ b/game.cpp @@ -5338,6 +5338,24 @@ point game::look_around() return point(-1, -1); } +bool game::list_items_match(std::string sText, std::string sPattern) +{ + unsigned long iPos; + + do { + iPos = sPattern.find(","); + + if (sText.find((iPos == std::string::npos) ? sPattern : sPattern.substr(0, iPos)) != std::string::npos) + return true; + + if (iPos != std::string::npos) + sPattern = sPattern.substr(iPos+1, sPattern.size()); + + } while(iPos != std::string::npos); + + return false; +} + void game::list_items() { int iInfoHeight = 10; @@ -5381,7 +5399,6 @@ void game::list_items() int iActiveX = 0; int iActiveY = 0; long ch = '.'; - std::string sFilter = ""; int iFilter = 0; do { @@ -5394,7 +5411,18 @@ void game::list_items() ch = '.'; } else if (ch == 'f' || ch == 'F') { - sFilter = string_input_popup("Filter:", 15, sFilter); + for (int i = 0; i < iInfoHeight-1; i++) + mvwprintz(w_item_info, i, 1, c_black, "%s", " "); + + mvwprintz(w_item_info, 0, 2, c_white, "%s", "How to use the filter:"); + mvwprintz(w_item_info, 1, 2, c_white, "%s", "Example: pi will match any itemname with pi in it."); + mvwprintz(w_item_info, 3, 2, c_white, "%s", "Seperate multiple items with ,"); + mvwprintz(w_item_info, 4, 2, c_white, "%s", "Example: back,flash,aid, ,band"); + mvwprintz(w_item_info, 6, 2, c_white, "%s", "To exclude certain items, place a - in front"); + mvwprintz(w_item_info, 7, 2, c_white, "%s", "Example: -pipe,chunk,steel"); + wrefresh(w_item_info); + + sFilter = string_input_popup("Filter:", 55, sFilter); iActive = 0; ch = '.'; @@ -5462,8 +5490,8 @@ void game::list_items() for (int iRow = (iSearchY * -1); iRow <= iSearchY; iRow++) { for (int iCol = (iSearchX * -1); iCol <= iSearchX; iCol++) { for (std::map< std::string, int>::iterator iter=grounditems[iCol][iRow].begin(); iter!=grounditems[iCol][iRow].end(); ++iter) { - if (sFilterTemp == "" || (sFilterTemp != "" && ((sFilterPre != "-" && iter->first.find(sFilterTemp) != std::string::npos) || - (sFilterPre == "-" && iter->first.find(sFilterTemp) == std::string::npos)))) { + if (sFilterTemp == "" || (sFilterTemp != "" && ((sFilterPre != "-" && list_items_match(iter->first, sFilterTemp)) || + (sFilterPre == "-" && !list_items_match(iter->first, sFilterTemp))))) { if (iNum >= iStartPos && iNum < iStartPos + ((iMaxRows > iItemNum) ? iItemNum : iMaxRows) ) { if (iNum == iActive) { iActiveX = iCol; diff --git a/game.h b/game.h index fbd94590f8c08..cf3f1cf312897 100644 --- a/game.h +++ b/game.h @@ -190,6 +190,8 @@ class game void peek(); point look_around();// Look at nearby terrain ';' void list_items(); //List all items around the player + bool list_items_match(std::string sText, std::string sPattern); + std::string sFilter; char inv(std::string title = "Inventory:"); char inv_type(std::string title = "Inventory:", int inv_item_type = 0); std::vector multidrop(); diff --git a/output.cpp b/output.cpp index 15765371f3dd7..f3ed42fe90ca8 100644 --- a/output.cpp +++ b/output.cpp @@ -428,53 +428,6 @@ std::string string_input_popup(std::string title, int max_length, std::string in } while (true); } -std::string string_input_popup(int max_length, const char *mes, ...) -{ - std::string ret; - va_list ap; - va_start(ap, mes); - char buff[1024]; - vsprintf(buff, mes, ap); - va_end(ap); - int startx = strlen(buff) + 2; - WINDOW* w = newwin(3, 80, 11, 0); - wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX, - LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX ); - mvwprintz(w, 1, 1, c_ltred, "%s", buff); - for (int i = startx + 1; i < 79; i++) - mvwputch(w, 1, i, c_ltgray, '_'); - int posx = startx; - mvwputch(w, 1, posx, h_ltgray, '_'); - do { - wrefresh(w); - long ch = getch(); - if (ch == 27) { // Escape - werase(w); - wrefresh(w); - delwin(w); - refresh(); - return ""; - } else if (ch == '\n') { - werase(w); - wrefresh(w); - delwin(w); - refresh(); - return ret; - } else if ((ch == KEY_BACKSPACE || ch == 127) && posx > startx) { -// Move the cursor back and re-draw it - ret = ret.substr(0, ret.size() - 1); - mvwputch(w, 1, posx, c_ltgray, '_'); - posx--; - mvwputch(w, 1, posx, h_ltgray, '_'); - } else if(ret.size() < max_length || max_length == 0) { - ret += ch; - mvwputch(w, 1, posx, c_magenta, ch); - posx++; - mvwputch(w, 1, posx, h_ltgray, '_'); - } - } while (true); -} - char popup_getkey(const char *mes, ...) { va_list ap;