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 #include "SceneResourceImpl.h"
16 
17 #include <scene_plugin/interface/intf_mesh.h>
18 #include <scene_plugin/interface/intf_node.h>
19 
20 #include "BaseObjectJS.h"
SceneResourceImpl(SceneResourceType type)21 SceneResourceImpl::SceneResourceImpl(SceneResourceType type) : type_(type)
22 {
23     LOG_F("SceneResourceImpl ++");
24 }
~SceneResourceImpl()25 SceneResourceImpl::~SceneResourceImpl()
26 {
27     LOG_F("SceneResourceImpl --");
28 }
29 
RegisterEnums(NapiApi::Object exports)30 void SceneResourceImpl::RegisterEnums(NapiApi::Object exports)
31 {
32     napi_value v;
33     NapiApi::Object SceneResourceType(exports.GetEnv());
34 
35 #define DECL_ENUM(enu, x)                                     \
36     {                                                         \
37         napi_create_uint32(enu.GetEnv(), SceneResourceType::x, &v); \
38         enu.Set(#x, v);                                 \
39     }
40     DECL_ENUM(SceneResourceType, UNKNOWN);
41     DECL_ENUM(SceneResourceType, NODE);
42     DECL_ENUM(SceneResourceType, ENVIRONMENT);
43     DECL_ENUM(SceneResourceType, MATERIAL);
44     DECL_ENUM(SceneResourceType, MESH);
45     DECL_ENUM(SceneResourceType, ANIMATION);
46     DECL_ENUM(SceneResourceType, SHADER);
47     DECL_ENUM(SceneResourceType, IMAGE);
48 #undef DECL_ENUM
49 
50     exports.Set("SceneResourceType", SceneResourceType);
51 }
52 
GetPropertyDescs(BASE_NS::vector<napi_property_descriptor> & props)53 void SceneResourceImpl::GetPropertyDescs(BASE_NS::vector<napi_property_descriptor>& props)
54 {
55     props.push_back(
56         TROGetSetProperty<BASE_NS::string, SceneResourceImpl, &SceneResourceImpl::GetName, &SceneResourceImpl::SetName>(
57             "name"));
58     props.push_back(TROGetProperty<NapiApi::Object, SceneResourceImpl, &SceneResourceImpl::GetUri>("uri"));
59     props.push_back(
60         TROGetProperty<BASE_NS::string, SceneResourceImpl, &SceneResourceImpl::GetObjectType>("resourceType"));
61     props.push_back(
62         MakeTROMethod<NapiApi::FunctionContext<>, SceneResourceImpl, &SceneResourceImpl::Dispose>("destroy"));
63 }
64 
GetInstanceImpl(uint32_t id)65 void* SceneResourceImpl::GetInstanceImpl(uint32_t id)
66 {
67     if (id == SceneResourceImpl::ID) {
68         return this;
69     }
70     return nullptr;
71 }
72 
Dispose(NapiApi::FunctionContext<> & ctx)73 napi_value SceneResourceImpl::Dispose(NapiApi::FunctionContext<>& ctx)
74 {
75     // Dispose of the native object. (makes the js object invalid)
76     if (TrueRootObject* instance = GetThisRootObject(ctx)) {
77         instance->DisposeNative();
78     }
79     uri_.Reset();
80     return ctx.GetUndefined();
81 }
82 
GetObjectType(NapiApi::FunctionContext<> & ctx)83 napi_value SceneResourceImpl::GetObjectType(NapiApi::FunctionContext<>& ctx)
84 {
85     uint32_t type = -1; // return -1 if the resource does not exist anymore
86     if (GetThisNativeObject(ctx)) {
87         type = type_;
88     }
89     napi_value value;
90     napi_status status = napi_create_uint32(ctx, type, &value);
91     return value;
92 }
93 
GetName(NapiApi::FunctionContext<> & ctx)94 napi_value SceneResourceImpl::GetName(NapiApi::FunctionContext<>& ctx)
95 {
96     BASE_NS::string name;
97     if (auto node = interface_pointer_cast<META_NS::INamed>(GetThisNativeObject(ctx))) {
98         ExecSyncTask([node, &name]() {
99             name = node->Name()->GetValue();
100             return META_NS::IAny::Ptr {};
101         });
102     }
103     napi_value value;
104     napi_status status = napi_create_string_utf8(ctx, name.c_str(), name.length(), &value);
105     return value;
106 }
SetName(NapiApi::FunctionContext<BASE_NS::string> & ctx)107 void SceneResourceImpl::SetName(NapiApi::FunctionContext<BASE_NS::string>& ctx)
108 {
109     if (auto node = interface_pointer_cast<META_NS::INamed>(GetThisNativeObject(ctx))) {
110         BASE_NS::string name = ctx.Arg<0>();
111         ExecSyncTask([node, name]() {
112             node->Name()->SetValue(name);
113             return META_NS::IAny::Ptr {};
114         });
115     }
116 }
SetUri(const NapiApi::StrongRef & uri)117 void SceneResourceImpl::SetUri(const NapiApi::StrongRef& uri)
118 {
119     uri_ = uri;
120 }
GetUri(NapiApi::FunctionContext<> & ctx)121 napi_value SceneResourceImpl::GetUri(NapiApi::FunctionContext<>& ctx)
122 {
123     return uri_.GetValue();
124 }