1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00

add search box

This commit is contained in:
Laserlicht 2024-10-01 22:53:05 +02:00
parent e86b694b22
commit 8e079cf016
3 changed files with 51 additions and 18 deletions

View File

@ -526,7 +526,7 @@ void AdventureMapShortcuts::search(bool next)
if(next) if(next)
selectObjOnMap(lastSel); selectObjOnMap(lastSel);
else else
GH.windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, CGI->generaltexth->translate("vcmi.adventureMap.search.hover"), CGI->generaltexth->translate("vcmi.adventureMap.search.help"), [selectObjOnMap](int index){ selectObjOnMap(index); }, lastSel); GH.windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, CGI->generaltexth->translate("vcmi.adventureMap.search.hover"), CGI->generaltexth->translate("vcmi.adventureMap.search.help"), [selectObjOnMap](int index){ selectObjOnMap(index); }, lastSel, std::vector<std::shared_ptr<IImage>>(), true);
} }
void AdventureMapShortcuts::nextObject() void AdventureMapShortcuts::nextObject()

View File

@ -1479,7 +1479,7 @@ void CObjectListWindow::CItem::showPopupWindow(const Point & cursorPosition)
parent->onPopup(index); parent->onPopup(index);
} }
CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images) CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images, bool searchBoxEnabled)
: CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")), : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
onSelect(Callback), onSelect(Callback),
selected(initialSelection), selected(initialSelection),
@ -1489,15 +1489,14 @@ CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::share
items.reserve(_items.size()); items.reserve(_items.size());
for(int id : _items) for(int id : _items)
{
items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName())); items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName()));
} itemsVisible = items;
init(titleWidget_, _title, _descr); init(titleWidget_, _title, _descr, searchBoxEnabled);
list->scrollTo(initialSelection); list->scrollTo(initialSelection);
} }
CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images) CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection, std::vector<std::shared_ptr<IImage>> images, bool searchBoxEnabled)
: CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")), : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
onSelect(Callback), onSelect(Callback),
selected(initialSelection), selected(initialSelection),
@ -1508,12 +1507,13 @@ CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, st
for(size_t i=0; i<_items.size(); i++) for(size_t i=0; i<_items.size(); i++)
items.push_back(std::make_pair(int(i), _items[i])); items.push_back(std::make_pair(int(i), _items[i]));
itemsVisible = items;
init(titleWidget_, _title, _descr); init(titleWidget_, _title, _descr, searchBoxEnabled);
list->scrollTo(initialSelection); list->scrollTo(initialSelection);
} }
void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr) void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, bool searchBoxEnabled)
{ {
titleWidget = titleWidget_; titleWidget = titleWidget_;
@ -1528,24 +1528,51 @@ void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::stri
titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2; titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2;
} }
list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1), list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1),
Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) ); Point(14, 151), Point(0, 25), 9, itemsVisible.size(), 0, 1, Rect(262, -32, 256, 256) );
list->setRedrawParent(true); list->setRedrawParent(true);
ok = std::make_shared<CButton>(Point(15, 402), AnimationPath::builtin("IOKAY.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT); ok = std::make_shared<CButton>(Point(15, 402), AnimationPath::builtin("IOKAY.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT);
ok->block(!list->size()); ok->block(!list->size());
if(!searchBoxEnabled)
return;
Rect r(50, 90, pos.w - 100, 16);
const ColorRGBA rectangleColor = ColorRGBA(0, 0, 0, 75);
const ColorRGBA borderColor = ColorRGBA(128, 100, 75);
const ColorRGBA grayedColor = ColorRGBA(158, 130, 105);
searchBoxRectangle = std::make_shared<TransparentFilledRectangle>(r.resize(1), rectangleColor, borderColor);
searchBoxDescription = std::make_shared<CLabel>(r.center().x, r.center().y, FONT_SMALL, ETextAlignment::CENTER, grayedColor, CGI->generaltexth->translate("vcmi.spellBook.search"));
searchBox = std::make_shared<CTextInput>(r, FONT_SMALL, ETextAlignment::CENTER, true);
searchBox->setCallback([this](const std::string & text){
searchBoxDescription->setEnabled(text.empty());
itemsVisible.clear();
for(auto & item : items)
if(boost::algorithm::contains(boost::algorithm::to_lower_copy(item.second), boost::algorithm::to_lower_copy(text)))
itemsVisible.push_back(item);
selected = 0;
list->resize(itemsVisible.size());
list->scrollTo(0);
ok->block(!itemsVisible.size());
redraw();
});
} }
std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index) std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index)
{ {
if(index < items.size()) if(index < itemsVisible.size())
return std::make_shared<CItem>(this, index, items[index].second); return std::make_shared<CItem>(this, index, itemsVisible[index].second);
return std::shared_ptr<CIntObject>(); return std::shared_ptr<CIntObject>();
} }
void CObjectListWindow::elementSelected() void CObjectListWindow::elementSelected()
{ {
std::function<void(int)> toCall = onSelect;//save std::function<void(int)> toCall = onSelect;//save
int where = items[selected].first; //required variables int where = itemsVisible[selected].first; //required variables
close();//then destroy window close();//then destroy window
toCall(where);//and send selected object toCall(where);//and send selected object
} }
@ -1601,13 +1628,13 @@ void CObjectListWindow::keyPressed(EShortcut key)
sel = 0; sel = 0;
break; case EShortcut::MOVE_LAST: break; case EShortcut::MOVE_LAST:
sel = static_cast<int>(items.size()); sel = static_cast<int>(itemsVisible.size());
break; default: break; default:
return; return;
} }
vstd::abetween<int>(sel, 0, items.size()-1); vstd::abetween<int>(sel, 0, itemsVisible.size()-1);
list->scrollTo(sel); list->scrollTo(sel);
changeSelection(sel); changeSelection(sel);
} }

