From 958655dfecc94c7b57b4f62ed6a47e93d50828cd Mon Sep 17 00:00:00 2001 From: Hugo Walbecq Date: Wed, 13 Mar 2024 13:22:30 +0100 Subject: [PATCH 1/2] feat: update section rendering logic to include border words --- .../lib/src/board_renderer.dart | 52 ++++++++----------- .../test/src/board_renderer_test.dart | 10 ++-- 2 files changed, 28 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..e1f47c21e 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; @@ -97,6 +98,7 @@ class BoardRenderer { Future renderBoard(List words) async { /// The size of each cell in the board when rendering in full size. const cellSize = 4; + // use bigger value var minPositionX = 0; var minPositionY = 0; @@ -191,26 +193,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 +225,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 { From ac70460819221e6425a26a740ff267342f31a765 Mon Sep 17 00:00:00 2001 From: Hugo Walbecq Date: Wed, 13 Mar 2024 16:35:33 +0100 Subject: [PATCH 2/2] chore: remove comment --- api/packages/board_renderer/lib/src/board_renderer.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/api/packages/board_renderer/lib/src/board_renderer.dart b/api/packages/board_renderer/lib/src/board_renderer.dart index e1f47c21e..da994e40e 100644 --- a/api/packages/board_renderer/lib/src/board_renderer.dart +++ b/api/packages/board_renderer/lib/src/board_renderer.dart @@ -98,7 +98,6 @@ class BoardRenderer { Future renderBoard(List words) async { /// The size of each cell in the board when rendering in full size. const cellSize = 4; - // use bigger value var minPositionX = 0; var minPositionY = 0;