1 
2 /*
3  * Copyright (C) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Vec2Proxy.h"
18 
Vec2Proxy(napi_env env,META_NS::Property<BASE_NS::Math::Vec2> prop)19 Vec2Proxy::Vec2Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec2> prop) : PropertyProxy(prop)
20 {
21     // Construct a "Lume::Vec2" instance
22     Create(env, "Vec2");
23     // hook to the objects members (set custom get/set)
24     Hook("x");
25     Hook("y");
26     SyncGet();
27 }
~Vec2Proxy()28 Vec2Proxy::~Vec2Proxy() {}
UpdateLocalValues()29 void Vec2Proxy::UpdateLocalValues()
30 {
31     // executed in javascript thread (locks handled outside)
32     value = META_NS::Property<BASE_NS::Math::Vec2>(prop_)->GetValue();
33 }
UpdateRemoteValues()34 void Vec2Proxy::UpdateRemoteValues()
35 {
36     // executed in engine thread (locks handled outside)
37     META_NS::Property<BASE_NS::Math::Vec2>(prop_)->SetValue(value);
38 }
SetValue(const BASE_NS::Math::Vec2 & v)39 void Vec2Proxy::SetValue(const BASE_NS::Math::Vec2& v)
40 {
41     // currently executed in engine thread, hence the locks.
42     duh.Lock();
43     if (value != v) {
44         value = v;
45         ScheduleUpdate();
46     }
47     duh.Unlock();
48 }
SetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)49 void Vec2Proxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
50 {
51     // should be executed in the javascript thread.
52     NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
53     float val = info.Arg<0>();
54     duh.Lock();
55     if ((memb == "x") && (val != value.x)) {
56         value.x = val;
57         ScheduleUpdate();
58     } else if ((memb == "y") && (val != value.y)) {
59         value.y = val;
60         ScheduleUpdate();
61     }
62     duh.Unlock();
63 }
GetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)64 napi_value Vec2Proxy::GetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
65 {
66     // should be executed in the javascript thread.
67     float res;
68     duh.Lock();
69     if (memb == "x") {
70         res = value.x;
71     } else if (memb == "y") {
72         res = value.y;
73     } else {
74         // invalid member?
75         duh.Unlock();
76         return {};
77     }
78     duh.Unlock();
79     napi_value value;
80     napi_status status = napi_create_double(cb.GetEnv(), res, &value);
81     return value;
82 }
83 
SetValue(NapiApi::Object obj)84 bool Vec2Proxy::SetValue(NapiApi::Object obj)
85 {
86     auto x = obj.Get<float>("x");
87     auto y = obj.Get<float>("y");
88     if (x.IsValid() && y.IsValid()) {
89         BASE_NS::Math::Vec2 v(x, y);
90         SetValue(v);
91         return true;
92     }
93     return false;
94 }