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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525 | #include "gfCLetterGrid.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/fsNMaths.h>
#include <fsAppCore/src/fsCEntity.h>
#include <fsAppCore/src/fsCEntityFactory.h>
#include <fsRenderer/src/fsCBrushManager.h>
#include <fsRenderer/src/fsCBrush.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include "gfCLetterPath.h"
#include "gfCMinigameLetterTileComponent.h"
const fsSColour4B gfCLetterGrid::mTileTint(0, 200, 100, 255);
const fsSColour4B gfCLetterGrid::mSelectionTint(0, 220, 90, 130);
fsBool gfCLetterGrid::sSelection::empty() const
{
return mTiles.empty();
}
void gfCLetterGrid::sSelection::tilesTint(fsSColour4B const& pTint)
{
std::for_each(std::begin(mTiles), std::end(mTiles),
[&](gfCMinigameLetterTileComponent* const tile)
{
tile->tint(pTint);
});
}
void gfCLetterGrid::sSelection::linesDestroy()
{
std::for_each(std::begin(mLines), std::end(mLines),
[](fsCEntity* const line)
{
line->destroy();
});
mLines.clear();
}
void gfCLetterGrid::sSelection::clear()
{
linesDestroy();
tilesTint(fsSColour4B::fsWHITE());
mWord = "";
mTiles.clear();
}
fsBool gfCLetterGrid::verticalGet(fsPoint const& pSize)
{
return fsNMaths::absS32(pSize.x) < fsNMaths::absS32(pSize.y);
}
fsStr gfCLetterGrid::spellGet(const std::vector<gfCMinigameLetterTileComponent*>& pTiles, fsBool pBackward)
{
fsStr word;
auto add = [&](gfCMinigameLetterTileComponent* const tile) { word += tile->letterGet(); };
if (pBackward)
{
std::for_each(std::rbegin(pTiles), std::rend(pTiles), add);
}
else
{
std::for_each(std::begin(pTiles), std::end(pTiles), add);
}
return word;
}
void gfCLetterGrid::hilightsSet(const fsStr& pHilight, const fsStr& pHilight2, const fsStr& pHilight2End)
{
mHilight = std::make_unique<fsCResourceName>(pHilight, fsCResourceName::eRES_LOCATION_ASSETS);
mHilight2 = std::make_unique<fsCResourceName>(pHilight2, fsCResourceName::eRES_LOCATION_ASSETS);
mHilight2End = std::make_unique<fsCResourceName>(pHilight2End, fsCResourceName::eRES_LOCATION_ASSETS);
}
void gfCLetterGrid::init(const fsStr& pTileSet, const std::vector<std::vector<fsStr>>& pLetters, fsPoint const& pOrigin)
{
mTileSet = pTileSet;
const fsPoint dimensions(static_cast<fsS32>(pLetters[0].size()), static_cast<fsS32>(pLetters.size()));
auto tmpTile = fsCBrushManager::instanceGet()->get(
fsCResourceName(mTileSet + "/w.png", fsCResourceName::eRES_LOCATION_ASSETS), true);
const fsPoint cell(tmpTile->dimensionsGet().x, tmpTile->dimensionsGet().y);
// New (layer-positioned) scenes centre the grid on the anchor; legacy originX/Y scenes keep
// the old top-left anchor so they don't shift.
fsPoint gridOrigin = pOrigin;
if (pOrigin.x == 0 && pOrigin.y == 0)
{
gridOrigin.x -= dimensions.x * cell.x / 2 - cell.x / 2;
gridOrigin.y -= dimensions.y * cell.y / 2 - cell.y / 2;
}
mTiles.init(gridOrigin, dimensions, cell, {0, 0});
mTiles.generate(
[&](fsS32 col, fsS32 row) -> gfCMinigameLetterTileComponent*
{
return tileCreate(fsPoint{col, row}, pLetters);
}
);
}
gfCMinigameLetterTileComponent* gfCLetterGrid::tileCreate(fsPoint const& pGridPos,
const std::vector<std::vector<fsStr>>& pLetters)
{
fsCEntity* const tileEnt = mOwner->entityFactoryGet()->emptyEntityCreate(fsStr("tile"));
gfCMinigameLetterTileComponent* letter = tileEnt->componentAdd<gfCMinigameLetterTileComponent>();
letter->initialise(mTileSet, pLetters[pGridPos.y][pGridPos.x]);
mOwner->childAdd(tileEnt);
tileEnt->posSet(mTiles.worlddLocation(pGridPos));
return letter;
}
void gfCLetterGrid::tileAdd(sSelection& pSelection, gfCMinigameLetterTileComponent* const pTile) const
{
if (mDragTint)
{
pTile->tint(mTileTint);
}
pSelection.mTiles.push_back(pTile);
}
void gfCLetterGrid::tileRemoveLast(sSelection& pSelection) const
{
if (mDragTint)
{
pSelection.mTiles.back()->tint(fsSColour4B::fsWHITE());
}
pSelection.mTiles.pop_back();
}
void gfCLetterGrid::lineRefresh(sSelection& pSelection)
{
pSelection.linesDestroy();
lineCreate(pSelection, mSelectionTint);
}
void gfCLetterGrid::dragTo(sSelection& pSelection, fsPoint const& pPos, fsBool pSnake)
{
if (!mTiles.onGrid(pPos))
{
return;
}
// Only take a tile once the finger is well inside it, so cutting a corner doesn't catch a neighbour.
const fsPoint cellSize = mTiles.cellBorderedDimensionsGet();
const fsPoint inCell = {
(pPos.x - mTiles.originGet().x + mTiles.halfCell().x) % cellSize.x,
(pPos.y - mTiles.originGet().y + mTiles.halfCell().y) % cellSize.y};
const fsPoint margin = cellSize / 6;
if (inCell.x <= margin.x || inCell.x >= cellSize.x - margin.x ||
inCell.y <= margin.y || inCell.y >= cellSize.y - margin.y)
{
return;
}
const fsPoint gridPos = mTiles.gridLocation(pPos);
gfCMinigameLetterTileComponent* const tile = mTiles.getCell(gridPos.x, gridPos.y);
const auto& tiles = pSelection.mTiles;
if (tiles.empty())
{
tileAdd(pSelection, tile);
return;
}
// Dragging back onto a tile already in the selection trims it back to there.
if (std::end(tiles) != std::find(std::begin(tiles), std::end(tiles), tile))
{
if (tile != tiles.back())
{
while (tile != tiles.back())
{
tileRemoveLast(pSelection);
}
lineRefresh(pSelection);
}
return;
}
// A fast drag can skip tiles, so fill a straight run from the tail; any other jump is ignored.
const fsPoint tailPos = mTiles.gridLocation(tiles.back()->parentGet()->posGet());
const fsPoint delta = gridPos - tailPos;
const fsS32 steps = std::max(fsNMaths::absS32(delta.x), fsNMaths::absS32(delta.y));
if ((delta.x != 0 && fsNMaths::absS32(delta.x) != steps) ||
(delta.y != 0 && fsNMaths::absS32(delta.y) != steps))
{
return;
}
const fsPoint step = delta / steps;
// Unless snaking, the first two tiles fix the direction.
if (!pSnake && tiles.size() > 1 &&
step != mTiles.gridLocation(tiles[1]->parentGet()->posGet()) - mTiles.gridLocation(tiles[0]->parentGet()->posGet()))
{
return;
}
std::vector<gfCMinigameLetterTileComponent*> run;
for (fsS32 i = 1; i <= steps; ++i)
{
gfCMinigameLetterTileComponent* const runTile = mTiles.getCell(tailPos.x + step.x * i, tailPos.y + step.y * i);
if (std::end(tiles) != std::find(std::begin(tiles), std::end(tiles), runTile))
{
return;
}
run.push_back(runTile);
}
for (gfCMinigameLetterTileComponent* const runTile : run)
{
tileAdd(pSelection, runTile);
}
lineRefresh(pSelection);
}
fsBool gfCLetterGrid::wordFind(sSelection& pSelection, fsBool pSnake)
{
std::vector<std::string> letters;
mTiles.forEach(mTiles.begin(), mTiles.end(),
[&](gfCMinigameLetterTileComponent* const tile)
{
letters.push_back(tile->letterGet().utf8Get());
});
const std::vector<gfCLetterPath::sCell> path = gfCLetterPath::find(
mTiles.columnsGet(), mTiles.rowsGet(), letters, pSelection.mWord.utf8Get(), pSnake);
for (const gfCLetterPath::sCell& cell : path)
{
pSelection.mTiles.push_back(mTiles.getCell(cell.mColumn, cell.mRow));
}
return !path.empty();
}
std::vector<gfCMinigameLetterTileComponent*> gfCLetterGrid::cornerTilesGet(const sSelection& pSelection) const
{
const auto& tiles = pSelection.mTiles;
std::vector<gfCMinigameLetterTileComponent*> corners({tiles.front()});
fsPoint direction{0, 0};
for (std::size_t i = 1; i < tiles.size(); ++i)
{
const fsPoint step = mTiles.gridLocation(tiles[i]->parentGet()->posGet())
- mTiles.gridLocation(tiles[i - 1]->parentGet()->posGet());
if (i > 1 && step != direction)
{
corners.push_back(tiles[i - 1]);
}
direction = step;
}
corners.push_back(tiles.back());
return corners;
}
std::vector<fsPoint> gfCLetterGrid::lineVertsGet(const sSelection& pSelection) const
{
std::vector<fsPoint> lineVerts;
for (gfCMinigameLetterTileComponent* const tile : cornerTilesGet(pSelection))
{
lineVerts.push_back(tile->parentGet()->posGet());<--- Consider using std::transform algorithm instead of a raw loop.
}
return lineVerts;
}
void gfCLetterGrid::lineCreate(sSelection& pSelection, fsSColour4B const& pTint)
{
if (pSelection.mTiles.size() < 2)
{
return;
}
auto linePos = lineVertsGet(pSelection);
if (linePos.size() > 2 && hilight2Available())
{
pathCreate(pSelection, linePos, pTint);
return;
}
auto l = linePos.begin() + 1;
while (l != linePos.end())
{
segmentCreate(pSelection, *(l-1),*l, pTint);
pSelection.mLines.back()->visibleSet(true);
++l;
}
}
void gfCLetterGrid::pathCreate(sSelection& pSelection, const std::vector<fsPoint>& pVerts, fsSColour4B const& pTint)
{
const std::size_t last = pVerts.size() - 1;
fsCEntity* const startCap = spriteCreate(pSelection, *mHilight2End, pTint);
fsCEntity* const endCap = spriteCreate(pSelection, *mHilight2End, pTint);
const fsPoint start = pVerts[0] - capOffsetGet(startCap, pVerts[0], pVerts[1]);
const fsPoint end = pVerts[last] + capOffsetGet(endCap, pVerts[last - 1], pVerts[last]);
capAdjust(startCap, start, angleGet(pVerts[0], pVerts[1]));
capAdjust(endCap, end, angleGet(pVerts[last - 1], pVerts[last]) + fsNMaths::fsPI);
for (std::size_t i = 0; i < last; ++i)
{
const fsPoint from = i == 0 ? start : pVerts[i];
const fsPoint to = i == last - 1 ? end : pVerts[i + 1];
const fsPoint size = to - from;
const fsF32 length = fsNMaths::sqrtF32(static_cast<fsF32>(size.x * size.x + size.y * size.y));
// Not spriteAdjust: its diagonal test is for whole runs between tile centres.
fsCEntity* const body = spriteCreate(pSelection, *mHilight2, pTint);
body->angleSet(-fsNMaths::atan2F32(static_cast<fsF32>(size.y), static_cast<fsF32>(size.x)));
body->spriteComponentGet()->widthSet(static_cast<fsS32>(length));
body->posSet(fsPoint{from.x + size.x / 2, from.y + size.y / 2});
// Round off the bend with a cap whose flat edge sits on the corner tile, facing back along the
// run in; bodies stop at the tile so the translucent tint barely overlaps.
if (i > 0)
{
fsCEntity* const joint = spriteCreate(pSelection, *mHilight2End, pTint);
const fsF32 back = joint->spriteComponentGet()->widthGet() / 2.0f / length;
capAdjust(joint,
fsPoint{from.x - static_cast<fsS32>(size.x * back), from.y - static_cast<fsS32>(size.y * back)},
angleGet(pVerts[i], pVerts[i + 1]));
}
}
}
fsBool gfCLetterGrid::hilight2Available() const
{
return mHilight2
&& mHilight2End
&& fsIFile::fileExistsGet(*mHilight2)
&& fsIFile::fileExistsGet(*mHilight2End);
}
fsCEntity* gfCLetterGrid::spriteCreate(sSelection& pSelection, const fsCResourceName& pBrushName,
fsSColour4B const& pTint)
{
pSelection.mLines.push_back(mOwner->entityFactoryGet()->emptyEntityCreate("selectionEnt"));
auto* sprtComp = pSelection.mLines.back()->componentAdd<fsCSpriteComponent>();
sprtComp->brushSet(fsCBrushManager::instanceGet()->get(pBrushName, true));
sprtComp->colourSet(pTint);
mOwner->childAdd(pSelection.mLines.back());
return pSelection.mLines.back();
}
void gfCLetterGrid::segmentCreate(sSelection& pSelection, fsPoint const& pStart, fsPoint const& pEnd,
fsSColour4B const& pTint)
{
if (!hilight2Available())
{
spriteAdjust(spriteCreate(pSelection, *mHilight, fsSColour4B::fsWHITE()), pStart, pEnd);
return;
}
fsCEntity* const line = spriteCreate(pSelection, *mHilight2, pTint);
fsCEntity* const startCap = spriteCreate(pSelection, *mHilight2End, pTint);
fsCEntity* const endCap = spriteCreate(pSelection, *mHilight2End, pTint);
const fsF32 angle = angleGet(pStart, pEnd);
const fsPoint capOffset = capOffsetGet(startCap, pStart, pEnd);
const fsPoint extendedStart = pStart - capOffset;
const fsPoint extendedEnd = pEnd + capOffset;
spriteAdjust(line, extendedStart, extendedEnd);
capAdjust(startCap, extendedStart, angle);
capAdjust(endCap, extendedEnd, angle + fsNMaths::fsPI);
}
void gfCLetterGrid::capAdjust(fsCEntity* const pCap, fsPoint const& pPos, fsF32 pAngle) const<--- The member function 'gfCLetterGrid::capAdjust' can be static.
{
pCap->angleSet(pAngle);
pCap->posSet(pPos);
}
fsPoint gfCLetterGrid::capOffsetGet(fsCEntity* const pCap, fsPoint const& pStart, fsPoint const& pEnd) const
{
const fsPoint size = {pEnd - pStart};
fsF32 dirX = 0.0f;
fsF32 dirY = 0.0f;
if (diagonalGet(size))
{
const fsF32 diagonalUnit = fsNMaths::sqrtF32(0.5f);
dirX = size.x < 0 ? -diagonalUnit : diagonalUnit;
dirY = size.y < 0 ? -diagonalUnit : diagonalUnit;
}
else if (verticalGet(size))
{
dirY = size.y < 0 ? -1.0f : 1.0f;
}
else
{
dirX = size.x < 0 ? -1.0f : 1.0f;
}
const fsPoint halfCell = mTiles.cellDimensionsGet() / 4;
const fsF32 absDirX = fsNMaths::fabsF32(dirX);
const fsF32 absDirY = fsNMaths::fabsF32(dirY);
const fsF32 tileExtension =
absDirY > absDirX
? halfCell.y / absDirY
: halfCell.x / absDirX;
const fsF32 capExtension = pCap->spriteComponentGet()->widthGet() / 2.0f;
const fsF32 extension = capExtension + tileExtension;
return fsPoint(
static_cast<fsS32>(dirX * extension),
static_cast<fsS32>(dirY * extension));
}
fsBool gfCLetterGrid::diagonalGet(fsPoint const& pSize) const
{
const fsS32 absX = fsNMaths::absS32(pSize.x);
const fsS32 absY = fsNMaths::absS32(pSize.y);
if (absX < mTiles.rowStrideGet() || absY < mTiles.columnStrideGet())
{
return false;
}
return fsNMaths::absS32(absX - absY) <= mTiles.cellBorderedDimensionsGet().x;
}
fsF32 gfCLetterGrid::angleGet(fsPoint const& pStart, fsPoint const& pEnd) const
{
const fsPoint size = {pEnd - pStart};
if (diagonalGet(size))
{
const fsF32 angle = size.x * size.y < 0 ? fsNMaths::fsPI / 4.0f : -fsNMaths::fsPI / 4.0f;
return size.x < 0 ? angle + fsNMaths::fsPI : angle;
}
if (verticalGet(size))
{
return size.y < 0 ? fsNMaths::fsPI / 2.0f : -fsNMaths::fsPI / 2.0f;
}
return size.x < 0 ? fsNMaths::fsPI : 0.0f;
}
void gfCLetterGrid::spriteAdjust(fsCEntity* const pSprite, fsPoint const& pStart, fsPoint const& pEnd) const
{
const fsPoint size = {pEnd - pStart};
if (diagonalGet(size))
{
if (size.x * size.y < 0)
{
pSprite->angleSet(fsNMaths::fsPI / 4.0f);
}
else
{
pSprite->angleSet(-fsNMaths::fsPI / 4.0f);
}
pSprite->spriteComponentGet()->widthSet(fsNMaths::absS32(fsNMaths::sqrtF32(size.x * size.x + size.y * size.y)));
pSprite->posSet(fsPoint{pStart.x + size.x / 2, pStart.y + size.y / 2});
}
else if (verticalGet(size))
{
pSprite->angleSet(fsNMaths::fsPI / 2.0f);
pSprite->spriteComponentGet()->widthSet(fsNMaths::absS32(size.y));
pSprite->posSet(fsPoint{pStart.x, pStart.y + size.y / 2});
}
else
{
pSprite->angleSet(0.0f);
pSprite->spriteComponentGet()->widthSet(fsNMaths::absS32(size.x));
pSprite->posSet(fsPoint{pStart.x + size.x / 2, pStart.y});
}
}
fsStr gfCLetterGrid::pathSaveStrGet(const sSelection& pSelection) const
{
fsStr saveStr = pSelection.mWord + "|";
for (gfCMinigameLetterTileComponent* const tile : pSelection.mTiles)
{
const fsPoint gridPos = mTiles.gridLocation(tile->parentGet()->posGet());
saveStr += fsStr(gridPos.x) + "," + fsStr(gridPos.y) + ";";
}
return saveStr;
}
fsBool gfCLetterGrid::pathRestore(sSelection& pSelection, const fsStr& pPath)
{
std::istringstream path(pPath.utf8Get());
fsS32 column = 0;
fsS32 row = 0;
char comma = 0;
char semicolon = 0;
while (path >> column >> comma >> row >> semicolon)
{
if (column < 0 || column >= mTiles.columnsGet() || row < 0 || row >= mTiles.rowsGet())
{
pSelection.mTiles.clear();
return false;
}
pSelection.mTiles.push_back(mTiles.getCell(column, row));
}
// The tiles must still spell the word, either way round.
if (spellGet(pSelection.mTiles, false) != pSelection.mWord && spellGet(pSelection.mTiles, true) != pSelection.mWord)
{
pSelection.mTiles.clear();
return false;
}
return true;
}
gfCLetterGrid::gfCLetterGrid(fsCEntity* const pOwner) :
mOwner(pOwner)
{
}
gfCLetterGrid::~gfCLetterGrid()
{
}
|