click handle
This commit is contained in:
parent
5700809299
commit
f3dd92d85b
@ -3,8 +3,8 @@
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use PHPNative\Framework\Application;
|
||||
use PHPNative\Ui\Widget\Container;
|
||||
use PHPNative\Ui\Widget\Button;
|
||||
use PHPNative\Ui\Widget\Container;
|
||||
use PHPNative\Ui\Widget\Label;
|
||||
|
||||
// Check PHP version
|
||||
@ -20,42 +20,54 @@ $mainContainer = new Container(style: 'p-10 bg-gray-100');
|
||||
// Title
|
||||
$title = new Label(
|
||||
text: 'Button Sizing Example',
|
||||
style: 'text-xl text-black'
|
||||
style: 'text-xl text-black',
|
||||
);
|
||||
$mainContainer->addComponent($title);
|
||||
|
||||
// Button with m-10 p-10, should be: 12 (text) + 10 (padding-top) + 10 (padding-bottom) = 32px height
|
||||
$button1 = new Button(
|
||||
text: 'Click Me',
|
||||
style: 'm-10 p-10 bg-blue-500 rounded-lg'
|
||||
style: 'm-10 p-10 bg-blue-500 rounded-lg',
|
||||
);
|
||||
$mainContainer->addComponent($button1);
|
||||
|
||||
// Button with different padding
|
||||
$button2 = new Button(
|
||||
text: 'Another Button',
|
||||
style: 'm-5 p-15 bg-green-500 rounded-lg'
|
||||
style: 'm-5 p-15 bg-green-500 rounded-lg',
|
||||
onClick: function () {
|
||||
echo 'test2';
|
||||
},
|
||||
);
|
||||
$mainContainer->addComponent($button2);
|
||||
|
||||
// Button with no padding
|
||||
$button3 = new Button(
|
||||
text: 'No Padding',
|
||||
style: 'm-10 bg-red-500 rounded-lg'
|
||||
style: 'm-10 bg-red-500 rounded-lg',
|
||||
);
|
||||
$mainContainer->addComponent($button3);
|
||||
|
||||
// Container with multiple labels (should stack)
|
||||
$labelContainer = new Container(style: 'm-10 p-10 bg-white rounded-lg');
|
||||
$labelContainer->addComponent(new Label(text: 'Label 1', style: 'text-black'));
|
||||
$labelContainer->addComponent(new Label(text: 'Label 2', style: 'text-black'));
|
||||
$labelContainer->addComponent(new Label(text: 'Label 3', style: 'text-black'));
|
||||
$labelContainer->addComponent(new Label(
|
||||
text: 'Label 1',
|
||||
style: 'text-black',
|
||||
));
|
||||
$labelContainer->addComponent(new Label(
|
||||
text: 'Label 2',
|
||||
style: 'text-black',
|
||||
));
|
||||
$labelContainer->addComponent(new Label(
|
||||
text: 'Label 3',
|
||||
style: 'text-black',
|
||||
));
|
||||
$mainContainer->addComponent($labelContainer);
|
||||
|
||||
// Info text
|
||||
$info = new Label(
|
||||
text: 'Containers should auto-size to their content',
|
||||
style: 'text-black m-10'
|
||||
style: 'text-black m-10',
|
||||
);
|
||||
$mainContainer->addComponent($info);
|
||||
|
||||
|
||||
@ -133,7 +133,6 @@ class Application
|
||||
|
||||
case RGFW_mouseButtonPressed:
|
||||
$button = $event['button'] ?? 0;
|
||||
|
||||
// Propagate click to root component
|
||||
if ($this->rootComponent) {
|
||||
$this->rootComponent->handleMouseClick($this->mouseX, $this->mouseY, $button);
|
||||
|
||||
@ -7,20 +7,23 @@ use PHPNative\Framework\TextRenderer;
|
||||
class Button extends Container
|
||||
{
|
||||
private Label $label;
|
||||
private $onClick = null;
|
||||
|
||||
public function __construct(
|
||||
public string $text = '',
|
||||
public string $style = '',
|
||||
null|callable $onClick = null,
|
||||
) {
|
||||
parent::__construct($style);
|
||||
|
||||
// Create label inside button
|
||||
$this->label = new Label(
|
||||
text: $text,
|
||||
style: ''
|
||||
style: '',
|
||||
);
|
||||
|
||||
$this->addComponent($this->label);
|
||||
$this->onClick = $onClick;
|
||||
}
|
||||
|
||||
public function setText(string $text): void
|
||||
@ -33,4 +36,29 @@ class Button extends Container
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
public function setOnClick(callable $onClick): void
|
||||
{
|
||||
$this->onClick = $onClick;
|
||||
}
|
||||
|
||||
public function handleMouseClick(float $mouseX, float $mouseY, int $button): bool
|
||||
{
|
||||
// Check if click is within button bounds
|
||||
if (
|
||||
$mouseX >= $this->viewport->x &&
|
||||
$mouseX <= ($this->viewport->x + $this->viewport->width) &&
|
||||
$mouseY >= $this->viewport->y &&
|
||||
$mouseY <= ($this->viewport->y + $this->viewport->height)
|
||||
) {
|
||||
// Call onClick callback if set
|
||||
if ($this->onClick !== null) {
|
||||
($this->onClick)();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Propagate to parent if click was outside button
|
||||
return parent::handleMouseClick($mouseX, $mouseY, $button);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user