• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <meta/api/make_callback.h>
17 #include <meta/interface/intf_task_queue.h>
18 #include <meta/interface/intf_task_queue_registry.h>
19 #include <scene/interface/intf_material.h>
20 
21 #include "MaterialJS.h"
22 #include "MaterialPropertyJS.h"
23 
24 using IntfPtr = BASE_NS::shared_ptr<CORE_NS::IInterface>;
25 using IntfWeakPtr = BASE_NS::weak_ptr<CORE_NS::IInterface>;
26 using namespace SCENE_NS;
27 
Init(napi_env env,napi_value exports)28 void MaterialPropertyJS::Init(napi_env env, napi_value exports)
29 {
30     using namespace NapiApi;
31 
32     BASE_NS::vector<napi_property_descriptor> node_props;
33     node_props.push_back(
34         GetSetProperty<Object, MaterialPropertyJS, &MaterialPropertyJS::GetImage, &MaterialPropertyJS::SetImage>(
35             "image"));
36     node_props.push_back(
37         GetSetProperty<Object, MaterialPropertyJS, &MaterialPropertyJS::GetFactor, &MaterialPropertyJS::SetFactor>(
38             "factor"));
39     node_props.push_back(
40         MakeTROMethod<FunctionContext<>, MaterialPropertyJS, &MaterialPropertyJS::Dispose>("destroy"));
41 
42     napi_value func;
43     auto status = napi_define_class(env, "MaterialProperty", NAPI_AUTO_LENGTH, BaseObject::ctor<MaterialPropertyJS>(),
44         nullptr, node_props.size(), node_props.data(), &func);
45 
46     MyInstanceState* mis;
47     GetInstanceData(env, (void**)&mis);
48     mis->StoreCtor("MaterialProperty", func);
49 }
MaterialPropertyJS(napi_env e,napi_callback_info i)50 MaterialPropertyJS::MaterialPropertyJS(napi_env e, napi_callback_info i) : BaseObject<MaterialPropertyJS>(e, i)
51 {
52     NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object> fromJs(e, i);
53     if (!fromJs) {
54         return;
55     }
56 
57     NapiApi::Object meJs(fromJs.This());
58     NapiApi::Object args = fromJs.Arg<1>();
59     if (auto obj = GetNativeObjectParam<META_NS::IObject>(args)) {
60         SetNativeObject(obj, false);
61         StoreJsObj(obj, meJs);
62     }
63 }
~MaterialPropertyJS()64 MaterialPropertyJS::~MaterialPropertyJS()
65 {
66     DisposeNative(nullptr);
67     if (!GetNativeObject()) {
68         return;
69     }
70 }
GetInstanceImpl(uint32_t id)71 void* MaterialPropertyJS::GetInstanceImpl(uint32_t id)
72 {
73     if (id == MaterialPropertyJS::ID)
74         return this;
75     return nullptr;
76 }
Dispose(NapiApi::FunctionContext<> & ctx)77 napi_value MaterialPropertyJS::Dispose(NapiApi::FunctionContext<>& ctx)
78 {
79     DisposeNative(nullptr);
80     return {};
81 }
Finalize(napi_env env)82 void MaterialPropertyJS::Finalize(napi_env env)
83 {
84     DisposeNative(nullptr);
85     BaseObject<MaterialPropertyJS>::Finalize(env);
86 }
DisposeNative(void *)87 void MaterialPropertyJS::DisposeNative(void*)
88 {
89     if (!disposed_) {
90         disposed_ = true;
91         SetNativeObject(nullptr, false);
92         SetNativeObject(nullptr, true);
93     }
94 }
GetImage(NapiApi::FunctionContext<> & ctx)95 napi_value MaterialPropertyJS::GetImage(NapiApi::FunctionContext<>& ctx)
96 {
97     auto texture = interface_pointer_cast<SCENE_NS::ITexture>(GetThisNativeObject(ctx));
98     if (!texture) {
99         return ctx.GetUndefined();
100     }
101 
102     SCENE_NS::IBitmap::Ptr image = META_NS::GetValue(texture->Image());
103     auto obj = interface_pointer_cast<META_NS::IObject>(image);
104 
105     if (auto cached = FetchJsObj(obj)) {
106         return cached.ToNapiValue();
107     }
108     napi_value args[] = { ctx.This().ToNapiValue() };
109     return CreateFromNativeInstance(ctx.GetEnv(), obj, false, BASE_NS::countof(args), args).ToNapiValue();
110 }
SetImage(NapiApi::FunctionContext<NapiApi::Object> & ctx)111 void MaterialPropertyJS::SetImage(NapiApi::FunctionContext<NapiApi::Object>& ctx)
112 {
113     auto texture = interface_pointer_cast<SCENE_NS::ITexture>(GetThisNativeObject(ctx));
114     if (!texture) {
115         return;
116     }
117 
118     NapiApi::Object imageJS = ctx.Arg<0>();
119     if (auto nat = imageJS.Native<TrueRootObject>()) {
120         SCENE_NS::IBitmap::Ptr image = interface_pointer_cast<SCENE_NS::IBitmap>(nat->GetNativeObject());
121         META_NS::SetValue(texture->Image(), image);
122     }
123 }
GetFactor(NapiApi::FunctionContext<> & ctx)124 napi_value MaterialPropertyJS::GetFactor(NapiApi::FunctionContext<>& ctx)
125 {
126     auto texture = interface_pointer_cast<SCENE_NS::ITexture>(GetThisNativeObject(ctx));
127     if (!texture) {
128         return ctx.GetUndefined();
129     }
130 
131     BASE_NS::Math::Vec4 factor = META_NS::GetValue(texture->Factor());
132 
133     NapiApi::Env env(ctx.Env());
134     NapiApi::Object res(env);
135     res.Set("x", NapiApi::Value<float> { env, factor.x });
136     res.Set("y", NapiApi::Value<float> { env, factor.y });
137     res.Set("z", NapiApi::Value<float> { env, factor.z });
138     res.Set("w", NapiApi::Value<float> { env, factor.w });
139     return res.ToNapiValue();
140 }
SetFactor(NapiApi::FunctionContext<NapiApi::Object> & ctx)141 void MaterialPropertyJS::SetFactor(NapiApi::FunctionContext<NapiApi::Object>& ctx)
142 {
143     auto texture = interface_pointer_cast<SCENE_NS::ITexture>(GetThisNativeObject(ctx));
144     if (!texture) {
145         return;
146     }
147 
148     if (NapiApi::Object factorJS = ctx.Arg<0>()) {
149         auto x = factorJS.Get<float>("x");
150         auto y = factorJS.Get<float>("y");
151         auto z = factorJS.Get<float>("z");
152         auto w = factorJS.Get<float>("w");
153         META_NS::SetValue(texture->Factor(), { x, y, z, w });
154     }
155 }
156