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 #ifndef SCENEPLUGINAPI_LIGHT_H
17 #define SCENEPLUGINAPI_LIGHT_H
18 
19 #include <scene_plugin/api/light_uid.h>
20 #include <scene_plugin/interface/intf_light.h>
21 
22 #include <meta/api/internal/object_api.h>
23 SCENE_BEGIN_NAMESPACE()
24 
25 /**
26  * @brief Light class wraps ILight interface. It keeps the referenced object alive using strong ref.
27  *        The construction of the object is asynchronous, the properties of the engine may not be available
28  *        right after the object instantiation, but OnLoaded() event can be used to observe the state changes.
29  */
30 class Light final : public META_NS::Internal::ObjectInterfaceAPI<Light, ClassId::Light> {
31     META_API(Light)
32     META_API_OBJECT_CONVERTIBLE(ILight)
33     META_API_CACHE_INTERFACE(ILight, Light)
34     META_API_OBJECT_CONVERTIBLE(INode)
35     META_API_CACHE_INTERFACE(INode, Node)
36 public:
37     // From Node
38     META_API_INTERFACE_PROPERTY_CACHED(Node, Name, BASE_NS::string)
39     META_API_INTERFACE_PROPERTY_CACHED(Node, Position, BASE_NS::Math::Vec3)
40     META_API_INTERFACE_PROPERTY_CACHED(Node, Scale, BASE_NS::Math::Vec3)
41     META_API_INTERFACE_PROPERTY_CACHED(Node, Rotation, BASE_NS::Math::Quat)
42     META_API_INTERFACE_PROPERTY_CACHED(Node, Visible, bool)
43     META_API_INTERFACE_PROPERTY_CACHED(Node, LayerMask, uint64_t)
44     META_API_INTERFACE_PROPERTY_CACHED(Node, LocalMatrix, BASE_NS::Math::Mat4X4)
45 
46 public:
47     META_API_INTERFACE_PROPERTY_CACHED(Light, Color, BASE_NS::Color)
48     META_API_INTERFACE_PROPERTY_CACHED(Light, Intensity, float)
49     META_API_INTERFACE_PROPERTY_CACHED(Light, NearPlane, float)
50     META_API_INTERFACE_PROPERTY_CACHED(Light, ShadowEnabled, bool)
51     META_API_INTERFACE_PROPERTY_CACHED(Light, ShadowStrength, float)
52     META_API_INTERFACE_PROPERTY_CACHED(Light, ShadowDepthBias, float)
53     META_API_INTERFACE_PROPERTY_CACHED(Light, ShadowNormalBias, float)
54     META_API_INTERFACE_PROPERTY_CACHED(Light, SpotInnerAngle, float)
55     META_API_INTERFACE_PROPERTY_CACHED(Light, SpotOuterAngle, float)
56     META_API_INTERFACE_PROPERTY_CACHED(Light, Type, uint8_t)
57     META_API_INTERFACE_PROPERTY_CACHED(Light, AdditionalFactor, BASE_NS::Math::Vec4)
58     META_API_INTERFACE_PROPERTY_CACHED(Light, LightLayerMask, uint64_t)
59     META_API_INTERFACE_PROPERTY_CACHED(Light, ShadowLayerMask, uint64_t)
60 
61     /**
62      * @brief Construct Light instance from INode strong pointer.
63      * @param node the object pointed by interface is kept alive
64      */
Light(const INode::Ptr& node)65     explicit Light(const INode::Ptr& node)
66     {
67         Initialize(interface_pointer_cast<META_NS::IObject>(node));
68     }
69 
70     /**
71      * @brief Construct Light instance from ILight strong pointer.
72      * @param node the object pointed by interface is kept alive
73      */
Light(const ILight::Ptr& node)74     explicit Light(const ILight::Ptr& node)
75     {
76         Initialize(interface_pointer_cast<META_NS::IObject>(node));
77     }
78 
79     /**
80      * @brief Gets OnLoaded event reference from INode-interface
81      * @return INode::OnLoaded
82      */
OnLoaded()83     auto OnLoaded()
84     {
85         return META_API_CACHED_INTERFACE(Node)->OnLoaded();
86     }
87 
88     /**
89      * @brief Runs a callback once the light is loaded. If light is already initialized, callback will not run.
90      * @param callback Code to run, if strong reference is passed, it will keep the instance alive
91      *                 causing engine to report memory leak on application exit.
92      * @return reference to this instance of Light.
93      */
94     template<class Callback>
OnLoaded(Callback&& callback)95     auto OnLoaded(Callback&& callback)
96     {
97         OnLoaded()->AddHandler(META_NS::MakeCallback<META_NS::IOnChanged>(callback));
98         return *this;
99     }
100 };
101 
102 SCENE_END_NAMESPACE()
103 
104 #endif // SCENEPLUGINAPI_LIGHT_H
105