18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci#include "Vec4Proxy.h" 168bf80f4bSopenharmony_ciVec4Proxy::Vec4Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec4> prop) : PropertyProxy(prop) 178bf80f4bSopenharmony_ci{ 188bf80f4bSopenharmony_ci name_ = prop->GetName(); 198bf80f4bSopenharmony_ci // Construct a "Lume::Vec4" instance 208bf80f4bSopenharmony_ci Create(env, "Vec4"); 218bf80f4bSopenharmony_ci // hook to the objects members (set custom get/set) 228bf80f4bSopenharmony_ci Hook("x"); 238bf80f4bSopenharmony_ci Hook("y"); 248bf80f4bSopenharmony_ci Hook("z"); 258bf80f4bSopenharmony_ci Hook("w"); 268bf80f4bSopenharmony_ci SyncGet(); 278bf80f4bSopenharmony_ci} 288bf80f4bSopenharmony_ciVec4Proxy::~Vec4Proxy() 298bf80f4bSopenharmony_ci{ 308bf80f4bSopenharmony_ci} 318bf80f4bSopenharmony_civoid Vec4Proxy::UpdateLocalValues() 328bf80f4bSopenharmony_ci{ 338bf80f4bSopenharmony_ci // executed in javascript thread (locks handled outside) 348bf80f4bSopenharmony_ci value = META_NS::Property<BASE_NS::Math::Vec4>(prop_)->GetValue(); 358bf80f4bSopenharmony_ci} 368bf80f4bSopenharmony_civoid Vec4Proxy::UpdateRemoteValues() 378bf80f4bSopenharmony_ci{ 388bf80f4bSopenharmony_ci // executed in engine thread (locks handled outside) 398bf80f4bSopenharmony_ci META_NS::Property<BASE_NS::Math::Vec4>(prop_)->SetValue(value); 408bf80f4bSopenharmony_ci} 418bf80f4bSopenharmony_civoid Vec4Proxy::SetValue(const BASE_NS::Math::Vec4& v) 428bf80f4bSopenharmony_ci{ 438bf80f4bSopenharmony_ci // currently executed in engine thread, hence the locks. 448bf80f4bSopenharmony_ci duh.Lock(); 458bf80f4bSopenharmony_ci if (value != v) { 468bf80f4bSopenharmony_ci value = v; 478bf80f4bSopenharmony_ci ScheduleUpdate(); 488bf80f4bSopenharmony_ci } 498bf80f4bSopenharmony_ci duh.Unlock(); 508bf80f4bSopenharmony_ci} 518bf80f4bSopenharmony_civoid Vec4Proxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb) 528bf80f4bSopenharmony_ci{ 538bf80f4bSopenharmony_ci // should be executed in the javascript thread. 548bf80f4bSopenharmony_ci NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo()); 558bf80f4bSopenharmony_ci float val = info.Arg<0>(); 568bf80f4bSopenharmony_ci duh.Lock(); 578bf80f4bSopenharmony_ci if ((memb == "x") && (val != value.x)) { 588bf80f4bSopenharmony_ci value.x = val; 598bf80f4bSopenharmony_ci ScheduleUpdate(); 608bf80f4bSopenharmony_ci } else if ((memb == "y") && (val != value.y)) { 618bf80f4bSopenharmony_ci value.y = val; 628bf80f4bSopenharmony_ci ScheduleUpdate(); 638bf80f4bSopenharmony_ci } else if ((memb == "z") && (val != value.z)) { 648bf80f4bSopenharmony_ci value.z = val; 658bf80f4bSopenharmony_ci ScheduleUpdate(); 668bf80f4bSopenharmony_ci } else if ((memb == "w") && (val != value.w)) { 678bf80f4bSopenharmony_ci value.w = val; 688bf80f4bSopenharmony_ci ScheduleUpdate(); 698bf80f4bSopenharmony_ci } 708bf80f4bSopenharmony_ci duh.Unlock(); 718bf80f4bSopenharmony_ci} 728bf80f4bSopenharmony_cinapi_value Vec4Proxy::GetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb) 738bf80f4bSopenharmony_ci{ 748bf80f4bSopenharmony_ci // should be executed in the javascript thread. 758bf80f4bSopenharmony_ci float res; 768bf80f4bSopenharmony_ci duh.Lock(); 778bf80f4bSopenharmony_ci if (memb == "x") { 788bf80f4bSopenharmony_ci res = value.x; 798bf80f4bSopenharmony_ci } else if (memb == "y") { 808bf80f4bSopenharmony_ci res = value.y; 818bf80f4bSopenharmony_ci } else if (memb == "z") { 828bf80f4bSopenharmony_ci res = value.z; 838bf80f4bSopenharmony_ci } else if (memb == "w") { 848bf80f4bSopenharmony_ci res = value.w; 858bf80f4bSopenharmony_ci } else { 868bf80f4bSopenharmony_ci // invalid member? 878bf80f4bSopenharmony_ci duh.Unlock(); 888bf80f4bSopenharmony_ci return {}; 898bf80f4bSopenharmony_ci } 908bf80f4bSopenharmony_ci duh.Unlock(); 918bf80f4bSopenharmony_ci napi_value value; 928bf80f4bSopenharmony_ci napi_status status = napi_create_double(cb.GetEnv(), res, &value); 938bf80f4bSopenharmony_ci return value; 948bf80f4bSopenharmony_ci} 958bf80f4bSopenharmony_ci 968bf80f4bSopenharmony_cibool Vec4Proxy::SetValue(NapiApi::Object obj) 978bf80f4bSopenharmony_ci{ 988bf80f4bSopenharmony_ci auto x = obj.Get<float>("x"); 998bf80f4bSopenharmony_ci auto y = obj.Get<float>("y"); 1008bf80f4bSopenharmony_ci auto z = obj.Get<float>("z"); 1018bf80f4bSopenharmony_ci auto w = obj.Get<float>("w"); 1028bf80f4bSopenharmony_ci if (x.IsValid() && y.IsValid() && z.IsValid() && w.IsValid()) { 1038bf80f4bSopenharmony_ci BASE_NS::Math::Vec4 v { x, y, z, w }; 1048bf80f4bSopenharmony_ci SetValue(v); 1058bf80f4bSopenharmony_ci return true; 1068bf80f4bSopenharmony_ci } 1078bf80f4bSopenharmony_ci return false; 1088bf80f4bSopenharmony_ci} 109