-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlightedimageitem.h
152 lines (140 loc) · 5.42 KB
/
lightedimageitem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// The MIT License (MIT)
//
// Copyright (c) 2015 Jocelyn Turcotte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef LIGHTEDIMAGEITEM_H
#define LIGHTEDIMAGEITEM_H
#include <QQmlListProperty>
#include <QQuickItem>
#include <QVector3D>
#include <array>
class LightGroup : public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<QQuickItem> sources READ sourceItems FINAL)
public:
QQmlListProperty<QQuickItem> sourceItems() {
return QQmlListProperty<QQuickItem>(this, &m_sourceItems, sourceAppend, sourceCount, sourceAt, sourceClear);
}
typedef std::array<QVector2D, 5> LightPosArray;
typedef std::array<float, 5> LightIntensityArray;
LightPosArray *lightWorldPositions() { return &m_syncedLightWorldPositions; }
LightIntensityArray *lightIntensities() { return &m_syncedLightIntensities; }
void sync();
signals:
void someLightMoved();
private slots:
void needsUpdate() {
m_dirty = true;
emit someLightMoved();
}
private:
static void sourceAppend(QQmlListProperty<QQuickItem> *p, QQuickItem *v) {
QObject::connect(v, SIGNAL(xChanged()), p->object, SLOT(needsUpdate()));
QObject::connect(v, SIGNAL(yChanged()), p->object, SLOT(needsUpdate()));
reinterpret_cast<QList<QQuickItem *> *>(p->data)->append(v);
}
static int sourceCount(QQmlListProperty<QQuickItem> *p) {
return reinterpret_cast<QList<QQuickItem *> *>(p->data)->count();
}
static QQuickItem *sourceAt(QQmlListProperty<QQuickItem> *p, int idx) {
return reinterpret_cast<QList<QQuickItem *> *>(p->data)->at(idx);
}
static void sourceClear(QQmlListProperty<QQuickItem> *p) {
auto list = reinterpret_cast<QList<QQuickItem *> *>(p->data);
for (auto i : *list)
i->disconnect(p->object);
auto o = static_cast<LightGroup*>(p->object);
o->m_dirty = true;
emit o->someLightMoved();
return list->clear();
}
bool m_dirty = true;
QList<QQuickItem *> m_sourceItems;
LightPosArray m_syncedLightWorldPositions;
LightIntensityArray m_syncedLightIntensities;
};
class LightedImageItem : public QQuickItem {
Q_OBJECT
Q_PROPERTY(LightGroup *lightSources READ lightSources WRITE setLightSources NOTIFY lightSourcesChanged FINAL)
Q_PROPERTY(QUrl sourceImage READ sourceImage WRITE setSourceImage NOTIFY sourceImageChanged FINAL)
Q_PROPERTY(QUrl normalsImage READ normalsImage WRITE setNormalsImage NOTIFY normalsImageChanged FINAL)
Q_PROPERTY(float hRepeat READ hRepeat WRITE setHRepeat NOTIFY hRepeatChanged FINAL)
Q_PROPERTY(float vRepeat READ vRepeat WRITE setVRepeat NOTIFY vRepeatChanged FINAL)
public:
LightedImageItem();
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) override;
LightGroup *lightSources() const { return m_lightSources; }
void setLightSources(LightGroup *value) {
if (m_lightSources != value) {
if (m_lightSources)
m_lightSources->disconnect(this);
m_lightSources = value;
if (m_lightSources)
connect(m_lightSources, SIGNAL(someLightMoved()), SLOT(update()));
emit lightSourcesChanged();
update();
}
}
QUrl sourceImage() const { return m_sourceImage; }
void setSourceImage(QUrl value) {
if (m_sourceImage != value) {
m_sourceImage = value;
emit sourceImageChanged();
update();
}
}
QUrl normalsImage() const { return m_normalsImage; }
void setNormalsImage(QUrl value) {
if (m_normalsImage != value) {
m_normalsImage = value;
emit normalsImageChanged();
update();
}
}
float hRepeat() const { return m_hRepeat; }
void setHRepeat(float value) {
if (m_hRepeat != value) {
m_hRepeat = value;
emit hRepeatChanged();
update();
}
}
float vRepeat() const { return m_vRepeat; }
void setVRepeat(float value) {
if (m_vRepeat != value) {
m_vRepeat = value;
emit vRepeatChanged();
update();
}
}
signals:
void lightSourcesChanged();
void sourceImageChanged();
void normalsImageChanged();
void hRepeatChanged();
void vRepeatChanged();
private:
LightGroup *m_lightSources = nullptr;
QUrl m_sourceImage;
QUrl m_normalsImage;
float m_hRepeat = 1;
float m_vRepeat = 1;
};
#endif