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 "ColorProxy.h"
ColorProxy(napi_env env,META_NS::Property<SCENE_NS::Color> prop)17 ColorProxy::ColorProxy(napi_env env, META_NS::Property<SCENE_NS::Color> prop) : PropertyProxy(prop)
18 {
19 Create(env, "Color");
20 Hook("r");
21 Hook("g");
22 Hook("b");
23 Hook("a");
24 SyncGet();
25 }
~ColorProxy()26 ColorProxy::~ColorProxy() {}
UpdateLocalValues()27 void ColorProxy::UpdateLocalValues()
28 {
29 // update local values. (runs in engine thread)
30 value = META_NS::Property<SCENE_NS::Color>(prop_)->GetValue();
31 }
UpdateRemoteValues()32 void ColorProxy::UpdateRemoteValues()
33 {
34 META_NS::Property<SCENE_NS::Color>(prop_)->SetValue(value);
35 }
SetValue(const SCENE_NS::Color & v)36 void ColorProxy::SetValue(const SCENE_NS::Color& v)
37 {
38 duh.Lock();
39 if (value != v) {
40 value = v;
41 ScheduleUpdate();
42 }
43 duh.Unlock();
44 }
SetValue(NapiApi::FunctionContext<> & cb,BASE_NS::string_view memb)45 void ColorProxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
46 {
47 NapiApi::FunctionContext<float> info(cb);
48 float val = info.Arg<0>();
49 duh.Lock();
50 if ((memb == "r") && (val != value.x)) {
51 value.x = val;
52 ScheduleUpdate();
53 } else if ((memb == "g") && (val != value.y)) {
54 value.y = val;
55 ScheduleUpdate();
56 } else if ((memb == "b") && (val != value.z)) {
57 value.z = val;
58 ScheduleUpdate();
59 } else if ((memb == "a") && (val != value.w)) {
60 value.w = val;
61 ScheduleUpdate();
62 }
63 duh.Unlock();
64 }
GetValue(NapiApi::FunctionContext<> & info,BASE_NS::string_view memb)65 napi_value ColorProxy::GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
66 {
67 float res;
68 duh.Lock();
69 if (memb == "r") {
70 res = value.x;
71 } else if (memb == "g") {
72 res = value.y;
73 } else if (memb == "b") {
74 res = value.z;
75 } else if (memb == "a") {
76 res = value.w;
77 } else {
78 // invalid member?
79 duh.Unlock();
80 return {};
81 }
82 duh.Unlock();
83 napi_value value;
84 napi_status status = napi_create_double(info, res, &value);
85 return value;
86 }
SetValue(NapiApi::Object obj)87 bool ColorProxy::SetValue(NapiApi::Object obj)
88 {
89 auto r = obj.Get<float>("r");
90 auto g = obj.Get<float>("g");
91 auto b = obj.Get<float>("b");
92 auto a = obj.Get<float>("a");
93 if (r.IsValid() && g.IsValid() && b.IsValid() && a.IsValid()) {
94 SCENE_NS::Color c(r, g, b, a);
95 SetValue(c);
96 return true;
97 }
98 return false;
99 }