• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "geometry_definition/CubeJS.h"
17 
18 #include <Vec3Proxy.h>
19 #include <napi_api.h>
20 
21 namespace GeometryDefinition {
22 
CubeJS(const BASE_NS::Math::Vec3 & size)23 CubeJS::CubeJS(const BASE_NS::Math::Vec3& size) : GeometryDefinition(), size_(size) {}
24 
Init(napi_env env,napi_value exports)25 void CubeJS::Init(napi_env env, napi_value exports)
26 {
27     auto ctor = [](napi_env env, napi_callback_info info) -> napi_value {
28         napi_value thisVar = nullptr;
29         napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
30         NapiApi::Object { env, thisVar }.Set("size", NapiApi::Object(env, "Vec3", {}));
31         return thisVar;
32     };
33     auto getType = [](napi_env e, napi_callback_info) { return NapiApi::Env { e }.GetNumber(GeometryType::CUBE); };
34 
35     napi_value undefined;
36     napi_get_undefined(env, &undefined);
37     const auto props = BASE_NS::vector<napi_property_descriptor> {
38         // clang-format off
39         { "geometryType", nullptr, nullptr, getType, nullptr, nullptr,   napi_default_jsproperty, nullptr },
40         { "size",         nullptr, nullptr, nullptr, nullptr, undefined, napi_default_jsproperty, nullptr },
41         // clang-format on
42     };
43     DefineClass(env, exports, "CubeGeometry", props, ctor);
44 }
45 
FromJs(NapiApi::Object & jsDefinition)46 GeometryDefinition* CubeJS::FromJs(NapiApi::Object& jsDefinition)
47 {
48     if (NapiApi::Object jsSize = jsDefinition.Get<NapiApi::Object>("size")) {
49         bool conversionOk = false;
50         const auto size = Vec3Proxy::ToNative(jsSize, conversionOk);
51         if (conversionOk) {
52             return new CubeJS(size);
53         }
54     }
55     LOG_E("Unable to create CubeJS: Invalid JS object given");
56     return {};
57 }
58 
CreateMesh(const SCENE_NS::ICreateMesh::Ptr & creator,const SCENE_NS::MeshConfig & config) const59 SCENE_NS::IMesh::Ptr CubeJS::CreateMesh(
60     const SCENE_NS::ICreateMesh::Ptr& creator, const SCENE_NS::MeshConfig& config) const
61 {
62     return creator->CreateCube(config, size_.x, size_.y, size_.z).GetResult();
63 }
64 
65 } // namespace GeometryDefinition
66