Skip to content

Commit

Permalink
Some work on GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Sep 9, 2024
1 parent 7b217d4 commit b53b1cc
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 19 deletions.
48 changes: 37 additions & 11 deletions src/FlyUI/FUIButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class FUIButton extends FUIView

public string $buttonId;

/**
* If set to true, the button will take the full width of the parent container
* instead of basing its size on the text width
*/
public bool $fullWidth = false;

/**
* Constructs a new view
*/
Expand All @@ -30,7 +36,7 @@ public function __construct(
?string $buttonId = null
)
{
parent::__construct(FlyUI::$instance->theme->buttonPadding);
parent::__construct(FlyUI::$instance->theme->buttonPadding->copy());

$this->backgroundColor = FlyUI::$instance->theme->buttonPrimaryBackgroundColor;
$this->hoverBackgroundColor = FlyUI::$instance->theme->buttonPrimaryHoverBackgroundColor;
Expand All @@ -52,6 +58,15 @@ public function setId(string $id) : self
return $this;
}

/**
* Sets the button to full width mode
*/
public function setFullWidth(bool $fullWidth = true) : self
{
$this->fullWidth = $fullWidth;
return $this;
}

/**
* Returns the height of the current view and its children
* This is used for layouting purposes
Expand All @@ -68,11 +83,19 @@ public function getEstimatedHeight(FUIRenderContext $ctx) : float
*/
public function getEstimatedWidth(FUIRenderContext $ctx) : float
{
$ctx->ensureFontFace('inter-semibold');
if ($this->fullWidth) {
return $ctx->containerSize->x;
}

$ctx->ensureSemiBoldFontFace();
$ctx->vg->fontSize($this->fontSize);
return $ctx->vg->textBounds(0, 0, $this->text) + $this->padding->x * 2;
}

private const BUTTON_PRESS_NONE = 0;
private const BUTTON_PRESS_STARTED = 1;
private const BUTTON_PRESS_ENDED = 2;

/**
* Renders the current view using the provided context
*/
Expand All @@ -85,12 +108,12 @@ public function render(FUIRenderContext $ctx) : float
$ctx->containerSize->y = $height;
$ctx->containerSize->x = $width;

// Check if the mouse is inside the button
// check if the mouse is inside the button
$isInside = $ctx->isHovered();

static $fuiButtonPressStates = [];
if (!isset($fuiButtonPressStates[$this->buttonId])) {
$fuiButtonPressStates[$this->buttonId] = false;
$fuiButtonPressStates[$this->buttonId] = self::BUTTON_PRESS_NONE;
}

if ($isInside && $ctx->input->isMouseButtonPressed(MouseButton::LEFT)) {
Expand All @@ -109,15 +132,16 @@ public function render(FUIRenderContext $ctx) : float
);
$ctx->vg->stroke();


if (!$fuiButtonPressStates[$this->buttonId]) {
$fuiButtonPressStates[$this->buttonId] = true;
if ($this->onClick !== null) {
($this->onClick)();
}
if ($fuiButtonPressStates[$this->buttonId] === self::BUTTON_PRESS_NONE) {
$fuiButtonPressStates[$this->buttonId] = self::BUTTON_PRESS_STARTED;
}
} else if ($isInside && $fuiButtonPressStates[$this->buttonId] === self::BUTTON_PRESS_STARTED) {
$fuiButtonPressStates[$this->buttonId] = self::BUTTON_PRESS_ENDED;
if ($this->onClick) {
($this->onClick)();
}
} else {
$fuiButtonPressStates[$this->buttonId] = false;
$fuiButtonPressStates[$this->buttonId] = self::BUTTON_PRESS_NONE;
}

// render the button background
Expand Down Expand Up @@ -146,6 +170,8 @@ public function render(FUIRenderContext $ctx) : float
// wile ignoring letters like 'g' or 'y' that go below the baseline
$ctx->origin->y = floor($ctx->origin->y + $this->fontSize * 0.15);

$ctx->ensureSemiBoldFontFace();
$ctx->vg->fontSize($this->fontSize);
$ctx->vg->textAlign(VGAlign::CENTER | VGAlign::MIDDLE);
$ctx->vg->fillColor($this->textColor);
$ctx->vg->text($ctx->origin->x, $ctx->origin->y, $this->text);
Expand Down
2 changes: 1 addition & 1 deletion src/FlyUI/FUICard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FUICard extends FUILayout
*/
public function __construct()
{
parent::__construct(FlyUI::$instance->theme->cardPadding);
parent::__construct(FlyUI::$instance->theme->cardPadding->copy());

$this->backgroundColor = FlyUI::$instance->theme->cardBackgroundColor;
$this->borderRadius = FlyUI::$instance->theme->cardBorderRadius;
Expand Down
9 changes: 9 additions & 0 deletions src/FlyUI/FUILayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ public function verticalFit() : self
return $this;
}

/**
* Sets the vertical spacing between the children
*/
public function spacingY(float $spacing) : self
{
$this->spacingY = $spacing;
return $this;
}

