PHP code example of refaltor / easy-ui-builder

1. Go to this page and download the library: Download refaltor/easy-ui-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

refaltor / easy-ui-builder example snippets


spl_autoload_register(function (string $classname): void {
    if (str_contains($classname, "refaltor\\")) {
        

use refaltor\ui\builders\Root;
use refaltor\ui\elements\Label;
use refaltor\ui\colors\BasicColor;

$root = Root::create("my_namespace");
$root->addElement(
    Label::create("hello", "Hello Minecraft!")
        ->setFontSize(Label::FONT_EXTRA_LARGE)
        ->setShadow()
        ->setColor(BasicColor::yellow())
);
$root->generateAndSaveJson("ui/my_screen.json");

use refaltor\ui\components\Modification;

$panel = Panel::create("my_panel")
    ->addModification(
        Modification::insertBack(Modification::ARRAY_CONTROLS)
            ->setValue([
                ["[email protected]_panel" => []]
            ])
    )
    ->addModification(
        Modification::remove(Modification::ARRAY_BINDINGS, [
            "binding_name" => "#obsolete_binding"
        ])
    );

use refaltor\ui\components\Modification;

$root = Root::create("my_namespace");

// Hide the HUD title when text matches a value
$root->modifyVanillaElement("hud_title_text/title_frame/title", [
    Modification::insertBack(Modification::ARRAY_BINDINGS)
        ->setValue([
            [
                "binding_type" => "view",
                "source_property_name" => "(not (#text = 'hidden'))",
                "target_property_name" => "#visible",
            ]
        ]),
]);

// Add a custom element to the HUD screen
$root->modifyVanillaElement("hud_screen", [
    Modification::insertBack(Modification::ARRAY_CONTROLS)
        ->setValue([
            ["my_element@my_namespace.my_panel" => []]
        ]),
]);

// Replace a binding in a vanilla element
$root->modifyVanillaElement("start_screen/play_button", [
    Modification::replace(Modification::ARRAY_BINDINGS, [
        "binding_name" => "#old_binding"
    ])->setValue([
        "binding_name" => "#new_binding",
        "binding_type" => "global",
    ]),
]);

// Swap two bindings
$root->modifyVanillaElement("inventory_screen/root_panel", [
    Modification::swap(
        Modification::ARRAY_BINDINGS,
        ["binding_name" => "#binding_a"],
        ["binding_name" => "#binding_b"]
    ),
]);

$entry = new Entry();

// Disable validation entirely
$entry->setValidationEnabled(false);

// Enable strict mode (warns about unknown properties)
$entry->setStrictValidation(true);

use refaltor\ui\validators\JsonValidator;

$validator = new JsonValidator(strict: true);
$result = $validator->validate($jsonData);

foreach ($result->getErrors() as $error) {
    echo "ERROR: $error\n";
}
foreach ($result->getWarnings() as $warning) {
    echo "WARNING: $warning\n";
}

echo $result->isValid() ? "Valid!" : "Invalid.";

class MyScreen implements RootBuild
{
    public function root(): Root
    {
        $root = Root::create();
        $root->addElement(
            Label::create("title", "My Screen")
                ->setFontSize(Label::FONT_EXTRA_LARGE)
                ->setShadow()
        );
        return $root;
    }

    public function getNamespace(): string { return "my_screen"; }
    public function getPathName(): string { return "./resources/pack_example/"; }
    public function titleCondition(): string { return "MY_SCREEN"; }
}
bash
php start.php