<commit_before>#pragma once

#include "class.IGameState.hpp"
#include "../class.Scene.hpp"
#include "..\..\..\..\com.carlos.architecture\db\class.DBService.hpp"

using namespace DB;

class TouristInfoState : public IGameState {
private:

	class ObjectPostion {
	public:
		uint id;
		Point2f position;

		ObjectPostion(uint id, Point2f position) {
			this->id = id;
			this->position = position;
		}
	};

	vector<ObjectPostion> positions;

	bool isTouristInfoCommand(ControllerCommands command) {
		return (
			command == ControllerCommands::MORE_ABOUT_OBJECT ||
			command == ControllerCommands::WHAT_IS_OBJECT
		);
	}

	ControllerCommands lastCommand;

	void showTouristInfo(ControllerCommands command, const DB::Object *object, Point2f &pos) {
		const char *str;

		if (command == ControllerCommands::WHAT_IS_OBJECT) {
			str = object->name.c_str();
		} else if (command == ControllerCommands::MORE_ABOUT_OBJECT) {
			str = object->short_description.c_str();
		} else {
			mScene->mVisualController->renderTexture(mScene->mResManager->infoImage, pos.x,
				pos.y, mScene->getWindowWidth(), mScene->getWindowHeight());
			return;
		}

		vector<string> lines;

		mScene->mFormatter.formatTextToLines(str, lines);

		unsigned int maxCharsOnLine = mScene->mFormatter.countMaxCharsOnLine(lines);

		for (int i=0; i<lines.size(); i++) {
			Point2f newPos;
			mScene->mFormatter.formatPosition(pos, newPos, i, lines.size(), 
				maxCharsOnLine, mScene->getWindowWidth(), mScene->getWindowHeight());
			printLineOfText(lines[i].c_str(), newPos.x, newPos.y);
		}
	}

public:
	TouristInfoState() : IGameState(GameStates::TOURIST_INFO) {
		lastCommand = ControllerCommands::NO_ACTION;
	}

	virtual void switchOn(IGameState* predchodca) {
		mScene->setBackgroud(mScene->mResManager->bgTouristInfoIntro);
		mScene->zasobnikVstupov.clear();
	}

	virtual void frame(FrameData* frame) {
		// Prepneme sa do hry z turistickeho infa
		if (frame->getCommand() == ControllerCommands::GAME) {
			mScene->mStates->switchTo(GameStates::UVODNA_OBRAZOVKA);
			return;
		}

		// Pockame 5 sekund do zacatia ukazovania turistickeho infa
		if (this->getCasBehu() < 5.0) {
			return;
		}

		mScene->mVisualController->renderTexture(
			mScene->mResManager->touristInfoLogo, 0, 0, mScene->getWindowWidth(), mScene->getWindowHeight()
		);

		ModulVykreslovania::In *in = frame->getVstup();

		if (frame->hasVstup()) {
			positions.clear();

			vector<ModulVypocitaniaPolohy::Out> najdeneObjekty = in->najdeneObjekty;

			if (!najdeneObjekty.empty()) {
				if (isTouristInfoCommand(frame->getCommand()) ||
					isTouristInfoCommand(lastCommand)) {

					for (int i = 0; i < najdeneObjekty.size(); i++) {
						if (najdeneObjekty.at(i).najdeny) {
							uint id = najdeneObjekty.at(i).id; 
							Point2f position = najdeneObjekty.at(i).polohaTextu;

							// Prepocitame poziciu textu na obrazovke do pixelov
							position.x = position.x * mScene->getWindowWidth();
							position.y = position.y * mScene->getWindowHeight();

							positions.push_back(ObjectPostion(id, position));
						}
					}

					lastCommand = frame->getCommand();
				}
			} else {
				lastCommand = ControllerCommands::NO_ACTION;
			}
		}

		mScene->setBlackBackground();

		for (int i = 0; i < positions.size(); i++) {
			uint id = positions[i].id;
			const DB::Object *object = DB::DBService::getInstance().getObjectById(id);

			if (object != NULL) {
				showTouristInfo(lastCommand, object, positions[i].position);
			}
		}

		/*plain->setLastCommand(ControllerCommands::WHAT_IS_OBJECT);
		//Object *object = DB::DBService::getInstance().getObjectById(4);
		Object *object = new Object();
		object->name = "Testovacie menoTestovacie meno";
		showTouristInfo(object, Point2f(599, 449));
		visualController->renderTexture(resManager->infoImage, 0, 0, getWindowWidth(), getWindowHeight());*/
	}
};<commit_msg>Upravenie, aby sa turisticke info o objekte zorazovalo aj ked objekt nie je detegovany v dalsom frame.<commit_after>#pragma once

