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