diff --git a/radiantcore/brush/Face.cpp b/radiantcore/brush/Face.cpp index 621dba8bb3..bbf32ad2c6 100644 --- a/radiantcore/brush/Face.cpp +++ b/radiantcore/brush/Face.cpp @@ -16,7 +16,7 @@ #include "BrushModule.h" // The structure that is saved in the undostack -class Face::SavedState : +class Face::SavedState final : public IUndoMemento { public: @@ -29,15 +29,6 @@ class Face::SavedState : _texdefState(face.getProjection()), _materialName(face.getShader()) {} - - virtual ~SavedState() {} - - void exportState(Face& face) const - { - _planeState.exportState(face.getPlane()); - face.setShader(_materialName); - face.getProjection().assign(_texdefState); - } }; Face::Face(Brush& owner) : @@ -184,14 +175,18 @@ void Face::undoSave() // undoable IUndoMementoPtr Face::exportState() const { - return IUndoMementoPtr(new SavedState(*this)); + return std::make_shared(*this); } void Face::importState(const IUndoMementoPtr& data) { undoSave(); - std::static_pointer_cast(data)->exportState(*this); + auto state = std::static_pointer_cast(data); + + state->_planeState.exportState(getPlane()); + setShader(state->_materialName); + _texdef = state->_texdefState; planeChanged(); _owner.onFaceConnectivityChanged(); @@ -464,7 +459,7 @@ void Face::GetTexdef(TextureProjection& projection) const void Face::SetTexdef(const TextureProjection& projection) { undoSave(); - _texdef.assign(projection); + _texdef = projection; texdefChanged(); } diff --git a/radiantcore/brush/TextureMatrix.cpp b/radiantcore/brush/TextureMatrix.cpp index a3ce4e7d13..7c9050eb36 100644 --- a/radiantcore/brush/TextureMatrix.cpp +++ b/radiantcore/brush/TextureMatrix.cpp @@ -7,22 +7,22 @@ TextureMatrix::TextureMatrix() { - coords[0][0] = 2.0f; - coords[0][1] = 0.f; - coords[0][2] = 0.f; - coords[1][0] = 0.f; - coords[1][1] = 2.0f; - coords[1][2] = 0.f; + _coords[0][0] = 2.0f; + _coords[0][1] = 0.f; + _coords[0][2] = 0.f; + _coords[1][0] = 0.f; + _coords[1][1] = 2.0f; + _coords[1][2] = 0.f; } TextureMatrix::TextureMatrix(const Matrix3& transform) { - coords[0][0] = transform.xx(); - coords[0][1] = transform.yx(); - coords[0][2] = transform.zx(); - coords[1][0] = transform.xy(); - coords[1][1] = transform.yy(); - coords[1][2] = transform.zy(); + _coords[0][0] = transform.xx(); + _coords[0][1] = transform.yx(); + _coords[0][2] = transform.zx(); + _coords[1][0] = transform.xy(); + _coords[1][1] = transform.yy(); + _coords[1][2] = transform.zy(); } TextureMatrix::TextureMatrix(const ShiftScaleRotation& ssr) @@ -34,12 +34,12 @@ TextureMatrix::TextureMatrix(const ShiftScaleRotation& ssr) auto x = 1.0 / ssr.scale[0]; auto y = 1.0 / ssr.scale[1]; - coords[0][0] = x * c; - coords[1][0] = x * s; - coords[0][1] = y * -s; - coords[1][1] = y * c; - coords[0][2] = -ssr.shift[0]; - coords[1][2] = ssr.shift[1]; + _coords[0][0] = x * c; + _coords[1][0] = x * s; + _coords[0][1] = y * -s; + _coords[1][1] = y * c; + _coords[0][2] = -ssr.shift[0]; + _coords[1][2] = ssr.shift[1]; } void TextureMatrix::shift(double s, double t) @@ -48,34 +48,34 @@ void TextureMatrix::shift(double s, double t) // this depends on the texture size and the pixel/texel ratio // as a ratio against texture size // the scale of the texture is not relevant here (we work directly on a transformation from the base vectors) - coords[0][2] -= s; - coords[1][2] += t; + _coords[0][2] -= s; + _coords[1][2] += t; } void TextureMatrix::addScale(std::size_t width, std::size_t height) { - coords[0][0] /= width; - coords[0][1] /= width; - coords[0][2] /= width; - coords[1][0] /= height; - coords[1][1] /= height; - coords[1][2] /= height; + _coords[0][0] /= width; + _coords[0][1] /= width; + _coords[0][2] /= width; + _coords[1][0] /= height; + _coords[1][1] /= height; + _coords[1][2] /= height; } ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const { ShiftScaleRotation ssr; - ssr.scale[0] = 1.0 / Vector2(coords[0][0], coords[1][0]).getLength(); - ssr.scale[1] = 1.0 / Vector2(coords[0][1], coords[1][1]).getLength(); + ssr.scale[0] = 1.0 / Vector2(_coords[0][0], _coords[1][0]).getLength(); + ssr.scale[1] = 1.0 / Vector2(_coords[0][1], _coords[1][1]).getLength(); - ssr.rotate = -radians_to_degrees(arctangent_yx(coords[1][0], coords[0][0])); + ssr.rotate = -radians_to_degrees(arctangent_yx(_coords[1][0], _coords[0][0])); - ssr.shift[0] = -coords[0][2]; - ssr.shift[1] = coords[1][2]; + ssr.shift[0] = -_coords[0][2]; + ssr.shift[1] = _coords[1][2]; // determine whether or not an axis is flipped using a 2d cross-product - auto cross = Vector2(coords[0][0], coords[0][1]).crossProduct(Vector2(coords[1][0], coords[1][1])); + auto cross = Vector2(_coords[0][0], _coords[0][1]).crossProduct(Vector2(_coords[1][0], _coords[1][1])); if (cross < 0) { @@ -99,25 +99,25 @@ ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const void TextureMatrix::normalise(float width, float height) { - coords[0][2] = float_mod(coords[0][2], width); - coords[1][2] = float_mod(coords[1][2], height); + _coords[0][2] = float_mod(_coords[0][2], width); + _coords[1][2] = float_mod(_coords[1][2], height); } Matrix3 TextureMatrix::getMatrix3() const { return Matrix3::byRows( - coords[0][0], coords[0][1], coords[0][2], - coords[1][0], coords[1][1], coords[1][2], + _coords[0][0], _coords[0][1], _coords[0][2], + _coords[1][0], _coords[1][1], _coords[1][2], 0, 0, 1 ); } bool TextureMatrix::isSane() const { - return !std::isnan(coords[0][0]) && !std::isinf(coords[0][0]) && - !std::isnan(coords[0][1]) && !std::isinf(coords[0][1]) && - !std::isnan(coords[0][2]) && !std::isinf(coords[0][2]) && - !std::isnan(coords[1][0]) && !std::isinf(coords[1][0]) && - !std::isnan(coords[1][1]) && !std::isinf(coords[1][1]) && - !std::isnan(coords[1][2]) && !std::isinf(coords[1][2]); + return !std::isnan(_coords[0][0]) && !std::isinf(_coords[0][0]) && + !std::isnan(_coords[0][1]) && !std::isinf(_coords[0][1]) && + !std::isnan(_coords[0][2]) && !std::isinf(_coords[0][2]) && + !std::isnan(_coords[1][0]) && !std::isinf(_coords[1][0]) && + !std::isnan(_coords[1][1]) && !std::isinf(_coords[1][1]) && + !std::isnan(_coords[1][2]) && !std::isinf(_coords[1][2]); } diff --git a/radiantcore/brush/TextureMatrix.h b/radiantcore/brush/TextureMatrix.h index 7b3b2cc796..6776ecccc8 100644 --- a/radiantcore/brush/TextureMatrix.h +++ b/radiantcore/brush/TextureMatrix.h @@ -35,11 +35,14 @@ class TextureMatrix final { private: - double coords[2][3]; + double _coords[2][3]; public: TextureMatrix(); + TextureMatrix(const TextureMatrix& other) = default; + TextureMatrix& operator=(const TextureMatrix& other) = default; + // Copy-construct from the relevant components from the given transform // (which is everything except the last row: xz() and yz() are 0, zz() is 1) TextureMatrix(const Matrix3& transform); @@ -75,7 +78,7 @@ class TextureMatrix final inline std::ostream& operator<<(std::ostream& st, const TextureMatrix& texdef) { - st << "<" << texdef.coords[0][0] << ", " << texdef.coords[0][1] << ", " << texdef.coords[0][2] << ">\n"; - st << "<" << texdef.coords[1][0] << ", " << texdef.coords[1][1] << ", " << texdef.coords[1][2] << ">"; + st << "<" << texdef._coords[0][0] << ", " << texdef._coords[0][1] << ", " << texdef._coords[0][2] << ">\n"; + st << "<" << texdef._coords[1][0] << ", " << texdef._coords[1][1] << ", " << texdef._coords[1][2] << ">"; return st; } diff --git a/radiantcore/brush/TextureProjection.cpp b/radiantcore/brush/TextureProjection.cpp index 2966705df0..dbb6c17819 100644 --- a/radiantcore/brush/TextureProjection.cpp +++ b/radiantcore/brush/TextureProjection.cpp @@ -6,7 +6,7 @@ #include TextureProjection::TextureProjection() : - TextureProjection(GetDefaultProjection()) + TextureProjection(Default()) {} TextureProjection::TextureProjection(const TextureProjection& other) : @@ -27,7 +27,7 @@ TextureMatrix& TextureProjection::getTextureMatrix() return _matrix; } -TextureMatrix TextureProjection::GetDefaultProjection() +TextureMatrix TextureProjection::Default() { // Cache the registry key because this constructor is called a lot static registry::CachedKey scaleKey( @@ -42,10 +42,10 @@ TextureMatrix TextureProjection::GetDefaultProjection() return TextureMatrix(ssr); } -// Assigns an projection to this one -void TextureProjection::assign(const TextureProjection& other) +TextureProjection& TextureProjection::operator=(const TextureProjection& other) { _matrix = other._matrix; + return *this; } void TextureProjection::setTransform(const Matrix3& transform) @@ -102,7 +102,7 @@ void TextureProjection::fitTexture(std::size_t width, std::size_t height, } // Sanity-check the matrix, if it contains any NaNs or INFs we fall back to the default projection (#5371) - Matrix4 st2tex = _matrix.isSane() ? getMatrix4() : getMatrix4FromTextureMatrix(GetDefaultProjection().getMatrix3()); + Matrix4 st2tex = _matrix.isSane() ? getMatrix4() : getMatrix4FromTextureMatrix(Default().getMatrix3()); // the current texture transform Matrix4 local2tex = st2tex; diff --git a/radiantcore/brush/TextureProjection.h b/radiantcore/brush/TextureProjection.h index 02d622b665..1b24b819bd 100644 --- a/radiantcore/brush/TextureProjection.h +++ b/radiantcore/brush/TextureProjection.h @@ -38,9 +38,7 @@ class TextureProjection final const TextureMatrix& getTextureMatrix() const; TextureMatrix& getTextureMatrix(); - static TextureMatrix GetDefaultProjection(); - - void assign(const TextureProjection& other); + TextureProjection& operator=(const TextureProjection& other); void setTransform(const Matrix3& transform); void setFromShiftScaleRotate(const ShiftScaleRotation& ssr); @@ -69,6 +67,8 @@ class TextureProjection final void calculateFromPoints(const Vector3 points[3], const Vector2 uvs[3], const Vector3& normal); private: + static TextureMatrix Default(); + Matrix4 getMatrix4() const; void setTransformFromMatrix4(const Matrix4& transform);