Skip to content

ConfigBuilder

Makes it possible to construct a config menu in a fairly human readable manner.

Description

The ConfigBuilder is used to create mod configs. It is supposed to provide an intuitive and human readable api for doing so. To achieve this, it relies on method chaining so the code for setting up a config may look structurally similar to the resulting config file. The following general types of methods are provided:

A ConfigBuilder is obtained by creating a mod config using the ModConfigApi:

var mod_id: String = "CreepyCre.ExampleMod"
var config_title: String = "Example Mod Config"
var config_file: String = self.Global.Root + "config.json"
var builder = self.Global.API.ModConfigApi.create_config(mod_id, config_title, config_file)

Control nodes may then be created and appended to the config screen using their appropriate methods. The config can be built at the end using
build ( bool should_load = true, bool should_free = true ):

# multiline method chaining does not allow comments inbetween
var config = builder\
    .label("Hello World!")\
    .check_button("key1", true, "This is a check Button!")\
    .build()
Writing a \ at the end of a line allows for method chaining across multiple lines. Config nodes such as the CheckButton created through
check_button ( String save_entry, bool default_value, String text = "" )
take a save_entry and default_value as parameters. The save_entry is the key the setting will be saved under in the config file.

For appending nodes as children of other nodes enter ( ) and exit ( ) allow hopping into and out of nodes:

builder\
    .v_box_container().enter()\
        .h_box_container().enter()\
            .label("Hello World!")\
        .exit()\
    .exit()
Mind the indentation in this example. Though it serves no actual purpose indenting methods indicating the depth of the node entered is recommended for increased readability.

Also mind the optional save_entry parameter available for container type node creation. Supplying save_entry will create sub categories for all contained config nodes. For example:

var config = builder\
            .h_box_container("category").enter()\
                .check_button("key1", true, "Hello")\
                .check_box("key2", true, "World")\
            .exit()\
            .line_edit("key3", "This is the default text.")\
            .build()

Will result in the following config:

{
    "category": {
        "key1": true,
        "key2": true
    }
    "key3": "This is the default text."
}

Config values may then be accessed and modified through the ConfigAgent returned when building the config:

# access value
print(config.category.key1)
# change value
config.category.key1 = false

For styling the nodes there are some shorthand methods available like
size_flags_h ( int flags ) or rect_min_size ( Vector2 min_size ).
For accessing properties and methods that do not have shorthand equivalents use
with ( String property, value ) and call_on ( String method_name, args... ) which both perform actions on the last node created. with directly sets the given property to value while call_on forwards its parameters to Object#call:

# hidden spin box. idk why you'd want this, it's just an example
builder.spin_box("key", 70)\
        .with("suffix","dpi")\
        .call_on("hide")

For connecting node signals use connect_current and connect_to_prop. While connect_current forwards its parameters to connect, connect_to_prop actually forwards the signal to a utility methods that always updates the property property of target with the emitted value.

How would you handle connecting different config nodes signals together? To avoid having to keep references to nodes which would interrupt code flow use ref ( String reference_name ). It assigns the key reference_name to the last node so it may later be retrieved using get_ref ( String reference_name ) or be called on using
call_on_ref ( String reference_name, String method_name, args... ),
connect_ref ( String reference_name, String signal_name, Object target, String method_name, Array binds = [], int flags = 0 ) and
connect_ref_to_prop ( String reference_name, String signal_name, target, String property ). E.g.:

builder\
    .label().ref("slider_label")\
    .h_slider("key", 42).size_flags_h(Control.SIZE_EXPAND_FILL)\
        .connect_to_prop("loaded", builder.get_ref("slider_label"), "text")\
        .connect_to_prop("value_changed", builder.get_ref("slider_label"), "text")\
In this example the text label has been connected to the sliders loaded and value_changed signals to make it display the current slider value.

Methods

