浏览代码

search feature working

Laserlicht 1 年之前
父节点
当前提交
bd58caac13
共有 2 个文件被更改,包括 35 次插入16 次删除
  1. 30 16
      client/adventureMap/AdventureMapShortcuts.cpp
  2. 5 0
      client/adventureMap/AdventureMapShortcuts.h

+ 30 - 16
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<ObjectInstanceID> 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<std::string> texts;
 	for(auto & obj : mapObjCountList)
 		texts.push_back(VLC->objtypeh->getObjectName(obj.first, 0) + " (" + std::to_string(obj.second) + ")");
 
-	GH.windows().createAndPushWindow<CObjectListWindow>(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);
-										}
-											
-
-									},
-									0);
+	GH.windows().createAndPushWindow<CObjectListWindow>(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;
+
+			// filter for matching objects
+			std::vector<ObjectInstanceID> 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()

+ 5 - 0
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();