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
168bf80f4bSopenharmony_ci#include "Vec3Proxy.h"
178bf80f4bSopenharmony_ciVec3Proxy::Vec3Proxy(napi_env env, META_NS::Property<BASE_NS::Math::Vec3> prop) : PropertyProxy(prop)
188bf80f4bSopenharmony_ci{
198bf80f4bSopenharmony_ci    // Construct a "Lume::Vec3" instance
208bf80f4bSopenharmony_ci    Create(env, "Vec3");
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    SyncGet();
268bf80f4bSopenharmony_ci}
278bf80f4bSopenharmony_ciVec3Proxy::~Vec3Proxy() {}
288bf80f4bSopenharmony_civoid Vec3Proxy::UpdateLocalValues()
298bf80f4bSopenharmony_ci{
308bf80f4bSopenharmony_ci    // executed in javascript thread (locks handled outside)
318bf80f4bSopenharmony_ci    value = META_NS::Property<BASE_NS::Math::Vec3>(prop_)->GetValue();
328bf80f4bSopenharmony_ci}
338bf80f4bSopenharmony_civoid Vec3Proxy::UpdateRemoteValues()
348bf80f4bSopenharmony_ci{
358bf80f4bSopenharmony_ci    // executed in engine thread (locks handled otside)
368bf80f4bSopenharmony_ci    META_NS::Property<BASE_NS::Math::Vec3>(prop_)->SetValue(value);
378bf80f4bSopenharmony_ci}
388bf80f4bSopenharmony_civoid Vec3Proxy::SetValue(const BASE_NS::Math::Vec3& v)
398bf80f4bSopenharmony_ci{
408bf80f4bSopenharmony_ci    // currently executed in engine thread, hence the locks.
418bf80f4bSopenharmony_ci    duh.Lock();
428bf80f4bSopenharmony_ci    if (value != v) {
438bf80f4bSopenharmony_ci        value = v;
448bf80f4bSopenharmony_ci        ScheduleUpdate();
458bf80f4bSopenharmony_ci    }
468bf80f4bSopenharmony_ci    duh.Unlock();
478bf80f4bSopenharmony_ci}
488bf80f4bSopenharmony_civoid Vec3Proxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
498bf80f4bSopenharmony_ci{
508bf80f4bSopenharmony_ci    // should be executed in the javascript thread.
518bf80f4bSopenharmony_ci    NapiApi::FunctionContext<float> info(cb.GetEnv(), cb.GetInfo());
528bf80f4bSopenharmony_ci    float val = info.Arg<0>();
538bf80f4bSopenharmony_ci    duh.Lock();
548bf80f4bSopenharmony_ci    if ((memb == "x") && (val != value.x)) {
558bf80f4bSopenharmony_ci        value.x = val;
568bf80f4bSopenharmony_ci        ScheduleUpdate();
578bf80f4bSopenharmony_ci    } else if ((memb == "y") && (val != value.y)) {
588bf80f4bSopenharmony_ci        value.y = val;
598bf80f4bSopenharmony_ci        ScheduleUpdate();
608bf80f4bSopenharmony_ci    } else if ((memb == "z") && (val != value.z)) {
618bf80f4bSopenharmony_ci        value.z = val;
628bf80f4bSopenharmony_ci        ScheduleUpdate();
638bf80f4bSopenharmony_ci    }
648bf80f4bSopenharmony_ci    duh.Unlock();
658bf80f4bSopenharmony_ci}
668bf80f4bSopenharmony_cinapi_value Vec3Proxy::GetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
678bf80f4bSopenharmony_ci{
688bf80f4bSopenharmony_ci    // should be executed in the javascript thread.
698bf80f4bSopenharmony_ci    float res;
708bf80f4bSopenharmony_ci    duh.Lock();
718bf80f4bSopenharmony_ci    if (memb == "x") {
728bf80f4bSopenharmony_ci        res = value.x;
738bf80f4bSopenharmony_ci    } else if (memb == "y") {
748bf80f4bSopenharmony_ci        res = value.y;
758bf80f4bSopenharmony_ci    } else if (memb == "z") {
768bf80f4bSopenharmony_ci        res = value.z;
778bf80f4bSopenharmony_ci    } else {
788bf80f4bSopenharmony_ci        // invalid member?
798bf80f4bSopenharmony_ci        duh.Unlock();
808bf80f4bSopenharmony_ci        return {};
818bf80f4bSopenharmony_ci    }
828bf80f4bSopenharmony_ci    duh.Unlock();
838bf80f4bSopenharmony_ci    napi_value value;
848bf80f4bSopenharmony_ci    napi_status status = napi_create_double(cb.GetEnv(), res, &value);
858bf80f4bSopenharmony_ci    return value;
868bf80f4bSopenharmony_ci}
878bf80f4bSopenharmony_ci
888bf80f4bSopenharmony_cibool Vec3Proxy::SetValue(NapiApi::Object obj)
898bf80f4bSopenharmony_ci{
908bf80f4bSopenharmony_ci    auto x = obj.Get<float>("x");
918bf80f4bSopenharmony_ci    auto y = obj.Get<float>("y");
928bf80f4bSopenharmony_ci    auto z = obj.Get<float>("z");
938bf80f4bSopenharmony_ci    if (x.IsValid() && y.IsValid() && z.IsValid()) {
948bf80f4bSopenharmony_ci        BASE_NS::Math::Vec3 v(x, y, z);
958bf80f4bSopenharmony_ci        SetValue(v);
968bf80f4bSopenharmony_ci        return true;
978bf80f4bSopenharmony_ci    }
988bf80f4bSopenharmony_ci    return false;
998bf80f4bSopenharmony_ci}