Test Failed
Push — trunk ( dc8fe4...b76cfb )
by SuperNova.WS
10:59
created

SpriteLine::generate()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
nc 7
nop 2
dl 0
loc 30
rs 9.3222
c 1
b 0
f 0
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 $widthList = [];
16
17
  public $height = 0;
18
  public $width = 0;
19
20
  /** @var ImageContainer|null $image */
21
  public $image = null;
22
  /** @var string */
23
  public $css = '';
24
25
  /**
26
   * @param ImageFile $imageFile
27
   *
28
   * @return void
29
   */
30
  public function addImage($imageFile) {
31
    $this->files[] = $imageFile;
32
33
    $this->height = max($this->height, $imageFile->height);
34
    $this->width  += $imageFile->width;
35
  }
36
37
  public function getImageCount() {
38
    return count($this->files);
39
  }
40
41
  public function generate($posY, $scaleToPx) {
42
    unset($this->widthList);
43
    unset($this->image);
44
45
    $this->image = ImageContainer::create($this->width, $this->height);
46
47
    $position = 0;
48
    foreach ($this->files as $file) {
49
      $this->image->copyFrom($file->getImageContainer(), $position, 0);
50
51
      $this->widthList[] = $file->width;
52
53
      $onlyName = explode('.', $file->fileName);
54
      if (count($onlyName) > 1) {
55
        array_pop($onlyName);
56
      }
57
      $onlyName = implode('.', $onlyName);
58
59
      $css = "%1\$s{$onlyName}%2\$s{background-position: -{$position}px -{$posY}px;";
60
      if ($scaleToPx > 0) {
61
        $maxSize = max($file->width, $file->height);
62
        if ($maxSize != $scaleToPx) {
63
          $css .= "zoom: calc({$scaleToPx}/{$maxSize});";
64
        }
65
      }
66
      $css .= "width: {$file->width}px;height: {$file->height}px;}\n";
67
68
      $this->css .= $css;
69
70
      $position += $file->width;
71
    }
72
  }
73
74
}
75