CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUSceneLoader.h
1 //
2 // CUSceneLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // a scene graph. Scene graphs are always specified as a JSON tree. This
7 // loader is very experiment, as the language is still evolving, particularly
8 // with regards to layout managers.
9 //
10 // As with all of our loaders, this loader is designed to be attached to an
11 // asset manager. In addition, this class uses our standard shared-pointer
12 // architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // WARNING: This loader is highly experimental. It has only minimal error
24 // checking. It is provided as-is for the UX designers to mock-up simple
25 // scenes.
26 //
27 //
28 // CUGL MIT License:
29 // This software is provided 'as-is', without any express or implied
30 // warranty. In no event will the authors be held liable for any damages
31 // arising from the use of this software.
32 //
33 // Permission is granted to anyone to use this software for any purpose,
34 // including commercial applications, and to alter it and redistribute it
35 // freely, subject to the following restrictions:
36 //
37 // 1. The origin of this software must not be misrepresented; you must not
38 // claim that you wrote the original software. If you use this software
39 // in a product, an acknowledgment in the product documentation would be
40 // appreciated but is not required.
41 //
42 // 2. Altered source versions must be plainly marked as such, and must not
43 // be misrepresented as being the original software.
44 //
45 // 3. This notice may not be removed or altered from any source distribution.
46 //
47 // Author: Walker White
48 // Version: 1/8/18
49 //
50 #ifndef __CU_SCENE_LOADER_H__
51 #define __CU_SCENE_LOADER_H__
52 #include <cugl/assets/CULoader.h>
53 #include <cugl/2d/CUNode.h>
54 #include <unordered_map>
55 
56 namespace cugl {
57 
58 class AssetManager;
59 
77 class SceneLoader : public Loader<Node> {
78 private:
80  CU_DISALLOW_COPY_AND_ASSIGN(SceneLoader);
81 
82 protected:
88  enum class Widget {
90  NODE,
92  IMAGE,
94  POLY,
96  PATH,
98  WIRE,
100  ANIMATE,
102  NINE,
104  LABEL,
106  BUTTON,
108  PROGRESS,
110  SLIDER,
112  TEXTFIELD,
116  UNKNOWN
117  };
118 
124  enum class Form {
126  NONE,
128  ANCHORED,
130  FLOAT,
132  GRID,
134  UNKNOWN
135  };
136 
138  std::unordered_map<std::string,Widget> _types;
139 
141  std::unordered_map<std::string,Form> _forms;
142 
158  void materialize(const std::shared_ptr<Node>& node, LoaderCallback callback);
159 
194  virtual bool read(const std::string& key, const std::string& source,
195  LoaderCallback callback, bool async) override;
196 
213  virtual bool read(const std::shared_ptr<JsonValue>& json,
214  LoaderCallback callback, bool async) override;
231  virtual bool purge(const std::shared_ptr<JsonValue>& json) override;
232 
245  bool attach(const std::string& key, const std::shared_ptr<Node>& node);
246 
256  std::shared_ptr<JsonValue> getWidgetJson(const std::shared_ptr<JsonValue>& json) const;
257 
258 public:
259 #pragma mark -
260 #pragma mark Constructors
261 
268 
284  virtual bool init() override {
285  return init(nullptr);
286  }
287 
302  virtual bool init(const std::shared_ptr<ThreadPool>& threads) override;
303 
315  void dispose() override {
316  _manager = nullptr;
317  _assets.clear();
318  _loader = nullptr;
319  _types.clear();
320  _forms.clear();
321  }
322 
335  static std::shared_ptr<SceneLoader> alloc() {
336  std::shared_ptr<SceneLoader> result = std::make_shared<SceneLoader>();
337  return (result->init() ? result : nullptr);
338  }
339 
351  static std::shared_ptr<SceneLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
352  std::shared_ptr<SceneLoader> result = std::make_shared<SceneLoader>();
353  return (result->init(threads) ? result : nullptr);
354  }
355 
385  std::shared_ptr<Node> build(const std::string& key, const std::shared_ptr<JsonValue>& json) const;
386 
387 };
388 
389 }
390 
391 #endif /* __CU_SCENE_LOADER_H__ */
cugl::SceneLoader::SceneLoader
SceneLoader()
Definition: CUSceneLoader.h:267
cugl::SceneLoader::Widget::TEXTFIELD
cugl::Loader
Definition: CULoader.h:749
cugl::BaseLoader::_manager
AssetManager * _manager
Definition: CULoader.h:112
cugl::SceneLoader::alloc
static std::shared_ptr< SceneLoader > alloc()
Definition: CUSceneLoader.h:335
cugl::SceneLoader::purge
virtual bool purge(const std::shared_ptr< JsonValue > &json) override
cugl::SceneLoader::Widget::ANIMATE
cugl::SceneLoader::Widget
Widget
Definition: CUSceneLoader.h:88
cugl::SceneLoader::Widget::POLY
cugl::SceneLoader::Widget::NINE
cugl::SceneLoader::Widget::UNKNOWN
cugl::BaseLoader::_loader
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
cugl::SceneLoader::Widget::BUTTON
cugl::SceneLoader::Widget::LABEL
cugl::SceneLoader::build
std::shared_ptr< Node > build(const std::string &key, const std::shared_ptr< JsonValue > &json) const
cugl::SceneLoader::dispose
void dispose() override
Definition: CUSceneLoader.h:315
cugl::SceneLoader::alloc
static std::shared_ptr< SceneLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUSceneLoader.h:351
cugl::SceneLoader::Form::UNKNOWN
cugl::SceneLoader::Widget::EXTERNAL_IMPORT
cugl::SceneLoader::Widget::SLIDER
cugl::SceneLoader::Form::ANCHORED
cugl::SceneLoader::materialize
void materialize(const std::shared_ptr< Node > &node, LoaderCallback callback)
cugl::SceneLoader::Widget::PROGRESS
cugl::SceneLoader::Widget::PATH
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::SceneLoader::Form::FLOAT
cugl::SceneLoader::_forms
std::unordered_map< std::string, Form > _forms
Definition: CUSceneLoader.h:141
cugl::SceneLoader::Form::NONE
cugl::SceneLoader::Form
Form
Definition: CUSceneLoader.h:124
cugl::SceneLoader::read
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
cugl::Loader< Node >::_assets
std::unordered_map< std::string, std::shared_ptr< Node > > _assets
Definition: CULoader.h:752
cugl::SceneLoader::init
virtual bool init() override
Definition: CUSceneLoader.h:284
cugl::SceneLoader::attach
bool attach(const std::string &key, const std::shared_ptr< Node > &node)
cugl::SceneLoader::_types
std::unordered_map< std::string, Widget > _types
Definition: CUSceneLoader.h:138
cugl::SceneLoader::getWidgetJson
std::shared_ptr< JsonValue > getWidgetJson(const std::shared_ptr< JsonValue > &json) const
cugl::SceneLoader::Widget::IMAGE
cugl::SceneLoader::Widget::NODE
cugl::SceneLoader::Widget::WIRE
cugl::SceneLoader::Form::GRID