Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update section rendering logic to include border words #68

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions api/packages/board_renderer/lib/src/board_renderer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:game_domain/game_domain.dart';
import 'package:http/http.dart' as http;
import 'package:image/image.dart' as img;
Expand Down Expand Up @@ -191,26 +192,12 @@ class BoardRenderer {

/// Renders a section of the board in an image.
Future<Uint8List> renderSection(BoardSection section) async {
final words = section.words;
final words = [...section.words, ...section.borderWords];

const cellSize = 40;

var maxPositionX = 0;
var maxPositionY = 0;

for (final word in words) {
final x = word.position.x - section.position.x * section.size;
final y = word.position.y - section.position.y * section.size;

final sizeX = word.axis == Axis.horizontal ? word.answer.length : 1;
final sizeY = word.axis == Axis.vertical ? word.answer.length : 1;

maxPositionX = math.max(maxPositionX, x + sizeX);
maxPositionY = math.max(maxPositionY, y + sizeY);
}

final totalWidth = maxPositionX * cellSize;
final totalHeight = maxPositionY * cellSize;
final totalWidth = section.size * cellSize;
final totalHeight = section.size * cellSize;

final image = _createImage(
width: totalWidth,
Expand All @@ -237,22 +224,28 @@ class BoardRenderer {
final char = wordCharacters.elementAt(c).toUpperCase();
final charIndex = char.codeUnitAt(0) - 65;

_compositeImage(
image,
texture,
dstX: word.axis == Axis.horizontal
final (dstX, dstY) = (
word.axis == Axis.horizontal
? (position.$1 + c) * cellSize
: position.$1 * cellSize,
dstY: word.axis == Axis.vertical
word.axis == Axis.vertical
? (position.$2 + c) * cellSize
: position.$2 * cellSize,
dstW: cellSize,
dstH: cellSize,
srcX: charIndex * cellSize,
srcY: 0,
srcW: cellSize,
srcH: cellSize,
: position.$2 * cellSize
);
if (dstX < totalWidth && dstY < totalHeight && dstX >= 0 && dstY >= 0) {
_compositeImage(
image,
texture,
dstX: dstX,
dstY: dstY,
dstW: cellSize,
dstH: cellSize,
srcX: charIndex * cellSize,
srcY: 0,
srcW: cellSize,
srcH: cellSize,
);
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions api/packages/board_renderer/test/src/board_renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ void main() {
group('renderSection', () {
final words = [
Word(
position: Point(1, 1),
position: Point(18, 12),
axis: Axis.horizontal,
answer: 'hello',
clue: '',
hints: const [],
solvedTimestamp: null,
),
Word(
position: Point(2, 7),
position: Point(10, 11),
axis: Axis.vertical,
answer: 'there',
clue: '',
Expand All @@ -154,10 +154,10 @@ void main() {
position: Point(1, 1),
size: 10,
words: words,
borderWords: const [],
borderWords: words,
);

test('render the received section', () async {
test('render the received section words', () async {
final command = _MockCommand();
final image = _MockImage();

Expand Down Expand Up @@ -216,7 +216,7 @@ void main() {

await renderer.renderSection(section);

expect(calls, 10);
expect(calls, 14);
});

test("throws when can't get the texture", () async {
Expand Down
Loading