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 "QuatProxy.h"
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <napi_api.h>
198bf80f4bSopenharmony_ciQuatProxy::QuatProxy(napi_env env, META_NS::Property<BASE_NS::Math::Quat> prop) : PropertyProxy(prop)
208bf80f4bSopenharmony_ci{
218bf80f4bSopenharmony_ci    Create(env, "Quaternion");
228bf80f4bSopenharmony_ci    Hook("x");
238bf80f4bSopenharmony_ci    Hook("y");
248bf80f4bSopenharmony_ci    Hook("z");
258bf80f4bSopenharmony_ci    Hook("w");
268bf80f4bSopenharmony_ci    SyncGet();
278bf80f4bSopenharmony_ci}
288bf80f4bSopenharmony_ciQuatProxy::~QuatProxy() {}
298bf80f4bSopenharmony_civoid QuatProxy::UpdateLocalValues()
308bf80f4bSopenharmony_ci{
318bf80f4bSopenharmony_ci    // update local values. (runs in engine thread)
328bf80f4bSopenharmony_ci    value = META_NS::Property<BASE_NS::Math::Quat>(prop_)->GetValue();
338bf80f4bSopenharmony_ci}
348bf80f4bSopenharmony_civoid QuatProxy::UpdateRemoteValues()
358bf80f4bSopenharmony_ci{
368bf80f4bSopenharmony_ci    META_NS::Property<BASE_NS::Math::Quat>(prop_)->SetValue(value);
378bf80f4bSopenharmony_ci}
388bf80f4bSopenharmony_civoid QuatProxy::SetValue(const BASE_NS::Math::Quat& v)
398bf80f4bSopenharmony_ci{
408bf80f4bSopenharmony_ci    duh.Lock();
418bf80f4bSopenharmony_ci    if (value != v) {
428bf80f4bSopenharmony_ci        value = v;
438bf80f4bSopenharmony_ci        ScheduleUpdate();
448bf80f4bSopenharmony_ci    }
458bf80f4bSopenharmony_ci    duh.Unlock();
468bf80f4bSopenharmony_ci}
478bf80f4bSopenharmony_civoid QuatProxy::SetValue(NapiApi::FunctionContext<>& cb, BASE_NS::string_view memb)
488bf80f4bSopenharmony_ci{
498bf80f4bSopenharmony_ci    NapiApi::FunctionContext<float> info(cb);
508bf80f4bSopenharmony_ci    float val = info.Arg<0>();
518bf80f4bSopenharmony_ci    duh.Lock();
528bf80f4bSopenharmony_ci    if ((memb == "x") && (val != value.x)) {
538bf80f4bSopenharmony_ci        value.x = val;
548bf80f4bSopenharmony_ci        ScheduleUpdate();
558bf80f4bSopenharmony_ci    } else if ((memb == "y") && (val != value.y)) {
568bf80f4bSopenharmony_ci        value.y = val;
578bf80f4bSopenharmony_ci        ScheduleUpdate();
588bf80f4bSopenharmony_ci    } else if ((memb == "z") && (val != value.z)) {
598bf80f4bSopenharmony_ci        value.z = val;
608bf80f4bSopenharmony_ci        ScheduleUpdate();
618bf80f4bSopenharmony_ci    } else if ((memb == "w") && (val != value.w)) {
628bf80f4bSopenharmony_ci        value.w = val;
638bf80f4bSopenharmony_ci        ScheduleUpdate();
648bf80f4bSopenharmony_ci    }
658bf80f4bSopenharmony_ci    duh.Unlock();
668bf80f4bSopenharmony_ci}
678bf80f4bSopenharmony_cinapi_value QuatProxy::GetValue(NapiApi::FunctionContext<>& info, BASE_NS::string_view memb)
688bf80f4bSopenharmony_ci{
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 if (memb == "w") {
788bf80f4bSopenharmony_ci        res = value.w;
798bf80f4bSopenharmony_ci    } else {
808bf80f4bSopenharmony_ci        // invalid member?
818bf80f4bSopenharmony_ci        duh.Unlock();
828bf80f4bSopenharmony_ci        return {};
838bf80f4bSopenharmony_ci    }
848bf80f4bSopenharmony_ci    duh.Unlock();
858bf80f4bSopenharmony_ci    napi_value value;
868bf80f4bSopenharmony_ci    napi_status status = napi_create_double(info, res, &value);
878bf80f4bSopenharmony_ci    return value;
888bf80f4bSopenharmony_ci}
898bf80f4bSopenharmony_ci
908bf80f4bSopenharmony_cibool QuatProxy::SetValue(NapiApi::Object obj)
918bf80f4bSopenharmony_ci{
928bf80f4bSopenharmony_ci    auto x = obj.Get<float>("x");
938bf80f4bSopenharmony_ci    auto y = obj.Get<float>("y");
948bf80f4bSopenharmony_ci    auto z = obj.Get<float>("z");
958bf80f4bSopenharmony_ci    auto w = obj.Get<float>("w");
968bf80f4bSopenharmony_ci    if (x.IsValid() && y.IsValid() && z.IsValid() && w.IsValid()) {
978bf80f4bSopenharmony_ci        BASE_NS::Math::Quat q { x, y, z, w };
988bf80f4bSopenharmony_ci        SetValue(q);
998bf80f4bSopenharmony_ci        return true;
1008bf80f4bSopenharmony_ci    }
1018bf80f4bSopenharmony_ci    return false;
1028bf80f4bSopenharmony_ci}