sdl3/examples/simple_two_windows.php
2025-10-24 11:39:17 +02:00

136 lines
3.6 KiB
PHP

<?php
// Enable event debugging (optional - set to false to disable)
define('DEBUG_EVENTS', true);
require_once __DIR__ . '/../vendor/autoload.php';
use PHPNative\Framework\Application;
use PHPNative\Ui\Widget\Button;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Label;
echo "Starting two-window test application...\n";
echo 'DEBUG_EVENTS: ' . (DEBUG_EVENTS ? 'enabled' : 'disabled') . "\n";
// Create application
$app = new Application();
// Frame counter for window 1
$window1FrameCount = 0;
echo "Creating Window 1...\n";
$window1 = $app->createWindow('Window 1', 400, 300, 100, 100);
$container1 = new Container('flex flex-col p-6 gap-4 bg-blue-100');
$title1 = new Label(
text: 'Window 1',
style: 'text-2xl text-blue-900',
);
$frameLabel1 = new Label(
text: 'Frame: 0',
style: 'text-base text-blue-700',
);
$button1 = new Button(
text: 'Click Me (Window 1)',
style: 'bg-blue-500 hover:bg-blue-700 text-white p-4 rounded',
);
$clickCount1 = 0;
$button1->setOnClick(function () use (&$clickCount1, $button1) {
$clickCount1++;
$button1->setText("Clicked {$clickCount1} times");
echo "Window 1 button clicked! Count: {$clickCount1}\n";
});
$container1->addComponent($title1);
$container1->addComponent($frameLabel1);
$container1->addComponent($button1);
$window1->setRoot($container1);
// Frame counter for window 2
$window2FrameCount = 0;
echo "Creating Window 2...\n";
$window2 = $app->createWindow('Window 2', 400, 300, 520, 100);
$container2 = new Container('flex flex-col p-6 gap-4 bg-green-100');
$title2 = new Label(
text: 'Window 2',
style: 'text-2xl text-green-900',
);
$frameLabel2 = new Label(
text: 'Frame: 0',
style: 'text-base text-green-700',
);
$button2 = new Button(
text: 'Click Me (Window 2)',
style: 'bg-green-500 hover:bg-green-700 text-white p-4 rounded',
);
$clickCount2 = 0;
$button2->setOnClick(function () use (&$clickCount2, $button2) {
$clickCount2++;
$button2->setText("Clicked {$clickCount2} times");
echo "Window 2 button clicked! Count: {$clickCount2}\n";
});
$container2->addComponent($title2);
$container2->addComponent($frameLabel2);
$container2->addComponent($button2);
$window2->setRoot($container2);
echo "Starting main loop...\n";
echo "Click the buttons to test interaction!\n";
echo "Close all windows to exit.\n\n";
// Custom run loop with frame counter updates
$running = true;
while ($running && count($app->getWindows()) > 0) {
// Update frame counters
$window1FrameCount++;
$window2FrameCount++;
$frameLabel1->setText("Frame: {$window1FrameCount}");
$frameLabel2->setText("Frame: {$window2FrameCount}");
// Layout all windows FIRST (sets window references and calculates positions)
foreach ($app->getWindows() as $windowId => $window) {
$window->layout();
}
// Handle events for all windows (now that layout is done)
foreach ($app->getWindows() as $windowId => $window) {
$window->handleEvents();
}
// Update async tasks (global)
PHPNative\Async\TaskManager::getInstance()->update();
// Update all windows
foreach ($app->getWindows() as $windowId => $window) {
$window->update();
}
// Render all windows
foreach ($app->getWindows() as $windowId => $window) {
$window->render();
}
// Remove closed windows
$windowsCopy = $app->getWindows();
foreach ($windowsCopy as $windowId => $window) {
if ($window->shouldClose()) {
echo "Window {$windowId} closing...\n";
}
}
// Limit frame rate to ~60 FPS
usleep(16666);
}
echo "Application exited.\n";