From bd58caac132fc3bda66e2555ab040d424bd6b548 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 1 Oct 2024 20:20:19 +0200 Subject: [PATCH] search feature working --- client/adventureMap/AdventureMapShortcuts.cpp | 44 ++++++++++++------- client/adventureMap/AdventureMapShortcuts.h | 5 +++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/client/adventureMap/AdventureMapShortcuts.cpp b/client/adventureMap/AdventureMapShortcuts.cpp index 0980e6634..e4b4b3284 100644 --- a/client/adventureMap/AdventureMapShortcuts.cpp +++ b/client/adventureMap/AdventureMapShortcuts.cpp @@ -43,6 +43,8 @@ AdventureMapShortcuts::AdventureMapShortcuts(AdventureMapInterface & owner) : owner(owner) , state(EAdventureState::NOT_INITIALIZED) , mapLevel(0) + , searchLast(MapObjectID::NO_OBJ) + , searchPos(0) {} void AdventureMapShortcuts::setState(EAdventureState newState) @@ -462,6 +464,7 @@ void AdventureMapShortcuts::zoom( int distance) void AdventureMapShortcuts::search() { + // get all relevant objects std::vector visitableObjInstances; int3 mapSizes = LOCPLINT->cb->getMapSize(); for(int x = 0; x < mapSizes.x; x++) @@ -486,26 +489,37 @@ void AdventureMapShortcuts::search() } ); + // get pos of last selection + int lastSel = 0; + for(int i = 0; i < mapObjCountList.size(); i++) + if(mapObjCountList[i].first == searchLast) + lastSel = i; + + // create texts std::vector texts; for(auto & obj : mapObjCountList) texts.push_back(VLC->objtypeh->getObjectName(obj.first, 0) + " (" + std::to_string(obj.second) + ")"); - GH.windows().createAndPushWindow(texts, nullptr, - CGI->generaltexth->translate("vcmi.adventureMap.search.hover"), - CGI->generaltexth->translate("vcmi.adventureMap.search.help"), - [this, mapObjCountList, visitableObjInstances](int index) - { - auto selObj = mapObjCountList[index].first; - for(auto & obj : visitableObjInstances) - { - auto objInst = LOCPLINT->cb->getObjInstance(obj); - if(selObj == objInst->getObjGroupIndex()) - owner.centerOnObject(objInst); - } - + GH.windows().createAndPushWindow(texts, nullptr, CGI->generaltexth->translate("vcmi.adventureMap.search.hover"), CGI->generaltexth->translate("vcmi.adventureMap.search.help"), + [this, mapObjCountList, visitableObjInstances](int index) + { + auto selObj = mapObjCountList[index].first; - }, - 0); + // filter for matching objects + std::vector selVisitableObjInstances; + for(auto & obj : visitableObjInstances) + if(selObj == LOCPLINT->cb->getObjInstance(obj)->getObjGroupIndex()) + selVisitableObjInstances.push_back(obj); + + if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj) + searchPos++; + else + searchPos = 0; + + auto objInst = LOCPLINT->cb->getObjInstance(selVisitableObjInstances[searchPos]); + owner.centerOnObject(objInst); + searchLast = objInst->getObjGroupIndex(); + }, lastSel); } void AdventureMapShortcuts::nextObject() diff --git a/client/adventureMap/AdventureMapShortcuts.h b/client/adventureMap/AdventureMapShortcuts.h index cfe07b781..e0c8146b1 100644 --- a/client/adventureMap/AdventureMapShortcuts.h +++ b/client/adventureMap/AdventureMapShortcuts.h @@ -10,6 +10,8 @@ #pragma once +#include "../../lib/constants/EntityIdentifiers.h" + VCMI_LIB_NAMESPACE_BEGIN class Point; class Rect; @@ -33,6 +35,9 @@ class AdventureMapShortcuts EAdventureState state; int mapLevel; + MapObjectID searchLast; + int searchPos; + void showOverview(); void worldViewBack(); void worldViewScale1x();