Browse Source

Trade: add a MaterialData::attributeData(layer, id) accessor.

For cases where the whole MaterialAttributeData instance is needed and
calculating the right offset into the array returned by attributeData()
would be too error-prone.

Similar accessor is in MeshData already, so this achieves better feature
parity between the two.
pull/605/head
Vladimír Vondruš 4 years ago
parent
commit
df45cfadd7
  1. 8
      src/Magnum/Trade/MaterialData.cpp
  2. 17
      src/Magnum/Trade/MaterialData.h
  3. 13
      src/Magnum/Trade/Test/MaterialDataTest.cpp

8
src/Magnum/Trade/MaterialData.cpp

@ -748,6 +748,14 @@ UnsignedInt MaterialData::attributeId(const MaterialLayer layer, const MaterialA
return attributeId(string, name); return attributeId(string, name);
} }
const MaterialAttributeData& MaterialData::attributeData(const UnsignedInt layer, const UnsignedInt id) const {
CORRADE_ASSERT(layer < layerCount(),
"Trade::MaterialData::attributeData(): index" << layer << "out of range for" << layerCount() << "layers", _data[0]);
CORRADE_ASSERT(id < attributeCount(layer),
"Trade::MaterialData::attributeData(): index" << id << "out of range for" << attributeCount(layer) << "attributes in layer" << layer, _data[0]);
return _data[layerOffset(layer) + id];
}
Containers::StringView MaterialData::attributeName(const UnsignedInt layer, const UnsignedInt id) const { Containers::StringView MaterialData::attributeName(const UnsignedInt layer, const UnsignedInt id) const {
CORRADE_ASSERT(layer < layerCount(), CORRADE_ASSERT(layer < layerCount(),
"Trade::MaterialData::attributeName(): index" << layer << "out of range for" << layerCount() << "layers", {}); "Trade::MaterialData::attributeName(): index" << layer << "out of range for" << layerCount() << "layers", {});

17
src/Magnum/Trade/MaterialData.h

@ -2451,6 +2451,23 @@ class MAGNUM_TRADE_EXPORT MaterialData {
return attributeId(0, name); return attributeId(0, name);
} /**< @overload */ } /**< @overload */
/**
* @brief Raw attribute data
*
* The @p layer is expected to be smaller than @ref layerCount() const,
* @p id is expected to be smaller than @ref attributeCount(UnsignedInt) const.
*/
const MaterialAttributeData& attributeData(UnsignedInt layer, UnsignedInt id) const;
/**
* @brief Raw attribute data in the base material
*
* The @p @p id is expected to be smaller than @ref attributeCount() const.
*/
const MaterialAttributeData& attributeData(UnsignedInt id) const {
return attributeData(0, id);
}
/** /**
* @brief Name of an attribute in given material layer * @brief Name of an attribute in given material layer
* *

13
src/Magnum/Trade/Test/MaterialDataTest.cpp

@ -998,6 +998,11 @@ void MaterialDataTest::construct() {
CORRADE_COMPARE(data.attributeName(3), "highlightColor"); CORRADE_COMPARE(data.attributeName(3), "highlightColor");
/* Access by ID */ /* Access by ID */
CORRADE_COMPARE(data.attributeData(0).name(), "AmbientTextureMatrix");
CORRADE_COMPARE(data.attributeData(1).name(), "DiffuseTextureCoordinates");
CORRADE_COMPARE(data.attributeData(2).name(), "DoubleSided");
CORRADE_COMPARE(data.attributeData(3).name(), "highlightColor");
CORRADE_COMPARE(data.attributeType(0), MaterialAttributeType::Matrix3x3); CORRADE_COMPARE(data.attributeType(0), MaterialAttributeType::Matrix3x3);
CORRADE_COMPARE(data.attributeType(1), MaterialAttributeType::UnsignedInt); CORRADE_COMPARE(data.attributeType(1), MaterialAttributeType::UnsignedInt);
CORRADE_COMPARE(data.attributeType(2), MaterialAttributeType::Bool); CORRADE_COMPARE(data.attributeType(2), MaterialAttributeType::Bool);
@ -1220,6 +1225,10 @@ void MaterialDataTest::constructLayers() {
CORRADE_COMPARE(data.attributeName(3, 1), "thickness"); CORRADE_COMPARE(data.attributeName(3, 1), "thickness");
/* Access by layer ID and attribute ID */ /* Access by layer ID and attribute ID */
CORRADE_COMPARE(data.attributeData(0, 0).name(), "DiffuseTextureCoordinates");
CORRADE_COMPARE(data.attributeData(1, 2).name(), "highlightColor");
CORRADE_COMPARE(data.attributeData(3, 1).name(), "thickness");
CORRADE_COMPARE(data.attributeType(0, 0), MaterialAttributeType::UnsignedInt); CORRADE_COMPARE(data.attributeType(0, 0), MaterialAttributeType::UnsignedInt);
CORRADE_COMPARE(data.attributeType(1, 2), MaterialAttributeType::Vector4); CORRADE_COMPARE(data.attributeType(1, 2), MaterialAttributeType::Vector4);
CORRADE_COMPARE(data.attributeType(3, 1), MaterialAttributeType::Float); CORRADE_COMPARE(data.attributeType(3, 1), MaterialAttributeType::Float);
@ -2587,6 +2596,7 @@ void MaterialDataTest::accessLayerOutOfBounds() {
data.findAttributeId(2, MaterialAttribute::AlphaMask); data.findAttributeId(2, MaterialAttribute::AlphaMask);
data.attributeId(2, "AlphaMask"); data.attributeId(2, "AlphaMask");
data.attributeId(2, MaterialAttribute::AlphaMask); data.attributeId(2, MaterialAttribute::AlphaMask);
data.attributeData(2, 0);
data.attributeName(2, 0); data.attributeName(2, 0);
data.attributeType(2, 0); data.attributeType(2, 0);
data.attributeType(2, "AlphaMask"); data.attributeType(2, "AlphaMask");
@ -2626,6 +2636,7 @@ void MaterialDataTest::accessLayerOutOfBounds() {
"Trade::MaterialData::findAttributeId(): index 2 out of range for 2 layers\n" "Trade::MaterialData::findAttributeId(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeId(): index 2 out of range for 2 layers\n" "Trade::MaterialData::attributeId(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeId(): index 2 out of range for 2 layers\n" "Trade::MaterialData::attributeId(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeData(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeName(): index 2 out of range for 2 layers\n" "Trade::MaterialData::attributeName(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeType(): index 2 out of range for 2 layers\n" "Trade::MaterialData::attributeType(): index 2 out of range for 2 layers\n"
"Trade::MaterialData::attributeType(): index 2 out of range for 2 layers\n" "Trade::MaterialData::attributeType(): index 2 out of range for 2 layers\n"
@ -2844,6 +2855,7 @@ void MaterialDataTest::accessOutOfBoundsInLayerIndex() {
std::ostringstream out; std::ostringstream out;
Error redirectError{&out}; Error redirectError{&out};
data.attributeData(1, 2);
data.attributeName(1, 2); data.attributeName(1, 2);
data.attributeType(1, 2); data.attributeType(1, 2);
data.attribute(1, 2); data.attribute(1, 2);
@ -2853,6 +2865,7 @@ void MaterialDataTest::accessOutOfBoundsInLayerIndex() {
data.mutableAttribute<Int>(1, 2); data.mutableAttribute<Int>(1, 2);
data.mutableAttribute<Containers::MutableStringView>(1, 2); data.mutableAttribute<Containers::MutableStringView>(1, 2);
CORRADE_COMPARE(out.str(), CORRADE_COMPARE(out.str(),
"Trade::MaterialData::attributeData(): index 2 out of range for 2 attributes in layer 1\n"
"Trade::MaterialData::attributeName(): index 2 out of range for 2 attributes in layer 1\n" "Trade::MaterialData::attributeName(): index 2 out of range for 2 attributes in layer 1\n"
"Trade::MaterialData::attributeType(): index 2 out of range for 2 attributes in layer 1\n" "Trade::MaterialData::attributeType(): index 2 out of range for 2 attributes in layer 1\n"
"Trade::MaterialData::attribute(): index 2 out of range for 2 attributes in layer 1\n" "Trade::MaterialData::attribute(): index 2 out of range for 2 attributes in layer 1\n"

Loading…
Cancel
Save