CUGL 1.2
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,
114  UNKNOWN
115  };
116 
122  enum class Form {
124  NONE,
126  ANCHORED,
128  FLOAT,
130  GRID,
132  UNKNOWN
133  };
134 
136  std::unordered_map<std::string,Widget> _types;
137 
139  std::unordered_map<std::string,Form> _forms;
140 
156  void materialize(const std::shared_ptr<Node>& node, LoaderCallback callback);
157 
192  virtual bool read(const std::string& key, const std::string& source,
193  LoaderCallback callback, bool async) override;
194 
211  virtual bool read(const std::shared_ptr<JsonValue>& json,
212  LoaderCallback callback, bool async) override;
229  virtual bool purge(const std::shared_ptr<JsonValue>& json) override;
230 
243  bool attach(const std::string& key, const std::shared_ptr<Node>& node);
244 
245 public:
246 #pragma mark -
247 #pragma mark Constructors
248 
255 
271  virtual bool init() override {
272  return init(nullptr);
273  }
274 
289  virtual bool init(const std::shared_ptr<ThreadPool>& threads) override;
290 
302  void dispose() override {
303  _manager = nullptr;
304  _assets.clear();
305  _loader = nullptr;
306  _types.clear();
307  _forms.clear();
308  }
309 
322  static std::shared_ptr<SceneLoader> alloc() {
323  std::shared_ptr<SceneLoader> result = std::make_shared<SceneLoader>();
324  return (result->init() ? result : nullptr);
325  }
326 
338  static std::shared_ptr<SceneLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
339  std::shared_ptr<SceneLoader> result = std::make_shared<SceneLoader>();
340  return (result->init(threads) ? result : nullptr);
341  }
342 
372  std::shared_ptr<Node> build(const std::string& key, const std::shared_ptr<JsonValue>& json) const;
373 
374 };
375 
376 }
377 
378 #endif /* __CU_SCENE_LOADER_H__ */
std::unordered_map< std::string, Form > _forms
Definition: CUSceneLoader.h:139
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
std::shared_ptr< Node > build(const std::string &key, const std::shared_ptr< JsonValue > &json) const
static std::shared_ptr< SceneLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUSceneLoader.h:338
virtual bool init() override
Definition: CUSceneLoader.h:271
AssetManager * _manager
Definition: CULoader.h:112
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
void dispose() override
Definition: CUSceneLoader.h:302
std::unordered_map< std::string, std::shared_ptr< Node > > _assets
Definition: CULoader.h:736
virtual bool purge(const std::shared_ptr< JsonValue > &json) override
bool attach(const std::string &key, const std::shared_ptr< Node > &node)
SceneLoader()
Definition: CUSceneLoader.h:254
Definition: CULoader.h:733
void materialize(const std::shared_ptr< Node > &node, LoaderCallback callback)
Definition: CUSceneLoader.h:77
Form
Definition: CUSceneLoader.h:122
std::unordered_map< std::string, Widget > _types
Definition: CUSceneLoader.h:136
Definition: CUAction.h:51
static std::shared_ptr< SceneLoader > alloc()
Definition: CUSceneLoader.h:322
Widget
Definition: CUSceneLoader.h:88