|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Arakne\MapParser\Renderer; |
| 4 | + |
| 5 | +use Arakne\MapParser\Loader\Map; |
| 6 | +use Arakne\MapParser\Tile\Coordinate\Point; |
| 7 | +use GdImage; |
| 8 | + |
| 9 | +use function assert; |
| 10 | +use function imagecopyresampled; |
| 11 | +use function imagecreatetruecolor; |
| 12 | +use function imagesx; |
| 13 | +use function imagesy; |
| 14 | + |
| 15 | +/** |
| 16 | + * Handle the scaling and offset to apply to a map when rendering it |
| 17 | + */ |
| 18 | +final readonly class MapScale |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + /** |
| 22 | + * The scaling factor to apply to the map (1.0 = 100%) |
| 23 | + */ |
| 24 | + public float $scale, |
| 25 | + |
| 26 | + /** |
| 27 | + * The horizontal offset to apply to the map in pixels |
| 28 | + * This offset can be negative if the map is wider than the display area |
| 29 | + */ |
| 30 | + public int $offsetX, |
| 31 | + |
| 32 | + /** |
| 33 | + * The vertical offset to apply to the map in pixels |
| 34 | + * This offset can be negative if the map is taller than the display area |
| 35 | + */ |
| 36 | + public int $offsetY, |
| 37 | + |
| 38 | + /** |
| 39 | + * The actual width of the map after scaling, in pixels, but before cropping |
| 40 | + * |
| 41 | + * @var non-negative-int |
| 42 | + */ |
| 43 | + public int $scaledWidth, |
| 44 | + |
| 45 | + /** |
| 46 | + * The actual height of the map after scaling, in pixels, but before cropping |
| 47 | + * |
| 48 | + * @var non-negative-int |
| 49 | + */ |
| 50 | + public int $scaledHeight, |
| 51 | + ) {} |
| 52 | + |
| 53 | + /** |
| 54 | + * Transform the given pixel coordinates by applying the scaling + offset |
| 55 | + * |
| 56 | + * @param int $x |
| 57 | + * @param int $y |
| 58 | + * |
| 59 | + * @return Point |
| 60 | + */ |
| 61 | + public function applyToCoordinates(int $x, int $y): Point |
| 62 | + { |
| 63 | + return new Point( |
| 64 | + (int) ($x * $this->scale) + $this->offsetX, |
| 65 | + (int) ($y * $this->scale) + $this->offsetY, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Resize and crop the given image to fit in the display area |
| 71 | + * |
| 72 | + * @param GdImage $img The original map image |
| 73 | + * @return GdImage The resized and cropped image |
| 74 | + */ |
| 75 | + public function applyToImage(GdImage $img): GdImage |
| 76 | + { |
| 77 | + $result = imagecreatetruecolor(MapRenderer::DISPLAY_WIDTH, MapRenderer::DISPLAY_HEIGHT); |
| 78 | + assert($result !== false); |
| 79 | + |
| 80 | + imagecopyresampled( |
| 81 | + $result, |
| 82 | + $img, |
| 83 | + $this->offsetX, |
| 84 | + $this->offsetY, |
| 85 | + 0, |
| 86 | + 0, |
| 87 | + $this->scaledWidth, |
| 88 | + $this->scaledHeight, |
| 89 | + imagesx($img), |
| 90 | + imagesy($img), |
| 91 | + ); |
| 92 | + |
| 93 | + return $result; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the scaling + offset to apply to a map of arbitrary size to fit in the display area |
| 98 | + * |
| 99 | + * @param Map $map The map to compute the scale for |
| 100 | + * |
| 101 | + * @see https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/mc/Container.as#L154 |
| 102 | + */ |
| 103 | + public static function for(Map $map): MapScale |
| 104 | + { |
| 105 | + if ($map->height === MapRenderer::DEFAULT_HEIGHT && $map->width === MapRenderer::DEFAULT_WIDTH) { |
| 106 | + return new MapScale(1.0, 0, 0, MapRenderer::DISPLAY_WIDTH, MapRenderer::DISPLAY_HEIGHT); |
| 107 | + } |
| 108 | + |
| 109 | + // Scaling is only applied if both dimensions are greater than the default size |
| 110 | + // Otherwise, the map is displayed at its original size and simply cropped/centered |
| 111 | + if ($map->height > MapRenderer::DEFAULT_HEIGHT && $map->width > MapRenderer::DEFAULT_WIDTH) { |
| 112 | + $scale = $map->height > $map->width |
| 113 | + ? MapRenderer::DISPLAY_WIDTH / (($map->width - 1) * MapRenderer::CELL_WIDTH) |
| 114 | + : MapRenderer::DISPLAY_HEIGHT / (($map->height - 1) * MapRenderer::CELL_HEIGHT) |
| 115 | + ; |
| 116 | + |
| 117 | + $actualWidth = (int) (($map->width - 1) * MapRenderer::CELL_WIDTH * $scale); |
| 118 | + $actualHeight = (int) (($map->height - 1) * MapRenderer::CELL_HEIGHT * $scale); |
| 119 | + } else { |
| 120 | + $scale = 1.0; |
| 121 | + $actualWidth = ($map->width - 1) * MapRenderer::CELL_WIDTH; |
| 122 | + $actualHeight = ($map->height - 1) * MapRenderer::CELL_HEIGHT; |
| 123 | + } |
| 124 | + |
| 125 | + // Map has the correct size, no need to crop |
| 126 | + if ($actualWidth === MapRenderer::DISPLAY_WIDTH && $actualHeight === MapRenderer::DISPLAY_HEIGHT) { |
| 127 | + return new MapScale($scale, 0, 0, $actualWidth, $actualHeight); |
| 128 | + } |
| 129 | + |
| 130 | + assert($actualWidth >= 0 && $actualHeight >= 0); |
| 131 | + |
| 132 | + $offsetX = (MapRenderer::DISPLAY_WIDTH - $actualWidth) / 2; |
| 133 | + $offsetY = (MapRenderer::DISPLAY_HEIGHT - $actualHeight) / 2; |
| 134 | + |
| 135 | + return new MapScale($scale, (int) $offsetX, (int) $offsetY, $actualWidth, $actualHeight); |
| 136 | + } |
| 137 | +} |
0 commit comments