Form Guide
LeviLamina provides a minimalist form API that allows developers to implement complex form functionalities with just a
few lines of code.
The header files for the Form API are stored in ll/api/form
. Additionally, since Minecraft's form IDs are unique at
runtime, we provide FormIdManager to retrieve Minecraft's form IDs, allowing developers to implement their own form
API.
FormIdManager
The header file for FormIdManager is located at ll/api/form/FormIdManager.h
, exporting the
ll::form::FormIdManager::genFormId
method, which is used to obtain a unique form ID.
Example
C++ | |
---|---|
1 2 3 |
|
SimpleForm
SimpleForm is a basic form that provides a title, content, and buttons.
The header file for SimpleForm is located at ll/api/form/SimpleForm.h
.
Additionally, since methods like addButton
in SimpleForm return a reference to SimpleForm, they support method
chaining.
Usage
- Include the header file
ll/api/form/SimpleForm.h
- Construct a SimpleForm object. You can use either a parameterized or parameterless constructor. The parameterized
constructor requires a title and content, while the parameterless constructor requires manually calling the
setTitle
andsetContent
methods to set the title and content. - Add buttons using the
appendButton
method. - Use the
sendTo
method to send the form to a player. This requires passing a reference to aPlayer
object and a callback function. The callback function parameters include a reference to thePlayer
object, the index of the button selected by the player, and an enumeration of the cancellation reason. For detailed parameters, refer to theCallback
declaration in thell/api/form/SimpleForm.h
header file.
Tip
When the button index is -1, it means the player canceled the form.
FormCancelReason
is essentially std::optional<ModalFormCancelReason>
, so you need to check whether it has a value
before using it. You should also use the enumeration values of ModalFormCancelReason
to determine the cancellation
reason. The example uses the magic_enum
library to retrieve the enumeration value name.
Example
C++ | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
CustomForm
CustomForm is an advanced form that provides a title, labels, input fields, toggles, dropdowns, sliders, and step sliders.
Usage
- Include the header file
ll/api/form/CustomForm.h
- Construct a CustomForm object. You can use either a parameterized or parameterless constructor. The parameterized
constructor requires a title, while the parameterless constructor requires manually calling the
setTitle
method to set the title. - Add various elements using
appendLabel
,appendInput
,appendToggle
,appendDropdown
,appendSlider
, andappendStepSlider
methods. - Use the
sendTo
method to send the form to a player. This requires passing a reference to aPlayer
object and a callback function. The callback function parameters include a reference to thePlayer
object, the form result, and an enumeration of the cancellation reason. For detailed parameters, refer to theCallback
declaration in thell/api/form/CustomForm.h
header file.
Tip
The form result CustomFormResult
is actually
std::optional<std::unordered_map<std::string, CustomFormElementResult>>
, an unordered associative container storing
element names and results. You can retrieve the corresponding result by element name. The result type is
CustomFormElementResult
, which is a variant (std::variant<std::monostate, uint64, double, std::string>
), so you need
to check its type using std::holds_alternative
before retrieving it using std::get
.
Example
C++ | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
ModalForm
ModalForm is a modal form that provides a title, content, and two buttons, typically used for binary choices.
Usage
- Include the header file
ll/api/form/ModalForm.h
- Construct a ModalForm object. You can use either a parameterized or parameterless constructor. The parameterized
constructor requires a title, content, upper button, and lower button, while the parameterless constructor requires
manually calling
setTitle
,setContent
,setUpperButton
, andsetLowerButton
methods. - Use the
sendTo
method to send the form to a player. This requires passing a reference to aPlayer
object and a callback function. The callback function parameters include a reference to thePlayer
object, the button selection result, and an enumeration of the cancellation reason.
Tip
ModalFormResult
is actually an alias for std::optional<ModalFormSelectedButton>
, and ModalFormSelectedButton
is a
Bool enumeration with two Upper = true
and Lower = false
values, representing the upper and lower buttons,
respectively.
Example
C++ | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|