• 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 #include "ColorProxy.h"
16 
ColorProxy(napi_env env,META_NS::Property<BASE_NS::Color> prop)17 ColorProxy::ColorProxy(napi_env env, META_NS::Property<BASE_NS::Color> prop) : ObjectPropertyProxy(prop)
18 {
19     isColorType_ = true;
20     Create(env, "Color");
21     Hook("r");
22     Hook("g");
23     Hook("b");
24     Hook("a");
25 }
ColorProxy(napi_env env,META_NS::Property<BASE_NS::Math::Vec4> prop)26 ColorProxy::ColorProxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec4> prop) : ObjectPropertyProxy(prop)
27 {
28     isColorType_ = false;
29     Create(env, "Color");
30     Hook("r");
31     Hook("g");
32     Hook("b");
33     Hook("a");
34 }
~ColorProxy()35 ColorProxy::~ColorProxy()
36 {
37     Reset();
38 }
SetValue(const BASE_NS::Color & v)39 void ColorProxy::SetValue(const BASE_NS::Color& v)
40 {
41     if (isColorType_) {
42         META_NS::SetValue(GetProperty<BASE_NS::Color>(), v);
43     } else {
44         META_NS::SetValue(GetProperty<BASE_NS::Math::Vec4>(), BASE_NS::Math::Vec4(v.r, v.g, v.b, v.a));
45     }
46 }
SetValue(const BASE_NS::Math::Vec4 & v)47 void ColorProxy::SetValue(const BASE_NS::Math::Vec4& v)
48 {
49     if (isColorType_) {
50         META_NS::SetValue(GetProperty<BASE_NS::Color>(), BASE_NS::Color(v.x, v.y, v.z, v.w));
51     } else {
52         META_NS::SetValue(GetProperty<BASE_NS::Math::Vec4>(), v);
53     }
54 }
55 
56 template<class T>
SetColorMemberValue(META_NS::Property<T> & p,BASE_NS::string_view memb,float val)57 auto SetColorMemberValue(META_NS::Property<T>& p, BASE_NS::string_view memb, float val)
58 {
59     auto value = META_NS::GetValue(p);
60 
61     bool changed = false;
62     if ((memb == "r") && (val != value.x)) {
63         value.x = val;
64         changed = true;
65     } else if ((memb == "g") && (val != value.y)) {
66         value.y = val;
67         changed = true;
68     } else if ((memb == "b") && (val != value.z)) {
69         value.z = val;
70         changed = true;
71     } else if ((memb == "a") && (val != value.w)) {
72         value.w = val;
73         changed = true;
74     }
75     if (changed) {
76         META_NS::SetValue(p, value);
77     }
78 }
79 
80 template<class T>
GetColorMemberValue(const NapiApi::Env & info,META_NS::Property<T> & p,BASE_NS::string_view memb,float & value)81 bool GetColorMemberValue(const NapiApi::Env& info, META_NS::Property<T>& p, BASE_NS::string_view memb, float& value)
82 {
83     auto val = META_NS::GetValue(p);
84     if (memb == "r") {
85         value = val.x;
86         return true;
87     }
88     if (memb == "g") {
89         value = val.y;
90         return true;
91     }
92     if (memb == "b") {
93         value = val.z;
94         return true;
95     }
96     if (memb == "a") {
97         value = val.w;
98         return true;
99     }
100     return false;
101 }
102 
SetMemberValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)103 void ColorProxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
104 {
105     NapiApi::FunctionContext<float> info(cb);
106     if (info) {
107         float val = info.Arg<0>();
108         if (isColorType_) {
109             auto p = GetProperty<BASE_NS::Color>();
110             SetColorMemberValue<BASE_NS::Color>(p, memb, val);
111         } else {
112             auto p = GetProperty<BASE_NS::Math::Vec4>();
113             SetColorMemberValue<BASE_NS::Math::Vec4>(p, memb, val);
114         }
115     }
116 }
GetMemberValue(const NapiApi::Env info,BASE_NS::string_view memb)117 napi_value ColorProxy::GetMemberValue(const NapiApi::Env info, BASE_NS::string_view memb)
118 {
119     float res;
120     bool success = false;
121     if (isColorType_) {
122         auto p = GetProperty<BASE_NS::Color>();
123         success = GetColorMemberValue<BASE_NS::Color>(info, p, memb, res);
124     } else {
125         auto p = GetProperty<BASE_NS::Math::Vec4>();
126         success = GetColorMemberValue<BASE_NS::Math::Vec4>(info, p, memb, res);
127     }
128     return success ? info.GetNumber(res) : info.GetUndefined();
129 }
SetValue(NapiApi::Object obj)130 void ColorProxy::SetValue(NapiApi::Object obj)
131 {
132     auto r = obj.Get<float>("r");
133     auto g = obj.Get<float>("g");
134     auto b = obj.Get<float>("b");
135     auto a = obj.Get<float>("a");
136     if (r.IsValid() && g.IsValid() && b.IsValid() && a.IsValid()) {
137         SetValue(BASE_NS::Color(r, g, b, a));
138     }
139 }
140 
ToNative(NapiApi::Object colorJs,bool & success)141 BASE_NS::Color ColorProxy::ToNative(NapiApi::Object colorJs, bool& success)
142 {
143     auto r = colorJs.Get<float>("r");
144     auto g = colorJs.Get<float>("g");
145     auto b = colorJs.Get<float>("b");
146     auto a = colorJs.Get<float>("a");
147     success = r.IsValid() && g.IsValid() && b.IsValid() && a.IsValid();
148     return BASE_NS::Color { r, g, b, a };
149 }
150 
ToNapiObject(BASE_NS::Color color,napi_env env)151 NapiApi::Object ColorProxy::ToNapiObject(BASE_NS::Color color, napi_env env)
152 {
153     auto colorJs = NapiApi::Object(env);
154     colorJs.Set("r", NapiApi::Value<float>(env, color.x));
155     colorJs.Set("g", NapiApi::Value<float>(env, color.y));
156     colorJs.Set("b", NapiApi::Value<float>(env, color.z));
157     colorJs.Set("a", NapiApi::Value<float>(env, color.w));
158     return colorJs;
159 }