Example #1
0
Icon* IconManager::getIcon(int, EmberEntity* entity) {

	std::string key = "entity_" + entity->getId();
	if (mIconStore.hasIcon(key)) {
		return mIconStore.getIcon(key);
	} else {
		std::string modelName;


		Mapping::ModelActionCreator actionCreator(*entity, [&](std::string newModelName){
			modelName = newModelName;
		}, [&](std::string partName){
			//Ignore parts
		});
		std::unique_ptr<EntityMapping::EntityMapping> modelMapping(Mapping::EmberEntityMappingManager::getSingleton().getManager().createMapping(*entity, actionCreator, &EmberOgre::getSingleton().getWorld()->getView()));


		if (modelMapping) {
			modelMapping->initialize();
		}
		//if there's no model defined for this use the placeholder model
		if (modelName.empty()) {
			modelName = "common/primitives/placeholder.modeldef";
		}
		auto modelDefPtr = Model::ModelDefinitionManager::getSingleton().getByName(modelName);
		if (modelDefPtr) {
			Model::ModelDefinition* modelDef = modelDefPtr.get();
			const std::string& iconPath(modelDef->getIconPath());
			if (!iconPath.empty()) {

				Ogre::TexturePtr texPtr;
				try {
					if (Ogre::TextureManager::getSingleton().resourceExists(iconPath, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)) {
						texPtr = static_cast<Ogre::TexturePtr> (Ogre::TextureManager::getSingleton().getByName(iconPath, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME));
						//try to load it to make sure that's it a working image
						texPtr->load();
					}
					if (!texPtr) {
						texPtr = Ogre::TextureManager::getSingleton().load(iconPath, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
					}
				} catch (...) {
					S_LOG_WARNING("Error when trying to load the icon " << iconPath << ". The icon will be rendered dynamically.");
					texPtr.reset();
				}
				if (texPtr) {
					Icon* icon = mIconStore.createIcon(key, texPtr);
					return icon;
				}
			}
		}
		Icon* icon = mIconStore.createIcon(key);
		if (icon) {
			//update the model preview window
			// 				Model::Model* model = Model::Model::createModel(mIconRenderer.getRenderContext()->getSceneManager(), modelName);
			render(*icon, modelName);
			// 				mIconRenderer.getRenderContext()->getSceneManager()->destroyMovableObject(model);
		}
		return icon;
	}

}