Graphics/Sprite: Add SetMaterial and SetTexture overloads for searching a resource name

This commit is contained in:
Lynix 2016-11-17 18:24:36 +01:00
parent 23a85fb5ab
commit cd0e9d97b8
4 changed files with 94 additions and 8 deletions

View File

@ -281,13 +281,37 @@ namespace Ndk
sprite.BindMethod("SetColor", &Nz::Sprite::SetColor);
sprite.BindMethod("SetCornerColor", &Nz::Sprite::SetCornerColor);
sprite.BindMethod("SetDefaultMaterial", &Nz::Sprite::SetDefaultMaterial);
sprite.BindMethod("SetMaterial", &Nz::Sprite::SetMaterial, true);
sprite.BindMethod("SetOrigin", &Nz::Sprite::SetOrigin);
sprite.BindMethod("SetSize", (void(Nz::Sprite::*)(const Nz::Vector2f&)) &Nz::Sprite::SetSize);
sprite.BindMethod("SetTexture", &Nz::Sprite::SetTexture, true);
sprite.BindMethod("SetTextureCoords", &Nz::Sprite::SetTextureCoords);
sprite.BindMethod("SetTextureRect", &Nz::Sprite::SetTextureRect);
sprite.BindMethod("SetMaterial", [] (Nz::LuaInstance& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
bool resizeSprite = lua.CheckBoolean(argIndex + 1, true);
if (lua.IsOfType(argIndex, "Material"))
instance->SetMaterial(*static_cast<Nz::MaterialRef*>(lua.ToUserdata(argIndex)), resizeSprite);
else
instance->SetMaterial(lua.Check<Nz::String>(&argIndex), resizeSprite);
return 0;
});
sprite.BindMethod("SetTexture", [] (Nz::LuaInstance& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
bool resizeSprite = lua.CheckBoolean(argIndex + 1, true);
if (lua.IsOfType(argIndex, "Texture"))
instance->SetTexture(*static_cast<Nz::TextureRef*>(lua.ToUserdata(argIndex)), resizeSprite);
else
instance->SetTexture(lua.Check<Nz::String>(&argIndex), resizeSprite);
return 0;
});
/*********************************** Nz::SpriteLibrary ***********************************/
spriteLibrary.BindStaticMethod("Get", &Nz::SpriteLibrary::Get);

View File

@ -47,9 +47,11 @@ namespace Nz
inline void SetCornerColor(RectCorner corner, const Color& color);
inline void SetDefaultMaterial();
inline void SetMaterial(MaterialRef material, bool resizeSprite = true);
bool SetMaterial(String materialName, bool resizeSprite = true);
inline void SetOrigin(const Vector3f& origin);
inline void SetSize(const Vector2f& size);
inline void SetSize(float sizeX, float sizeY);
bool SetTexture(String textureName, bool resizeSprite = true);
inline void SetTexture(TextureRef texture, bool resizeSprite = true);
inline void SetTextureCoords(const Rectf& coords);
inline void SetTextureRect(const Rectui& rect);

View File

@ -184,12 +184,11 @@ namespace Nz
}
/*!
* \brief Sets the material of the sprite
* \brief Changes the material of the sprite
*
* \param material Material for the sprite
* \param resizeSprite Should sprite be resized to the material size (diffuse map)
* \param resizeSprite Should the sprite be resized to the texture size?
*/
inline void Sprite::SetMaterial(MaterialRef material, bool resizeSprite)
{
m_material = std::move(material);
@ -249,16 +248,19 @@ namespace Nz
/*!
* \brief Sets the texture of the sprite
*
* Assign a texture to the sprite material
*
* \param texture Texture for the sprite
* \param resizeSprite Should sprite be resized to the texture size
* \param resizeSprite Should the sprite be resized to the texture size?
*
* \remark The sprite material gets copied to prevent accidentally changing other drawable materials
*/
inline void Sprite::SetTexture(TextureRef texture, bool resizeSprite)
{
if (!m_material)
SetDefaultMaterial();
else if (m_material->GetReferenceCount() > 1)
m_material = Material::New(*m_material); // Copie
m_material = Material::New(*m_material); // Copy the material
if (resizeSprite && texture && texture->IsValid())
SetSize(Vector2f(Vector2ui(texture->GetSize())));

View File

@ -44,6 +44,64 @@ namespace Nz
m_boundingVolume.Set(-origin, m_size.x*Vector3f::Right() + m_size.y*Vector3f::Down() - origin);
}
/*!
* \brief Sets the material of the sprite from a name
*
* Tries to get a material from the MaterialLibrary and then the MaterialManager (which will treat the name as a path)
* Fails if the texture name is not a part of the MaterialLibrary nor the MaterialManager (which fails if it couldn't load the texture from its filepath)
*
* \param materialName Named texture for the material
* \param resizeSprite Should the sprite be resized to the material diffuse map size?
*
* \return True if the material was found or loaded from its name/path, false if it couldn't
*/
bool Sprite::SetMaterial(String materialName, bool resizeSprite)
{
MaterialRef material = MaterialLibrary::Query(materialName);
if (!material)
{
material = MaterialManager::Get(materialName);
if (!material)
{
NazaraError("Failed to get material \"" + materialName + "\"");
return false;
}
}
SetMaterial(std::move(material), resizeSprite);
return true;
}
/*!
* \brief Sets the texture of the sprite from a name
*
* Tries to get a texture from the TextureLibrary and then the TextureManager (which will treat the name as a path)
* Fails if the texture name is not a part of the TextureLibrary nor the TextureManager (which fails if it couldn't load the texture from its filepath)
*
* \param textureName Named texture for the sprite
* \param resizeSprite Should the sprite be resized to the texture size?
*
* \return True if the texture was found or loaded from its name/path, false if it couldn't
*
* \remark The sprite material gets copied to prevent accidentally changing other drawable materials
*/
bool Sprite::SetTexture(String textureName, bool resizeSprite)
{
TextureRef texture = TextureLibrary::Query(textureName);
if (!texture)
{
texture = TextureManager::Get(textureName);
if (!texture)
{
NazaraError("Failed to get texture \"" + textureName + "\"");
return false;
}
}
SetTexture(std::move(texture), resizeSprite);
return true;
}
/*!
* \brief Updates the data of the sprite
*