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