ConfigBuilder add_color_override ( String name, Color color )
ConfigBuilder add_constant_override ( String name, int constant )
ConfigBuilder add_font_override ( String name, Font font )
ConfigBuilder add_icon_override ( String name, Texture texture )
ConfigBuilder add_node ( Control node, bool legible_unique_name = false )
ConfigBuilder add_node_direct ( Control node, bool legible_unique_name = false )
ConfigBuilder add_shader_override ( String name, Shader shader )
ConfigBuilder add_stylebox_override ( String name, StyleBox stylebox )
ConfigBuilder aspect_ratio_container ( String save_entry = "" )
ConfigAgent build ( bool should_load = true, bool should_free = true )
ConfigBuilder call_on ( String method_name, args... )
ConfigBuilder call_on_ref ( String reference_name, String method_name, args... )
ConfigBuilder center_container ( String save_entry = "" )
ConfigBuilder check_box ( String save_entry, bool default_value, String text = "" )
ConfigBuilder check_button ( String save_entry, bool default_value, String text = "" )
ConfigBuilder color_picker ( String save_entry, Color default_value )
ConfigBuilder color_picker_button ( String save_entry, Color default_value )
ConfigBuilder color_rect ( Color color )
ConfigBuilder connect_current ( String signal_name, Object target, String method_name, Array binds = [], int flags = 0 )
ConfigBuilder connect_ref ( String reference_name, String signal_name, Object target, String method_name, Array binds = [], int flags = 0 )
ConfigBuilder connect_ref_to_prop ( String reference_name, String signal_name, target, String property )
ConfigBuilder connect_to_prop ( String signal_name, target, String property )
ConfigBuilder enter ( )
ConfigBuilder exit ( )
ConfigBuilder extend ( String save_entry, Control node )
ConfigBuilder flatten ( bool value = true )
ConfigAgent get_agent ( )
Control get_current ( )
Control get_ref ( String reference_name )
Control get_root ( )
ConfigBuilder grid_container ( String save_entry = "" )
ConfigBuilder h_box_container ( String save_entry = "" )
ConfigBuilder h_separator ( )
ConfigBuilder h_slider ( String save_entry, float default_value )
ConfigBuilder h_split_container ( String save_entry = "" )
ConfigBuilder label ( String text = "" )
ConfigBuilder line_edit ( String save_entry, String default_value, bool require_hit_enter = true )
ConfigBuilder margin_container ( String save_entry = "" )
ConfigBuilder nine_patch_rect ( )
ConfigBuilder option_button ( String save_entry, int default_value, Array options )
ConfigBuilder panel ( )
ConfigBuilder panel_container ( String save_entry = "" )
ConfigBuilder rect_min_size ( Vector2 min_size )
ConfigBuilder rect_min_x ( float min_x )
ConfigBuilder rect_min_y ( float min_y )
ConfigBuilder rect_size ( Vector2 size )
ConfigBuilder rect_x ( float x )
ConfigBuilder rect_y ( float y )
ConfigBuilder ref ( String reference_name )
ConfigBuilder reference_rect ( )
ConfigBuilder rich_text_label ( String bbcode_text = "" )
ConfigBuilder scroll_container ( String save_entry = "" )
ConfigBuilder shortcuts ( String save_entry, Dictionary definitions )
ConfigBuilder size_expand_fill ( )
ConfigBuilder size_flags_h ( int flags )
ConfigBuilder size_flags_v ( int flags )
ConfigBuilder spin_box ( String save_entry, float default_value )
ConfigBuilder tab_container ( String save_entry = "" )
ConfigBuilder text_edit ( String save_entry, String default_value )
ConfigBuilder texture_rect ( Texture texture )
ConfigBuilder v_box_container ( String save_entry = "" )
ConfigBuilder v_separator ( )
ConfigBuilder v_slider ( String save_entry, float default_value )
ConfigBuilder v_split_container ( String save_entry = "" )
ConfigBuilder with ( String property, value )
ConfigBuilder wrap ( String save_entry, Control root_node, target_node = null )

Method Descriptions

ConfigBuilder enter ( ):
Enters the last Control appended so new nodes will be created as its children.

ConfigBuilder exit ( ):
Exits the node previously entered.

ConfigBuilder add_node ( Control node, bool legible_unique_name = false ):
Adds node as child of the node currently entered.

ConfigBuilder add_node_direct ( Control node, bool legible_unique_name = false ):
Directly adds node as child of the node currently entered bypassing the get_target() call.

