CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUGridLayout.h
1 //
2 // CUGridLayout.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an support for a grid layout. A grid layout subdivides
6 // the node into equal sized grid regions. Each grid region may receive a
7 // a single child. A grid region behaves like an AnchoredLayout for the
8 // rules on placing the child. The result is a slightly more flexible
9 // layout manager than the grid layout in Java.
10 //
11 // This class uses our standard shared-pointer architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 //
23 // CUGL MIT License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Walker White and Enze Zhou
43 // Version: 1/8/18
44 //
45 #ifndef __CU_GRID_LAYOUT_H__
46 #define __CU_GRID_LAYOUT_H__
47 #include <cugl/2d/layout/CULayout.h>
48 #include <unordered_map>
49 
50 namespace cugl {
51 
66 class GridLayout : public Layout {
67 protected:
74  class Entry {
75  public:
77  Uint32 x;
79  Uint32 y;
82  };
83 
85  std::unordered_map<std::string,Entry> _entries;
87  Uint32 _gwidth;
89  Uint32 _gheight;
90 
91 #pragma mark Constructors
92 public:
99  GridLayout();
100 
105 
121  virtual bool initWithData(const std::shared_ptr<JsonValue>& data) override;
122 
128  virtual void dispose() override { _entries.clear(); }
129 
139  static std::shared_ptr<GridLayout> alloc() {
140  std::shared_ptr<GridLayout> result = std::make_shared<GridLayout>();
141  return (result->init() ? result : nullptr);
142  }
143 
159  static std::shared_ptr<GridLayout> allocWithData(const std::shared_ptr<JsonValue>& data) {
160  std::shared_ptr<GridLayout> result = std::make_shared<GridLayout>();
161  return (result->initWithData(data) ? result : nullptr);
162  }
163 
164 #pragma mark Layout
165 
173  Size getGridSize() const { return Size((float)_gwidth, (float)_gheight); }
174 
184  void setGridSize(const Size& size) { setGridSize((Uint32)size.width, (Uint32)size.height); }
185 
194  void setGridSize(Uint32 width, Uint32 height);
195 
221  virtual bool add(const std::string key, const std::shared_ptr<JsonValue>& data) override;
222 
243  bool addPosition(const std::string key, unsigned int x, unsigned int y, Anchor anchor);
244 
259  virtual bool remove(const std::string key) override;
260 
279  virtual void layout(Node* node) override;
280 
281 #pragma mark Internal Helpers
282 protected:
296  bool validate(Uint32 width, Uint32 height);
297 };
298 
299 }
300 
301 #endif /* __CU_GRID_LAYOUT_H__ */
cugl::GridLayout::~GridLayout
~GridLayout()
Definition: CUGridLayout.h:104
cugl::Layout
Definition: CULayout.h:61
cugl::GridLayout::initWithData
virtual bool initWithData(const std::shared_ptr< JsonValue > &data) override
cugl::GridLayout::GridLayout
GridLayout()
cugl::GridLayout::validate
bool validate(Uint32 width, Uint32 height)
cugl::GridLayout::Entry::x
Uint32 x
Definition: CUGridLayout.h:77
cugl::GridLayout::addPosition
bool addPosition(const std::string key, unsigned int x, unsigned int y, Anchor anchor)
cugl::GridLayout::alloc
static std::shared_ptr< GridLayout > alloc()
Definition: CUGridLayout.h:139
cugl::Size
Definition: CUSize.h:57
cugl::GridLayout
Definition: CUGridLayout.h:66
cugl::GridLayout::remove
virtual bool remove(const std::string key) override
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::GridLayout::Entry
Definition: CUGridLayout.h:74
cugl::GridLayout::_gheight
Uint32 _gheight
Definition: CUGridLayout.h:89
cugl::GridLayout::_entries
std::unordered_map< std::string, Entry > _entries
Definition: CUGridLayout.h:85
cugl::Node
Definition: CUNode.h:92
cugl::GridLayout::dispose
virtual void dispose() override
Definition: CUGridLayout.h:128
cugl::GridLayout::layout
virtual void layout(Node *node) override
cugl::GridLayout::allocWithData
static std::shared_ptr< GridLayout > allocWithData(const std::shared_ptr< JsonValue > &data)
Definition: CUGridLayout.h:159
cugl::GridLayout::Entry::anchor
Anchor anchor
Definition: CUGridLayout.h:81
cugl::GridLayout::setGridSize
void setGridSize(const Size &size)
Definition: CUGridLayout.h:184
cugl::GridLayout::_gwidth
Uint32 _gwidth
Definition: CUGridLayout.h:87
cugl::GridLayout::getGridSize
Size getGridSize() const
Definition: CUGridLayout.h:173
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::Layout::Anchor
Anchor
Definition: CULayout.h:73
cugl::GridLayout::Entry::y
Uint32 y
Definition: CUGridLayout.h:79
cugl::GridLayout::add
virtual bool add(const std::string key, const std::shared_ptr< JsonValue > &data) override