/**
* Returns the height of the content aka the sum of all children
*/
Expand Down
21 changes: 19 additions & 2 deletions src/FlyUI/FUIRenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FUIRenderContext
/**
* Current font face
*/
public string $fontFace = '';
private string $fontFace = '';

/**
* Returns true if the mouse is currently hovering over the current bounds
Expand Down Expand Up @@ -71,6 +71,22 @@ public function ensureFontFace(string $fontFace) : void
$this->fontFace = $fontFace;
}
}

/**
* Ensures the default "regular" font face is set
*/
public function ensureRegularFontFace() : void
{
$this->ensureFontFace($this->theme->regularFont);
}

/**
* Ensures the default "semi bold" font face is set
*/
public function ensureSemiBoldFontFace() : void
{
$this->ensureFontFace($this->theme->semiBoldFont);
}

/**
* Sets a static value (persistant data, over multiple frames)
Expand All @@ -95,7 +111,8 @@ public function getStaticValue(string $key, mixed $default = null) : mixed
*/
public function __construct(
public VGContext $vg,
public Input $input
public Input $input,
public FUITheme $theme
)
{
$this->origin = new Vec2(0, 0);
Expand Down
33 changes: 32 additions & 1 deletion src/FlyUI/FUIText.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@

class FUIText extends FUIView
{
/**
* The font size of the text
*/
public float $fontSize;

/**
* Should the text be bold
*/
public bool $isBold = false;

/**
* Constructs a new view
*/
Expand All @@ -23,6 +31,24 @@ public function __construct(
$this->fontSize = FlyUI::$instance->theme->fontSize;
}

/**
* Sets the font size of the text
*/
public function fontSize(float $size) : self
{
$this->fontSize = $size;
return $this;
}

/**
* Sets the text to be bold
*/
public function bold() : self
{
$this->isBold = true;
return $this;
}

/**
* Returns the height of the current view and its children
* This is used for layouting purposes
Expand Down Expand Up @@ -51,7 +77,12 @@ public function render(FUIRenderContext $ctx) : float
// $ctx->vg->rect($ctx->origin->x, $ctx->origin->y, $ctx->containerSize->x, $height - 1);
// $ctx->vg->fill();

$ctx->ensureFontFace('inter-regular');
if ($this->isBold) {
$ctx->ensureSemiBoldFontFace();
} else {
$ctx->ensureRegularFontFace();
}

$ctx->vg->textAlign(VGAlign::LEFT | VGAlign::TOP);
$ctx->vg->fontSize($this->fontSize);
$ctx->vg->fillColor(VGColor::black());
Expand Down
14 changes: 14 additions & 0 deletions src/FlyUI/FUITheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ class FUITheme
* ------------------------------ General ---------------------------------
*/

/**
* Default font used for text, buttons, etc.
* FlyUI loads the `inter` font automatically, if you want to use a different font you
* are responsible for loading it yourself.
*/
public string $regularFont = 'inter-regular';

/**
* Default "semi bold" font.
* FlyUI loads the `inter` font automatically, if you want to use a different font you
* are responsible for loading it yourself.
*/
public string $semiBoldFont = 'inter-semibold';

/**
* The general padding used to space elements
*/
Expand Down
6 changes: 3 additions & 3 deletions src/FlyUI/FlyUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ private function internalBeginFrame(Vec2 $resolution, float $contentScale = 1.0)
*/
private function internalEndFrame() : void
{
$ctx = new FUIRenderContext($this->vgContext, $this->input);
$ctx = new FUIRenderContext($this->vgContext, $this->input, $this->theme);
$ctx->containerSize = $this->currentResolution;

$this->vgContext->reset();

// set the inter font
$this->vgContext->fontFace('inter-regular');
// set the default font face
$ctx->ensureRegularFontFace();

// let all views render itself
$this->viewTree[0]->render($ctx);
Expand Down
15 changes: 14 additions & 1 deletion src/Runtime/GameLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class GameLoop
*/
public int $tickTimeSampleCount = 60;

/**
* If set to true, the game loop will force stop, even if the delegate doesn't want to.
*/
private bool $forceStop = false;

/**
* Constructor
*
Expand Down Expand Up @@ -208,6 +213,14 @@ public function getAverageFrameTimeFormatted() : string
return $this->formatNStoHuman((int) $this->getAverageFrameTime());
}

/**
* Force stops the game loop.
*/
public function forceStop() : void
{
$this->forceStop = true;
}

/**
* Starts and runs the game loop
*
Expand All @@ -225,7 +238,7 @@ public function start() : void
$frameTickCount = 0;

// loop forever until the delegate tells us to stop
while (!$this->delegate->shouldStop())
while (!$this->delegate->shouldStop() && !$this->forceStop)
{
// determine delta time since last frame
$deltaTime = Clock::now64() - $lastFrameStart;
Expand Down

0 comments on commit b53b1cc

Please sign in to comment.