ConfigBuilder with ( String property, value ):
Sets the property property to value on the most recent node.

ConfigBuilder size_flags_h ( int flags ):
Sets the property Control#size_flags_horizontal to flags on the most recent node.

ConfigBuilder size_flags_v ( int flags ):
Sets the property Control#size_flags_vertical to flags on the most recent node.

ConfigBuilder size_expand_fill ( ):
Shorthand for:

.size_flags_h(Control.SIZE_EXPAND_FILL)\
.size_flags_v(Control.SIZE_EXPAND_FILL)

ConfigBuilder rect_min_size ( Vector2 min_size ):
Sets the property Control#rect_min_size to min_size on the most recent node.

ConfigBuilder rect_min_x ( float min_x ):
Shorthand for:

.rect_min_size(Vector2(min_x, builder.get_current().rect_min_size.y))

ConfigBuilder rect_min_y ( float min_y ):
Shorthand for:

.rect_min_size(Vector2(builder.get_current().rect_min_size.x, min_y))

ConfigBuilder rect_size ( Vector2 size ):
Sets the property Control#rect_size to size on the most recent node.

ConfigBuilder rect_x ( float x ):
Shorthand for:

.rect_size(Vector2(x, builder.get_current().rect_size.y))

ConfigBuilder rect_y ( float y ):
Shorthand for:

.rect_size(Vector2(builder.get_current().rect_size.x, y))

ConfigBuilder flatten ( bool value = true ):
Calls flatten(value) on the most recent node. Setting value to true will make the most recent node flatten its child config entries into its parent node.

Control get_current ( ):
Returns the most recent node.

ConfigBuilder ref ( String reference_name ):
Gives the most recent node the identifier reference_name so it can be retrieved later using get_ref ( String reference_name )

Control get_ref ( String reference_name ):
Returns the node with identifier reference_name.

ConfigBuilder call_on ( String method_name, args... ):
Calls method method_name with arguments args on the most recent node.

ConfigBuilder call_on_ref ( String reference_name, String method_name, args... ):
Calls method method_name with arguments args on node with identifier reference_name.

ConfigBuilder connect_current ( String signal_name, Object target, String method_name, Array binds = [], int flags = 0 ):
Calls Object#connect on the most recent node using identical paramters.

ConfigBuilder connect_ref ( String reference_name, String signal_name, Object target, String method_name, Array binds = [], int flags = 0 ):
Calls Object#connect on the node with identifier reference_name using identical paramters besides reference_name.

ConfigBuilder connect_to_prop ( String signal_name, target, String property ):
Connects the signal signal_name from the most recent node such that the emitted value is applied to property :parameter:property in :parameter:target.

ConfigBuilder connect_ref_to_prop ( String reference_name, String signal_name, target, String property ):
Connects the signal signal_name from the node with identifier reference_name such that the emitted value is applied to property :parameter:property in :parameter:target.

ConfigBuilder add_color_override ( String name, Color color ):
Forwards call to Control#add_color_override on the most recent node.

ConfigBuilder add_constant_override ( String name, int constant ):
Forwards call to Control#add_constant_override on the most recent node.

ConfigBuilder add_font_override ( String name, Font font ):
Forwards call to Control#add_font_override on the most recent node.

ConfigBuilder add_icon_override ( String name, Texture texture ):
Forwards call to Control#add_icon_override on the most recent node.

ConfigBuilder add_shader_override ( String name, Shader shader ):
Forwards call to Control#add_shader_override on the most recent node.

ConfigBuilder add_stylebox_override ( String name, StyleBox stylebox ):
Forwards call to Control#add_stylebox_override on the most recent node.

ConfigBuilder wrap ( String save_entry, Control root_node, target_node = null ):
Wraps a scene in a WrappedControlConfigNode where root_node is the scene root and target_node is where new child nodes will be appended by the ConfigBuilder. The wrapped scenes config nodes will be saved into the category save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder extend ( String save_entry, Control node ):
Appends node as a child of the currently entered node and gives it config node capabilities by settings its script to ContainerExtensionConfigNode.

