<commit_before>/**
 * Copyright 2012 FAU (<NAME> of Erlangen-Nuremberg)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "i6engine/api/components/MeshAppearanceComponent.h"

#include "i6engine/utils/Exceptions.h"

#include "i6engine/core/configs/SubsystemConfig.h"

#include "i6engine/api/FrontendMessageTypes.h"
#include "i6engine/api/components/PhysicalStateComponent.h"
#include "i6engine/api/components/StaticStateComponent.h"
#include "i6engine/api/configs/ComponentConfig.h"
#include "i6engine/api/configs/GraphicsConfig.h"
#include "i6engine/api/facades/MessagingFacade.h"
#include "i6engine/api/facades/NetworkFacade.h"
#include "i6engine/api/objects/GameObject.h"

namespace i6engine {
namespace api {

	MeshAppearanceComponent::MeshAppearanceComponent(const int64_t id, const attributeMap & params) : Component(id, params), _meshName(), _isVisible(), _position(), _rotation(), _scale(), _material() {
		Component::_objFamilyID = components::MeshAppearanceComponent;
		Component::_objComponentID = components::MeshAppearanceComponent;

		parseAttribute<true>(params, "mesh", _meshName);
		parseAttribute<true>(params, "visibility", _isVisible);
		parseAttribute<true>(params, "pos", _position);
		parseAttribute<true>(params, "rot", _rotation);
		parseAttribute<true>(params, "scale", _scale);
		parseAttribute<false>(params, "material", _material);
	}

	MeshAppearanceComponent::~MeshAppearanceComponent() {
		// TODO: (Daniel) move this to Finalize method
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Delete, new graphics::Graphics_Mesh_Delete(_objOwnerID, getID()), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::Init() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Create, new graphics::Graphics_Mesh_Create(_objOwnerID, getID(), _meshName, _isVisible, _position, _rotation, _scale), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);

		if (!_material.empty()) {
			setMaterial(_material);
		}
	}

	void MeshAppearanceComponent::setVisibility(const bool visible) {
		_isVisible = visible;

		sendUpdateMessage();
	}

	void MeshAppearanceComponent::setMesh(const std::string & meshName) {
		_meshName = meshName;
		sendUpdateMessage();
	}

	void MeshAppearanceComponent::sendUpdateMessage() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Update, new graphics::Graphics_Mesh_Update(_objOwnerID, getID(), _meshName, _isVisible), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::setMaterial(const std::string & materialName) {
		_material = materialName;

		if (!_material.empty()) {
			GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMaterial, core::Method::Update, new graphics::Graphics_Material_Update(getID(), _objOwnerID, materialName), core::Subsystem::Object);

			EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
		}
	}

	void MeshAppearanceComponent::setCustomParameter(uint32_t num, const Vec4 & value) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraCusParam, core::Method::Update, new graphics::Graphics_CusParam_Update(getID(), _objOwnerID, num, value), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::playAnimation(const std::string & anim, bool looping, double offsetPercent) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraPlayAnimation, core::Method::Update, new graphics::Graphics_PlayAnimation_Update(getID(), _objOwnerID, anim, looping, offsetPercent), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::setAnimationSpeed(double animationSpeed) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraSetAnimationSpeed, core::Method::Update, new graphics::Graphics_SetAnimationSpeed_Update(getID(), _objOwnerID, animationSpeed), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::stopAnimation() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraStopAnimation, core::Method::Update, new graphics::Graphics_StopAnimation_Update(getID(), _objOwnerID), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	attributeMap MeshAppearanceComponent::synchronize() const {
		attributeMap params;

		_position.insertInMap("pos", params);
		_rotation.insertInMap("rot", params);
		_scale.insertInMap("scale", params);

		params["mesh"] = _meshName;
		params["visibility"] = std::to_string(_isVisible);

		if (!_material.empty()) {
			params["material"] = _material;
		}

		return params;
	}

	std::pair<AddStrategy, int64_t> MeshAppearanceComponent::howToAdd(const ComPtr & comp) const {
		return std::make_pair(AddStrategy::ADD, 0);
	}

	std::vector<componentOptions> MeshAppearanceComponent::getComponentOptions() {
		std::vector<componentOptions> result;

		result.push_back(std::make_tuple(AccessState::READWRITE, "Mesh", [this]() {
			return _meshName;
		}, [this](std::string s) {
			setMesh(s);
			return true;
		}));
		result.push_back(std::make_tuple(AccessState::READWRITE, "Visibility", [this]() {
			return std::to_string(_isVisible);
		}, [this](std::string s) {
			try {
				_isVisible = bool(std::stoi(s));
			} catch (boost::bad_lexical_cast &) {
				return false;
			}
			setVisibility(_isVisible);
			return true;
		}));
		result.push_back(std::make_tuple(AccessState::READWRITE, "Material", [this]() {<commit_msg>ISIXE-1529 fixed new warning<commit_after>/**
 * Copyright 2012 FAU (<NAME> University of Erlangen-Nuremberg)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "i6engine/api/components/MeshAppearanceComponent.h"

#include "i6engine/utils/Exceptions.h"

#include "i6engine/core/configs/SubsystemConfig.h"

#include "i6engine/api/FrontendMessageTypes.h"
#include "i6engine/api/components/PhysicalStateComponent.h"
#include "i6engine/api/components/StaticStateComponent.h"
#include "i6engine/api/configs/ComponentConfig.h"
#include "i6engine/api/configs/GraphicsConfig.h"
#include "i6engine/api/facades/MessagingFacade.h"
#include "i6engine/api/facades/NetworkFacade.h"
#include "i6engine/api/objects/GameObject.h"

namespace i6engine {
namespace api {

	MeshAppearanceComponent::MeshAppearanceComponent(const int64_t id, const attributeMap & params) : Component(id, params), _meshName(), _isVisible(), _position(), _rotation(), _scale(), _material() {
		Component::_objFamilyID = components::MeshAppearanceComponent;
		Component::_objComponentID = components::MeshAppearanceComponent;

		parseAttribute<true>(params, "mesh", _meshName);
		parseAttribute<true>(params, "visibility", _isVisible);
		parseAttribute<true>(params, "pos", _position);
		parseAttribute<true>(params, "rot", _rotation);
		parseAttribute<true>(params, "scale", _scale);
		parseAttribute<false>(params, "material", _material);
	}

	MeshAppearanceComponent::~MeshAppearanceComponent() {
		// TODO: (Daniel) move this to Finalize method
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Delete, new graphics::Graphics_Mesh_Delete(_objOwnerID, getID()), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::Init() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Create, new graphics::Graphics_Mesh_Create(_objOwnerID, getID(), _meshName, _isVisible, _position, _rotation, _scale), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);

		if (!_material.empty()) {
			setMaterial(_material);
		}
	}

	void MeshAppearanceComponent::setVisibility(const bool visible) {
		_isVisible = visible;

		sendUpdateMessage();
	}

	void MeshAppearanceComponent::setMesh(const std::string & meshName) {
		_meshName = meshName;
		sendUpdateMessage();
	}

	void MeshAppearanceComponent::sendUpdateMessage() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMesh, core::Method::Update, new graphics::Graphics_Mesh_Update(_objOwnerID, getID(), _meshName, _isVisible), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::setMaterial(const std::string & materialName) {
		_material = materialName;

		if (!_material.empty()) {
			GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraMaterial, core::Method::Update, new graphics::Graphics_Material_Update(getID(), _objOwnerID, materialName), core::Subsystem::Object);

			EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
		}
	}

	void MeshAppearanceComponent::setCustomParameter(uint32_t num, const Vec4 & value) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraCusParam, core::Method::Update, new graphics::Graphics_CusParam_Update(getID(), _objOwnerID, num, value), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::playAnimation(const std::string & anim, bool looping, double offsetPercent) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraPlayAnimation, core::Method::Update, new graphics::Graphics_PlayAnimation_Update(getID(), _objOwnerID, anim, looping, offsetPercent), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::setAnimationSpeed(double animationSpeed) {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraSetAnimationSpeed, core::Method::Update, new graphics::Graphics_SetAnimationSpeed_Update(getID(), _objOwnerID, animationSpeed), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	void MeshAppearanceComponent::stopAnimation() {
		GameMessage::Ptr msg = boost::make_shared<GameMessage>(messages::GraphicsNodeMessageType, graphics::GraStopAnimation, core::Method::Update, new graphics::Graphics_StopAnimation_Update(getID(), _objOwnerID), core::Subsystem::Object);

		EngineController::GetSingletonPtr()->getMessagingFacade()->deliverMessage(msg);
	}

	attributeMap MeshAppearanceComponent::synchronize() const {
		attributeMap params;

		_position.insertInMap("pos", params);
		_rotation.insertInMap("rot", params);
		_scale.insertInMap("scale", params);

		params["mesh"] = _meshName;
		params["visibility"] = std::to_string(_isVisible);

		if (!_material.empty()) {
			params["material"] = _material;
		}

		return params;
	}

	std::pair<AddStrategy, int64_t> MeshAppearanceComponent::howToAdd(const ComPtr & comp) const {
		return std::make_pair(AddStrategy::ADD, 0);
	}

	std::vector<componentOptions> MeshAppearanceComponent::getComponentOptions() {
		std::vector<componentOptions> result;

		result.push_back(std::make_tuple(AccessState::READWRITE, "Mesh", [this]() {
			return _meshName;
		}, [this](std::string s) {
			setMesh(s);
			return true;
		}));
		result.push_back(std::make_tuple(AccessState::READWRITE, "Visibility", [this]() {
			return std::to_string(_isVisible);
		}, [this](std::string s) {
			try {
				_isVisible = std::stoi(s) == 1;
			} catch (boost::bad_lexical_cast &) {
				return false;
			}
			setVisibility(_isVisible);
			return true;
		}));
		result.push_back(std::make_tuple(AccessState::READWRITE, "Material", [this]() {
{'fraction_non_alphanumeric': 0.13591439399568997, 'fraction_numerical': 0.004532956825444007, 'mean_word_length': 7.853947368421053, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4670679', 'n_tokens_mistral': 4168, 'n_tokens_neox': 3920, 'n_words': 727}