Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/Core/Scenes/TitleScene.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,28 @@ class TitleScene extends AbstractScene
*/
protected int|string $newGameSceneTarget = 1;

protected int $uiHeight {
get {
$gap = 1;
return $this->titleText->getHeight() + $gap + $this->menu->getItems()->count() + 2; // 1 for each border
}
}

/**
* @inheritDoc
* @throws Exception
*/
public function awake(): void
{
$gameName = getGameName() ?? $this->name;
$screenWidth = $this->resolveScreenWidth();
$titleTextHeight = 5;

$this->titleText = new Text(
scene: $this,
name: $gameName,
position: new Vector2(0, $this->titleTopMargin),
size: new Vector2($this->resolveScreenWidth(), 5)
size: new Vector2($screenWidth, $titleTextHeight)
);
$this->titleText->setFontName(FontName::BIG->value);
$this->setTitleText($gameName);
Expand All @@ -100,7 +109,8 @@ public function awake(): void
$gameName = $_ENV['GAME_NAME'] ?? $this->name;
}

$this->menu = new Menu(title: $gameName, description: 'q:quit', dimensions: new Rect(new Vector2($this->getMenuLeftMargin(), $this->getMenuTopMargin()), new Vector2($this->menuWidth, $this->menuHeight)), cancelKey: [KeyCode::Q, KeyCode::q], onCancel: fn() => quitGame());
$menuDimensions = new Rect(new Vector2($this->getMenuLeftMargin(), $this->getMenuTopMargin()), new Vector2($this->menuWidth, $this->menuHeight));
$this->menu = new Menu(title: $gameName, description: 'q:quit', dimensions: $menuDimensions, cancelKey: [KeyCode::Q, KeyCode::q], onCancel: fn() => quitGame());
$this->menu->addItem(new MenuItem(label: 'New Game', description: 'Start a new game', icon: '🎮'));
$this->menu->addItem(new MenuItem(label: 'Quit', description: 'Quit the game', icon: '🚪', callback: function () {
quitGame();
Expand All @@ -121,7 +131,6 @@ public function awake(): void
private function getMenuLeftMargin(): int
{
$screenWidth = $this->resolveScreenWidth();
Debug::log("Screen width: $screenWidth");
return (int)round($screenWidth / 2) - (int)round($this->menuWidth / 2);
}

Expand All @@ -141,10 +150,31 @@ private function getMenuTopMargin(): int
public function setTitleText(string $text): self
{
$this->titleText->setText($text);
usleep(300000);
// Ensure the Text has fresh dimensions — Figlet/font rendering or
// console init may have completed after setText() ran. Refresh
// dimensions if the helper exists so getWidth() is reliable here.
try {
if (method_exists($this->titleText, 'refreshDimensions')) {
$this->titleText->refreshDimensions();
}
} catch (\Throwable $_) {
// best-effort
}

$screenWidth = $this->resolveScreenWidth();
$this->titleLeftMargin = round(($screenWidth / 2) - ($this->titleText->getWidth() / 2));
$this->titleLeftMargin = (int)intdiv(max(0, $screenWidth - $this->titleText->getWidth()), 2);
$this->titleTopMargin = self::TOP_MARGIN_OFFSET;
$this->titleText->setPosition(new Vector2(round($this->titleLeftMargin), round($this->titleTopMargin)));
$this->titleText->setPosition(new Vector2($this->titleLeftMargin, $this->titleTopMargin));
Debug::log(var_export([
'screenWidth' => $screenWidth,
'titleText' => $this->titleText->getText(),
'titleTextWidth' => $this->titleText->getWidth(),
'titleLeftMargin' => $this->titleLeftMargin,
'titleTopMargin' => $this->titleTopMargin,
'titlePosition' => (string)$this->titleText->getPosition(),
'timestamp' => time(),
], true));

return $this;
}
Expand Down Expand Up @@ -269,7 +299,6 @@ public function addMenuItems(MenuItemInterface ...$item): self
private function resolveScreenWidth(): int
{
return $this->resolveDimension(
get_screen_width(),
$this->screenWidth,
$this->sceneManager->getSettings('screen_width'),
$this->getSettings('screen_width'),
Expand Down
2 changes: 1 addition & 1 deletion src/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Game implements ObservableInterface
public int $screenHeight {
get {
if (is_null($this->resolvedScreenWidth)) {
return exec('tput cols') ?: 80;
return exec('tput lines') ?: 40;
}

return $this->resolvedScreenHeight;
Expand Down
Loading
Loading