Skip to content

Commit

Permalink
Merge pull request #36436 from reduz/new-variant-types
Browse files Browse the repository at this point in the history
Add support for Vector2i, Rect2i and Vector3i to Variant
  • Loading branch information
akien-mga authored Feb 23, 2020
2 parents f2d8c4e + 6da0eef commit 63b0d82
Show file tree
Hide file tree
Showing 11 changed files with 1,026 additions and 10 deletions.
74 changes: 74 additions & 0 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
if (r_len)
(*r_len) += 4 * 2;

} break; // 5
case Variant::VECTOR2I: {

ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA);
Vector2i val;
val.x = decode_uint32(&buf[0]);
val.y = decode_uint32(&buf[4]);
r_variant = val;

if (r_len)
(*r_len) += 4 * 2;

} break; // 5
case Variant::RECT2: {

Expand All @@ -200,6 +212,20 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
if (r_len)
(*r_len) += 4 * 4;

} break;
case Variant::RECT2I: {

ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA);
Rect2i val;
val.position.x = decode_uint32(&buf[0]);
val.position.y = decode_uint32(&buf[4]);
val.size.x = decode_uint32(&buf[8]);
val.size.y = decode_uint32(&buf[12]);
r_variant = val;

if (r_len)
(*r_len) += 4 * 4;

} break;
case Variant::VECTOR3: {

Expand All @@ -213,6 +239,19 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
if (r_len)
(*r_len) += 4 * 3;

} break;
case Variant::VECTOR3I: {

ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA);
Vector3i val;
val.x = decode_uint32(&buf[0]);
val.y = decode_uint32(&buf[4]);
val.z = decode_uint32(&buf[8]);
r_variant = val;

if (r_len)
(*r_len) += 4 * 3;

} break;
case Variant::TRANSFORM2D: {

Expand Down Expand Up @@ -966,6 +1005,17 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo

r_len += 2 * 4;

} break; // 5
case Variant::VECTOR2I: {

if (buf) {
Vector2i v2 = p_variant;
encode_uint32(v2.x, &buf[0]);
encode_uint32(v2.y, &buf[4]);
}

r_len += 2 * 4;

} break; // 5
case Variant::RECT2: {

Expand All @@ -978,6 +1028,18 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
}
r_len += 4 * 4;

} break;
case Variant::RECT2I: {

if (buf) {
Rect2i r2 = p_variant;
encode_uint32(r2.position.x, &buf[0]);
encode_uint32(r2.position.y, &buf[4]);
encode_uint32(r2.size.x, &buf[8]);
encode_uint32(r2.size.y, &buf[12]);
}
r_len += 4 * 4;

} break;
case Variant::VECTOR3: {

Expand All @@ -990,6 +1052,18 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo

r_len += 3 * 4;

} break;
case Variant::VECTOR3I: {

if (buf) {
Vector3i v3 = p_variant;
encode_uint32(v3.x, &buf[0]);
encode_uint32(v3.y, &buf[4]);
encode_uint32(v3.z, &buf[8]);
}

r_len += 3 * 4;

} break;
case Variant::TRANSFORM2D: {

Expand Down
56 changes: 56 additions & 0 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ enum {
VARIANT_CALLABLE = 42,
VARIANT_SIGNAL = 43,
VARIANT_STRING_NAME = 44,
VARIANT_VECTOR2I = 45,
VARIANT_RECT2I = 46,
VARIANT_VECTOR3I = 47,
OBJECT_EMPTY = 0,
OBJECT_EXTERNAL_RESOURCE = 1,
OBJECT_INTERNAL_RESOURCE = 2,
Expand Down Expand Up @@ -158,6 +161,14 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
v.y = f->get_real();
r_v = v;

} break;
case VARIANT_VECTOR2I: {

Vector2i v;
v.x = f->get_32();
v.y = f->get_32();
r_v = v;

} break;
case VARIANT_RECT2: {

Expand All @@ -168,6 +179,16 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
v.size.y = f->get_real();
r_v = v;

} break;
case VARIANT_RECT2I: {

Rect2i v;
v.position.x = f->get_32();
v.position.y = f->get_32();
v.size.x = f->get_32();
v.size.y = f->get_32();
r_v = v;

} break;
case VARIANT_VECTOR3: {

Expand All @@ -177,6 +198,14 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
v.z = f->get_real();
r_v = v;
} break;
case VARIANT_VECTOR3I: {

Vector3i v;
v.x = f->get_32();
v.y = f->get_32();
v.z = f->get_32();
r_v = v;
} break;
case VARIANT_PLANE: {

Plane v;
Expand Down Expand Up @@ -1292,6 +1321,14 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
f->store_real(val.x);
f->store_real(val.y);

} break;
case Variant::VECTOR2I: {

f->store_32(VARIANT_VECTOR2I);
Vector2i val = p_property;
f->store_32(val.x);
f->store_32(val.y);

} break;
case Variant::RECT2: {

Expand All @@ -1302,6 +1339,16 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
f->store_real(val.size.x);
f->store_real(val.size.y);

} break;
case Variant::RECT2I: {

f->store_32(VARIANT_RECT2I);
Rect2i val = p_property;
f->store_32(val.position.x);
f->store_32(val.position.y);
f->store_32(val.size.x);
f->store_32(val.size.y);

} break;
case Variant::VECTOR3: {

Expand All @@ -1311,6 +1358,15 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
f->store_real(val.y);
f->store_real(val.z);

} break;
case Variant::VECTOR3I: {

f->store_32(VARIANT_VECTOR3I);
Vector3i val = p_property;
f->store_32(val.x);
f->store_32(val.y);
f->store_32(val.z);

} break;
case Variant::PLANE: {

Expand Down
5 changes: 5 additions & 0 deletions core/math/rect2.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ struct Rect2i {
size = end - begin;
}

_FORCE_INLINE_ Rect2i abs() const {

return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
}

operator String() const { return String(position) + ", " + String(size); }

operator Rect2() const { return Rect2(position, size); }
Expand Down
7 changes: 6 additions & 1 deletion core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,15 @@ struct Vector2i {
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }

bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }

bool operator==(const Vector2i &p_vec2) const;
bool operator!=(const Vector2i &p_vec2) const;

real_t get_aspect() const { return width / (real_t)height; }
real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }

operator String() const { return String::num(x) + ", " + String::num(y); }

Expand Down
Loading

0 comments on commit 63b0d82

Please sign in to comment.