#include "class.IGameState.hpp"
#include "../class.Scene.hpp"
#include "..\..\..\..\com.carlos.architecture\db\class.DBService.hpp"

using namespace DB;

class TouristInfoState : public IGameState {
private:

	class RenderedObject {
	public:
		uint id;

		/* Pozicia objektu na obrazovke v pixeloch */
		Point2f position;	

		/* Cas, kedy bol objekt zdetegovany na obrazovke od zaciatku behu modulu */
		double detectionTime;

		/* Cas od zaciatku behu mobulu, kedy sa objekt zacal renderovat inak, ako 
		 * len informacnou ikonkou (pouzivatel stlacil prikaz) */
		double renderingStartTime;

		/* Prikaz, ktory pouzivatel stlacil, respektive nestlacil a povie nam 
		 * ako mame objekt renderovat na obrazovke */
		ControllerCommands command;

		RenderedObject(uint id, Point2f position, double detectionTime) {
			this->id = id;
			this->position = position;
			this->detectionTime = detectionTime;
			this->renderingStartTime = -1.0;
			this->command = ControllerCommands::NO_ACTION;
		}

		// Ak bol objekt renderevonay inak ako len informacnou ikonkou (pouzivatel stlacil prikaz)
		bool isRendered() {
			return this->renderingStartTime >= 0.0;
		}
	};

	vector<RenderedObject> renderedObjects;

	bool isTouristInfoCommand(ControllerCommands command) {
		return (
			command == ControllerCommands::MORE_ABOUT_OBJECT ||
			command == ControllerCommands::WHAT_IS_OBJECT
		);
	}

	void showTouristInfo(ControllerCommands command, const DB::Object *object, Point2f &pos) {
		const char *str;

		if (command == ControllerCommands::WHAT_IS_OBJECT) {
			str = object->name.c_str();
		} else if (command == ControllerCommands::MORE_ABOUT_OBJECT) {
			str = object->short_description.c_str();
		} else {
			mScene->mVisualController->renderTexture(mScene->mResManager->infoImage, pos.x,
				pos.y, mScene->getWindowWidth(), mScene->getWindowHeight());
			return;
		}

		vector<string> lines;

		mScene->mFormatter.formatTextToLines(str, lines);

		unsigned int maxCharsOnLine = mScene->mFormatter.countMaxCharsOnLine(lines);

		for (int i=0; i<lines.size(); i++) {
			Point2f newPos;
			mScene->mFormatter.formatPosition(pos, newPos, i, lines.size(), 
				maxCharsOnLine, mScene->getWindowWidth(), mScene->getWindowHeight());
			printLineOfText(lines[i].c_str(), newPos.x, newPos.y);
		}
	}

	void eraseRenderedObjectsOutOfTime() {
		for (int i=0; i<renderedObjects.size(); i++) {
			RenderedObject &renderedObject = renderedObjects[i];

			if (renderedObject.isRendered()) {
				if (this->getCasBehu()-renderedObject.renderingStartTime > 5.0) {
					renderedObjects.erase(renderedObjects.begin() + i);
					i--;
				}
			} else {
				if (this->getCasBehu()-renderedObject.detectionTime > 5.0) {
					renderedObjects.erase(renderedObjects.begin() + i);
					i--;
				}
			}
		}
	}

