form

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2022 License: MIT Imports: 6 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Button

type Button struct {
	// Text holds the text displayed on the button. It may use Minecraft formatting codes and may have
	// newlines.
	Text string
	// Image holds a path to an image for the button. The Image may either be a URL pointing to an image,
	// such as 'https://someimagewebsite.com/someimage.png', or a path pointing to a local asset, such as
	// 'textures/blocks/grass_carried'.
	Image string
}

Button represents a button added to a Menu or Modal form. The button has text on it and an optional image, which may be either retrieved from a website or the local assets of the game.

func NewButton

func NewButton(text, image string) Button

NewButton creates and returns a new Button using the text and image passed.

func NoButton

func NoButton() Button

NoButton returns a Button which may be used as a default 'no' button for a modal form.

func YesButton

func YesButton() Button

YesButton returns a Button which may be used as a default 'yes' button for a modal form.

func (Button) MarshalJSON

func (b Button) MarshalJSON() ([]byte, error)

MarshalJSON ...

type Closer added in v0.3.0

type Closer interface {
	// Close is called when the Submitter closes a form.
	Close(submitter Submitter)
}

Closer represents a form which has special logic when being closed by a Submitter.

type Custom

type Custom struct {
	// contains filtered or unexported fields
}

Custom represents a form that may be sent to a player and has fields that should be filled out by the player that the form is sent to.

func New

func New(submittable Submittable, title ...any) Custom

New creates a new (custom) form with the title passed and returns it. The title is formatted according to the rules of fmt.Sprintln. The submittable passed is used to create the structure of the form. The values of the Submittable's form fields are used to set text, defaults and placeholders. If the Submittable passed is not a struct, New panics. New also panics if one of the exported field types of the Submittable is not one that implements the Element interface.

func (Custom) Elements

func (f Custom) Elements() []Element

Elements returns a list of all elements as set in the Submittable passed to form.New().

func (Custom) MarshalJSON

func (f Custom) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Custom) SubmitJSON

func (f Custom) SubmitJSON(b []byte, submitter Submitter) error

SubmitJSON submits a JSON data slice to the form. The form will check all values in the JSON array passed, making sure their values are valid for the form's elements. If the values are valid and can be parsed properly, the Submittable.Submit() method of the form's Submittable is called and the fields of the Submittable will be filled out.

func (Custom) Title

func (f Custom) Title() string

Title returns the formatted title passed when the form was created using New().

type Dropdown struct {
	// Text is the text displayed over the dropdown element. The text may contain Minecraft formatting codes.
	Text string
	// Options holds a list of options that a Submitter may select. The order of these options is retained
	// when shown to the submitter of the form.
	Options []string
	// DefaultIndex is the index in the Options slice that is used as default. When sent to a Submitter, the
	// value at this index in the Options slice will be selected.
	DefaultIndex int
	// contains filtered or unexported fields
}

Dropdown represents a dropdown which, when clicked, opens a window with the options set in the Options field. Submitters may select one of the options.

func NewDropdown

func NewDropdown(text string, options []string, defaultIndex int) Dropdown

NewDropdown creates and returns new Dropdown using the values passed.

func (d Dropdown) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (d Dropdown) Value() int

Value returns the value that the Submitter submitted. The value is an index pointing to the selected option in the Options slice.

type Element

type Element interface {
	json.Marshaler
	// contains filtered or unexported methods
}

Element represents an element that may be added to a Form. Any of the types in this package that implement the element interface may be used as struct fields when passing the form structure to form.New().

type Form

type Form interface {
	json.Marshaler
	SubmitJSON(b []byte, submitter Submitter) error
	// contains filtered or unexported methods
}

Form represents a form that may be sent to a Submitter. The three types of forms, custom forms, menu forms and modal forms implement this interface.

type Input

type Input struct {
	// Text is the text displayed over the input element. The text may contain Minecraft formatting codes.
	Text string
	// Default is the default value filled out in the input. The user may remove this value and fill out its
	// own text. The text may contain Minecraft formatting codes.
	Default string
	// Placeholder is the text displayed in the input box if it does not contain any text filled out by the
	// user. The text may contain Minecraft formatting codes.
	Placeholder string
	// contains filtered or unexported fields
}

Input represents a text input box element. Submitters may write any text in these boxes with no specific length.

func NewInput

func NewInput(text, defaultValue, placeholder string) Input

NewInput creates and returns a new Input with the values passed.

func (Input) MarshalJSON

func (i Input) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Input) Value

func (i Input) Value() string

Value returns the value filled out by the user.

type Label

type Label struct {
	// Text is the text held by the label. The text may contain Minecraft formatting codes.
	Text string
}

Label represents a static label on a form. It serves only to display a box of text, and users cannot submit values to it.

func NewLabel

func NewLabel(text string) Label

NewLabel creates and returns a new Label with the values passed.

func (Label) MarshalJSON

func (l Label) MarshalJSON() ([]byte, error)

MarshalJSON ...

type Menu struct {
	// contains filtered or unexported fields
}

Menu represents a menu form. These menus are made up of a title and a body, with a number of buttons which come below the body. These buttons may also have images on the side of them.

func NewMenu

func NewMenu(submittable MenuSubmittable, title ...any) Menu

NewMenu creates a new Menu form using the MenuSubmittable passed to handle the output of the form. The title passed is formatted following the rules of fmt.Sprintln.

func (m Menu) Body() string

Body returns the formatted text in the body passed to the menu using WithBody().

func (m Menu) Buttons() []Button

Buttons returns a list of all buttons of the MenuSubmittable. It parses them from the fields using reflection and returns them.

func (m Menu) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (m Menu) SubmitJSON(b []byte, submitter Submitter) error

SubmitJSON submits a JSON value to the menu, containing the index of the button clicked.

