Allow commutative multiplication in Variant

Also allow quaternions to be multiplied by integers.
This commit is contained in:
George Marques 2020-09-17 08:56:36 -03:00
parent 6f4384f206
commit e4942e4a96
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
4 changed files with 98 additions and 3 deletions

View file

@ -234,4 +234,8 @@ bool Color::operator<(const Color &p_color) const {
}
}
_FORCE_INLINE_ Color operator*(const real_t &p_real, const Color &p_color) {
return p_color * p_real;
}
#endif // COLOR_H

View file

@ -224,4 +224,8 @@ bool Quat::operator!=(const Quat &p_quat) const {
return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
}
_FORCE_INLINE_ Quat operator*(const real_t &p_real, const Quat &p_quat) {
return p_quat * p_real;
}
#endif // QUAT_H

View file

@ -150,7 +150,19 @@ _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec)
return p_vec - *this * (dot(p_vec) - p_d);
}
_FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
_FORCE_INLINE_ Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
return p_vec * p_scalar;
}
@ -304,6 +316,22 @@ struct Vector2i {
}
};
_FORCE_INLINE_ Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
_FORCE_INLINE_ Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) {
return p_vector * p_scalar;
}
typedef Vector2i Size2i;
typedef Vector2i Point2i;

View file

@ -1031,6 +1031,9 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
case FLOAT: {
_RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * p_b._data._float);
}
case INT: {
_RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * p_b._data._int);
}
default:
_RETURN_FAIL;
}
@ -1062,8 +1065,64 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
}
}
DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, INT, *, _int);
DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, FLOAT, *, _float);
CASE_TYPE(math, OP_MULTIPLY, INT) {
if (p_b.type == INT) {
_RETURN(p_a._data._int * p_b._data._int);
}
if (p_b.type == FLOAT) {
_RETURN(p_a._data._int * p_b._data._float);
}
if (p_b.type == VECTOR2) {
_RETURN(p_a._data._int * *reinterpret_cast<const Vector2 *>(p_b._data._mem));
}
if (p_b.type == VECTOR3) {
_RETURN(p_a._data._int * *reinterpret_cast<const Vector3 *>(p_b._data._mem));
}
if (p_b.type == VECTOR2I) {
_RETURN(p_a._data._int * *reinterpret_cast<const Vector2i *>(p_b._data._mem));
}
if (p_b.type == VECTOR3I) {
_RETURN(p_a._data._int * *reinterpret_cast<const Vector3i *>(p_b._data._mem));
}
if (p_b.type == QUAT) {
_RETURN(p_a._data._int * *reinterpret_cast<const Quat *>(p_b._data._mem));
}
if (p_b.type == COLOR) {
_RETURN(p_a._data._int * *reinterpret_cast<const Color *>(p_b._data._mem));
}
_RETURN_FAIL
}
CASE_TYPE(math, OP_MULTIPLY, FLOAT) {
if (p_b.type == INT) {
_RETURN(p_a._data._float * p_b._data._int);
}
if (p_b.type == FLOAT) {
_RETURN(p_a._data._float * p_b._data._float);
}
if (p_b.type == VECTOR2) {
_RETURN(p_a._data._float * *reinterpret_cast<const Vector2 *>(p_b._data._mem));
}
if (p_b.type == VECTOR3) {
_RETURN(p_a._data._float * *reinterpret_cast<const Vector3 *>(p_b._data._mem));
}
if (p_b.type == VECTOR2I) {
_RETURN(p_a._data._float * *reinterpret_cast<const Vector2i *>(p_b._data._mem));
}
if (p_b.type == VECTOR3I) {
_RETURN(p_a._data._float * *reinterpret_cast<const Vector3i *>(p_b._data._mem));
}
if (p_b.type == QUAT) {
_RETURN(p_a._data._float * *reinterpret_cast<const Quat *>(p_b._data._mem));
}
if (p_b.type == COLOR) {
_RETURN(p_a._data._float * *reinterpret_cast<const Color *>(p_b._data._mem));
}
_RETURN_FAIL
}
DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR2, *, Vector2);
DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR2I, *, Vector2i);
DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR3, *, Vector3);