• 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 }
~Vec2Proxy()26 Vec2Proxy::~Vec2Proxy()
27 {
28     Reset();
29 }
SetValue(const BASE_NS::Math::Vec2 & v)30 void Vec2Proxy::SetValue(const BASE_NS::Math::Vec2& v)
31 {
32     META_NS::SetValue(GetProperty<BASE_NS::Math::Vec2>(), v);
33 }
SetMemberValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)34 void Vec2Proxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
35 {
36     // should be executed in the javascript thread.
37     NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
38     if (info) {
39         auto p = GetProperty<BASE_NS::Math::Vec2>();
40         auto value = META_NS::GetValue(p);
41         float val = info.Arg<0>();
42         bool changed = false;
43         if ((memb == "x") && (val != value.x)) {
44             value.x = val;
45             changed = true;
46         } else if ((memb == "y") && (val != value.y)) {
47             value.y = val;
48             changed = true;
49         }
50         if (changed) {
51             META_NS::SetValue(p, value);
52         }
53     }
54 }
GetMemberValue(const NapiApi::Env cb,BASE_NS::string_view memb)55 napi_value Vec2Proxy::GetMemberValue(const NapiApi::Env cb, BASE_NS::string_view memb)
56 {
57     // should be executed in the javascript thread.
58     float res;
59     auto value = META_NS::GetValue(GetProperty<BASE_NS::Math::Vec2>());
60     if (memb == "x") {
61         res = value.x;
62     } else if (memb == "y") {
63         res = value.y;
64     } else {
65         // invalid member?
66         return cb.GetUndefined();
67     }
68     return cb.GetNumber(res);
69 }
70 
SetValue(NapiApi::Object obj)71 void Vec2Proxy::SetValue(NapiApi::Object obj)
72 {
73     auto x = obj.Get<float>("x");
74     auto y = obj.Get<float>("y");
75     if (x.IsValid() && y.IsValid()) {
76         SetValue({ x, y });
77     }
78 }
79 
ToNative(NapiApi::Object vec2,bool & success)80 BASE_NS::Math::Vec2 Vec2Proxy::ToNative(NapiApi::Object vec2, bool& success)
81 {
82     auto x = vec2.Get<float>("x");
83     auto y = vec2.Get<float>("y");
84     success = x.IsValid() && y.IsValid();
85     return BASE_NS::Math::Vec2 { x, y };
86 }
87 
ToNapiObject(BASE_NS::Math::Vec2 vec2,napi_env env)88 NapiApi::Object Vec2Proxy::ToNapiObject(BASE_NS::Math::Vec2 vec2, napi_env env)
89 {
90     auto screenCoordJs = NapiApi::Object(env);
91     screenCoordJs.Set("x", NapiApi::Value<float>(env, vec2.x));
92     screenCoordJs.Set("y", NapiApi::Value<float>(env, vec2.y));
93     return screenCoordJs;
94 }
95