func (m Menu) Title() string

Title returns the formatted title passed to the menu upon construction using NewMenu().

func (m Menu) WithBody(body ...any) Menu

WithBody creates a copy of the Menu form and changes its body to the body passed, after which the new Menu form is returned. The text is formatted following the rules of fmt.Sprintln.

func (m Menu) WithButtons(buttons ...Button) Menu

WithButtons creates a copy of the Menu form and appends the buttons passed to the existing buttons, after which the new Menu form is returned.

type MenuSubmittable interface {
	// Submit is called when the Submitter submits the menu form sent to it. The method is called with the
	// button that was pressed. It may be compared with buttons in the MenuSubmittable struct to check which
	// button was pressed.
	Submit(submitter Submitter, pressed Button)
}

MenuSubmittable is a structure which may be submitted by sending it as a form using form.NewMenu(), much like a Submittable. The struct will have its Submit method called with the button pressed. A struct that implements the MenuSubmittable interface must only have exported fields with the type form.Button.

type Modal struct {
	// contains filtered or unexported fields
}

Modal represents a modal form. These forms have a body with text and two buttons at the end, typically one for Yes and one for No. These buttons may have custom text, but can, unlike with a Menu form, not have images next to them.

func NewModal

func NewModal(submittable ModalSubmittable, title ...any) Modal

NewModal creates a new Modal form using the ModalSubmittable passed to handle the output of the form. The title passed is formatted following the fmt.Sprintln rules. Default 'yes' and 'no' buttons may be passed by setting the two exported struct fields of the submittable to YesButton() and NoButton() respectively.

func (Modal) Body

func (m Modal) Body() string

Body returns the formatted text in the body passed to the menu using WithBody().

func (Modal) Buttons

func (m Modal) Buttons() []Button

Buttons returns a list of all buttons of the Modal form, which will always be a total of two buttons.

func (Modal) MarshalJSON

func (m Modal) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Modal) SubmitJSON

func (m Modal) SubmitJSON(b []byte, submitter Submitter) error

SubmitJSON submits a JSON byte slice to the modal form. This byte slice contains a JSON encoded bool in it, which is used to determine which button was clicked.

func (Modal) Title

func (m Modal) Title() string

Title returns the formatted title passed to the menu upon construction using NewModal().

func (Modal) WithBody

func (m Modal) WithBody(body ...any) Modal

WithBody creates a copy of the Modal form and changes its body to the body passed, after which the new Modal form is returned. The text is formatted following the rules of fmt.Sprintln.

type ModalSubmittable

type ModalSubmittable MenuSubmittable

ModalSubmittable is a structure which may be submitted by sending it as a form using form.NewModal(), much like a Submittable and a MenuSubmittable. The struct will have its Submit method called with the button pressed. A struct that implements the ModalSubmittable interface must have exactly two exported fields with the type form.Button, which may be used to specify the text of the Modal form's buttons. Unlike with a Menu form, buttons on a Modal form will not have images.

type Slider

type Slider struct {
	// Text is the text displayed over the slider element. The text may contain Minecraft formatting codes.
	Text string
	// Min and Max are used to specify the minimum and maximum range of the slider. A value lower or higher
	// than these values cannot be selected.
	Min, Max float64
	// StepSize is the size that one step of the slider takes up. When set to 1.0 for example, a submitter
	// will be able to select only whole values.
	StepSize float64
	// Default is the default value filled out for the slider.
	Default float64
	// contains filtered or unexported fields
}

Slider represents a slider element. Submitters may move the slider to values within the range of the slider to select a value.

func NewSlider

func NewSlider(text string, min, max, stepSize, defaultValue float64) Slider

NewSlider creates and returns a new Slider using the values passed.

func (Slider) MarshalJSON

func (s Slider) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Slider) Value

func (s Slider) Value() float64

Value returns the value filled out by the user.

type StepSlider

type StepSlider Dropdown

StepSlider represents a slider that has a number of options that may be selected. It is essentially a combination of a Dropdown and a Slider, looking like a slider but having properties like a dropdown.

func NewStepSlider

func NewStepSlider(text string, options []string, defaultIndex int) StepSlider

NewStepSlider creates and returns new StepSlider using the values passed.

func (StepSlider) MarshalJSON

func (s StepSlider) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (StepSlider) Value

func (s StepSlider) Value() int

Value returns the value that the Submitter submitted. The value is an index pointing to the selected option in the Options slice.

type Submittable

type Submittable interface {
	// Submit is called when the Submitter submits the form sent to it. Once this method is called, all fields
	// in the struct will have their values filled out as filled out by the Submitter.
	Submit(submitter Submitter)
}

Submittable is a structure which may be submitted by sending it as a form using form.New(). When filled out and submitted, the struct will have its Submit method called and its fields will have the values that the Submitter passed filled out. The fields of a Submittable struct must be either unexported or have a type of one of those that implement the form.Element interface.

type Submitter

type Submitter interface {
	SendForm(form Form)
}

Submitter is an entity that is able to submit a form sent to it. It is able to fill out fields in the form which will then be present when handled.

type Toggle

type Toggle struct {
	// Text is the text displayed over the toggle element. The text may contain Minecraft formatting codes.
	Text string
	// Default is the default value filled out in the input. The user may remove this value and fill out its
	// own text. The text may contain Minecraft formatting codes.
	Default bool
	// contains filtered or unexported fields
}

Toggle represents an on-off button element. Submitters may either toggle this on or off, which will then hold a value of true or false respectively.

func NewToggle

func NewToggle(text string, defaultValue bool) Toggle

NewToggle creates and returns a new Toggle with the values passed.

func (Toggle) MarshalJSON

func (t Toggle) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Toggle) Value

func (t Toggle) Value() bool

Value returns the value filled out by the user.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL