Skip to content

Commit

Permalink
Added FlyUI render pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Sep 9, 2024
1 parent ed0e816 commit 7b217d4
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/FlyUI/FlyUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public static function checkbox(string $text, bool &$checked) : FUICheckbox

private float $currentContentScale = 1.0;

/**
* If true, FLyUI will create and end VGContext frames itself
*/
private bool $selfManageVGContext = false;

/**
* Constructor
*/
public function __construct(
private VGContext $vgContext,
private Dispatcher $dispatcher,
Expand All @@ -154,6 +162,16 @@ public function __construct(
$this->theme = $theme ?? new FUITheme();
}

/**
* Sets if FlyUI should manage the VGContext frames itself
*
* When enabled FlyUI will call beginFrame and endFrame itself.
*/
public function setSelfManageVGContext(bool $value) : void
{
$this->selfManageVGContext = $value;
}

/**
* Adds a view to the view tree
*/
Expand Down Expand Up @@ -212,6 +230,11 @@ private function internalBeginFrame(Vec2 $resolution, float $contentScale = 1.0)
// push the root view
$root = new FUIView();
$this->pushView($root);

// begin the VGContext frame
if ($this->selfManageVGContext) {
$this->vgContext->beginFrame($resolution->x, $resolution->y, $contentScale);
}
}

/**
Expand All @@ -229,5 +252,10 @@ private function internalEndFrame() : void

// let all views render itself
$this->viewTree[0]->render($ctx);

// end the VGContext frame
if ($this->selfManageVGContext) {
$this->vgContext->endFrame();
}
}
}
52 changes: 52 additions & 0 deletions src/Graphics/Rendering/Pass/FlyUIPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace VISU\Graphics\Rendering\Pass;

use Closure;
use VISU\FlyUI\FlyUI;
use VISU\Graphics\Rendering\PipelineContainer;
use VISU\Graphics\Rendering\PipelineResources;
use VISU\Graphics\Rendering\RenderPass;
use VISU\Graphics\Rendering\RenderPipeline;
use VISU\Graphics\Rendering\Resource\RenderTargetResource;

class FlyUIPass extends RenderPass
{
public function __construct(
private RenderTargetResource $renderTargetRes,
private Closure $uiRenderCallback,
)
{
}

/**
* Executes the render pass
*/
public function setup(RenderPipeline $pipeline, PipelineContainer $data): void
{
$pipeline->writes($this, $this->renderTargetRes);
}

/**
* Executes the render pass
*/
public function execute(PipelineContainer $data, PipelineResources $resources): void
{
$renderTarget = $resources->getRenderTarget($this->renderTargetRes);

// activate the render target
$resources->activateRenderTarget($this->renderTargetRes);

// begin a FlyUI frame
FlyUI::beginFrame($renderTarget->effectiveSizeVec(), $renderTarget->contentScaleX);

// execute the UI render callback
($this->uiRenderCallback)();

// end the FlyUI frame
FlyUI::endFrame();

// reset the GL state as the VGContext might have changed it
$resources->gl->reset();
}
}
48 changes: 48 additions & 0 deletions src/Graphics/Rendering/Renderer/FlyUIRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace VISU\Graphics\Rendering\Renderer;

use Closure;
use GL\VectorGraphics\VGContext;
use VISU\FlyUI\FlyUI;
use VISU\Graphics\Rendering\Pass\FlyUIPass;
use VISU\Graphics\Rendering\RenderPipeline;
use VISU\Graphics\Rendering\Resource\RenderTargetResource;
use VISU\Graphics\Rendering\Resource\TextureResource;
use VISU\OS\Input;
use VISU\Signal\Dispatcher;

class FlyUIRenderer
{
/**
* Constructor
*/
public function __construct(
private VGContext $vg,
private Dispatcher $dispatcher,
private Input $input,
)
{
FlyUI::initailize($vg, $dispatcher, $input);
FlyUI::$instance->setSelfManageVGContext(true);
}

/**
* Attaches a render pass to the pipeline
*
* @param RenderPipeline $pipeline
* @param RenderTargetResource $renderTarget
* @param TextureResource $depthTexture
*/
public function attachPass(
RenderPipeline $pipeline,
RenderTargetResource $renderTarget,
Closure $uiRenderCallback
) : void
{
$pipeline->addPass(new FlyUIPass(
$renderTarget,
$uiRenderCallback
));
}
}

0 comments on commit 7b217d4

Please sign in to comment.