Skip to content

Commit

Permalink
Merge pull request #101 from OzoneH3/master
Browse files Browse the repository at this point in the history
List Items: Filter multiple items, seperate with ','
  • Loading branch information
TheDarklingWolf committed Feb 24, 2013
2 parents 60b16b5 + 0d024a5 commit 986ab20
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
36 changes: 32 additions & 4 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5356,6 +5356,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;
Expand Down Expand Up @@ -5399,7 +5417,6 @@ void game::list_items()
int iActiveX = 0;
int iActiveY = 0;
long ch = '.';
std::string sFilter = "";
int iFilter = 0;

do {
Expand All @@ -5412,7 +5429,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 = '.';

Expand Down Expand Up @@ -5482,8 +5510,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;
Expand Down
2 changes: 2 additions & 0 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<item> multidrop();
Expand Down
47 changes: 0 additions & 47 deletions output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,53 +420,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;
Expand Down

0 comments on commit 986ab20

Please sign in to comment.