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