Class WidgetValue

java.lang.Object
edu.cornell.gdiac.assets.WidgetValue

public class WidgetValue extends Object
This class represents an externally defined widget in JSON form.

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

    Constructors
    Constructor
    Description
    Creates an empty WidgetValue.
    WidgetValue(com.badlogic.gdx.utils.JsonValue json)
    Initializes a new WidgetValue to wrap the given JsonValue.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.badlogic.gdx.utils.JsonValue
    Returns the JsonValue representation of this widget.
    Returns the list of variables in this widget.
    boolean
    Returns true if this widget value is valid.
    com.badlogic.gdx.utils.JsonValue
    substitute(com.badlogic.gdx.utils.JsonValue values)
    Returns the JSON value resulting from substituting the specified values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public List<String> 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.