Skip to content

Custom Form

VennV edited this page Jul 29, 2024 · 3 revisions

According to the introduction at HOME, This is the last type supported, and also the type with the most functions of the three types

Example Code:

use pocketmine\player\Player;
use venndev\vform\Main;
use venndev\vformoopapi\attributes\custom\VDropDown;
use venndev\vformoopapi\attributes\custom\VInput;
use venndev\vformoopapi\attributes\custom\VLabel;
use venndev\vformoopapi\attributes\custom\VSlider;
use venndev\vformoopapi\attributes\custom\VStepSlider;
use venndev\vformoopapi\attributes\custom\VToggle;
use venndev\vformoopapi\attributes\VForm;
use venndev\vformoopapi\Form;
use venndev\vformoopapi\utils\TypeForm;

#[VForm(
    title: "Place Form",
    type: TypeForm::CUSTOM_FORM,
    content: ""
)]
final class PlaceForm extends Form
{

    public function __construct(Player $player)
    {
        parent::__construct(
            player: $player,
            middleWare: function () {
                $players = Main::getInstance()->getServer()->getOnlinePlayers();
                $playersName = [];
                foreach ($players as $player) $playersName[] = $player->getName();

                // It is `Test DropDown` index
                // If you have set 'label' index, you can use $this->setIndexContent('labelName', ["options" => $playersName]);
                $this->setIndexContent(1, ["options" => $playersName]);
            }
        );
        $this->setContent("This is an example for CustomForm"); //<-- This is the content at 'content' before the class
    }

    #[VInput(
        text: "Test Input",
        placeholder: "Input your text here",
    )]
    public function input(Player $player, mixed $data): void
    {
        $player->sendMessage("You have inputted: " . $data);
    }

    #[VDropDown(
        text: "Test DropDown",
        options: [],
    )]
    public function dropDown(Player $player, mixed $data): void
    {
        $player->sendMessage("DropDown: You have selected: " . $data);
    }

    #[VLabel(
        text: "Test Label"
    )]
    public function label(Player $player, mixed $data): void
    {
        $player->sendMessage("You have clicked the label");
    }

    #[VSlider(
        text: "Test Slider",
        min: 0,
        max: 100,
        step: 1
    )]
    public function slider(Player $player, mixed $data): void
    {
        $player->sendMessage("Slider: You have selected: " . $data);
    }

    #[VStepSlider(
        text: "Test StepSlider",
        steps: ["Step 1", "Step 2", "Step 3", "Step 4", "Step 5"]
    )]
    public function stepSlider(Player $player, mixed $data): void
    {
        $player->sendMessage("StepSlider: You have selected: " . $data);
    }

    #[VToggle(
        text: "Test Toggle",
        default: true //false
    )]
    public function toggle(Player $player, mixed $data): void
    {
        $player->sendMessage("Toggle: You have selected: " . ($data ? "true" : "false"));
    }

    protected function onClose(Player $player): void
    {
        $player->sendMessage("You have closed the form");
    }

    // This method is called when the form is submitted
    protected function onCompletion(Player $player, mixed $data): void
    {
        // TODO: Implement onCompletion() method.
    }
}
  • NOTE:
  • You need to do something like Example Code for the menu to work
  • VForm => Create new menu
  • VToggle => Add toggle for menu
  • VDropDown => Add dropdown for menu
  • VSlider => Add Slider for menu
  • VStepSlider => Add step slider for menu
  • VLabel => Add label for menu
  • VInput => Add placeholder for menu
Clone this wiki locally