• 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 "Vec2Proxy.h"
17 
Vec2Proxy(napi_env env,META_NS::Property<BASE_NS::Math::Vec2> prop)18 Vec2Proxy::Vec2Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec2> prop) : ObjectPropertyProxy(prop)
19 {
20     // Construct a "Lume::Vec2" instance
21     Create(env, "Vec2");
22     // hook to the objects members (set custom get/set)
23     Hook("x");
24     Hook("y");
25     SyncGet();
26 }
~Vec2Proxy()27 Vec2Proxy::~Vec2Proxy()
28 {
29     Reset();
30 }
UpdateLocalValues()31 void Vec2Proxy::UpdateLocalValues()
32 {
33     // executed in javascript thread (locks handled outside)
34     value = META_NS::Property<BASE_NS::Math::Vec2>(prop_)->GetValue();
35 }
UpdateRemoteValues()36 void Vec2Proxy::UpdateRemoteValues()
37 {
38     // executed in engine thread (locks handled outside)
39     META_NS::Property<BASE_NS::Math::Vec2>(prop_)->SetValue(value);
40 }
SetValue(const BASE_NS::Math::Vec2 & v)41 void Vec2Proxy::SetValue(const BASE_NS::Math::Vec2& 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 Vec2Proxy::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         }
64         Unlock();
65     }
66 }
GetMemberValue(const NapiApi::Env cb,BASE_NS::string_view memb)67 napi_value Vec2Proxy::GetMemberValue(const NapiApi::Env cb, BASE_NS::string_view memb)
68 {
69     // should be executed in the javascript thread.
70     float res;
71     Lock();
72     if (memb == "x") {
73         res = value.x;
74     } else if (memb == "y") {
75         res = value.y;
76     } else {
77         // invalid member?
78         Unlock();
79         return cb.GetUndefined();
80     }
81     Unlock();
82     return cb.GetNumber(res);
83 }
84 
SetValue(NapiApi::Object obj)85 void Vec2Proxy::SetValue(NapiApi::Object obj)
86 {
87     auto x = obj.Get<float>("x");
88     auto y = obj.Get<float>("y");
89     if (x.IsValid() && y.IsValid()) {
90         SetValue({ x, y });
91     }
92 }
93 
ToNative(NapiApi::Object vec2,bool & success)94 BASE_NS::Math::Vec2 Vec2Proxy::ToNative(NapiApi::Object vec2, bool& success)
95 {
96     auto x = vec2.Get<float>("x");
97     auto y = vec2.Get<float>("y");
98     success = x.IsValid() && y.IsValid();
99     return BASE_NS::Math::Vec2 { x, y };
100 }
101 
ToNapiObject(BASE_NS::Math::Vec2 vec2,napi_env env)102 NapiApi::Object Vec2Proxy::ToNapiObject(BASE_NS::Math::Vec2 vec2, napi_env env)
103 {
104     auto screenCoordJs = NapiApi::Object(env);
105     screenCoordJs.Set("x", NapiApi::Value<float>(env, vec2.x));
106     screenCoordJs.Set("y", NapiApi::Value<float>(env, vec2.y));
107     return screenCoordJs;
108 }
109