Skip to content

Commit

Permalink
Binding AABB to ECMAScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Geequlim committed Mar 7, 2019
1 parent 1acb638 commit 240a4e6
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions duktape/builtin_binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"RID": "Variant::_RID",
"Transform2D": "Variant::TRANSFORM2D",
"Plane": "Variant::PLANE",
"AABB": "Variant::AABB",
}

def apply_parttern(template, values):
Expand Down
6 changes: 6 additions & 0 deletions duktape/duktape_binding_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ void DuktapeBindingHelper::duk_push_godot_variant(duk_context *ctx, const Varian
case Variant::PLANE:
ptr = memnew(Plane(var));
break;
case Variant::AABB:
ptr = memnew(AABB(var));
break;
default:
break;
}
Expand Down Expand Up @@ -474,6 +477,9 @@ Variant DuktapeBindingHelper::duk_get_godot_variant(duk_context *ctx, duk_idx_t
case Variant::PLANE:
ret = *(static_cast<Plane*>(ptr));
break;
case Variant::AABB:
ret = *(static_cast<AABB*>(ptr));
break;
default:
break;
}
Expand Down
57 changes: 57 additions & 0 deletions duktape/duktape_builtin_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ duk_ret_t quat_constructor(duk_context *ctx);
void quat_properties(duk_context *ctx);
duk_ret_t plane_constructor(duk_context *ctx);
void plane_properties(duk_context *ctx);
duk_ret_t aabb_constructor(duk_context *ctx);
void aabb_properties(duk_context *ctx);

void DuktapeBindingHelper::register_builtin_classes(duk_context *ctx) {

Expand All @@ -45,6 +47,7 @@ void DuktapeBindingHelper::register_builtin_classes(duk_context *ctx) {
register_builtin_class<Basis>(ctx, basis_constructor, 3, Variant::BASIS, "Basis");
register_builtin_class<Quat>(ctx, quat_constructor, 3, Variant::QUAT, "Quat");
register_builtin_class<Plane>(ctx, plane_constructor, 4, Variant::PLANE, "Plane");
register_builtin_class<AABB>(ctx, aabb_constructor, 4, Variant::AABB, "AABB");

// define properties of builtin classes
register_builtin_class_properties(ctx);
Expand All @@ -61,6 +64,7 @@ void register_builtin_class_properties(duk_context *ctx) {
basis_properties(ctx);
quat_properties(ctx);
plane_properties(ctx);
aabb_properties(ctx);
}

duk_ret_t vector2_constructor(duk_context *ctx) {
Expand Down Expand Up @@ -1189,3 +1193,56 @@ void plane_properties(duk_context *ctx) {
duk_pop(ctx);
}

duk_ret_t aabb_constructor(duk_context *ctx) {
ERR_FAIL_COND_V(!duk_is_constructor_call(ctx), DUK_ERR_SYNTAX_ERROR);

duk_push_this(ctx);

AABB *ptr = NULL;
Variant arg0 = duk_get_variant(ctx, 0);
switch (arg0.get_type()) {
case Variant::VECTOR3: {
Vector3 p1 = arg0;
Vector3 p2 = duk_get_variant(ctx, 1);
ptr = memnew(AABB(p1, p2));
} break;
case Variant::AABB: {
AABB p = arg0;
ptr = memnew(AABB(p));
} break;
default:
ptr = memnew(AABB);
break;
}
ERR_FAIL_NULL_V(ptr, DUK_ERR_TYPE_ERROR);

duk_push_pointer(ctx, ptr);
duk_put_prop_literal(ctx, -2, DUK_HIDDEN_SYMBOL("ptr"));
duk_push_int(ctx, Variant::AABB);
duk_put_prop_literal(ctx, -2, DUK_HIDDEN_SYMBOL("type"));
return DUK_NO_RET_VAL;
}

void aabb_properties(duk_context *ctx) {
duk_push_heapptr(ctx, class_prototypes->get(Variant::AABB));

duk_push_literal(ctx, "end");
duk_c_function func = [](duk_context *ctx) -> duk_ret_t {
duk_int_t argc = duk_get_top(ctx);
duk_push_this(ctx);
AABB *ptr = duk_get_builtin_ptr<AABB>(ctx, -1);
ERR_FAIL_NULL_V(ptr, DUK_ERR_TYPE_ERROR);
if (argc) {
Vector3 arg = duk_get_variant(ctx, 0);
ptr->size = arg - ptr->position;
}
duk_push_variant(ctx, ptr->size + ptr->position);
return DUK_HAS_RET_VAL;
};
duk_push_c_function(ctx, func, 0);
duk_push_c_function(ctx, func, 1);
duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER| DUK_DEFPROP_ENUMERABLE);

duk_pop(ctx);
}

7 changes: 6 additions & 1 deletion generate_builtin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
'Quat',
'RID',
'Transform2D',
'Plane'
'Plane',
'AABB',
]

TYPE_MAP = {
Expand Down Expand Up @@ -160,6 +161,7 @@
"Transform2D": ['x', 'y', 'origin', 'xform', 'xform_inv'],
"Basis": ['x', 'y', 'z'],
"Plane": ['x', 'y', 'z', 'intersects_segment', 'intersects_ray', 'intersect_3'],
"AABB": ['end'],
}

EXTRAL_METHODS = {
Expand Down Expand Up @@ -221,6 +223,9 @@
"Plane": [
METHOD_OP_EQUALS,
METHOD_OP_NEG,
],
"AABB": [
METHOD_OP_EQUALS,
]
}

Expand Down
80 changes: 80 additions & 0 deletions misc/godot.builtin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1863,4 +1863,84 @@ declare module godot {
equals(p_value: Plane): boolean;

}

/** Axis-Aligned Bounding Box.
AABB consists of a position, a size, and several utility functions. It is typically used for fast overlap tests. */
class AABB {

constructor(pos?: Vector3|AABB, size?: Vector3)

/** Beginning corner. */
position: Vector3;

/** Size from position to end. */
size: Vector3;

/** Ending corner. */
end: Vector3;


/** Returns `true` if this `AABB` completely encloses another one. */
encloses(p_with: AABB) : boolean;

/** Returns this `AABB` expanded to include a given point. */
expand(to_point: Vector3) : AABB;

/** Gets the area of the `AABB`. */
get_area() : number;

/** Gets the position of the 8 endpoints of the `AABB` in space. */
get_endpoint(idx: number) : Vector3;

/** Returns the normalized longest axis of the `AABB`. */
get_longest_axis() : Vector3;

/** Returns the index of the longest axis of the `AABB` (according to [Vector3]::AXIS* enum). */
get_longest_axis_index() : number;

/** Returns the scalar length of the longest axis of the `AABB`. */
get_longest_axis_size() : number;

/** Returns the normalized shortest axis of the `AABB`. */
get_shortest_axis() : Vector3;

/** Returns the index of the shortest axis of the `AABB` (according to [Vector3]::AXIS* enum). */
get_shortest_axis_index() : number;

/** Returns the scalar length of the shortest axis of the `AABB`. */
get_shortest_axis_size() : number;

/** Returns the support point in a given direction. This is useful for collision detection algorithms. */
get_support(dir: Vector3) : Vector3;

/** Returns a copy of the `AABB` grown a given amount of units towards all the sides. */
grow(by: number) : AABB;

/** Returns `true` if the `AABB` is flat or empty. */
has_no_area() : boolean;

/** Returns `true` if the `AABB` is empty. */
has_no_surface() : boolean;

/** Returns `true` if the `AABB` contains a point. */
has_point(point: Vector3) : boolean;

/** Returns the intersection between two `AABB`. An empty AABB (size 0,0,0) is returned on failure. */
intersection(p_with: AABB) : AABB;

/** Returns `true` if the `AABB` overlaps with another. */
intersects(p_with: AABB) : boolean;

/** Returns `true` if the `AABB` is on both sides of a plane. */
intersects_plane(plane: Plane) : boolean;

/** Returns `true` if the `AABB` intersects the line segment between `from` and `to`. */
intersects_segment(from: Vector3, to: Vector3) : boolean;

/** Returns a larger AABB that contains this AABB and `with`. */
merge(p_with: AABB) : AABB;

}

}
1 change: 1 addition & 0 deletions tools/editor_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ void ECMAScriptPlugin::_export_typescript_declare_file(const String &p_path) {
ignored_classes.insert("Basis");
ignored_classes.insert("Quat");
ignored_classes.insert("Plane");
ignored_classes.insert("AABB");

String constants = "";

Expand Down

0 comments on commit 240a4e6

Please sign in to comment.