summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/variant.cpp15
-rw-r--r--core/variant.h2
-rw-r--r--core/variant_op.cpp53
3 files changed, 70 insertions, 0 deletions
diff --git a/core/variant.cpp b/core/variant.cpp
index 3bd8d80528..1fdbc9f753 100644
--- a/core/variant.cpp
+++ b/core/variant.cpp
@@ -1116,6 +1116,21 @@ void Variant::reference(const Variant& p_variant) {
}
+void Variant::zero() {
+ switch(type) {
+ case NIL: break;
+ case BOOL: this->_data._bool = false; break;
+ case INT: this->_data._int = 0; break;
+ case REAL: this->_data._real = 0; break;
+ case VECTOR2: *reinterpret_cast<Vector2*>(this->_data._mem) = Vector2(); break;
+ case RECT2: *reinterpret_cast<Rect2*>(this->_data._mem) = Rect2(); break;
+ case VECTOR3: *reinterpret_cast<Vector3*>(this->_data._mem) = Vector3(); break;
+ case PLANE: *reinterpret_cast<Plane*>(this->_data._mem) = Plane(); break;
+ case QUAT: *reinterpret_cast<Quat*>(this->_data._mem) = Quat(); break;
+ case COLOR: *reinterpret_cast<Color*>(this->_data._mem) = Color(); break;
+ default: this->clear(); break;
+ }
+}
void Variant::clear() {
switch(type) {
diff --git a/core/variant.h b/core/variant.h
index b58c781bdd..d8813c4937 100644
--- a/core/variant.h
+++ b/core/variant.h
@@ -372,6 +372,8 @@ public:
return res;
}
+ void zero();
+ static void blend(const Variant& a, const Variant& b, float c,Variant &r_dst);
static void interpolate(const Variant& a, const Variant& b, float c,Variant &r_dst);
struct CallError {
diff --git a/core/variant_op.cpp b/core/variant_op.cpp
index 204a00e1d5..5463e1cabb 100644
--- a/core/variant_op.cpp
+++ b/core/variant_op.cpp
@@ -3431,6 +3431,59 @@ Variant Variant::iter_get(const Variant& r_iter,bool &r_valid) const {
}
+void Variant::blend(const Variant& a, const Variant& b, float c, Variant &r_dst) {
+ if (a.type!=b.type) {
+ if(a.is_num() && b.is_num()) {
+ real_t va=a;
+ real_t vb=b;
+ r_dst=va + vb * c;
+ } else {
+ r_dst=a;
+ }
+ return;
+ }
+
+ switch(a.type) {
+ case NIL: { r_dst=Variant(); } return;
+ case INT:{
+ int va=a._data._int;
+ int vb=b._data._int;
+ r_dst=int(va + vb * c + 0.5);
+ } return;
+ case REAL:{
+ double ra=a._data._real;
+ double rb=b._data._real;
+ r_dst=ra + rb * c;
+ } return;
+ case VECTOR2:{ r_dst=*reinterpret_cast<const Vector2*>(a._data._mem)+*reinterpret_cast<const Vector2*>(b._data._mem)*c; } return;
+ case RECT2:{
+ const Rect2 *ra = reinterpret_cast<const Rect2*>(a._data._mem);
+ const Rect2 *rb = reinterpret_cast<const Rect2*>(b._data._mem);
+ r_dst=Rect2(ra->pos + rb->pos * c, ra->size + rb->size * c);
+ } return;
+ case VECTOR3:{ r_dst=*reinterpret_cast<const Vector2*>(a._data._mem)+*reinterpret_cast<const Vector2*>(b._data._mem)*c; } return;
+ case _AABB:{
+ const AABB *ra = reinterpret_cast<const AABB*>(a._data._mem);
+ const AABB *rb = reinterpret_cast<const AABB*>(b._data._mem);
+ r_dst=AABB(ra->pos + rb->pos * c, ra->size + rb->size * c);
+ } return;
+ case COLOR:{
+ const Color *ca = reinterpret_cast<const Color*>(a._data._mem);
+ const Color *cb = reinterpret_cast<const Color*>(b._data._mem);
+ float r = ca->r + cb->r * c;
+ float g = ca->g + cb->g * c;
+ float b = ca->b + cb->b * c;
+ float a = ca->a + cb->a * c;
+ r = r > 1.0 ? 1.0 : r;
+ g = g > 1.0 ? 1.0 : g;
+ b = b > 1.0 ? 1.0 : b;
+ a = a > 1.0 ? 1.0 : a;
+ r_dst=Color(r, g, b, a);
+ } return;
+ default:{ r_dst = c<0.5 ? a : b; } return;
+ }
+}
+
void Variant::interpolate(const Variant& a, const Variant& b, float c,Variant &r_dst) {
if (a.type!=b.type) {