1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt3D module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qabstractlight.h"
41 #include "qabstractlight_p.h"
42 
43 QT_BEGIN_NAMESPACE
44 
45 namespace Qt3DRender
46 {
47 
48 /*!
49  * \qmltype Light
50  * \inqmlmodule Qt3D.Render
51  * \instantiates Qt3DRender::QAbstractLight
52  * \brief Encapsulate a QAbstractLight object in a Qt 3D scene.
53  * \since 5.6
54  */
55 
QAbstractLightPrivate(QAbstractLight::Type type)56 QAbstractLightPrivate::QAbstractLightPrivate(QAbstractLight::Type type)
57     : m_type(type)
58     , m_shaderData(new QShaderData)
59 {
60     m_shaderData->setProperty("type", type);
61     m_shaderData->setProperty("color", QColor(Qt::white));
62     m_shaderData->setProperty("intensity", 0.5f);
63 }
64 
~QAbstractLightPrivate()65 QAbstractLightPrivate::~QAbstractLightPrivate()
66 {
67 }
68 
69 /*!
70     \qmlproperty enumeration Qt3D.Render::Light::type
71     \readonly
72 
73     Holds the particular type of light.
74 
75     \value Light.PointLight
76            A point light
77     \value Light.DirectionalLight
78            A directional light
79     \value Light.SpotLight
80            a spot light
81 */
82 /*!
83     \property Qt3DRender::QAbstractLight::type
84 
85     The type of light.
86 */
87 /*!
88     \enum Qt3DRender::QAbstractLight::Type
89 
90     Identifies the particular type of light.
91 
92     \value PointLight
93     \value DirectionalLight
94     \value SpotLight
95 */
createNodeCreationChange() const96 Qt3DCore::QNodeCreatedChangeBasePtr QAbstractLight::createNodeCreationChange() const
97 {
98     auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QAbstractLightData>::create(this);
99     auto &data = creationChange->data;
100     Q_D(const QAbstractLight);
101     data.shaderDataId = qIdForNode(d->m_shaderData);
102     return creationChange;
103 }
104 
105 /*!
106     \class Qt3DRender::QAbstractLight
107     \inmodule Qt3DRender
108     \brief Encapsulate a QAbstractLight object in a Qt 3D scene.
109     \since 5.6
110 */
111 
112 /*! \internal */
QAbstractLight(QAbstractLightPrivate & dd,QNode * parent)113 QAbstractLight::QAbstractLight(QAbstractLightPrivate &dd, QNode *parent)
114     : QComponent(dd, parent)
115 {
116     Q_D(QAbstractLight);
117     d->m_shaderData->setParent(this);
118 }
119 
~QAbstractLight()120 QAbstractLight::~QAbstractLight()
121 {
122 }
123 
124 /*!
125     Holds the current QAbstractLight type.
126 */
type() const127 QAbstractLight::Type QAbstractLight::type() const
128 {
129     Q_D(const QAbstractLight);
130     return d->m_type;
131 }
132 
133 /*!
134  *  \qmlproperty QColor Qt3D.Render.Light::color
135  *
136  *  Holds the current Light color.
137  */
138 /*!
139  *  \property Qt3DRender::QAbstractLight::color
140  *
141  * Holds the current QAbstractLight color.
142  */
color() const143 QColor QAbstractLight::color() const
144 {
145     Q_D(const QAbstractLight);
146     return d->m_shaderData->property("color").value<QColor>();
147 }
148 
setColor(const QColor & c)149 void QAbstractLight::setColor(const QColor &c)
150 {
151     Q_D(QAbstractLight);
152     if (color() != c) {
153         d->m_shaderData->setProperty("color", c);
154         emit colorChanged(c);
155     }
156 }
157 
158 /*!
159     \qmlproperty float Qt3D.Render.Light::intensity
160 
161     Holds the current Light intensity.
162 */
163 /*!
164     \property Qt3DRender::QAbstractLight::intensity
165 
166     Holds the current QAbstractLight intensity.
167 */
intensity() const168 float QAbstractLight::intensity() const
169 {
170     Q_D(const QAbstractLight);
171     return d->m_shaderData->property("intensity").toFloat();
172 }
173 
setIntensity(float value)174 void QAbstractLight::setIntensity(float value)
175 {
176     Q_D(QAbstractLight);
177     if (intensity() != value) {
178         d->m_shaderData->setProperty("intensity", value);
179         emit intensityChanged(value);
180     }
181 }
182 
183 } // namespace Qt3DRender
184 
185 QT_END_NAMESPACE
186