View File

@ -45,6 +45,7 @@ class IImage;
class VideoWidget; class VideoWidget;
class VideoWidgetOnce; class VideoWidgetOnce;
class GraphicalPrimitiveCanvas; class GraphicalPrimitiveCanvas;
class TransparentFilledRectangle;
enum class EUserEvent; enum class EUserEvent;
@ -186,9 +187,14 @@ class CObjectListWindow : public CWindowObject
std::shared_ptr<CButton> ok; std::shared_ptr<CButton> ok;
std::shared_ptr<CButton> exit; std::shared_ptr<CButton> exit;
std::vector< std::pair<int, std::string> > items;//all items present in list std::shared_ptr<CTextInput> searchBox;
std::shared_ptr<TransparentFilledRectangle> searchBoxRectangle;
std::shared_ptr<CLabel> searchBoxDescription;
void init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr); std::vector< std::pair<int, std::string> > items; //all items present in list
std::vector< std::pair<int, std::string> > itemsVisible; //visible items present in list
void init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, bool searchBoxEnabled);
void exitPressed(); void exitPressed();
public: public:
size_t selected;//index of currently selected item size_t selected;//index of currently selected item
@ -200,8 +206,8 @@ public:
/// Callback will be called when OK button is pressed, returns id of selected item. initState = initially selected item /// Callback will be called when OK button is pressed, returns id of selected item. initState = initially selected item
/// Image can be nullptr /// Image can be nullptr
///item names will be taken from map objects ///item names will be taken from map objects
CObjectListWindow(const std::vector<int> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {}); CObjectListWindow(const std::vector<int> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {}, bool searchBoxEnabled = false);
CObjectListWindow(const std::vector<std::string> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {}); CObjectListWindow(const std::vector<std::string> &_items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection = 0, std::vector<std::shared_ptr<IImage>> images = {}, bool searchBoxEnabled = false);
std::shared_ptr<CIntObject> genItem(size_t index); std::shared_ptr<CIntObject> genItem(size_t index);
void elementSelected();//call callback and close this window void elementSelected();//call callback and close this window