Skip to content

LibForm #6

@ImperaZim

Description

@ImperaZim

LibForm

First, I’d like to consolidate all of the form‑creation logic into a single imperazim/form directory—merging what is today split between:

  • imperazim/vendor/libform
  • imperazim/components/ui/form

Important

Unified Directory: All form-building logic now resides in imperazim/form. This merge eliminates duplicate code and streamlines maintenance by using a single namespace for forms.

Tip

Dual API Styles: Developers can choose between a LongForm (class-based) approach or a dynamic, fluent API (using DynamicLongForm). Both approaches produce equivalent forms and can be used interchangeably according to preference.

Class-Based LongForm Example

<?php
use imperazim\form\long\LongForm;
use imperazim\form\long\elements\Button;
use imperazim\form\long\elements\ButtonTexture;
use imperazim\form\long\elements\ButtonCollection;
use imperazim\form\long\response\ButtonResponse;
use imperazim\form\base\Title;
use imperazim\form\base\Content;

class MyListForm extends LongForm {
    protected function title(Player $player, FormData $formData): Title {
        return new Title('Main Menu');
    }

    protected function content(Player $player, FormData $formData): Content {
        return new Content('Select an option:');
    }

    protected function buttons(Player $player, FormData $formData): ButtonCollection {
        return ButtonCollection::fromArray([
            new Button(
                'Option 1',
                new ButtonTexture('textures/ui/icon_recipe', ButtonTexture::PATH),
                new ButtonResponse(function(Player $player): FormResult {
                    $player->sendMessage("You clicked Option 1!");
                    return FormResult::CLOSE;
                })
            ),
            new Button(
                'Option 2',
                null,
                new ButtonResponse(function(Player $player): FormResult {
                    $player->sendMessage("Option 2 selected.");
                    return FormResult::CLOSE;
                })
            ),
        ]);
    }
}
?>

Sending the Form

After defining the form class, you can display it to a player with:

new MyListForm($player, $formData, true);

Fluent DynamicLongForm Example

<?php
use imperazim\form\long\DynamicLongForm;

DynamicLongForm::create("Quick Menu")
    ->setContent("What do you want to do?")
    ->addButton(
        "Status",
        'textures/blocks/diamond_block',
        function(Player $player): FormResult {
            $player->sendMessage("Status chosen!");
            return FormResult::CLOSE;
        }
    )
    ->addButton(
        "Settings",
        null,
        function(Player $player): FormResult {
            $player->sendMessage("Opening settings...");
            return FormResult::CLOSE;
        }
    )
    ->sendTo($player);
?>

Note

Usage: Modal and Custom form types follow the same logic as above. The fluent API uses chained method calls (create(), setContent(), addButton(), and finally ->sendTo($player)) to build and send the form in one go.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions