Skip to content

Commit 283cfe4

Browse files
committed
Bind end and grow_margin of Rect2
1 parent a32bddd commit 283cfe4

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

misc/godot.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ declare module godot {
421421
size: Vector2;
422422

423423
/** Ending corner. */
424-
// end: Vector2;
424+
end: Vector2;
425425

426426
/** Returns a `Rect2` with equivalent position and area, modified so that the top-left corner is the origin and `width` and `height` are positive. */
427427
abs() : Rect2;
@@ -445,7 +445,7 @@ declare module godot {
445445
grow_individual(left: number, top: number, right: number, bottom: number) : Rect2;
446446

447447
/** Returns a copy of the `Rect2` grown a given amount of units towards the `Margin` direction. */
448-
// grow_margin(margin: number, by: number) : Rect2;
448+
grow_margin(margin: Margin, by: number) : Rect2;
449449

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

quickjs/quickjs_builtin_binder.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,50 @@ void QuickJSBuiltinBinder::bind_builtin_propties_manually() {
694694
binder->get_builtin_binder().register_property(Variant::COLOR, "v", getter, setter, 6);
695695
}
696696

697+
{ // Rect2
698+
JSCFunctionMagic *getter = [](JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) -> JSValue {
699+
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS(ctx, this_val);
700+
const Rect2 *ptr = bind->getRect2();
701+
switch (magic) {
702+
case 0:
703+
return QuickJSBinder::variant_to_var(ctx, ptr->size + ptr->position);
704+
}
705+
return JS_UNDEFINED;
706+
};
707+
JSCFunctionMagic *setter = [](JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) -> JSValue {
708+
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS(ctx, this_val);
709+
Rect2 *ptr = bind->getRect2();
710+
switch (magic) {
711+
case 0:
712+
#ifdef DEBUG_METHODS_ENABLED
713+
ERR_FAIL_COND_V(!QuickJSBinder::validate_type(ctx, Variant::VECTOR2, argv[0]), (JS_ThrowTypeError(ctx, "Vector2 expected for Rect2.end")));
714+
#endif
715+
ptr->size = Vector2(QuickJSBinder::var_to_variant(ctx, argv[0])) - ptr->position;
716+
break;
717+
}
718+
return JS_DupValue(ctx, argv[0]);
719+
};
720+
binder->get_builtin_binder().register_property(Variant::RECT2, "end", getter, setter, 0);
721+
722+
binder->get_builtin_binder().register_method(
723+
Variant::RECT2,
724+
"grow_margin",
725+
[](JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
726+
#ifdef DEBUG_METHODS_ENABLED
727+
ERR_FAIL_COND_V(argc < 2, (JS_ThrowTypeError(ctx, "Two arguments expected for Rect2.grow_margin")));
728+
#endif
729+
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS(ctx, this_val);
730+
Rect2 *ptr = bind->getRect2();
731+
#ifdef DEBUG_METHODS_ENABLED
732+
ERR_FAIL_COND_V(!QuickJSBinder::validate_type(ctx, Variant::INT, argv[0]), (JS_ThrowTypeError(ctx, "number expected for argument 0 of Rect2.grow_margin")));
733+
ERR_FAIL_COND_V(!QuickJSBinder::validate_type(ctx, Variant::REAL, argv[0]), (JS_ThrowTypeError(ctx, "number expected for argument 1 of Rect2.grow_margin")));
734+
#endif
735+
Rect2 ret = ptr->grow_margin(Margin(QuickJSBinder::js_to_int(ctx, argv[0])), QuickJSBinder::js_to_number(ctx, argv[1]));
736+
return QuickJSBinder::variant_to_var(ctx, ret);
737+
},
738+
2);
739+
}
740+
697741
{ // PoolByteArray
698742
// PoolByteArray.prototype.compress
699743
binder->get_builtin_binder().register_method(

0 commit comments

Comments
 (0)