CUGL 1.3
Cornell University Game Library
CUAnchoredLayout.h
1 //
2 // CUAnchoredLayout.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an support for an anchored layout. An anchored layout
6 // attaches a child node to one of nine "anchors" in the parent (corners, sides
7 // or middle), together with a percentage (or absolute) offset. As the parent
8 // grows or shinks, the child will move according to its anchor. For example,
9 // nodes in the center will stay centered, while nodes on the left side will
10 // move to keep the appropriate distance from the left side.
11 //
12 // This class uses our standard shared-pointer 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 //
24 // CUGL MIT License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // Author: Walker White and Enze Zhou
44 // Version: 1/8/18
45 //
46 #ifndef __CU_ANCHORED_LAYOUT_H__
47 #define __CU_ANCHORED_LAYOUT_H__
48 #include <cugl/2d/layout/CULayout.h>
49 #include <unordered_map>
50 
51 namespace cugl {
68 class AnchoredLayout : public Layout {
69 protected:
77  class Entry {
78  public:
80  float x_offset;
82  float y_offset;
86  bool absolute;
87  };
88 
90  std::unordered_map<std::string,Entry> _entries;
91 
92 #pragma mark -
93 #pragma mark Constructors
94 public:
102 
107 
118  virtual bool initWithData(const std::shared_ptr<JsonValue>& data) override { return true; }
119 
125  virtual void dispose() override { _entries.clear(); }
126 
136  static std::shared_ptr<AnchoredLayout> alloc() {
137  std::shared_ptr<AnchoredLayout> result = std::make_shared<AnchoredLayout>();
138  return (result->init() ? result : nullptr);
139  }
140 
151  static std::shared_ptr<AnchoredLayout> allocWithData(const std::shared_ptr<JsonValue>& data) {
152  std::shared_ptr<AnchoredLayout> result = std::make_shared<AnchoredLayout>();
153  return (result->initWithData(data) ? result : nullptr);
154  }
155 
156 #pragma mark Layout
157 
185  virtual bool add(const std::string key, const std::shared_ptr<JsonValue>& data) override;
186 
205  bool addAbsolute(const std::string key, Anchor anchor, const Vec2& offset);
206 
225  bool addRelative(const std::string key, Anchor anchor, const Vec2& offset);
226 
241  virtual bool remove(const std::string key) override;
242 
267  virtual void layout(Node* node) override;
268 
269 };
270 
271 }
272 #endif /* __CU_ANCHORED_LAYOUT_H__ */
cugl::Layout
Definition: CULayout.h:61
cugl::AnchoredLayout::layout
virtual void layout(Node *node) override
cugl::AnchoredLayout::alloc
static std::shared_ptr< AnchoredLayout > alloc()
Definition: CUAnchoredLayout.h:136
cugl::AnchoredLayout::addAbsolute
bool addAbsolute(const std::string key, Anchor anchor, const Vec2 &offset)
cugl::AnchoredLayout::Entry::y_offset
float y_offset
Definition: CUAnchoredLayout.h:82
cugl::AnchoredLayout::Entry::absolute
bool absolute
Definition: CUAnchoredLayout.h:86
cugl::AnchoredLayout::~AnchoredLayout
~AnchoredLayout()
Definition: CUAnchoredLayout.h:106
cugl::AnchoredLayout::Entry
Definition: CUAnchoredLayout.h:77
cugl::AnchoredLayout::_entries
std::unordered_map< std::string, Entry > _entries
Definition: CUAnchoredLayout.h:90
cugl::AnchoredLayout::addRelative
bool addRelative(const std::string key, Anchor anchor, const Vec2 &offset)
cugl::AnchoredLayout::Entry::x_offset
float x_offset
Definition: CUAnchoredLayout.h:80
cugl::Node
Definition: CUNode.h:92
cugl::AnchoredLayout::allocWithData
static std::shared_ptr< AnchoredLayout > allocWithData(const std::shared_ptr< JsonValue > &data)
Definition: CUAnchoredLayout.h:151
cugl::Vec2
Definition: CUVec2.h:61
cugl::AnchoredLayout::remove
virtual bool remove(const std::string key) override
cugl::AnchoredLayout::dispose
virtual void dispose() override
Definition: CUAnchoredLayout.h:125
cugl::AnchoredLayout::Entry::anchor
Anchor anchor
Definition: CUAnchoredLayout.h:84
cugl::AnchoredLayout::initWithData
virtual bool initWithData(const std::shared_ptr< JsonValue > &data) override
Definition: CUAnchoredLayout.h:118
cugl::AnchoredLayout::add
virtual bool add(const std::string key, const std::shared_ptr< JsonValue > &data) override
cugl::AnchoredLayout
Definition: CUAnchoredLayout.h:68
cugl::Layout::Anchor
Anchor
Definition: CULayout.h:73
cugl::AnchoredLayout::AnchoredLayout
AnchoredLayout()
Definition: CUAnchoredLayout.h:101