• 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 "Vec4Proxy.h"
Vec4Proxy(napi_env env,META_NS::Property<BASE_NS::Math::Vec4> prop)17 Vec4Proxy::Vec4Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec4> prop) : ObjectPropertyProxy(prop)
18 {
19     // Construct a "Lume::Vec4" instance
20     Create(env, "Vec4");
21     // hook to the objects members (set custom get/set)
22     Hook("x");
23     Hook("y");
24     Hook("z");
25     Hook("w");
26 }
~Vec4Proxy()27 Vec4Proxy::~Vec4Proxy()
28 {
29     Reset();
30 }
31 
SetValue(const BASE_NS::Math::Vec4 & v)32 void Vec4Proxy::SetValue(const BASE_NS::Math::Vec4& v)
33 {
34     META_NS::SetValue(GetProperty<BASE_NS::Math::Vec4>(), v);
35 }
SetMemberValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)36 void Vec4Proxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
37 {
38     // should be executed in the javascript thread.
39     NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
40     if (info) {
41         auto p = GetProperty<BASE_NS::Math::Vec4>();
42         auto value = META_NS::GetValue(p);
43         float val = info.Arg<0>();
44         bool changed = false;
45         if ((memb == "x") && (val != value.x)) {
46             value.x = val;
47             changed = true;
48         } else if ((memb == "y") && (val != value.y)) {
49             value.y = val;
50             changed = true;
51         } else if ((memb == "z") && (val != value.z)) {
52             value.z = val;
53             changed = true;
54         } else if ((memb == "w") && (val != value.w)) {
55             value.w = val;
56             changed = true;
57         }
58         if (changed) {
59             META_NS::SetValue(p, value);
60         }
61     }
62 }
GetMemberValue(const NapiApi::Env cb,BASE_NS::string_view memb)63 napi_value Vec4Proxy::GetMemberValue(const NapiApi::Env cb, BASE_NS::string_view memb)
64 {
65     // should be executed in the javascript thread.
66     float res;
67     auto value = META_NS::GetValue(GetProperty<BASE_NS::Math::Vec4>());
68     if (memb == "x") {
69         res = value.x;
70     } else if (memb == "y") {
71         res = value.y;
72     } else if (memb == "z") {
73         res = value.z;
74     } else if (memb == "w") {
75         res = value.w;
76     } else {
77         // invalid member?
78         return cb.GetUndefined();
79     }
80     return cb.GetNumber(res);
81 }
82 
SetValue(NapiApi::Object obj)83 void Vec4Proxy::SetValue(NapiApi::Object obj)
84 {
85     auto x = obj.Get<float>("x");
86     auto y = obj.Get<float>("y");
87     auto z = obj.Get<float>("z");
88     auto w = obj.Get<float>("w");
89     if (x.IsValid() && y.IsValid() && z.IsValid() && w.IsValid()) {
90         SetValue({ x, y, z, w });
91     }
92 }
93