Browse Source

add constants

blueloveTH 1 year ago
parent
commit
10f886bb9b
2 changed files with 21 additions and 3 deletions
  1. 9 0
      include/typings/linalg.pyi
  2. 12 3
      src/linalg.cpp

+ 9 - 0
include/typings/linalg.pyi

@@ -5,6 +5,9 @@ class vec2(_StructLike['vec2']):
     x: float
     y: float
 
+    ZERO: 'vec2' = ...
+    ONE: 'vec2' = ...
+
     def __init__(self, x: float, y: float) -> None: ...
     def __add__(self, other: vec2) -> vec2: ...
     def __sub__(self, other: vec2) -> vec2: ...
@@ -47,6 +50,9 @@ class vec3(_StructLike['vec3']):
     y: float
     z: float
 
+    ZERO: 'vec3' = ...
+    ONE: 'vec3' = ...
+
     def __init__(self, x: float, y: float, z: float) -> None: ...
     def __add__(self, other: vec3) -> vec3: ...
     def __sub__(self, other: vec3) -> vec3: ...
@@ -74,6 +80,9 @@ class vec4(_StructLike['vec4']):
     z: float
     w: float
 
+    ZERO: 'vec4' = ...
+    ONE: 'vec4' = ...
+
     def __init__(self, x: float, y: float, z: float, w: float) -> None: ...
     def __add__(self, other: vec4) -> vec4: ...
     def __sub__(self, other: vec4) -> vec4: ...

+ 12 - 3
src/linalg.cpp

@@ -117,6 +117,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
     void Vec2::_register(VM* vm, PyObject* mod, PyObject* type){
         PY_STRUCT_LIKE(Vec2)
 
+        type->attr().set("ZERO", vm->new_user_object<Vec2>(0, 0));
+        type->attr().set("ONE", vm->new_user_object<Vec2>(1, 1));
+
         vm->bind_func(type, __new__, 3, [](VM* vm, ArgsView args){
             float x = CAST_F(args[1]);
             float y = CAST_F(args[2]);
@@ -187,6 +190,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
     void Vec3::_register(VM* vm, PyObject* mod, PyObject* type){
         PY_STRUCT_LIKE(Vec3)
 
+        type->attr().set("ZERO", vm->new_user_object<Vec3>(0, 0, 0));
+        type->attr().set("ONE", vm->new_user_object<Vec3>(1, 1, 1));
+
         vm->bind_func(type, __new__, 4, [](VM* vm, ArgsView args){
             float x = CAST_F(args[1]);
             float y = CAST_F(args[2]);
@@ -222,6 +228,9 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
     void Vec4::_register(VM* vm, PyObject* mod, PyObject* type){
         PY_STRUCT_LIKE(Vec4)
 
+        type->attr().set("ZERO", vm->new_user_object<Vec4>(0, 0, 0, 0));
+        type->attr().set("ONE", vm->new_user_object<Vec4>(1, 1, 1, 1));
+
         vm->bind_func(type, __new__, 5, [](VM* vm, ArgsView args){
             float x = CAST_F(args[1]);
             float y = CAST_F(args[2]);
@@ -431,17 +440,17 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s
         });
 
         // @staticmethod
-        vm->bind(type, "zeros()", [](VM* vm, ArgsView args){
+        vm->bind_func(type, "zeros", 0, [](VM* vm, ArgsView args){
             return vm->new_user_object<Mat3x3>(Mat3x3::zeros());
         }, {}, BindType::STATICMETHOD);
 
         // @staticmethod
-        vm->bind(type, "ones()", [](VM* vm, ArgsView args){
+        vm->bind_func(type, "ones", 0, [](VM* vm, ArgsView args){
             return vm->new_user_object<Mat3x3>(Mat3x3::ones());
         }, {}, BindType::STATICMETHOD);
 
         // @staticmethod
-        vm->bind(type, "identity()", [](VM* vm, ArgsView args){
+        vm->bind_func(type, "identity", 0, [](VM* vm, ArgsView args){
             return vm->new_user_object<Mat3x3>(Mat3x3::identity());
         }, {}, BindType::STATICMETHOD);