|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | namespace PhpGui\Widget;
|
3 | 4 |
|
4 | 5 | /**
|
|
7 | 8 | *
|
8 | 9 | * @package PhpGui\Widget
|
9 | 10 | */
|
10 |
| -class Canvas extends AbstractWidget { |
11 |
| - protected function create(): void { |
12 |
| - $this->tcl->evalTcl("canvas .{$this->parentId}.{$this->id}"); |
| 11 | +class Canvas extends AbstractWidget |
| 12 | +{ |
| 13 | + public function __construct(string $parentId, array $options = []) |
| 14 | + { |
| 15 | + parent::__construct($parentId, $options); |
| 16 | + $this->create(); |
| 17 | + } |
| 18 | + |
| 19 | + protected function create(): void |
| 20 | + { |
| 21 | + $extra = $this->getOptionString(); |
| 22 | + $this->tcl->evalTcl("canvas .{$this->parentId}.{$this->id} {$extra}"); |
| 23 | + } |
| 24 | + |
| 25 | + protected function getOptionString(): string |
| 26 | + { |
| 27 | + $opts = ""; |
| 28 | + foreach ($this->options as $key => $value) { |
| 29 | + $opts .= " -$key \"$value\""; |
| 30 | + } |
| 31 | + return $opts; |
| 32 | + } |
| 33 | + |
| 34 | + public function drawLine(int $x1, int $y1, int $x2, int $y2, array $options = []): string |
| 35 | + { |
| 36 | + $optStr = $this->formatOptions($options); |
| 37 | + return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create line $x1 $y1 $x2 $y2 $optStr"); |
| 38 | + } |
| 39 | + |
| 40 | + public function drawRectangle(int $x1, int $y1, int $x2, int $y2, array $options = []): string |
| 41 | + { |
| 42 | + $optStr = $this->formatOptions($options); |
| 43 | + return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create rectangle $x1 $y1 $x2 $y2 $optStr"); |
| 44 | + } |
| 45 | + |
| 46 | + public function drawOval(int $x1, int $y1, int $x2, int $y2, array $options = []): string |
| 47 | + { |
| 48 | + $optStr = $this->formatOptions($options); |
| 49 | + return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create oval $x1 $y1 $x2 $y2 $optStr"); |
| 50 | + } |
| 51 | + |
| 52 | + public function drawText(int $x, int $y, string $text, array $options = []): string |
| 53 | + { |
| 54 | + $optStr = $this->formatOptions($options); |
| 55 | + return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create text $x $y -text {$text} $optStr"); |
| 56 | + } |
| 57 | + |
| 58 | + public function delete(string $itemId): void |
| 59 | + { |
| 60 | + $this->tcl->evalTcl(".{$this->parentId}.{$this->id} delete $itemId"); |
| 61 | + } |
| 62 | + |
| 63 | + public function clear(): void |
| 64 | + { |
| 65 | + $this->tcl->evalTcl(".{$this->parentId}.{$this->id} delete all"); |
| 66 | + } |
| 67 | + |
| 68 | + protected function formatOptions(array $options): string |
| 69 | + { |
| 70 | + $result = []; |
| 71 | + foreach ($options as $key => $value) { |
| 72 | + $result[] = "-$key {$value}"; |
| 73 | + } |
| 74 | + return implode(' ', $result); |
13 | 75 | }
|
14 | 76 | }
|
0 commit comments