CUGL 2.0
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
cugl::Scene2Loader Class Reference

#include <CUScene2Loader.h>

Inheritance diagram for cugl::Scene2Loader:
cugl::Loader< scene2::SceneNode > cugl::BaseLoader

Public Member Functions

 Scene2Loader ()
 
virtual bool init () override
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads) override
 
void dispose () override
 
std::shared_ptr< scene2::SceneNodebuild (const std::string &key, const std::shared_ptr< JsonValue > &json) const
 
- Public Member Functions inherited from cugl::Loader< scene2::SceneNode >
 Loader ()
 
std::shared_ptr< scene2::SceneNodeget (const std::string &key) const
 
std::shared_ptr< scene2::SceneNodeget (const char *key) const
 
std::shared_ptr< scene2::SceneNodeoperator[] (const std::string &key) const
 
std::shared_ptr< scene2::SceneNodeoperator[] (const char *key) const
 
size_t loadCount () const override
 
size_t waitCount () const override
 
void unloadAll () override
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
void setManager (AssetManager *manager)
 
const AssetManagergetManager () const
 
bool load (const std::string &key, const std::string &source)
 
bool load (const char *key, const std::string &source)
 
bool load (const std::string &key, const char *source)
 
bool load (const char *key, const char *source)
 
bool load (const std::shared_ptr< JsonValue > &json)
 
void loadAsync (const std::string &key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const char *key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const std::string &key, const char *source, LoaderCallback callback)
 
void loadAsync (const char *key, const char *source, LoaderCallback callback)
 
void loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
 
bool unload (const std::string &key)
 
bool unload (const char *key)
 
bool unload (const std::shared_ptr< JsonValue > &json)
 
bool contains (const std::string &key) const
 
bool contains (const char *key) const
 
bool complete () const
 
float progress () const
 

Static Public Member Functions

static std::shared_ptr< Scene2Loaderalloc ()
 
static std::shared_ptr< Scene2Loaderalloc (const std::shared_ptr< ThreadPool > &threads)
 

Protected Types

enum  Widget {
  Widget::NODE, Widget::IMAGE, Widget::SOLID, Widget::POLY,
  Widget::PATH, Widget::WIRE, Widget::ANIMATE, Widget::NINE,
  Widget::LABEL, Widget::BUTTON, Widget::PROGRESS, Widget::SLIDER,
  Widget::TEXTFIELD, Widget::EXTERNAL_IMPORT, Widget::UNKNOWN
}
 
enum  Form {
  Form::NONE, Form::ANCHORED, Form::FLOAT, Form::GRID,
  Form::UNKNOWN
}
 

Protected Member Functions

void materialize (const std::shared_ptr< scene2::SceneNode > &node, LoaderCallback callback)
 
