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 | #include "fsCNineSliceSpriteComponent.h"
#include <fsAppCore/src/fsCEntity.h>
#include <fsCore/fsRect.h>
#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/fsNMaths.h>
#include "../fsCSprite.h"
#include "../fsCBrush.h"
#include "../fsCBrushManager.h"
#include <cmath>
const fsCUniqueId fsCNineSliceSpriteComponent::mTypeId("fsCNineSliceSpriteComponent");
fsCNineSliceSpriteComponent::fsCNineSliceSpriteComponent(fsCEntity* pParent) :
fsISpriteComponent(pParent)
{
}
fsCNineSliceSpriteComponent::~fsCNineSliceSpriteComponent() = default;
fsIComponent* fsCNineSliceSpriteComponent::create(fsCEntity* pParent)
{
return new fsCNineSliceSpriteComponent(pParent);
}
void fsCNineSliceSpriteComponent::initialiseFromVariableTableDo(const fsCVariableTable& vars)
{
fsISpriteComponent::initialiseFromVariableTableDo(vars);
const auto inset = vars.s32GetWithDefault("sliceInset", 16);
mLeft = vars.s32GetWithDefault("sliceLeft", inset);
mTop = vars.s32GetWithDefault("sliceTop", inset);
mRight = vars.s32GetWithDefault("sliceRight", inset);
mBottom = vars.s32GetWithDefault("sliceBottom", inset);
const auto image = vars.strGet("image");
if (!image.emptyGet())
brushSet(fsCBrushManager::instanceGet()->get(
fsCResourceName(image, fsCResourceName::eRES_LOCATION_ASSETS), vars.strGet("hitMap") == "true"));
mWidth = std::max(0, vars.s32GetWithDefault("width", mWidth));
mHeight = std::max(0, vars.s32GetWithDefault("height", mHeight));
const auto rotation = vars.s32Get("rotation");
if (rotation)
parentGet()->angleSet(rotation * fsNMaths::fsPI / 180.0f);
const auto scale = vars.f32Get("scale");
if (scale)
parentGet()->scaleSet(fsVec2d(scale, scale));
slicesRebuild();
}
void fsCNineSliceSpriteComponent::insetsSet(fsS32 l, fsS32 t, fsS32 r, fsS32 b)
{
mLeft = l;
mTop = t;
mRight = r;
mBottom = b;
slicesRebuild();
}
void fsCNineSliceSpriteComponent::brushSetDo(std::shared_ptr<fsCBrush> brush)
{
mBrush = std::move(brush);
if (mBrush)
{
mWidth = mBrush->dimensionsGet().x;
mHeight = mBrush->dimensionsGet().y;
}
slicesRebuild();
}
void fsCNineSliceSpriteComponent::widthSetDo(fsS32 width, fsBool modify)
{
mWidth = std::max(0, width);
if (modify)
slicesRebuild();
}
void fsCNineSliceSpriteComponent::heightSetDo(fsS32 height, fsBool modify)
{
mHeight = std::max(0, height);
if (modify)
slicesRebuild();
}
void fsCNineSliceSpriteComponent::colourSetDo(const fsSColour4B& colour)
{
mTint = colour;
for (auto& slice : mSlices)
if (slice)
slice->colourSet(colour);
}
void fsCNineSliceSpriteComponent::alphaBlendSetDo(fsS32 blend)
{
mBlend = blend;
mBlendSet = true;
for (auto& slice : mSlices)
if (slice)
slice->alphaBlendModeSet(blend);
}
void fsCNineSliceSpriteComponent::slicesRebuild()
{
if (!mBrush || mWidth <= 0 || mHeight <= 0)
{
for (auto& slice : mSlices)
slice.reset();
return;
}
const auto source = mBrush->dimensionsGet();
mX = fsSNineSliceAxis::make(source.x, mWidth, mLeft, mRight);
mY = fsSNineSliceAxis::make(source.y, mHeight, mTop, mBottom);
for (int y = 0; y < 3; ++y)
for (int x = 0; x < 3; ++x)
{
auto& slice = mSlices[y * 3 + x];
const int sw = mX.source[x + 1] - mX.source[x], sh = mY.source[y + 1] - mY.source[y];
const int dw = mX.destination[x + 1] - mX.destination[x], dh = mY.destination[y + 1] - mY.destination[y];
if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0)
{
slice.reset();
continue;
}
auto crop = std::make_shared<fsCBrush>();
// Include the original brush offset: texture-page images are not at (0,0).
const auto offset = mBrush->offsetGet();
crop->init(mBrush->textureGet(), fsPoint(offset.x + mX.source[x], offset.y + mY.source[y]),
fsPoint(sw, sh));
if (!slice)
slice = std::make_unique<fsCSprite>();
slice->brushSet(crop);
// A unit quad avoids integer half-size truncation for odd-width tiles.
slice->widthSet(2);
slice->heightSet(2);
slice->colourSet(mTint);
if (mBlendSet)
slice->alphaBlendModeSet(mBlend);
}
matrixApplyDo();
}
void fsCNineSliceSpriteComponent::matrixApplyDo()
{
for (int y = 0; y < 3; ++y)
for (int x = 0; x < 3; ++x)
{
auto& slice = mSlices[y * 3 + x];
if (!slice)
continue;
const auto dw = mX.destination[x + 1] - mX.destination[x], dh = mY.destination[y + 1] - mY.destination[y];
const auto cx = (mX.destination[x] + mX.destination[x + 1] - mX.destination[3]) / 2.0f;
auto cy = (mY.destination[y] + mY.destination[y + 1] - mY.destination[3]) / 2.0f;
#if defined fsINVERTED_Y_AXIS
cy = -cy;
#endif
slice->matrixApply(parentGet()->renderMatrixGet() * fsAffineMtx2d(fsVec2d(cx, cy), dw / 2.0f, dh / 2.0f));
}
}
void fsCNineSliceSpriteComponent::renderDo()
{
for (auto& slice : mSlices)
if (slice)
slice->render();
}
fsBool fsCNineSliceSpriteComponent::hitTestDo(const fsPoint& point, fsBool requireMask) const
{
if (!mBrush || !parentGet()->visibleGet() || mWidth <= 0 || mHeight <= 0
|| mBrush->dimensionsGet().x <= 0 || mBrush->dimensionsGet().y <= 0)
return false;
fsVec2d local;
if (!parentGet()->screenCoordConvertToLocal(point, local))
return false;
const double x = local.x + mWidth / 2.0, y = local.y + mHeight / 2.0;
if (x < 0 || y < 0 || x >= mWidth || y >= mHeight)
return false;
if (!mBrush->hasHitMapGet())
return !requireMask;
return mBrush->hitTest(fsPoint(static_cast<fsS32>(mX.sourceCoordinate(x)),
static_cast<fsS32>(mY.sourceCoordinate(y))));
}
namespace
{
fsRect nineSliceBounds(fsISpriteComponent* sprite)
{
const float w = sprite->widthGet() / 2.0f, h = sprite->heightGet() / 2.0f;
fsVec2d corners[4] = {{-w, -h}, {w, -h}, {w, h}, {-w, h}}, transformed;<--- Variable 'corners' can be declared as const array
float l = INFINITY, r = -INFINITY, t = INFINITY, b = -INFINITY;
for (auto corner : corners)
{
sprite->parentGet()->renderMatrixGet().fsVec2dTransform(corner, &transformed);
l = std::min(l, transformed.x);
r = std::max(r, transformed.x);
t = std::min(t, transformed.y);
b = std::max(b, transformed.y);
}
return fsRect(static_cast<int>(std::floor(l)), static_cast<int>(std::ceil(r)),
static_cast<int>(std::floor(t)), static_cast<int>(std::ceil(b)));
}
}
fsBool fsCNineSliceSpriteComponent::hitTestDo(fsISpriteComponent* other, fsBool requireMask) const
{
if (!mBrush || mWidth <= 0 || mHeight <= 0
|| mBrush->dimensionsGet().x <= 0 || mBrush->dimensionsGet().y <= 0
|| !other || !other->brushGet() || other->brushGet()->dimensionsGet().x <= 0
|| other->brushGet()->dimensionsGet().y <= 0
|| !parentGet()->visibleGet() || !other->parentGet()->visibleGet())
return false;
auto box = nineSliceBounds(const_cast<fsCNineSliceSpriteComponent*>(this)).intersectGet(nineSliceBounds(other));
if (!box.isValidGet())
return false;
if (!requireMask)
return true;
for (int y = box.tGet(); y < box.bGet(); ++y)
for (int x = box.lGet(); x < box.rGet(); ++x)
if (hitTestDo(fsPoint(x, y), true) && other->hitTest(fsPoint(x, y), true))
return true;
return false;
}
fsBool fsCNineSliceSpriteComponent::obscuredByBoundingBoxCheckDo(fsISpriteComponent* other)
{
return other && nineSliceBounds(this).obscuredBy(nineSliceBounds(other));
}
|