![]() |
CUGL 3.0
Cornell University Game Library
|
#include <CUWidgetValue.h>
Public Member Functions | |
WidgetValue () | |
~WidgetValue () | |
bool | init (const std::shared_ptr< JsonValue > &json) |
const std::shared_ptr< JsonValue > | getJson () const |
std::vector< std::string > | getVariables () const |
std::shared_ptr< JsonValue > | substitute (const std::shared_ptr< JsonValue > &values) const |
bool | isValid () const |
Static Public Member Functions | |
static std::shared_ptr< WidgetValue > | alloc (std::shared_ptr< JsonValue > json) |
Protected Attributes | |
std::shared_ptr< JsonValue > | _json |
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
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.
|
inline |
Creates a null WidgetValue.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this WidgetValue and all of its resources.
If no other references own the descendants of this node, they will all be recursively deleted as well.
|
inlinestatic |
Returns a newly allocated WidgetValue to wrap the given JsonValue.
json | The JSON definition of this widget. |
|
inline |
std::vector< std::string > cugl::WidgetValue::getVariables | ( | ) | const |
Returns the list of variables in this widget.
The variables are JSON locations in the tree that can be substituted with new JSON values. Variables names are used in conjuction with substitute
to produce a new JSON value
|
inline |
Initializes a new WidgetValue to wrap the give JsonValue.
This initializer simply wraps the provided JSON
json | A shared pointer to JsonValue that defines this widget. |
bool cugl::WidgetValue::isValid | ( | ) | const |
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.
std::shared_ptr< JsonValue > cugl::WidgetValue::substitute | ( | const std::shared_ptr< JsonValue > & | values | ) | const |
Returns the JSON value resulting from substituting the specified values.
The values should be a JSON object whose entries are collection are some 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.
values | The variable substitutions |
|
protected |
The JSON entry representing this widget