Class WidgetValue
A widget is a JSON with substitution semantics. That is, it has variables that refer to nodes in the JSON tree, and allows these nodes to be replaced with other JSON trees. The purpose of this is to manage heavily nested JSON structures, such as those specifying Figma scene graphs.
More specifically, a widget value is a JSON object with two keys: "variables" and "contents". The former is JSON object with (string) keys mapping to paths in "contents". For example:
{
"variables" : {
"first" : ["outer", "inner", "one"],
"second" : ["outer" , "middle"]
},
"contents : {
"outer" : {
"inner" : {
"one" : 1,
"two" : 2,
},
"middle": 3
}
}
}
A call to substitute(com.badlogic.gdx.utils.JsonValue) on the JSON `{ "first": 4}` will return the JSON object
{
"inner" : {
"outer" : {
"one" : 4,
"two" : 2,
},
"middle": 3
}
}
The substitution semantics is rather simple. That means it is undefined if any of the variables
are prefixes of one another. The method isValid() is used to check for valid widgets.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty WidgetValue.WidgetValue(com.badlogic.gdx.utils.JsonValue json) Initializes a new WidgetValue to wrap the given JsonValue. -
Method Summary
Modifier and TypeMethodDescriptioncom.badlogic.gdx.utils.JsonValuegetJson()Returns the JsonValue representation of this widget.Returns the list of variables in this widget.booleanisValid()Returns true if this widget value is valid.com.badlogic.gdx.utils.JsonValuesubstitute(com.badlogic.gdx.utils.JsonValue values) Returns the JSON value resulting from substituting the specified values.
-
Constructor Details
-
WidgetValue
public WidgetValue()Creates an empty WidgetValue. -
WidgetValue
public WidgetValue(com.badlogic.gdx.utils.JsonValue json) Initializes a new WidgetValue to wrap the given JsonValue.This initializer simply wraps the provided JSON.
- Parameters:
json- A JsonValue that defines this widget.- Throws:
IllegalArgumentException- if the provided json is null.
-
-
Method Details
-
getJson
public com.badlogic.gdx.utils.JsonValue getJson()Returns the JsonValue representation of this widget.- Returns:
- the JsonValue representation of this widget.
-
getVariables
Returns the list of variables in this widget.The variables are JSON locations in the tree that can be substituted with new JSON values. Variable names are used in conjunction with
substitute(com.badlogic.gdx.utils.JsonValue)to produce a new JSON value.- Returns:
- the list of variables in this widget.
-
substitute
public com.badlogic.gdx.utils.JsonValue substitute(com.badlogic.gdx.utils.JsonValue values) Returns the JSON value resulting from substituting the specified values.The values should be a JSON object whose entries are a subset of
getVariables(). For each entry, this function will find the internal node for that variable and replace the subtree with the value for that variable.This function creates a new JsonValue and does not modify the widget. Therefore, the widget can be safely reused for other substitutions.
- Parameters:
values- The variable substitutions.- Returns:
- The JSON value resulting from substituting the specified values.
-
isValid
public boolean isValid()Returns true if this widget value is valid.A valid widget value is a JSON in the correct format, and which does not have any variables that are prefixes of another.
- Returns:
- true if this widget value is valid.
-