• 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 "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) : ObjectPropertyProxy(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()
28 {
29     Reset();
30 }
UpdateLocalValues()31 void Vec3Proxy::UpdateLocalValues()
32 {
33     // executed in javascript thread (locks handled outside)
34     value = META_NS::Property<BASE_NS::Math::Vec3>(prop_)->GetValue();
35 }
UpdateRemoteValues()36 void Vec3Proxy::UpdateRemoteValues()
37 {
38     // executed in engine thread (locks handled outside)
39     META_NS::Property<BASE_NS::Math::Vec3>(prop_)->SetValue(value);
40 }
SetValue(const BASE_NS::Math::Vec3 & v)41 void Vec3Proxy::SetValue(const BASE_NS::Math::Vec3& 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 Vec3Proxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
51 {
52     // should be executed in the javascript thread.
53     NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
54     if (info) {
55         float val = info.Arg<0>();
56         Lock();
57         if ((memb == "x") && (val != value.x)) {
58             value.x = val;
59             ScheduleUpdate();
60         } else if ((memb == "y") && (val != value.y)) {
61             value.y = val;
62             ScheduleUpdate();
63         } else if ((memb == "z") && (val != value.z)) {
64             value.z = val;
65             ScheduleUpdate();
66         }
67         Unlock();
68     }
69 }
GetMemberValue(const NapiApi::Env cb,BASE_NS::string_view memb)70 napi_value Vec3Proxy::GetMemberValue(const NapiApi::Env cb, BASE_NS::string_view memb)
71 {
72     // should be executed in the javascript thread.
73     float res;
74     Lock();
75     if (memb == "x") {
76         res = value.x;
77     } else if (memb == "y") {
78         res = value.y;
79     } else if (memb == "z") {
80         res = value.z;
81     } else {
82         // invalid member?
83         Unlock();
84         return cb.GetUndefined();
85     }
86     Unlock();
87     return cb.GetNumber(res);
88 }
89 
SetValue(NapiApi::Object obj)90 void Vec3Proxy::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     if (x.IsValid() && y.IsValid() && z.IsValid()) {
96         SetValue({ x, y, z });
97     }
98 }
99 
ToNative(NapiApi::Object vec3,bool & success)100 BASE_NS::Math::Vec3 Vec3Proxy::ToNative(NapiApi::Object vec3, bool& success)
101 {
102     auto x = vec3.Get<float>("x");
103     auto y = vec3.Get<float>("y");
104     auto z = vec3.Get<float>("z");
105     success = x.IsValid() && y.IsValid() && z.IsValid();
106     return BASE_NS::Math::Vec3 { x, y, z };
107 }
108 
ToNapiObject(BASE_NS::Math::Vec3 vec3,napi_env env)109 NapiApi::Object Vec3Proxy::ToNapiObject(BASE_NS::Math::Vec3 vec3, napi_env env)
110 {
111     auto screenCoordJs = NapiApi::Object(env);
112     screenCoordJs.Set("x", NapiApi::Value<float>(env, vec3.x));
113     screenCoordJs.Set("y", NapiApi::Value<float>(env, vec3.y));
114     screenCoordJs.Set("z", NapiApi::Value<float>(env, vec3.z));
115     return screenCoordJs;
116 }
117