CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::WidgetValue Class Reference

#include <CUWidgetValue.h>

Public Member Functions

 WidgetValue ()
 
 ~WidgetValue ()
 
bool init (const std::shared_ptr< JsonValue > &json)
 
const std::shared_ptr< JsonValuegetJson () const
 
std::vector< std::string > getVariables () const
 
std::shared_ptr< JsonValuesubstitute (const std::shared_ptr< JsonValue > &values) const
 
bool isValid () const
 

Static Public Member Functions

static std::shared_ptr< WidgetValuealloc (std::shared_ptr< JsonValue > json)
 

Protected Attributes

std::shared_ptr< JsonValue_json
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ WidgetValue()

cugl::WidgetValue::WidgetValue ( )
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.

◆ ~WidgetValue()

cugl::WidgetValue::~WidgetValue ( )
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.

Member Function Documentation

◆ alloc()

static std::shared_ptr< WidgetValue > cugl::WidgetValue::alloc ( std::shared_ptr< JsonValue json)
inlinestatic

Returns a newly allocated WidgetValue to wrap the given JsonValue.

Parameters
jsonThe JSON definition of this widget.
Returns
a newly allocated WidgetValue wrapping the provided JsonValue.

◆ getJson()

const std::shared_ptr< JsonValue > cugl::WidgetValue::getJson ( ) const
inline

Returns the JsonValue representation of this widget.

Returns
a shared pointer to the JsonValue representation of this widget.

◆ getVariables()

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

Returns
the list of variables in this widget.

◆ init()

bool cugl::WidgetValue::init ( const std::shared_ptr< JsonValue > &  json)
inline

Initializes a new WidgetValue to wrap the give JsonValue.

This initializer simply wraps the provided JSON

Parameters
jsonA shared pointer to JsonValue that defines this widget.
Returns
true if the JsonValue is not a nullptr, false otherwise.

◆ isValid()

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.

Returns
true if this widget value is valid.

◆ substitute()

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.

Parameters
valuesThe variable substitutions
Returns
the JSON value resulting from substituting the specified values.

Member Data Documentation

◆ _json

std::shared_ptr<JsonValue> cugl::WidgetValue::_json
protected

The JSON entry representing this widget


The documentation for this class was generated from the following file: