Improved cursor rendering

* Added support for different cursor styles
This commit is contained in:
Chris 2021-02-16 23:49:14 +01:00
parent 0a61c34d43
commit b2c5ac9633

View File

@ -7,6 +7,15 @@ use NoccyLabs\TermBuf\TerminalBuffer;
class GdRenderer
{
const CURSOR_NONE = 0;
const CURSOR_BLOCK = 1;
const CURSOR_SOLID = 2;
const CURSOR_UNDER = 3;
const CURSOR_LEFT = 4;
const CURSOR_IBEAM = 5;
private static $ValidCusors = [ self::CURSOR_NONE, self::CURSOR_BLOCK, self::CURSOR_SOLID, self::CURSOR_UNDER,self::CURSOR_LEFT ];
private $font = __DIR__."/../../firacode.ttf";
private $boldFont = __DIR__."/../../firacode-bold.ttf";
@ -33,6 +42,8 @@ class GdRenderer
private $cellHeight;
private $cursorStyle;
public function render(TerminalBuffer $buffer)
{
@ -54,11 +65,7 @@ class GdRenderer
}
[$cursorLine,$cursorColumn] = $buffer->getCursorPosition();
$cursorX = $cursorColumn * $cw;
$cursorY = $cursorLine * $ch;
imagerectangle($this->gd, $cursorX, $cursorY, $cursorX + $cw, $cursorY + $ch - 1, 0xFFFFFF);
$this->drawCursor($cursorLine, $cursorColumn);
$this->drawAnnotations();
$this->drawKeyOverlay();
@ -112,6 +119,39 @@ class GdRenderer
imagepng($this->gd, $filename);
}
public function setCursorStyle(int $style)
{
if (!in_array($style, self::$ValidCusors)) {
throw new \RuntimeException("Invalid cursor style");
}
$this->cursorStyle = $style;
}
private function drawCursor($line, $column)
{
$cursorX1 = $column * $this->cellWidth;
$cursorY1 = $line * $this->cellHeight;
$cursorX2 = $cursorX1 + $this->cellWidth;
$cursorY2 = $cursorY1 + $this->cellHeight - 2;
switch ($this->cursorStyle) {
case self::CURSOR_NONE:
return;
case self::CURSOR_BLOCK:
imagerectangle($this->gd, $cursorX1, $cursorY1, $cursorX2, $cursorY2, 0xFFFFFF);
break;
case self::CURSOR_SOLID:
imagefilledrectangle($this->gd, $cursorX1, $cursorY1, $cursorX2, $cursorY2, 0xFFFFFF);
break;
case self::CURSOR_LEFT:
case self::CURSOR_UNDER:
case self::CURSOR_IBEAM:
}
}
private function drawAnnotations()
{
$fw = imagefontwidth($this->annotationFontSize);