	void renderObjects() {
		for (int i = 0; i < renderedObjects.size(); i++) {
			uint id = renderedObjects[i].id;
			const DB::Object *object = DB::DBService::getInstance().getObjectById(id);

			if (object != NULL) {
				showTouristInfo(renderedObjects[i].command, object, renderedObjects[i].position);
			}
		}
	}

public:
	TouristInfoState() : IGameState(GameStates::TOURIST_INFO) {}

	virtual void switchOn(IGameState* predchodca) {
		mScene->setBackgroud(mScene->mResManager->bgTouristInfoIntro);
		mScene->zasobnikVstupov.clear();
	}

	virtual void frame(FrameData* frame) {
		// Prepneme sa do hry z turistickeho infa
		if (frame->getCommand() == ControllerCommands::GAME) {
			mScene->mStates->switchTo(GameStates::UVODNA_OBRAZOVKA);
			return;
		}

		// Pockame 5 sekund do zacatia ukazovania turistickeho infa
		if (this->getCasBehu() < 5.0) {
			return;
		}

		// Vyrenderujeme obrazok loga turistickeho infa
		mScene->mVisualController->renderTexture(
			mScene->mResManager->touristInfoLogo, 0, 0, mScene->getWindowWidth(), mScene->getWindowHeight()
		);

		if (frame->hasVstup()) {
			ModulVykreslovania::In *in = frame->getVstup();
			vector<ModulVypocitaniaPolohy::Out> najdeneObjekty = in->najdeneObjekty;

			for (int i = 0; i < najdeneObjekty.size(); i++) {
				if (najdeneObjekty[i].najdeny) {
					uint id = najdeneObjekty[i].id;
					int j = 0;

					for (; j<renderedObjects.size() && id!=renderedObjects[j].id; j++);

					Point2f position = najdeneObjekty[i].polohaTextu;

					// Prepocitame poziciu textu na obrazovke do pixelov
					position.x = position.x * mScene->getWindowWidth();
					position.y = position.y * mScene->getWindowHeight();

					if (j == renderedObjects.size()) { // Objekt je zdetegovany
						renderedObjects.push_back(RenderedObject(id, position, this->getCasBehu()));
					} else { // Objekt uz bol zdetegovany
						RenderedObject &renderedObject = renderedObjects[j];

						// Aktualizujeme poziciu
						renderedObject.position = position;

						// Aktualizujeme bud cas detekcie alebo cas renderovania, aby sa objekt neprestaval ukazovat na obrazovke
						if (renderedObject.isRendered()) {
							renderedObject.renderingStartTime = this->getCasBehu();
						} else {
							renderedObject.detectionTime = this->getCasBehu();
						}

						// Ak sme dostali prikaz z mobilu
						if (renderedObject.command == ControllerCommands::NO_ACTION &&
								isTouristInfoCommand(frame->getCommand())) {

							renderedObject.command = frame->getCommand();
							renderedObject.renderingStartTime = this->getCasBehu();
						}
					}
				}
			}
		} 

		mScene->setBlackBackground();

		eraseRenderedObjectsOutOfTime();

		renderObjects();

		/*plain->setLastCommand(ControllerCommands::WHAT_IS_OBJECT);
		//Object *object = DB::DBService::getInstance().getObjectById(4);
		Object *object = new Object();
		object->name = "<NAME>Test<NAME>";
		showTouristInfo(object, Point2f(599, 449));
		visualController->renderTexture(resManager->infoImage, 0, 0, getWindowWidth(), getWindowHeight());*/
	}
};
{'fraction_non_alphanumeric': 0.1346506416785496, 'fraction_numerical': 0.005703809329802404, 'mean_word_length': 4.143530644316396, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 106, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552642', 'n_tokens_mistral': 3938, 'n_tokens_neox': 3347, 'n_words': 639}