• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "Vec3Proxy.h"
Vec3Proxy(napi_env env,META_NS::Property<BASE_NS::Math::Vec3> prop)17 Vec3Proxy::Vec3Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec3> prop) : ObjectPropertyProxy(prop)
18 {
19     // Construct a "Lume::Vec3" instance
20     Create(env, "Vec3");
21     // hook to the objects members (set custom get/set)
22     Hook("x");
23     Hook("y");
24     Hook("z");
25 }
~Vec3Proxy()26 Vec3Proxy::~Vec3Proxy()
27 {
28     Reset();
29 }
SetValue(const BASE_NS::Math::Vec3 & v)30 void Vec3Proxy::SetValue(const BASE_NS::Math::Vec3& v)
31 {
32     META_NS::SetValue(GetProperty<BASE_NS::Math::Vec3>(), v);
33 }
SetMemberValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)34 void Vec3Proxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
35 {
36     // should be executed in the javascript thread.
37     NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
38     if (info) {
39         auto p = GetProperty<BASE_NS::Math::Vec3>();
40         auto value = META_NS::GetValue(p);
41         float val = info.Arg<0>();
42         bool changed = false;
43         if ((memb == "x") && (val != value.x)) {
44             value.x = val;
45             changed = true;
46         } else if ((memb == "y") && (val != value.y)) {
47             value.y = val;
48             changed = true;
49         } else if ((memb == "z") && (val != value.z)) {
50             value.z = val;
51             changed = true;
52         }
53         if (changed) {
54             META_NS::SetValue(p, value);
55         }
56     }
57 }
GetMemberValue(const NapiApi::Env cb,BASE_NS::string_view memb)58 napi_value Vec3Proxy::GetMemberValue(const NapiApi::Env cb, BASE_NS::string_view memb)
59 {
60     // should be executed in the javascript thread.
61     float res;
62     auto value = META_NS::GetValue(GetProperty<BASE_NS::Math::Vec3>());
63     if (memb == "x") {
64         res = value.x;
65     } else if (memb == "y") {
66         res = value.y;
67     } else if (memb == "z") {
68         res = value.z;
69     } else {
70         // invalid member?
71         return cb.GetUndefined();
72     }
73     return cb.GetNumber(res);
74 }
75 
SetValue(NapiApi::Object obj)76 void Vec3Proxy::SetValue(NapiApi::Object obj)
77 {
78     auto x = obj.Get<float>("x");
79     auto y = obj.Get<float>("y");
80     auto z = obj.Get<float>("z");
81     if (x.IsValid() && y.IsValid() && z.IsValid()) {
82         SetValue({ x, y, z });
83     }
84 }
85 
ToNative(NapiApi::Object vec3,bool & success)86 BASE_NS::Math::Vec3 Vec3Proxy::ToNative(NapiApi::Object vec3, bool& success)
87 {
88     auto x = vec3.Get<float>("x");
89     auto y = vec3.Get<float>("y");
90     auto z = vec3.Get<float>("z");
91     success = x.IsValid() && y.IsValid() && z.IsValid();
92     return BASE_NS::Math::Vec3 { x, y, z };
93 }
94 
ToNapiObject(BASE_NS::Math::Vec3 vec3,napi_env env)95 NapiApi::Object Vec3Proxy::ToNapiObject(BASE_NS::Math::Vec3 vec3, napi_env env)
96 {
97     auto screenCoordJs = NapiApi::Object(env);
98     screenCoordJs.Set("x", NapiApi::Value<float>(env, vec3.x));
99     screenCoordJs.Set("y", NapiApi::Value<float>(env, vec3.y));
100     screenCoordJs.Set("z", NapiApi::Value<float>(env, vec3.z));
101     return screenCoordJs;
102 }
103