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) : PropertyProxy(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 SyncGet();
26 }
~Vec3Proxy()27 Vec3Proxy::~Vec3Proxy() {}
UpdateLocalValues()28 void Vec3Proxy::UpdateLocalValues()
29 {
30 // executed in javascript thread (locks handled outside)
31 value = META_NS::Property<BASE_NS::Math::Vec3>(prop_)->GetValue();
32 }
UpdateRemoteValues()33 void Vec3Proxy::UpdateRemoteValues()
34 {
35 // executed in engine thread (locks handled otside)
36 META_NS::Property<BASE_NS::Math::Vec3>(prop_)->SetValue(value);
37 }
SetValue(const BASE_NS::Math::Vec3 & v)38 void Vec3Proxy::SetValue(const BASE_NS::Math::Vec3& v)
39 {
40 // currently executed in engine thread, hence the locks.
41 duh.Lock();
42 if (value != v) {
43 value = v;
44 ScheduleUpdate();
45 }
46 duh.Unlock();
47 }
SetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)48 void Vec3Proxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
49 {
50 // should be executed in the javascript thread.
51 NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
52 float val = info.Arg<0>();
53 duh.Lock();
54 if ((memb == "x") && (val != value.x)) {
55 value.x = val;
56 ScheduleUpdate();
57 } else if ((memb == "y") && (val != value.y)) {
58 value.y = val;
59 ScheduleUpdate();
60 } else if ((memb == "z") && (val != value.z)) {
61 value.z = val;
62 ScheduleUpdate();
63 }
64 duh.Unlock();
65 }
GetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)66 napi_value Vec3Proxy::GetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
67 {
68 // should be executed in the javascript thread.
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 {
78 // invalid member?
79 duh.Unlock();
80 return {};
81 }
82 duh.Unlock();
83 napi_value value;
84 napi_status status = napi_create_double(cb.GetEnv(), res, &value);
85 return value;
86 }
87
SetValue(NapiApi::Object obj)88 bool Vec3Proxy::SetValue(NapiApi::Object obj)
89 {
90 auto x = obj.Get<float>("x");
91 auto y = obj.Get<float>("y");
92 auto z = obj.Get<float>("z");
93 if (x.IsValid() && y.IsValid() && z.IsValid()) {
94 BASE_NS::Math::Vec3 v(x, y, z);
95 SetValue(v);
96 return true;
97 }
98 return false;
99 }