| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | // LOVE |
| 24 | #include "common/Object.h" |
| 25 | #include "common/Matrix.h" |
| 26 | #include "common/Vector.h" |
| 27 | #include "common/StringMap.h" |
| 28 | |
| 29 | namespace love |
| 30 | { |
| 31 | namespace math |
| 32 | { |
| 33 | |
| 34 | class Transform : public Object |
| 35 | { |
| 36 | public: |
| 37 | |
| 38 | enum MatrixLayout |
| 39 | { |
| 40 | MATRIX_ROW_MAJOR, |
| 41 | MATRIX_COLUMN_MAJOR, |
| 42 | MATRIX_MAX_ENUM |
| 43 | }; |
| 44 | |
| 45 | static love::Type type; |
| 46 | |
| 47 | Transform(); |
| 48 | Transform(const Matrix4 &m); |
| 49 | Transform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky); |
| 50 | |
| 51 | virtual ~Transform(); |
| 52 | |
| 53 | Transform *clone(); |
| 54 | Transform *inverse(); |
| 55 | |
| 56 | void apply(Transform *other); |
| 57 | |
| 58 | void translate(float x, float y); |
| 59 | void rotate(float angle); |
| 60 | void scale(float x, float y); |
| 61 | void shear(float x, float y); |
| 62 | |
| 63 | void reset(); |
| 64 | void setTransformation(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky); |
| 65 | |
| 66 | love::Vector2 transformPoint(love::Vector2 p) const; |
| 67 | love::Vector2 inverseTransformPoint(love::Vector2 p); |
| 68 | |
| 69 | const Matrix4 &getMatrix() const; |
| 70 | void setMatrix(const Matrix4 &m); |
| 71 | |
| 72 | static bool getConstant(const char *in, MatrixLayout &out); |
| 73 | static bool getConstant(MatrixLayout in, const char *&out); |
| 74 | static std::vector<std::string> getConstants(MatrixLayout); |
| 75 | |
| 76 | private: |
| 77 | |
| 78 | inline const Matrix4 &getInverseMatrix() |
| 79 | { |
| 80 | if (inverseDirty) |
| 81 | { |
| 82 | inverseDirty = false; |
| 83 | inverseMatrix = matrix.inverse(); |
| 84 | } |
| 85 | |
| 86 | return inverseMatrix; |
| 87 | } |
| 88 | |
| 89 | Matrix4 matrix; |
| 90 | bool inverseDirty; |
| 91 | Matrix4 inverseMatrix; |
| 92 | |
| 93 | static StringMap<MatrixLayout, MATRIX_MAX_ENUM>::Entry matrixLayoutEntries[]; |
| 94 | static StringMap<MatrixLayout, MATRIX_MAX_ENUM> matrixLayouts; |
| 95 | |
| 96 | }; // Transform |
| 97 | |
| 98 | |
| 99 | } // math |
| 100 | } // love |
| 101 | |