From 3654670954f583510435542a3bb45758dff6b1d0 Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Mon, 28 Oct 2024 09:54:08 +0100 Subject: [PATCH] Fixes --- .../Framework/src/Application/Gui.php | 1 + .../Framework/src/Loop/OrderedEventLoop.php | 7 ++ src/PHPNative/Framework/src/PHPNative.php | 1 + src/PHPNative/Renderer/src/Widget.php | 4 + .../Renderer/src/Widgets/Checkbox.php | 79 +++++++++++++++++++ .../Renderer/src/Widgets/Container.php | 56 +++++++------ .../Renderer/src/Widgets/Container/Column.php | 3 +- .../Renderer/src/Widgets/Container/Row.php | 5 +- src/PHPNative/Renderer/src/Widgets/Text.php | 42 +++++++--- .../Renderer/src/Widgets/TextEdit.php | 21 ++++- src/PHPNative/UI/src/Widget/Button.php | 4 +- src/PHPNative/UI/src/Widget/Checkbox.php | 33 ++++++++ 12 files changed, 211 insertions(+), 45 deletions(-) create mode 100644 src/PHPNative/Renderer/src/Widgets/Checkbox.php create mode 100644 src/PHPNative/UI/src/Widget/Checkbox.php diff --git a/src/PHPNative/Framework/src/Application/Gui.php b/src/PHPNative/Framework/src/Application/Gui.php index ccdafbd..f1011d0 100644 --- a/src/PHPNative/Framework/src/Application/Gui.php +++ b/src/PHPNative/Framework/src/Application/Gui.php @@ -35,6 +35,7 @@ final readonly class Gui implements Application public function run(): void { + try { $application = $this->container->get(App::class); $lifeCycle = $this->container->get(Lifecycle::class); diff --git a/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php b/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php index 19b3fcc..a061668 100644 --- a/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php +++ b/src/PHPNative/Framework/src/Loop/OrderedEventLoop.php @@ -21,6 +21,8 @@ class OrderedEventLoop extends EventLoop { $this->render->rate($frameRate)->touch(); $this->updates->rate($updateRate)->touch(); + $tray = new \Tray("Demo App", __DIR__ .'/../../../../../../app/app/Assets/icon.png'); + $tray->init(); while ($this->running) { $now = \microtime(true); @@ -31,6 +33,10 @@ class OrderedEventLoop extends EventLoop if (($delta = $this->render->next($now)) !== null) { $this->render($delta); + $poll = $tray->poll(); + if($poll != null) { + echo sprintf("%s", $poll).PHP_EOL; + } } while ($event = $this->eventDriver->pollEvent()) { @@ -38,5 +44,6 @@ class OrderedEventLoop extends EventLoop $this->poll($event); } } + $tray->close(); } } \ No newline at end of file diff --git a/src/PHPNative/Framework/src/PHPNative.php b/src/PHPNative/Framework/src/PHPNative.php index 1c3e201..c1716ba 100644 --- a/src/PHPNative/Framework/src/PHPNative.php +++ b/src/PHPNative/Framework/src/PHPNative.php @@ -17,6 +17,7 @@ final class PHPNative \SDL_Init(\SDL_INIT_VIDEO); \SDL_TTF_Init(); + // Kernel return (new Kernel( root: $root, diff --git a/src/PHPNative/Renderer/src/Widget.php b/src/PHPNative/Renderer/src/Widget.php index 611aafd..abe640a 100644 --- a/src/PHPNative/Renderer/src/Widget.php +++ b/src/PHPNative/Renderer/src/Widget.php @@ -3,6 +3,7 @@ namespace PHPNative\Renderer; use PHPNative\Renderer\Cache\Styles; +use PHPNative\UI\Widget\Checkbox; use PHPNative\UI\Widget\Icon; use PHPNative\UI\Widget\Image; use PHPNative\UI\Widget\TextEdit; @@ -28,6 +29,9 @@ class Widget if($view instanceof TextEdit) { return \PHPNative\Renderer\Widgets\TextEdit::render($styleCache, $thread, $viewPort, $view, $index); } + if($view instanceof Checkbox) { + return \PHPNative\Renderer\Widgets\Checkbox::render($styleCache, $thread, $viewPort, $view, $index); + } if($view instanceof Container) { return \PHPNative\Renderer\Widgets\Container::render($styleCache, $thread, $viewPort, $view, $index); } diff --git a/src/PHPNative/Renderer/src/Widgets/Checkbox.php b/src/PHPNative/Renderer/src/Widgets/Checkbox.php new file mode 100644 index 0000000..6e6fb89 --- /dev/null +++ b/src/PHPNative/Renderer/src/Widgets/Checkbox.php @@ -0,0 +1,79 @@ +getStyle($view->getId(), $viewport->windowMediaQuery, $view->state, $view->style); + + $myViewport = clone $viewport; + + if(isset($styles[Margin::class]) && $m = $styles[Margin::class]) { + $viewport->x += $m->left; + $viewport->width -= ($m->right + $m->left); + $viewport->y += $m->top; + $viewport->height += ($m->bottom + $m->top); + } + + if(isset($styles[Width::class]) && $m = $styles[Width::class]) { + $viewport->width = $m->value; + } + + if(isset($styles[Height::class]) && $m = $styles[Height::class]) { + $viewport->height = $m->value; + } + + if($thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEBUTTON_UP) { + if( $viewport->x <= $thread->getEvent()->x && + $thread->getEvent()->x <= $viewport->x + $viewport->width && + $viewport->y <= $thread->getEvent()->y && $thread->getEvent()->y <= + $viewport->y + $viewport->height ) { + $view->checked = !$view->checked; + $view->onClick($thread->worker); + } + } + + \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $viewport, $index); + + if($view->checked) { + $texture = \SDL_CreateTexture($viewport->renderPtr, \SDL_PIXELFORMAT_RGBA8888, \SDL_TEXTUREACCESS_TARGET, $viewport->width, $viewport->height); + \SDL_SetRenderTarget($viewport->renderPtr, $texture); + \SDL_SetRenderDrawColor($viewport->renderPtr, 255, 255, 255, 0); + \SDL_RenderClear($viewport->renderPtr); + \SDL_SetRenderDrawColor($viewport->renderPtr, 0, 0, 0, 255); + \SDL_RenderLine($viewport->renderPtr, 2, 2, $viewport->width-4, $viewport->height-4); + \SDL_RenderLine($viewport->renderPtr, 2, $viewport->height-2, $viewport->width-4, 2); + $thread->addToRenderStack(new Item($view->getId(), $texture, new \SDL_FRect($viewport->x, $viewport->y, $viewport->width, $viewport->height), $index)); + + } + + return $viewport; + } +} \ No newline at end of file diff --git a/src/PHPNative/Renderer/src/Widgets/Container.php b/src/PHPNative/Renderer/src/Widgets/Container.php index ae11532..a149897 100644 --- a/src/PHPNative/Renderer/src/Widgets/Container.php +++ b/src/PHPNative/Renderer/src/Widgets/Container.php @@ -17,12 +17,12 @@ use PHPNative\Tailwind\Style\Overflow; use PHPNative\Tailwind\Style\OverflowEnum; use PHPNative\Tailwind\Style\Padding; use PHPNative\Tailwind\Style\StateEnum; +use PhpParser\Builder\Class_; class Container { public static function render(Styles $stylesCache, Thread $thread, Viewport $viewport, \PHPNative\UI\Widget\Container $view, int $index = 0): Viewport { - $styles = $stylesCache->getStyle($view->getId(), $viewport->windowMediaQuery, $view->state, $view->style); $myViewport = clone $viewport; @@ -42,25 +42,6 @@ class Container $viewport->height -= ($m->bottom + $m->top); } - if($thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEMOVE) { - if( $viewport->x <= $thread->getEvent()->x && - $thread->getEvent()->x <= $viewport->x + $viewport->width && - $viewport->y <= $thread->getEvent()->y && - $thread->getEvent()->y <= $viewport->y + $viewport->height ) { - $view->state = StateEnum::hover; - }else{ - $view->state = StateEnum::normal; - } - } - - if($view instanceof \PHPNative\UI\Widget\Button && $thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEBUTTON_UP) { - if( $viewport->x <= $thread->getEvent()->x && - $thread->getEvent()->x <= $viewport->x + $viewport->width && - $viewport->y <= $thread->getEvent()->y && $thread->getEvent()->y <= - $viewport->y + $viewport->height ) { - $view->onClick($thread->worker); - } - } $resultViewPort = null; if($view->getViews() != null) { $view = LayoutParser::sortByStyles($stylesCache, $view, $viewport); @@ -123,22 +104,49 @@ class Container } } if($resultViewPort != null) { + if($view instanceof \PHPNative\UI\Widget\Button && $thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEBUTTON_UP) { + if( $resultViewPort->x <= $thread->getEvent()->x && + $thread->getEvent()->x <= $resultViewPort->x + $resultViewPort->width && + $resultViewPort->y <= $thread->getEvent()->y && $thread->getEvent()->y <= + $resultViewPort->y + $resultViewPort->height ) { + $view->onClick($thread->worker); + } + } + + if($thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEMOVE) { + if( $resultViewPort->x <= $thread->getEvent()->x && + $thread->getEvent()->x <= $resultViewPort->x + $resultViewPort->width && + $resultViewPort->y <= $thread->getEvent()->y && + $thread->getEvent()->y <= $resultViewPort->y + $resultViewPort->height ) { + $view->state = StateEnum::hover; + }else{ + $view->state = StateEnum::normal; + } + } + if (isset($styles[Overflow::class]) && $f = $styles[Overflow::class] && ( $styles[Overflow::class]->y == OverflowEnum::scroll || ($styles[Overflow::class]->y == OverflowEnum::auto) )) { - \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $backgroundViewport, $index); + \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, clone $backgroundViewport, $index); }else{ - if(isset($styles[Margin::class]) && $m = $styles[Margin::class]) { + if(isset($styles[Padding::class]) && $m = $styles[Padding::class]) { $resultViewPort->x -= $m->left; $resultViewPort->width += ($m->right + $m->left); $resultViewPort->y -= $m->top; $resultViewPort->height += ($m->bottom + $m->top); } - \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $resultViewPort, $index); + \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, clone $resultViewPort, $index); + if(isset($styles[Margin::class]) && $m = $styles[Margin::class]) { + //$resultViewPort->x -= $m->left; + $resultViewPort->width += ($m->right + $m->left); + //$resultViewPort->y -= $m->top; + $resultViewPort->height += ($m->bottom + $m->top); + } + return $resultViewPort; } }else{ - \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $viewport, $index); + \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, clone $viewport, $index); } return $myViewport; diff --git a/src/PHPNative/Renderer/src/Widgets/Container/Column.php b/src/PHPNative/Renderer/src/Widgets/Container/Column.php index d84a2b6..508b982 100644 --- a/src/PHPNative/Renderer/src/Widgets/Container/Column.php +++ b/src/PHPNative/Renderer/src/Widgets/Container/Column.php @@ -44,7 +44,8 @@ class Column } if ($found) { - $vp = new Viewport($viewport->windowId, $viewport->windowPtr, $viewport->renderPtr, $viewport->x, $viewport->y, $viewport->width, $height, $viewport->windowWidth, $viewport->windowHeight, $viewport->windowMediaQuery); + $vp = new Viewport($viewport->windowId, $viewport->windowPtr, $viewport->renderPtr, $viewport->x, $viewport->y, $viewport->width, $height, + $viewport->windowWidth, $viewport->windowHeight, 0,0, $viewport->windowMediaQuery); $flexStyles[$subView->getRenderSort()] = $vp; } } diff --git a/src/PHPNative/Renderer/src/Widgets/Container/Row.php b/src/PHPNative/Renderer/src/Widgets/Container/Row.php index 654c070..62f0024 100644 --- a/src/PHPNative/Renderer/src/Widgets/Container/Row.php +++ b/src/PHPNative/Renderer/src/Widgets/Container/Row.php @@ -42,7 +42,8 @@ class Row } if ($found) { - $vp = new Viewport($viewport->windowId, $viewport->windowPtr, $viewport->renderPtr, $viewport->x, $viewport->y, $width, $viewport->height, $viewport->windowWidth, $viewport->windowHeight, $viewport->windowMediaQuery); + $vp = new Viewport($viewport->windowId, $viewport->windowPtr, $viewport->renderPtr, $viewport->x, $viewport->y, $width, + $viewport->height, $viewport->windowWidth, $viewport->windowHeight, 0,0, $viewport->windowMediaQuery); $flexStyles[$subView->getRenderSort()] = $vp; } else { $flexStyles[$subView->getRenderSort()] = clone $viewport; @@ -54,7 +55,7 @@ class Row $subViewPort = $flexStyles[$subView->getRenderSort()]; $subViewPort->x += $viewportX; $vp = Widget::render($stylesCache, $thread, clone $subViewPort, $subView, $index+1); - $viewportX += $vp->width; + $viewportX += ($vp->width); $viewportHeight = $vp->height > $viewportHeight ? $vp->height : $viewportHeight; } diff --git a/src/PHPNative/Renderer/src/Widgets/Text.php b/src/PHPNative/Renderer/src/Widgets/Text.php index ec3f8a2..23029f9 100644 --- a/src/PHPNative/Renderer/src/Widgets/Text.php +++ b/src/PHPNative/Renderer/src/Widgets/Text.php @@ -39,7 +39,11 @@ class Text $font = \SDL_TTF_OpenFont(__DIR__ . DIRECTORY_SEPARATOR . '../../../../../assets/segoe-ui.ttf' , 16); $color = new \SDL_Color(0, 0, 0, 255); } - $surface = \SDL_TTF_RenderText_Blended($font, $view->text, $color); + if($view->text == "") { + $surface = \SDL_TTF_RenderText_Blended($font, " ", $color); + }else{ + $surface = \SDL_TTF_RenderText_Blended($font, $view->text, $color); + } if(!isset($styles[Basis::class]) && !isset($styles[Width::class]) && !isset($styles[Flex::class])) { $viewport->width = $surface->w; @@ -48,22 +52,30 @@ class Text $viewport->height = $surface->h; } + $returnViewport = clone $viewport; + $backdropViewport = clone $viewport; + if(isset($styles[Margin::class]) && $m = $styles[Margin::class]) { $viewport->x += $m->left; $viewport->y += $m->top; + $backdropViewport->x += $m->left; + $backdropViewport->y += $m->top; if(isset($styles[Basis::class]) || isset($styles[Width::class]) || isset($styles[Flex::class])) { $viewport->width -= ($m->right + $m->left); + $backdropViewport->width -= ($m->right + $m->left); + }else{ + $viewport->width += ($m->right + $m->left); + $returnViewport->width += ($m->right + $m->left); } if(isset($styles[Height::class])) { $viewport->height -= ($m->bottom + $m->top); + $backdropViewport->height -= ($m->bottom + $m->top); }else{ $viewport->height += ($m->bottom + $m->top); + $returnViewport->height += ($m->bottom + $m->top); } } - $backdropViewport = clone $viewport; - $backdropViewport->height = $surface->h; - if($thread->getEvent() && $thread->getEvent()->getType() === EventType::MOUSEMOVE) { if( $viewport->x <= $thread->getEvent()->x && $thread->getEvent()->x <= $viewport->x + $viewport->width && @@ -77,15 +89,21 @@ class Text if(isset($styles[Padding::class]) && $m = $styles[Padding::class]) { $viewport->x += $m->left; - $viewport->width += ($m->right + $m->left); $viewport->y += $m->top; - if(!isset($styles[Height::class])) { - $viewport->height += ($m->bottom + $m->top); - } - $backdropViewport->height += ($m->top + $m->bottom); - if(!isset($styles[Basis::class]) && !isset($styles[Width::class]) && !isset($styles[Flex::class])) { + if(isset($styles[Basis::class]) || isset($styles[Width::class]) || isset($styles[Flex::class])) { + $viewport->width -= ($m->right + $m->left); + }else{ + $viewport->width += ($m->right + $m->left); + $returnViewport->width += ($m->right + $m->left); $backdropViewport->width += ($m->left + $m->right); } + if(isset($styles[Height::class])) { + $viewport->height -= ($m->bottom + $m->top); + }else{ + $viewport->height += ($m->bottom + $m->top); + $returnViewport->height += ($m->bottom + $m->top); + $backdropViewport->height += ($m->bottom + $m->top); + } } \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $backdropViewport, $index); @@ -94,7 +112,7 @@ class Text $textureFont = \SDL_CreateTextureFromSurface($viewport->renderPtr, $surface); if($surface->w > $viewport->width) { - $thread->addToRenderStack(new Item($textureFont, + $thread->addToRenderStack(new Item($view->getId(), $textureFont, new \SDL_FRect($viewport->x, $viewport->y, $surface->w, $surface->h), 3, new \SDL_FRect(0, 0, $viewport->width, $surface->h) )); @@ -126,7 +144,7 @@ class Text } } - return $viewport; + return $returnViewport; } } \ No newline at end of file diff --git a/src/PHPNative/Renderer/src/Widgets/TextEdit.php b/src/PHPNative/Renderer/src/Widgets/TextEdit.php index 16185bc..5089ce2 100644 --- a/src/PHPNative/Renderer/src/Widgets/TextEdit.php +++ b/src/PHPNative/Renderer/src/Widgets/TextEdit.php @@ -32,6 +32,8 @@ class TextEdit $styles = $stylesCache->getStyle($view->getId(), $viewport->windowMediaQuery, $view->state, $view->style); + $myViewport = clone $viewport; + if(isset($styles[Text::class]) && $t = $styles[Text::class]) { $font = \SDL_TTF_OpenFont(__DIR__ . DIRECTORY_SEPARATOR . '../../../../../assets/segoe-ui.ttf' , $t->size); if($t->color->red != -1) { @@ -43,6 +45,7 @@ class TextEdit $font = \SDL_TTF_OpenFont(__DIR__ . DIRECTORY_SEPARATOR . '../../../../../assets/segoe-ui.ttf' , 16); $color = new \SDL_Color(0, 0, 0, 255); } + if($view->value == "" && $view->placeholder != null) { $color = new \SDL_Color(0, 0, 0, 128); $surface = \SDL_TTF_RenderText_Blended($font, $view->placeholder, $color); @@ -55,12 +58,14 @@ class TextEdit } $viewport->height = $surface->h; + $myViewport->height = $surface->h; if(isset($styles[Margin::class]) && $m = $styles[Margin::class]) { $viewport->x += $m->left; $viewport->width -= ($m->right + $m->left); $viewport->y += $m->top; $viewport->height += ($m->bottom + $m->top); + $myViewport->height += ($m->bottom + $m->top); } $backdropViewport = clone $viewport; @@ -131,7 +136,9 @@ class TextEdit $viewport->width -= ($m->right + $m->left); $viewport->y += $m->top; $viewport->height += ($m->bottom + $m->top); + $myViewport->height += ($m->bottom + $m->top); $backdropViewport->height += ($m->top + $m->bottom); + } \PHPNative\Renderer\Visuals\Visuals::render($view->getId(), $thread, $styles, $backdropViewport, $index); @@ -145,9 +152,9 @@ class TextEdit Cursor::render($view->getId(), $thread, $styles, $viewportCorsor, $index); } - if((isset($styles[Width::class]) && $m = $styles[Width::class]) || (isset($styles[Basis::class]) && $m = $styles[Basis::class])) { + $textureFont = \SDL_CreateTextureFromSurface($viewport->renderPtr, $surface); - $textureFont = \SDL_CreateTextureFromSurface($viewport->renderPtr, $surface); + if((isset($styles[Width::class]) && $m = $styles[Width::class]) || (isset($styles[Basis::class]) && $m = $styles[Basis::class])) { if($surface->w > $viewport->width) { $thread->addToRenderStack(new Item($view->getId(), $textureFont, new \SDL_FRect($viewport->x, $viewport->y, $viewport->width, $surface->h), 3, @@ -164,10 +171,16 @@ class TextEdit } }else{ - + if(isset($styles[Text::class]) && $styles[Text::class]->align == AlignEnum::center) { + $thread->addToRenderStack(new Item($view->getId(), $textureFont, new \SDL_FRect($viewport->x + (($viewport->width - $surface->w) /2), $viewport->y, $surface->w, $surface->h), ++$index)); + }elseif(isset($styles[Text::class]) && $styles[Text::class]->align == AlignEnum::right) { + $thread->addToRenderStack(new Item($view->getId(), $textureFont, new \SDL_FRect($viewport->x + ($viewport->width - $surface->w), $viewport->y, $surface->w, $surface->h), ++$index)); + }else{ + $thread->addToRenderStack(new Item($view->getId(), $textureFont, new \SDL_FRect($viewport->x, $viewport->y, $surface->w, $surface->h), ++$index)); + } } - return $viewport; + return $myViewport; } private static function str_delete(string $str, int $pos = 0) { diff --git a/src/PHPNative/UI/src/Widget/Button.php b/src/PHPNative/UI/src/Widget/Button.php index 27e5084..7973732 100644 --- a/src/PHPNative/UI/src/Widget/Button.php +++ b/src/PHPNative/UI/src/Widget/Button.php @@ -12,9 +12,9 @@ class Button extends Container implements View { use Click; - public function __construct(public ?Views $views = null, string $style = "") + public function __construct(public ?string $id = null, public ?Views $views = null, string $style = "") { - parent::__construct(style: $style, views: $views); + parent::__construct(id: $id, style: $style, views: $views); } public function getViews(): ?\PHPNative\UI\Collection\Views diff --git a/src/PHPNative/UI/src/Widget/Checkbox.php b/src/PHPNative/UI/src/Widget/Checkbox.php new file mode 100644 index 0000000..589ddd1 --- /dev/null +++ b/src/PHPNative/UI/src/Widget/Checkbox.php @@ -0,0 +1,33 @@ +style = $style; + return $this; + } + + public function getViews(): ?\PHPNative\UI\Collection\Views + { + } +}