-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathimage.cpp
More file actions
259 lines (241 loc) · 6.36 KB
/
image.cpp
File metadata and controls
259 lines (241 loc) · 6.36 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "components/image.h"
#include "components/recttransform.h"
#include <resources/mesh.h>
#include <resources/material.h>
#include <resources/sprite.h>
#include <components/actor.h>
#include <components/spriterender.h>
#include <commandbuffer.h>
namespace {
const char *gMaterial = "Material";
const char *gBasemap = "BaseMap";
const char *gOverride = "texture0";
const char *gColor = "color0";
const char *gDefaultSprite = ".embedded/DefaultSprite.mtl";
};
static hash<string> hash_str;
/*!
\class Image
\brief The Image class represents an image or sprite that can be drawn on the screen.
\inmodule Gui
*/
Image::Image() :
m_color(1.0f),
m_mesh(Engine::objectCreate<Mesh>("")),
m_material(nullptr),
m_customMaterial(nullptr),
m_sheet(nullptr),
m_hash(0),
m_drawMode(Sliced) {
Material *material = Engine::loadResource<Material>(gDefaultSprite);
if(material) {
m_material = material->createInstance();
m_material->setVector4(gColor, &m_color);
}
}
Image::~Image() {
delete m_material;
}
/*!
\internal
*/
void Image::draw(CommandBuffer &buffer) {
if(m_mesh) {
Vector3Vector &verts = m_mesh->vertices();
if(!verts.empty()) {
Transform *t = actor()->transform();
if(t) {
Matrix4 mat(t->worldTransform());
mat[12] -= verts[0].x;
mat[13] -= verts[0].y;
buffer.setObjectId(actor()->uuid());
buffer.setMaterialId((m_customMaterial) ? m_customMaterial->material()->uuid() : m_material->material()->uuid());
buffer.drawMesh(mat, m_mesh, 0, CommandBuffer::UI, (m_customMaterial) ? m_customMaterial : m_material);
}
}
}
}
/*!
Returns an instantiated Material assigned to Image.
*/
Material *Image::material() const {
if(m_customMaterial) {
return m_customMaterial->material();
}
return nullptr;
}
/*!
Creates a new instance of \a material and assigns it.
*/
void Image::setMaterial(Material *material) {
if(!m_customMaterial || m_customMaterial->material() != material) {
if(m_customMaterial) {
delete m_customMaterial;
m_customMaterial = nullptr;
}
if(material) {
m_customMaterial = material->createInstance();
m_customMaterial->setVector4(gColor, &m_color);
if(m_sheet) {
m_customMaterial->setTexture(gOverride, m_sheet->page());
}
}
}
}
/*!
Returns the sprite assigned to the Image.
*/
Sprite *Image::sprite() const {
return m_sheet;
}
/*!
Replaces the current sprite \a sheet with a new one.
*/
void Image::setSprite(Sprite *sheet) {
if(m_sheet) {
m_sheet->unsubscribe(this);
}
m_sheet = sheet;
if(m_sheet) {
m_sheet->subscribe(&Image::spriteUpdated, this);
composeMesh();
setTexture(m_sheet->page());
}
}
/*!
Replaces the current \a image with a new one.
*/
void Image::setTexture(Texture *image) {
if(m_material) {
m_material->setTexture(gOverride, image);
}
if(m_customMaterial) {
m_customMaterial->setTexture(gOverride, image);
}
}
/*!
Returns the color of the image to be drawn.
*/
Vector4 Image::color() const {
return m_color;
}
/*!
Changes the \a color of the image to be drawn.
*/
void Image::setColor(const Vector4 color) {
m_color = color;
if(m_customMaterial) {
m_customMaterial->setVector4(gColor, &m_color);
} else if(m_material) {
m_material->setVector4(gColor, &m_color);
}
}
/*!
Returns the current item name of sprite from the sprite sheet.
*/
string Image::item() const {
return m_item;
}
/*!
Sets the current sub \a item name of sprite from the sprite sheet.
*/
void Image::setItem(const string item) {
m_item = item;
m_hash = hash_str(m_item);
composeMesh();
}
/*!
Returns a draw mode for the image.
Please check Image::DrawMode for more details.
*/
int Image::drawMode() const {
return m_drawMode;
}
/*!
Sets a draw \a mode for the image.
Please check Image::DrawMode for more details.
*/
void Image::setDrawMode(int mode) {
m_drawMode = mode;
composeMesh();
}
/*!
\internal
Callback method called when the \a bounds of the image change. Recomposes the mesh based on new bounds.
*/
void Image::boundChanged(const Vector2 &bounds) {
m_meshSize = bounds;
composeMesh();
}
/*!
\internal
Loads user data for the image.
*/
void Image::loadUserData(const VariantMap &data) {
Component::loadUserData(data);
{
auto it = data.find(gMaterial);
if(it != data.end()) {
setMaterial(Engine::loadResource<Material>((*it).second.toString()));
}
}
{
auto it = data.find(gBasemap);
if(it != data.end()) {
setSprite(Engine::loadResource<Sprite>((*it).second.toString()));
}
}
}
/*!
\internal
Saves user data for the image.
*/
VariantMap Image::saveUserData() const {
VariantMap result = Component::saveUserData();
{
string ref = Engine::reference(material());
if(!ref.empty()) {
result[gMaterial] = ref;
}
}
{
string ref = Engine::reference(sprite());
if(!ref.empty()) {
result[gBasemap] = ref;
}
}
return result;
}
/*!
\internal
Composes the mesh for rendering based on the current sprite, hash, mesh, size, and draw mode.
*/
void Image::composeMesh() {
if(m_mesh) {
SpriteRender::composeMesh(m_sheet, m_hash, m_mesh, m_meshSize, m_drawMode, false, 100.0f);
}
}
/*!
\internal
Callback method called when the sprite is updated. Handles updating the mesh and material based on sprite changes.
*/
void Image::spriteUpdated(int state, void *ptr) {
Image *p = static_cast<Image *>(ptr);
switch(state) {
case ResourceState::Ready: {
if(p->m_customMaterial) {
p->m_customMaterial->setTexture(gOverride, p->m_sheet->page());
}
p->composeMesh();
} break;
case ResourceState::ToBeDeleted: {
p->m_sheet = nullptr;
p->m_material->setTexture(gOverride, nullptr);
if(p->m_customMaterial) {
p->m_customMaterial->setTexture(gOverride, nullptr);
}
p->composeMesh();
} break;
default: break;
}
}