1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by Gorlum 08.01.2024 20:04 */ |
4
|
|
|
|
5
|
|
|
/** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */ |
6
|
|
|
|
7
|
|
|
namespace Tools; |
8
|
|
|
|
9
|
|
|
use ImageFile; |
10
|
|
|
|
11
|
|
|
class SpriteLine { |
12
|
|
|
/** @var ImageFile[] $files */ |
13
|
|
|
public $files = []; |
14
|
|
|
|
15
|
|
|
public $height = 0; |
16
|
|
|
public $width = 0; |
17
|
|
|
|
18
|
|
|
/** @var ImageContainer|null $image */ |
19
|
|
|
public $image = null; |
20
|
|
|
/** @var string */ |
21
|
|
|
public $css = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ImageFile $image |
25
|
|
|
* @param int $gridSize |
26
|
|
|
* |
27
|
|
|
* @return static|null |
28
|
|
|
*/ |
29
|
|
|
public function fillLine($image, $gridSize) { |
30
|
|
|
if($image->isAnimatedGif()) { |
31
|
|
|
$line = new SpriteLineGif(); |
32
|
|
|
} else { |
33
|
|
|
$line = $this->isFull($gridSize) ? new static() : $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$line->addImage($image); |
37
|
|
|
|
38
|
|
|
return $line; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ImageFile $imageFile |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function addImage($imageFile) { |
47
|
|
|
$this->files[] = $imageFile; |
48
|
|
|
|
49
|
|
|
$this->height = max($this->height, $imageFile->height); |
50
|
|
|
$this->width += $imageFile->width; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getImageCount() { |
54
|
|
|
return count($this->files); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function generate($posY, $scaleToPx) { |
58
|
|
|
unset($this->image); |
59
|
|
|
|
60
|
|
|
$this->image = ImageContainer::create($this->width, $this->height); |
61
|
|
|
|
62
|
|
|
$position = 0; |
63
|
|
|
foreach ($this->files as $file) { |
64
|
|
|
$this->image->copyFrom($file->getImageContainer(), $position, 0); |
65
|
|
|
|
66
|
|
|
$onlyName = explode('.', $file->fileName); |
67
|
|
|
if (count($onlyName) > 1) { |
68
|
|
|
array_pop($onlyName); |
69
|
|
|
} |
70
|
|
|
$onlyName = implode('.', $onlyName); |
71
|
|
|
|
72
|
|
|
$css = "%1\$s{$onlyName}%2\$s{background-position: -{$position}px -{$posY}px;"; |
73
|
|
|
if ($scaleToPx > 0) { |
74
|
|
|
$maxSize = max($file->width, $file->height); |
75
|
|
|
if ($maxSize != $scaleToPx) { |
76
|
|
|
$css .= "zoom: calc({$scaleToPx}/{$maxSize});"; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$css .= "width: {$file->width}px;height: {$file->height}px;}\n"; |
80
|
|
|
|
81
|
|
|
$this->css .= $css; |
82
|
|
|
|
83
|
|
|
$position += $file->width; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function isFull($gridSize) { |
88
|
|
|
return $this->getImageCount() >= $gridSize; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|