swf2ass/src/Rectangle.php

78 lines
2.9 KiB
PHP

<?php
namespace swf2ass;
class Rectangle implements ComplexShape {
public Vector2 $topLeft;
public Vector2 $bottomRight;
public function __construct(Vector2 $topLeft, Vector2 $bottomRight) {
$this->topLeft = $topLeft;
$this->bottomRight = $bottomRight;
}
public function inBounds(Vector2 $pos): bool {
return $pos->x >= $this->topLeft->x and $pos->y >= $this->topLeft->y and $pos->x <= $this->bottomRight->x and $pos->y <= $this->bottomRight->y;
}
public function getWidth() {
return ($this->bottomRight->x - $this->topLeft->x);
}
public function getHeight() {
return ($this->bottomRight->y - $this->topLeft->y);
}
public function getArea() {
return $this->getWidth() * $this->getHeight();
}
public function draw(): array {
return [new LineRecord(new Vector2($this->topLeft->x, $this->bottomRight->y), $this->topLeft), new LineRecord($this->bottomRight, new Vector2($this->topLeft->x, $this->bottomRight->y)), new LineRecord(new Vector2($this->bottomRight->x, $this->topLeft->y), $this->bottomRight), new LineRecord($this->topLeft, new Vector2($this->bottomRight->x, $this->topLeft->y)),];
}
public function drawOpen(): array {
return [new LineRecord(new Vector2($this->topLeft->x, $this->bottomRight->y), $this->topLeft), new LineRecord($this->bottomRight, new Vector2($this->topLeft->x, $this->bottomRight->y)), new LineRecord(new Vector2($this->bottomRight->x, $this->topLeft->y), $this->bottomRight),];
}
public function divide($size): Rectangle {
return new Rectangle($this->topLeft->divide($size), $this->bottomRight->divide($size));
}
public function multiply($size): Rectangle {
return new Rectangle($this->topLeft->multiply($size), $this->bottomRight->multiply($size));
}
public function toPixel($twipSize = Constants::TWIP_SIZE): Rectangle {
return $this->divide($twipSize);
}
public static function fromXML(\DOMElement $element): Rectangle {
return new Rectangle(new Vector2((int)$element->getAttribute("left"), (int)$element->getAttribute("top")), new Vector2((int)$element->getAttribute("right"), (int)$element->getAttribute("bottom")));
}
public static function fromData($bitdata, &$offset): Rectangle {
$nbits = Utils::binary2dec(substr($bitdata, $offset, 5));
var_dump($nbits);
$offset += 5;
$xMin = Utils::binary2dec(substr($bitdata, $offset, $nbits));
$offset += $nbits;
$xMax = Utils::binary2dec(substr($bitdata, $offset, $nbits));
$offset += $nbits;
$yMin = Utils::binary2dec(substr($bitdata, $offset, $nbits));
$offset += $nbits;
$yMax = Utils::binary2dec(substr($bitdata, $offset, $nbits));
$offset += $nbits;
return new Rectangle(new Vector2($xMin, $yMin), new Vector2($xMax, $yMax));
}
}