瀏覽代碼

NKAI: visual logger

Andrii Danylchenko 1 年之前
父節點
當前提交
047e076d05

+ 12 - 0
client/ClientCommandManager.cpp

@@ -37,6 +37,7 @@
 #include "../lib/modding/ModUtility.h"
 #include "../lib/CHeroHandler.h"
 #include "../lib/VCMIDirs.h"
+#include "../lib/logging/VisualLogger.h"
 #include "CMT.h"
 
 #ifdef SCRIPTING_ENABLED
@@ -427,6 +428,14 @@ void ClientCommandManager::handleCrashCommand()
 	//disaster!
 }
 
+void ClientCommandManager::handleVsLog(std::istringstream & singleWordBuffer)
+{
+	std::string key;
+	singleWordBuffer >> key;
+
+	logVisual->setKey(key);
+}
+
 void ClientCommandManager::printCommandMessage(const std::string &commandMessage, ELogLevel::ELogLevel messageType)
 {
 	switch(messageType)
@@ -546,6 +555,9 @@ void ClientCommandManager::processCommand(const std::string & message, bool call
 	else if(commandName == "crash")
 		handleCrashCommand();
 
+	else if(commandName == "vslog")
+		handleVsLog(singleWordBuffer);
+
 	else
 	{
 		if (!commandName.empty() && !vstd::iswithin(commandName[0], 0, ' ')) // filter-out debugger/IDE noise

+ 3 - 0
client/ClientCommandManager.h

@@ -81,6 +81,9 @@ class ClientCommandManager //take mantis #2292 issue about account if thinking a
 	// Crashes the game forcing an exception
 	void handleCrashCommand();
 
+	// shows object graph
+	void handleVsLog(std::istringstream & singleWordBuffer);
+
 	// Prints in Chat the given message
 	void printCommandMessage(const std::string &commandMessage, ELogLevel::ELogLevel messageType = ELogLevel::NOT_SET);
 	void giveTurn(const PlayerColor &color);

+ 33 - 0
client/mapView/MapView.cpp

@@ -31,6 +31,7 @@
 
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/mapObjects/CGHeroInstance.h"
+#include "../../lib/logging/VisualLogger.h"
 
 BasicMapView::~BasicMapView() = default;
 
@@ -57,11 +58,43 @@ BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
 	pos.h = dimensions.y;
 }
 
+class VisualLoggerRenderer : public ILogVisualizer
+{
+private:
+	Canvas & target;
+	std::shared_ptr<MapViewModel> model;
+
+public:
+	VisualLoggerRenderer(Canvas & target, std::shared_ptr<MapViewModel> model) : target(target), model(model)
+	{
+	}
+
+	virtual void drawLine(int3 start, int3 end) override
+	{
+		auto level = model->getLevel();
+
+		if(start.z != level || end.z != level)
+			return;
+
+		auto pStart = model->getTargetTileArea(start).topLeft();
+		auto pEnd = model->getTargetTileArea(end).topLeft();
+		auto viewPort = target.getRenderArea();
+
+		if(viewPort.isInside(pStart) && viewPort.isInside(pEnd))
+		{
+			target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 255, 0));
+		}
+	}
+};
+
 void BasicMapView::render(Canvas & target, bool fullUpdate)
 {
 	Canvas targetClipped(target, pos);
 	tilesCache->update(controller->getContext());
 	tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
+
+	VisualLoggerRenderer r(target, model);
+	logVisual->visualize(r);
 }
 
 void BasicMapView::tick(uint32_t msPassed)

+ 2 - 0
lib/CMakeLists.txt

@@ -76,6 +76,7 @@ set(lib_SRCS
 
 	logging/CBasicLogConfigurator.cpp
 	logging/CLogger.cpp
+	logging/VisualLogger.cpp
 
 	mapObjectConstructors/AObjectTypeHandler.cpp
 	mapObjectConstructors/CBankInstanceConstructor.cpp
@@ -424,6 +425,7 @@ set(lib_HEADERS
 
 	logging/CBasicLogConfigurator.h
 	logging/CLogger.h
+	logging/VisualLogger.h
 
 	mapObjectConstructors/AObjectTypeHandler.h
 	mapObjectConstructors/CBankInstanceConstructor.h

+ 42 - 0
lib/logging/VisualLogger.cpp

@@ -0,0 +1,42 @@
+/*
+ * CLogger.cpp, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+#include "StdInc.h"
+#include "VisualLogger.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+DLL_LINKAGE VisualLogger * logVisual = new VisualLogger();
+
+
+void VisualLogger::updateWithLock(std::string channel, std::function<void(IVisualLogBuilder & logBuilder)> func)
+{
+	std::lock_guard<std::mutex> lock(mutex);
+
+	mapLines[channel].clear();
+
+	func(VisualLogBuilder(mapLines[channel]));
+}
+
+void VisualLogger::visualize(ILogVisualizer & visulizer)
+{
+	std::lock_guard<std::mutex> lock(mutex);
+
+	for(auto line : mapLines[keyToShow])
+	{
+		visulizer.drawLine(line.start, line.end);
+	}
+}
+
+void VisualLogger::setKey(std::string key)
+{
+	keyToShow = key;
+}
+
+VCMI_LIB_NAMESPACE_END

+ 75 - 0
lib/logging/VisualLogger.h

@@ -0,0 +1,75 @@
+/*
+ * VisualLogger.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+#pragma once
+
+#include "../int3.h"
+#include "../constants/EntityIdentifiers.h"
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+class ILogVisualizer
+{
+public:
+	virtual void drawLine(int3 start, int3 end) = 0;
+};
+
+class IVisualLogBuilder
+{
+public:
+	virtual void addLine(int3 start, int3 end) = 0;
+};
+
+/// The logger is used to show screen overlay
+class DLL_LINKAGE VisualLogger
+{
+private:
+	struct MapLine
+	{
+		int3 start;
+		int3 end;
+
+		MapLine(int3 start, int3 end)
+			:start(start), end(end)
+		{
+		}
+	};
+
+	class VisualLogBuilder : public IVisualLogBuilder
+	{
+	private:
+		std::vector<MapLine> & mapLines;
+
+	public:
+		VisualLogBuilder(std::vector<MapLine> & mapLines)
+			:mapLines(mapLines)
+		{
+		}
+
+		virtual void addLine(int3 start, int3 end) override
+		{
+			mapLines.push_back(MapLine(start, end));
+		}
+	};
+
+private:
+	std::map<std::string, std::vector<MapLine>> mapLines;
+	std::mutex mutex;
+	std::string keyToShow;
+
+public:
+
+	void updateWithLock(std::string channel, std::function<void(IVisualLogBuilder & logBuilder)> func);
+	void visualize(ILogVisualizer & visulizer);
+	void setKey(std::string key);
+};
+
+extern DLL_LINKAGE VisualLogger * logVisual;
+
+VCMI_LIB_NAMESPACE_END