ConfigBuilder check_button ( String save_entry, bool default_value, String text = "" ):
Appends a CheckButton config node with config key save_entry, default value default_value and text text.

ConfigBuilder check_box ( String save_entry, bool default_value, String text = "" ):
Appends a CheckBox config node with config key save_entry, default value default_value and text text.

ConfigBuilder h_slider ( String save_entry, float default_value ):
Appends an HSlider config node with config key save_entry and default value default_value.

ConfigBuilder v_slider ( String save_entry, float default_value ):
Appends a VSlider config node with config key save_entry and default value default_value.

ConfigBuilder spin_box ( String save_entry, float default_value ):
Appends a SpinBox config node with config key save_entry and default value default_value.

ConfigBuilder color_picker ( String save_entry, Color default_value ):
Appends a ColorPicker config node with config key save_entry and default value default_value.

ConfigBuilder color_picker_button ( String save_entry, Color default_value ):
Appends a ColorPickerButton config node with config key save_entry and default value default_value.

ConfigBuilder option_button ( String save_entry, int default_value, Array options ):
Appends an OptionButton config node with config key save_entry, default value default_value and options options. The entries of options can either be Strings for a plain labelled option or dictionaries with possible entries "label", "icon" and "meta". Accessing the config value from the ConfigAgent will return the selected options meta if available or its label. The selected option can be changed through the ConfigAgent by setting it to either its index or its label:

agent.some_option_button = 2 # this works
agent.some_option_button = "label goes here" # this also works

ConfigBuilder line_edit ( String save_entry, String default_value, bool require_hit_enter = true ):
Appends a LineEdit config node with config key save_entry and default value default_value. require_hit_enter configures whether the Enter key must be hit to update the config value.

ConfigBuilder text_edit ( String save_entry, String default_value ):
Appends a TextEdit config node with config key save_entry and default value default_value.

ConfigBuilder shortcuts ( String save_entry, Dictionary definitions ):
Appends a Tree config node with config key save_entry that contains shortcuts defined via definitions. definitions has to be structured equivalent to actions in InputMapApi#define_actions, though any default shortcuts will be ignored here.

ConfigBuilder aspect_ratio_container ( String save_entry = "" ):
Appends an AspectRatioContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder center_container ( String save_entry = "" ):
Appends a CenterContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder h_box_container ( String save_entry = "" ):
Appends a HBoxContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder v_box_container ( String save_entry = "" ):
Appends a VBoxContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder grid_container ( String save_entry = "" ):
Appends a GridContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder h_split_container ( String save_entry = "" ):
Appends an HSplitContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder v_split_container ( String save_entry = "" ):
Appends a VSplitContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder margin_container ( String save_entry = "" ):
Appends a MarginContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder panel_container ( String save_entry = "" ):
Appends a PanelContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder scroll_container ( String save_entry = "" ):
Appends a ScrollContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder tab_container ( String save_entry = "" ):
Appends a TabContainer with config key save_entry. Setting save_entry to "" flattens the node.

ConfigBuilder color_rect ( Color color ):
Appends a ColorRect with color as its color.

ConfigBuilder h_separator ( ):
Appends an HSeparator.

ConfigBuilder v_separator ( ):
Appends a VSeparator.

ConfigBuilder label ( String text = "" ):
Appends a Label with text text.

ConfigBuilder nine_patch_rect ( ):
Appends a NinePatchRect.

ConfigBuilder panel ( ):
Appends a Panel.

ConfigBuilder reference_rect ( ):
Appends a ReferenceRect.

ConfigBuilder rich_text_label ( String bbcode_text = "" ):
Appends a bbcode enabled RichTextLabel with bbcode_text as its bbcode_text.

ConfigBuilder texture_rect ( Texture texture ):
Appends a TextureRect with texture texture.

ConfigAgent build ( bool should_load = true, bool should_free = true ):
Builds the config panel and returns the ConfigAgent. If should_load is set to true the config will be loaded upon building it. If should_free is set to true the ConfigBuilder will be freed after the config has been built.

ConfigAgent get_agent ( ):
Returns the ConfigAgent

Control get_root ( ):
Returns the root node of the config.