This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
vbbs/app/Classes/Control/EditFrame.php

198 lines
3.3 KiB
PHP

<?php
namespace App\Classes\Control;
use Illuminate\Support\Arr;
use App\Classes\Control;
use App\Classes\Frame;
use App\Classes\Server;
/**
* Class Edit Frame handles frame editing
*
* @package App\Classes\Control
*/
class EditFrame extends Control
{
private $x = 1;
private $y = 1;
// The frame applicable for this control (not the current rendered frame, thats in $so)
protected $fo = NULL;
public function __construct(Server $so,array $args=[])
{
if (! $args OR ! Arr::get($args,'fo') OR (! $args['fo'] instanceof Frame))
throw new \Exception('Missing frame to Edit');
$this->fo = $args['fo'];
parent::__construct($so);
}
protected function boot()
{
// Clear screen and setup edit.
$this->so->co->send(CLS.HOME.DOWN.CON);
// @todo Add page number + "EDIT" (prob only required for login pages which dont show page num)
$this->so->co->send($this->fo->raw().$this->so->moveCursor(1,2));
$this->updateBaseline();
}
public function handle(string $read)
{
static $esc = FALSE;
static $brace = FALSE;
static $out = '';
static $key = '';
$out .= $read;
switch ($read)
{
case 'A':
if ($esc AND $brace)
{
$this->y--;
if ($this->y < 1) {
$this->y = 1;
$out = '';
}
$brace = $esc = FALSE;
}
break;
case 'B':
if ($esc AND $brace)
{
$this->y++;
if ($this->y > $this->fo->frame_length()) {
$this->y = $this->fo->frame_length();
$out = '';
}
$brace =$esc = FALSE;
}
break;
case 'C':
if ($esc AND $brace)
{
$this->x++;
if ($this->x > $this->fo->frame_width()) {
$this->x = $this->fo->frame_width();
$out = '';
}
$brace =$esc = FALSE;
}
break;
case 'D':
if ($esc AND $brace)
{
$this->x--;
if ($this->x < 1) {
$this->x = 1;
$out = '';
}
$brace = $esc = FALSE;
}
break;
case '[':
if ($esc)
$brace = TRUE;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
if ($esc AND $brace) {
$key .= $read;
} else {
$this->x++;
}
break;
case '~':
if ($esc AND $brace)
{
switch ($key)
{
// F9 Pressed
case 20:
break;
// F10 Pressed
case 21:
$this->complete = TRUE;
$this->state = ['action'=>ACTION_GOTO,'mode'=>NULL];
break;
}
$brace = $esc = FALSE;
$key = '';
}
break;
case ESC;
$esc = TRUE;
break;
case LF: $this->y++; break;
case CR; $this->x = 1; break;
default:
if ($esc)
$esc = FALSE;
$this->x++;
}
if (! $esc)
{
printf(" . SENDING OUT: %s\n",$out);
$this->so->co->send($out);
$this->updateBaseline();
$out = '';
}
printf(" . X:%d,Y:%d,C:%s,ESC:%s\n",
$this->x,
$this->y,
(ord($read) < 32 ? '.' : $read),
($esc AND $brace) ? 'TRUE' : 'FALSE');
return $read;
}
public function updateBaseline()
{
$this->so->sendBaseline(
$this->so->co,
sprintf('%02.0f:%02.0f]%s'.RESET.'[',
$this->y,
$this->x,
($this->fo->attr($this->x,$this->y) != '-' ? ESC.'['.$this->fo->attr($this->x,$this->y) : '').$this->fo->char($this->x,$this->y),
)
);
}
public function process()
{
dump(__METHOD__);
}
}