From a7fcad76bd60c7bf5f62b1eb7c3dd2a349ef1501 Mon Sep 17 00:00:00 2001 From: Hugo Walbecq Date: Wed, 13 Mar 2024 17:42:40 +0100 Subject: [PATCH] feat: update section rendering logic to include border words (#68) * feat: update section rendering logic to include border words * chore: remove comment --- .../lib/src/board_renderer.dart | 51 ++++++++----------- .../test/src/board_renderer_test.dart | 10 ++-- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/api/packages/board_renderer/lib/src/board_renderer.dart b/api/packages/board_renderer/lib/src/board_renderer.dart index bd7d50e22..da994e40e 100644 --- a/api/packages/board_renderer/lib/src/board_renderer.dart +++ b/api/packages/board_renderer/lib/src/board_renderer.dart @@ -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; @@ -191,26 +192,12 @@ class BoardRenderer { /// Renders a section of the board in an image. Future 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, @@ -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, + ); + } } } diff --git a/api/packages/board_renderer/test/src/board_renderer_test.dart b/api/packages/board_renderer/test/src/board_renderer_test.dart index e94f4d7c7..b7296a827 100644 --- a/api/packages/board_renderer/test/src/board_renderer_test.dart +++ b/api/packages/board_renderer/test/src/board_renderer_test.dart @@ -132,7 +132,7 @@ void main() { group('renderSection', () { final words = [ Word( - position: Point(1, 1), + position: Point(18, 12), axis: Axis.horizontal, answer: 'hello', clue: '', @@ -140,7 +140,7 @@ void main() { solvedTimestamp: null, ), Word( - position: Point(2, 7), + position: Point(10, 11), axis: Axis.vertical, answer: 'there', clue: '', @@ -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(); @@ -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 {