CUGL 1.3
Cornell University Game Library
CUFloatLayout.h
1 //
2 // CUFloatLayout.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an support for a float layout. Children in a float
6 // layout are arranged in order, according to the layout orientation (horizontal
7 // or vertical). If there is not enough space in the Node for the children
8 // to all be in the same row or column (depending on orientation), then the
9 // later children wrap around to a new row or column. This is the same way
10 // that float layouts work in Java.
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_FLOAT_LAYOUT_H__
47 #define __CU_FLOAT_LAYOUT_H__
48 #include <cugl/2d/layout/CULayout.h>
49 #include <vector>
50 #include <unordered_map>
51 
52 namespace cugl {
53 
73 class FloatLayout : public Layout {
74 public:
87  enum class Alignment : int {
99  BOTTOM_LEFT = 0,
110  MIDDLE_LEFT = 1,
122  TOP_LEFT = 2,
133  BOTTOM_CENTER = 3,
143  CENTER = 4,
154  TOP_CENTER = 5,
166  BOTTOM_RIGHT = 6,
177  MIDDLE_RIGHT = 7,
189  TOP_RIGHT = 8
190  };
191 
192 protected:
194  std::vector<std::string> _priority;
196  std::unordered_map<std::string,size_t> _keyset;
197 
202 
203 #pragma mark Internal Helpers
204 
213  void layoutHorizontal(Node* node);
214 
224  void layoutVertical(Node* node);
225 
226 
227 #pragma mark Constructors
228 public:
235  FloatLayout();
236 
241 
258  virtual bool initWithData(const std::shared_ptr<JsonValue>& data) override;
259 
265  virtual void dispose() override { _priority.clear(); _keyset.clear(); }
266 
276  static std::shared_ptr<FloatLayout> alloc() {
277  std::shared_ptr<FloatLayout> result = std::make_shared<FloatLayout>();
278  return (result->init() ? result : nullptr);
279  }
280 
297  static std::shared_ptr<FloatLayout> allocWithData(const std::shared_ptr<JsonValue>& data) {
298  std::shared_ptr<FloatLayout> result = std::make_shared<FloatLayout>();
299  return (result->initWithData(data) ? result : nullptr);
300  }
301 
302 #pragma mark Layout
303 
310  bool isHorizontal() const { return _horizontal; }
311 
319  void setHorizontal(bool value) { _horizontal = value; }
320 
328  Alignment getAlignment() const { return _alignment; }
329 
337  void setAlignment(Alignment value) { _alignment = value; }
338 
361  virtual bool add(const std::string key, const std::shared_ptr<JsonValue>& data) override;
362 
380  bool addPriority(const std::string key, size_t priority);
381 
396  virtual bool remove(const std::string key) override;
397 
425  virtual void layout(Node* node) override;
426 
427 };
428 
429 }
430 
431 
432 #endif /* __CU_FLOAT_LAYOUT_H__ */
cugl::FloatLayout::layoutVertical
void layoutVertical(Node *node)
cugl::FloatLayout::remove
virtual bool remove(const std::string key) override
cugl::FloatLayout::Alignment::MIDDLE_RIGHT
cugl::Layout
Definition: CULayout.h:61
cugl::FloatLayout::initWithData
virtual bool initWithData(const std::shared_ptr< JsonValue > &data) override
cugl::FloatLayout::FloatLayout
FloatLayout()
cugl::FloatLayout::setAlignment
void setAlignment(Alignment value)
Definition: CUFloatLayout.h:337
cugl::FloatLayout::Alignment::TOP_RIGHT
cugl::FloatLayout::layoutHorizontal
void layoutHorizontal(Node *node)
cugl::FloatLayout::_alignment
Alignment _alignment
Definition: CUFloatLayout.h:201
cugl::FloatLayout::Alignment
Alignment
Definition: CUFloatLayout.h:87
cugl::FloatLayout::_keyset
std::unordered_map< std::string, size_t > _keyset
Definition: CUFloatLayout.h:196
cugl::FloatLayout::alloc
static std::shared_ptr< FloatLayout > alloc()
Definition: CUFloatLayout.h:276
cugl::FloatLayout::_horizontal
bool _horizontal
Definition: CUFloatLayout.h:199
cugl::FloatLayout::getAlignment
Alignment getAlignment() const
Definition: CUFloatLayout.h:328
cugl::FloatLayout::layout
virtual void layout(Node *node) override
cugl::FloatLayout::Alignment::BOTTOM_LEFT
cugl::FloatLayout::~FloatLayout
~FloatLayout()
Definition: CUFloatLayout.h:240
cugl::FloatLayout::_priority
std::vector< std::string > _priority
Definition: CUFloatLayout.h:194
cugl::FloatLayout::Alignment::MIDDLE_LEFT
cugl::FloatLayout::Alignment::BOTTOM_CENTER
cugl::Node
Definition: CUNode.h:92
cugl::FloatLayout::setHorizontal
void setHorizontal(bool value)
Definition: CUFloatLayout.h:319
cugl::FloatLayout::dispose
virtual void dispose() override
Definition: CUFloatLayout.h:265
cugl::FloatLayout::Alignment::CENTER
cugl::FloatLayout
Definition: CUFloatLayout.h:73
cugl::FloatLayout::Alignment::TOP_CENTER
cugl::FloatLayout::Alignment::TOP_LEFT
cugl::FloatLayout::add
virtual bool add(const std::string key, const std::shared_ptr< JsonValue > &data) override
cugl::FloatLayout::allocWithData
static std::shared_ptr< FloatLayout > allocWithData(const std::shared_ptr< JsonValue > &data)
Definition: CUFloatLayout.h:297
cugl::FloatLayout::Alignment::BOTTOM_RIGHT
cugl::FloatLayout::isHorizontal
bool isHorizontal() const
Definition: CUFloatLayout.h:310
cugl::FloatLayout::addPriority
bool addPriority(const std::string key, size_t priority)