virtual bool read (const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
 
virtual bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override
 
virtual bool purge (const std::shared_ptr< JsonValue > &json) override
 
bool attach (const std::string &key, const std::shared_ptr< scene2::SceneNode > &node)
 
std::shared_ptr< JsonValuegetWidgetJson (const std::shared_ptr< JsonValue > &json) const
 
- Protected Member Functions inherited from cugl::Loader< scene2::SceneNode >
bool purge (const std::string &key) override
 
bool verify (const std::string &key) const override
 

Protected Attributes

std::unordered_map< std::string, Widget_types
 
std::unordered_map< std::string, Form_forms
 
- Protected Attributes inherited from cugl::Loader< scene2::SceneNode >
std::unordered_map< std::string, std::shared_ptr< scene2::SceneNode > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

Detailed Description

This class is a specific implementation of Loader<Node>

This asset loader allows us to specify a scene graph subtree, which can be attached to a new or existing scene graph. This is particularly useful for desiging UI elements with a JSON directory structure rather than having to create them programmatically. While not exactly visual wireframing, this makes it easier for UX designers to layout user-interface elements, HUDS and the like.

As UI widgets typically require fonts and images to be loaded already, these should always be the last elements loaded in a loading phase.

As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook() to get the appropriate pointer for attaching the loader.

Member Enumeration Documentation

◆ Form

enum cugl::Scene2Loader::Form
strongprotected

This is an enumeration for identifying layout managers.

Each time you add a new layout, it should be added to this list.

Enumerator
NONE 

The default layout manager, using absolute position

ANCHORED 

A layout manager using anchor points

FLOAT 

A float layout manager

GRID 

A grid layout manager

UNKNOWN 

An unsupported form

◆ Widget

enum cugl::Scene2Loader::Widget
strongprotected

This is an enumeration for identifying scene node types.

Each time you add a new UI widget, it should be added to this list.

Enumerator
NODE 

The base Node type

IMAGE 

An image (PolygoNode) type

SOLID 

A rectangular PolygonNode type

POLY 

A (complex) PolygonNode type

PATH 

A PathNode type

WIRE 

A WireNode type

ANIMATE 

An animation node type

NINE 

A nine-patch type

LABEL 

A text label (uneditable) type

BUTTON 

A button type

PROGRESS 

A progress bar type

SLIDER 

A slider type

TEXTFIELD 

A single-line text field type

EXTERNAL_IMPORT 

A Node implied by an imported file

UNKNOWN 

An unsupported type

Constructor & Destructor Documentation

◆ Scene2Loader()

cugl::Scene2Loader::Scene2Loader ( )
inline

Creates a new, uninitialized scene loader

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr<Scene2Loader> cugl::Scene2Loader::alloc ( )
inlinestatic

Returns a newly allocated texture loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

Returns
a newly allocated texture loader.

◆ alloc() [2/2]

static std::shared_ptr<Scene2Loader> cugl::Scene2Loader::alloc ( const std::shared_ptr< ThreadPool > &  threads)
inlinestatic

Returns a newly allocated texture loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

Parameters
threadsThe thread pool for asynchronous loading
Returns
a newly allocated texture loader.

◆ attach()

bool cugl::Scene2Loader::attach ( const std::string &  key,
const std::shared_ptr< scene2::SceneNode > &  node 
)
protected

Attaches all generate nodes to the asset dictionary.

As the asset dictionary must be updated in the main thread, we do not update it until the entire node tree has been materialized. This method assumes that each Node is named with its asset look-up key.

Parameters
keyThe key to access the asset after loading
nodeThe scene asset
Returns
true if the node was successfully attached

◆ build()

std::shared_ptr<scene2::SceneNode> cugl::Scene2Loader::build ( const std::string &  key,
const std::shared_ptr< JsonValue > &  json 
) const

Recursively builds the scene from the given JSON tree.

This method allows us to maximize the asynchronous creation of scenes. The key is assigned as the name of the root Node of the scene.

The JSON tree should be a tree of widget objects, where each widget object has the following attribute values:

 "type":     The node type (a Node or any subclass)
 "data":     Data (images, labels) that define the widget.  This
             JSON object has a node-specific format.
 "format":   The layout manager to use for this Node. This layout
             manager will apply to all the children (see below).
             This JSON object has a layout-specific format.
 "layout":   Node placement using a the layout manager of the parent.
             This is applied after parsing "data' and will override
             any settings there. This JSON object has a
             layout-specific format.
 "children": Any child Nodes of this one. This JSON object has a
             named attribute for each child.

With the exception of "type", all of these attributes are JSON objects.

Parameters
keyThe key to access the scene after loading
jsonThe JSON object defining the scene
Returns
the SDL_Surface with the texture information

◆ dispose()

void cugl::Scene2Loader::dispose ( )
inlineoverridevirtual

Disposes all resources and assets of this loader

Any assets loaded by this object will be immediately released by the loader. However, a texture may still be available if it is referenced by another smart pointer. OpenGL will only release a texture asset once all smart pointer attached to the asset are null.

Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.

Reimplemented from cugl::BaseLoader.

◆ getWidgetJson()

std::shared_ptr<JsonValue> cugl::Scene2Loader::getWidgetJson ( const std::shared_ptr< JsonValue > &  json) const
protected

Translates the JSON of a widget to the JSON of the node that it encodes.

If this scene is built before the JSON of any used widgets have been loaded, this will fail.

Parameters
jsonThe JSON object specifying the widget's file and the values for its exposed variables
Returns
the JSON loaded from the widget file with all variables set based on the values presented in json.

◆ init() [1/2]

virtual bool cugl::Scene2Loader::init ( )
inlineoverridevirtual

Initializes a new asset loader.

This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

This method is abstract and should be overridden in the specific implementation for each asset.

Returns
true if the asset loader was initialized successfully

Reimplemented from cugl::BaseLoader.

◆ init() [2/2]

virtual bool cugl::Scene2Loader::init ( const std::shared_ptr< ThreadPool > &  threads)
overridevirtual

Initializes a new asset loader.

This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.

This method is abstract and should be overridden in the specific implementation for each asset.

Parameters
threadsThe thread pool for asynchronous loading support
Returns
true if the asset loader was initialized successfully

Reimplemented from cugl::BaseLoader.

◆ materialize()

void cugl::Scene2Loader::materialize ( const std::shared_ptr< scene2::SceneNode > &  node,
LoaderCallback  callback 
)
protected

Records the given Node with this loader, so that it may be unloaded later.

This method finishes the asset loading started in load. This step is not safe to be done in a separate thread, as it accesses the main asset table. Therefore, it takes place in the main CUGL thread via Application#schedule. The scene is stored using the name of the root Node as a key.

This method supports an optional callback function which reports whether the asset was successfully materialized.

Parameters
nodeThe scene asset
callbackAn optional callback for asynchronous loading

◆ purge()

virtual bool cugl::Scene2Loader::purge ( const std::shared_ptr< JsonValue > &  json)
overrideprotectedvirtual

Unloads the asset for the given directory entry

An asset may still be available if it is referenced by a smart pointer. See the description of the specific implementation for how assets are released.

This method is abstract and should be overridden in child classes. You will notice that this method is essentially identical to unload. We separated the methods because overloading and virtual methods do not place nice.

Parameters
jsonThe directory entry for the asset
Returns
true if the asset was successfully unloaded

Reimplemented from cugl::BaseLoader.

◆ read() [1/2]

virtual bool cugl::Scene2Loader::read ( const std::shared_ptr< JsonValue > &  json,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method is like the traditional read method except that it assumes the JSON data has already been parsed.

Parameters
jsonThe directory entry for the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

◆ read() [2/2]

virtual bool cugl::Scene2Loader::read ( const std::string &  key,
const std::string &  source,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

The sources must be a JSON file. It will parse the JSON tree of this file, assigning the given key to the root node. The JSON tree should be a tree of widget objects, where each widget object has the following attribute values:

 "type":     The node type (a Node or any subclass)
 "data":     Data (images, labels) that define the widget.  This
             JSON object has a node-specific format.
 "format":   The layout manager to use for this Node. This layout
             manager will apply to all the children (see below).
             This JSON object has a layout-specific format.
 "layout":   Node placement using a the layout manager of the parent.
             This is applied after parsing "data' and will override
             any settings there. This JSON object has a
             layout-specific format.
 "children": Any child Nodes of this one. This JSON object has a
             named attribute for each child.

With the exception of "type", all of these attributes are JSON objects.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

Member Data Documentation

◆ _forms

std::unordered_map<std::string,Form> cugl::Scene2Loader::_forms
protected

The type map for managing layout

◆ _types

std::unordered_map<std::string,Widget> cugl::Scene2Loader::_types
protected

The type map for managing constructors


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