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) : PropertyProxy(prop)
20 {
21     Create(env, "Quaternion");
22     Hook("x");
23     Hook("y");
24     Hook("z");
25     Hook("w");
26     SyncGet();
27 }
~QuatProxy()28 QuatProxy::~QuatProxy() {}
UpdateLocalValues()29 void QuatProxy::UpdateLocalValues()
30 {
31     // update local values. (runs in engine thread)
32     value = META_NS::Property<BASE_NS::Math::Quat>(prop_)->GetValue();
33 }
UpdateRemoteValues()34 void QuatProxy::UpdateRemoteValues()
35 {
36     META_NS::Property<BASE_NS::Math::Quat>(prop_)->SetValue(value);
37 }
SetValue(const BASE_NS::Math::Quat & v)38 void QuatProxy::SetValue(const BASE_NS::Math::Quat& v)
39 {
40     duh.Lock();
41     if (value != v) {
42         value = v;
43         ScheduleUpdate();
44     }
45     duh.Unlock();
46 }
SetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)47 void QuatProxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
48 {
49     NapiApi::FunctionContext<float> info(cb);
50     float val = info.Arg<0>();
51     duh.Lock();
52     if ((memb == "x") && (val != value.x)) {
53         value.x = val;
54         ScheduleUpdate();
55     } else if ((memb == "y") && (val != value.y)) {
56         value.y = val;
57         ScheduleUpdate();
58     } else if ((memb == "z") && (val != value.z)) {
59         value.z = val;
60         ScheduleUpdate();
61     } else if ((memb == "w") && (val != value.w)) {
62         value.w = val;
63         ScheduleUpdate();
64     }
65     duh.Unlock();
66 }
GetValue(NapiApi::FunctionContext<> & info,BASE_NS::string_view memb)67 napi_value QuatProxy::GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
68 {
69     float res;
70     duh.Lock();
71     if (memb == "x") {
72         res = value.x;
73     } else if (memb == "y") {
74         res = value.y;
75     } else if (memb == "z") {
76         res = value.z;
77     } else if (memb == "w") {
78         res = value.w;
79     } else {
80         // invalid member?
81         duh.Unlock();
82         return {};
83     }
84     duh.Unlock();
85     napi_value value;
86     napi_status status = napi_create_double(info, res, &value);
87     return value;
88 }
89 
SetValue(NapiApi::Object obj)90 bool QuatProxy::SetValue(NapiApi::Object obj)
91 {
92     auto x = obj.Get<float>("x");
93     auto y = obj.Get<float>("y");
94     auto z = obj.Get<float>("z");
95     auto w = obj.Get<float>("w");
96     if (x.IsValid() && y.IsValid() && z.IsValid() && w.IsValid()) {
97         BASE_NS::Math::Quat q { x, y, z, w };
98         SetValue(q);
99         return true;
100     }
101     return false;
102 }