diff --git a/examples/collector/assets/Scenes/SettingsScene.php b/examples/collector/assets/Scenes/SettingsScene.php index c6ac782..fc8dfe5 100644 --- a/examples/collector/assets/Scenes/SettingsScene.php +++ b/examples/collector/assets/Scenes/SettingsScene.php @@ -34,7 +34,6 @@ public function awake(): void $screenWidth = $sceneManager->getSettings('screen_width') ?? DEFAULT_SCREEN_WIDTH; $screenHeight = $sceneManager->getSettings('screen_height') ?? DEFAULT_SCREEN_HEIGHT; - Debug::log(var_export($this->settings, true)); $leftMargin = round($screenWidth / 2 - $settingsMenuWidth / 2); $topMargin = round($screenHeight / 2 - $settingsMenuHeight / 2); $settingsMenuBorderPack = new BorderPack(Path::join(Path::getVendorAssetsDirectory(), 'border-packs', 'slim.border.php')); diff --git a/logo.png b/logo.png index d95815a..6983136 100644 Binary files a/logo.png and b/logo.png differ diff --git a/src/Core/Behaviours/SimpleBackListener.php b/src/Core/Behaviours/SimpleBackListener.php index b565e14..b3648ac 100644 --- a/src/Core/Behaviours/SimpleBackListener.php +++ b/src/Core/Behaviours/SimpleBackListener.php @@ -32,7 +32,6 @@ public function onUpdate(): void $this->nextPrintTime = time() + 1; } if (Input::isAnyKeyPressed($this->backKeys)) { - Debug::log('Going back'); loadPreviousScene(); } } diff --git a/src/Core/Scenes/SceneManager.php b/src/Core/Scenes/SceneManager.php index a8f44f6..20bcfa0 100644 --- a/src/Core/Scenes/SceneManager.php +++ b/src/Core/Scenes/SceneManager.php @@ -111,11 +111,6 @@ public function hasScene(int|string $index): bool { $sceneList = $this->scenes->toArray(); - Debug::log(var_export([ - "index" => $index, - "total" => $this->scenes->count(), - "scenes" => array_map(fn(SceneInterface $scene) => $scene->getName(), $sceneList) - ], true)); foreach ($sceneList as $i => $scene) { if (is_int($index) && $i === $index) { return true; diff --git a/src/Core/Scenes/TitleScene.php b/src/Core/Scenes/TitleScene.php index ed9906e..1cfe01a 100644 --- a/src/Core/Scenes/TitleScene.php +++ b/src/Core/Scenes/TitleScene.php @@ -121,6 +121,7 @@ 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); } @@ -263,10 +264,12 @@ public function addMenuItems(MenuItemInterface ...$item): self * Resolves the screen width even while the scene is still in awake() and local scene settings are empty. * * @return int + * @throws Exception */ private function resolveScreenWidth(): int { return $this->resolveDimension( + get_screen_width(), $this->screenWidth, $this->sceneManager->getSettings('screen_width'), $this->getSettings('screen_width'), diff --git a/src/Core/Traits/DimensionTrait.php b/src/Core/Traits/DimensionTrait.php index ed2fb73..8d4a80d 100644 --- a/src/Core/Traits/DimensionTrait.php +++ b/src/Core/Traits/DimensionTrait.php @@ -7,58 +7,58 @@ */ trait DimensionTrait { - /** - * The width of the object. - * - * @var int - */ - protected int $width = 0; - /** - * The height of the object. - * - * @var int - */ - protected int $height = 0; + /** + * The width of the object. + * + * @var int + */ + protected int $width = 0; + /** + * The height of the object. + * + * @var int + */ + protected int $height = 0; - /** - * Get the width of the object. - * - * @return int The width of the object. - */ - public function getWidth(): int - { - return $this->width; - } + /** + * Get the width of the object. + * + * @return int The width of the object. + */ + public function getWidth(): int + { + return $this->width; + } - /** - * Set the width of the object. - * - * @param int $width The width of the object. - * @return void - */ - public function setWidth(int $width): void - { - $this->width = $width; - } + /** + * Set the width of the object. + * + * @param int $width The width of the object. + * @return void + */ + public function setWidth(int $width): void + { + $this->width = $width; + } - /** - * Get the height of the object. - * - * @return int The height of the object. - */ - public function getHeight(): int - { - return $this->height; - } + /** + * Get the height of the object. + * + * @return int The height of the object. + */ + public function getHeight(): int + { + return $this->height; + } - /** - * Set the height of the object. - * - * @param int $height The height of the object. - * @return void - */ - public function setHeight(int $height): void - { - $this->height = $height; - } + /** + * Set the height of the object. + * + * @param int $height The height of the object. + * @return void + */ + public function setHeight(int $height): void + { + $this->height = $height; + } } \ No newline at end of file diff --git a/src/Game.php b/src/Game.php index e91ac04..da0d847 100644 --- a/src/Game.php +++ b/src/Game.php @@ -116,7 +116,7 @@ class Game implements ObservableInterface */ private UIManager $uiManager; /** - * @var Cursor $consoleCursor + * @var Cursor|null $consoleCursor */ private ?Cursor $consoleCursor = null; /** @@ -139,15 +139,36 @@ class Game implements ObservableInterface private ?SplashScreen $splashScreen = null; private bool $consoleInitialized = false; + public int $screenWidth { + get { + if (is_null($this->resolvedScreenWidth)) { + return exec('tput cols') ?: 80; + } + + return $this->resolvedScreenWidth; + } + } + + public int $screenHeight { + get { + if (is_null($this->resolvedScreenWidth)) { + return exec('tput cols') ?: 80; + } + + return $this->resolvedScreenHeight; + } + } + /** * Game constructor. * * @param string $name The name of the game. - * @param int $screenWidth The width of the game screen. - * @param int $screenHeight The height of the game screen. - * @throws Exception + * @param int|null $resolvedScreenWidth The width of the game screen. + * @param int|null $resolvedScreenHeight The height of the game screen. + * @param string|null $workingDirectory + * @throws IOException */ - public function __construct(private readonly string $name, private readonly int $screenWidth = DEFAULT_SCREEN_WIDTH, private readonly int $screenHeight = DEFAULT_SCREEN_HEIGHT, private readonly ?string $workingDirectory = null) + public function __construct(private readonly string $name, private readonly ?int $resolvedScreenWidth = null, private readonly ?int $resolvedScreenHeight = null, private readonly ?string $workingDirectory = null) { try { $this->initializeObservers(); diff --git a/src/IO/Console/Console.php b/src/IO/Console/Console.php index 0f0b818..7ad3762 100644 --- a/src/IO/Console/Console.php +++ b/src/IO/Console/Console.php @@ -277,6 +277,36 @@ public static function getSize(bool $force = false): Rect return new Rect(new Vector2(1, 1), new Vector2($width, $height)); } + /** + * Returns the current logical viewport size (game screen dimensions). + * + * @return Rect The logical viewport size as a Rect starting at (1,1). + */ + public static function getLogicalSize(): Rect + { + return new Rect(new Vector2(1, 1), new Vector2(self::$logicalWidth, self::$logicalHeight)); + } + + /** + * Returns the logical viewport width. + * + * @return int + */ + public static function getLogicalWidth(): int + { + return max(1, self::$logicalWidth); + } + + /** + * Returns the logical viewport height. + * + * @return int + */ + public static function getLogicalHeight(): int + { + return max(1, self::$logicalHeight); + } + /** * Refreshes the logical viewport within the current terminal size. * diff --git a/src/UI/GUITexture/GUITexture.php b/src/UI/GUITexture/GUITexture.php index e8e240f..9aaba49 100644 --- a/src/UI/GUITexture/GUITexture.php +++ b/src/UI/GUITexture/GUITexture.php @@ -105,7 +105,9 @@ public function setSize(Vector2 $size): void public function render(): void { - $this->renderAt($this->position->getX(), $this->position->getY()); + if ($this->isActive()) { + $this->renderAt($this->position->getX(), $this->position->getY()); + } } public function renderAt(?int $x = null, ?int $y = null): void diff --git a/src/UI/UIElement.php b/src/UI/UIElement.php index 0188e0e..091e724 100644 --- a/src/UI/UIElement.php +++ b/src/UI/UIElement.php @@ -151,6 +151,7 @@ public function setTag(string $tag): void */ public function activate(): void { + $this->render(); $this->active = true; } @@ -159,6 +160,7 @@ public function activate(): void */ public function deactivate(): void { + $this->erase(); $this->active = false; } diff --git a/src/Util/Constants.php b/src/Util/Constants.php index baa4f57..e45e423 100644 --- a/src/Util/Constants.php +++ b/src/Util/Constants.php @@ -1,29 +1,29 @@ getWidth(); } + + if (!function_exists('get_terminal_width')) { + /** + * Returns the actual terminal width (freshly polled). + * + * @return int + * @throws Exception + */ + function get_terminal_width(): int + { + return Console::getSize(force: true)->getWidth(); + } + } + + if (!function_exists('get_logical_width')) { + /** + * Returns the game's logical viewport width (may be different than terminal width). + * + * @return int + */ + function get_logical_width(): int + { + return Console::getLogicalWidth(); + } + } } if (!function_exists('get_configured_screen_width')) { @@ -459,6 +484,31 @@ function get_screen_height(): int { return Console::getSize()->getHeight(); } + + if (!function_exists('get_terminal_height')) { + /** + * Returns the actual terminal height (freshly polled). + * + * @return int + * @throws Exception + */ + function get_terminal_height(): int + { + return Console::getSize(force: true)->getHeight(); + } + } + + if (!function_exists('get_logical_height')) { + /** + * Returns the game's logical viewport height. + * + * @return int + */ + function get_logical_height(): int + { + return Console::getLogicalHeight(); + } + } } if (!function_exists('get_configured_screen_height')) {