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"
ColorProxy(napi_env env,META_NS::Property<BASE_NS::Color> prop)16 ColorProxy::ColorProxy(napi_env env, META_NS::Property<BASE_NS::Color> prop) : ObjectPropertyProxy(prop)
17 {
18 Create(env, "Color");
19 Hook("r");
20 Hook("g");
21 Hook("b");
22 Hook("a");
23 SyncGet();
24 }
~ColorProxy()25 ColorProxy::~ColorProxy()
26 {
27 Reset();
28 }
UpdateLocalValues()29 void ColorProxy::UpdateLocalValues()
30 {
31 // update local values. (runs in engine thread)
32 value = META_NS::Property<BASE_NS::Color>(prop_)->GetValue();
33 }
UpdateRemoteValues()34 void ColorProxy::UpdateRemoteValues()
35 {
36 // update remote values. (runs in engine thread)
37 META_NS::Property<BASE_NS::Color>(prop_)->SetValue(value);
38 }
SetValue(const BASE_NS::Color & v)39 void ColorProxy::SetValue(const BASE_NS::Color& v)
40 {
41 Lock();
42 if (value != v) {
43 value = v;
44 ScheduleUpdate();
45 }
46 Unlock();
47 }
48
SetMemberValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)49 void ColorProxy::SetMemberValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
50 {
51 NapiApi::FunctionContext<float> info(cb);
52 if (info) {
53 float val = info.Arg<0>();
54 Lock();
55 if ((memb == "r") && (val != value.x)) {
56 value.x = val;
57 ScheduleUpdate();
58 } else if ((memb == "g") && (val != value.y)) {
59 value.y = val;
60 ScheduleUpdate();
61 } else if ((memb == "b") && (val != value.z)) {
62 value.z = val;
63 ScheduleUpdate();
64 } else if ((memb == "a") && (val != value.w)) {
65 value.w = val;
66 ScheduleUpdate();
67 }
68 Unlock();
69 }
70 }
GetMemberValue(const NapiApi::Env info,BASE_NS::string_view memb)71 napi_value ColorProxy::GetMemberValue(const NapiApi::Env info, BASE_NS::string_view memb)
72 {
73 float res;
74 Lock();
75 if (memb == "r") {
76 res = value.x;
77 } else if (memb == "g") {
78 res = value.y;
79 } else if (memb == "b") {
80 res = value.z;
81 } else if (memb == "a") {
82 res = value.w;
83 } else {
84 // invalid member?
85 Unlock();
86 return info.GetUndefined();
87 }
88 Unlock();
89 return info.GetNumber(res);
90 }
SetValue(NapiApi::Object obj)91 void ColorProxy::SetValue(NapiApi::Object obj)
92 {
93 auto r = obj.Get<float>("r");
94 auto g = obj.Get<float>("g");
95 auto b = obj.Get<float>("b");
96 auto a = obj.Get<float>("a");
97 if (r.IsValid() && g.IsValid() && b.IsValid() && a.IsValid()) {
98 SetValue({ r, g, b, a });
99 }
100 }
101
ToNative(NapiApi::Object colorJs,bool & success)102 BASE_NS::Color ColorProxy::ToNative(NapiApi::Object colorJs, bool& success)
103 {
104 auto r = colorJs.Get<float>("r");
105 auto g = colorJs.Get<float>("g");
106 auto b = colorJs.Get<float>("b");
107 auto a = colorJs.Get<float>("a");
108 success = r.IsValid() && g.IsValid() && b.IsValid() && a.IsValid();
109 return BASE_NS::Color { r, g, b, a };
110 }
111
ToNapiObject(BASE_NS::Color color,napi_env env)112 NapiApi::Object ColorProxy::ToNapiObject(BASE_NS::Color color, napi_env env)
113 {
114 auto colorJs = NapiApi::Object(env);
115 colorJs.Set("r", NapiApi::Value<float>(env, color.x));
116 colorJs.Set("g", NapiApi::Value<float>(env, color.y));
117 colorJs.Set("b", NapiApi::Value<float>(env, color.z));
118 colorJs.Set("a", NapiApi::Value<float>(env, color.w));
119 return colorJs;
120 }
121