CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUNinePatch.h
1 //
2 // CUNinePatch.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module implements a ninepatch for expandable UI elements. A ninepatch
6 // breaks up an image into nine parts. It expands the middle elements while
7 // preserving the corners. This allows us to arbitrarily stretch an image
8 // like a beveled button without distorting it. Ninepatches are used heavily
9 // in mobile UI development.
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 // CUGL MIT License:
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // Author: Walker White
42 // Version: 11/8/17
43 //
44 #ifndef __CU_NINEPATCH_H__
45 #define __CU_NINEPATCH_H__
46 
47 #include <string>
48 #include <cugl/math/CURect.h>
49 #include <cugl/2d/CUNode.h>
50 #include <cugl/renderer/CUTexture.h>
51 #include <cugl/renderer/CUVertex.h>
52 #include <cugl/renderer/CUSpriteBatch.h>
53 
54 namespace cugl {
55 
70 class NinePatch : public Node {
71 #pragma mark Values
72 protected:
74  std::shared_ptr<Texture> _texture;
75 
78 
80  bool _rendered;
82  std::vector<Vertex2> _vertices;
84  std::vector<unsigned short> _indices;
85 
89  GLenum _srcFactor;
91  GLenum _dstFactor;
92 
93 
94 #pragma mark -
95 #pragma mark Constructors
96 public:
105  NinePatch();
106 
111 
121  virtual void dispose() override;
122 
133  virtual bool init() override {
135  }
136 
148  bool initWithFile(const std::string& filename);
149 
166  bool initWithFile(const std::string& filename, const Rect& interior);
167 
179  bool initWithTexture(const std::shared_ptr<Texture>& texture);
180 
197  bool initWithTexture(const std::shared_ptr<Texture>& texture, const Rect& interior);
198 
218  virtual bool initWithData(const SceneLoader* loader, const std::shared_ptr<JsonValue>& data) override;
219 
220 
221 #pragma mark -
222 #pragma mark Static Constructors
223 
233  static std::shared_ptr<NinePatch> alloc() {
234  std::shared_ptr<NinePatch> node = std::make_shared<NinePatch>();
235  return (node->init() ? node : nullptr);
236  }
237 
253  static std::shared_ptr<NinePatch> allocWithFile(const std::string& filename) {
254  std::shared_ptr<NinePatch> node = std::make_shared<NinePatch>();
255  return (node->initWithFile(filename) ? node : nullptr);
256  }
257 
273  static std::shared_ptr<NinePatch> allocWithFile(const std::string& filename, const Rect& interior) {
274  std::shared_ptr<NinePatch> node = std::make_shared<NinePatch>();
275  return (node->initWithFile(filename,interior) ? node : nullptr);
276  }
277 
289  static std::shared_ptr<NinePatch> allocWithTexture(const std::shared_ptr<Texture>& texture) {
290  std::shared_ptr<NinePatch> node = std::make_shared<NinePatch>();
291  return (node->initWithTexture(texture) ? node : nullptr);
292  }
293 
310  static std::shared_ptr<NinePatch> allocWithTexture(const std::shared_ptr<Texture>& texture,
311  const Rect& interior) {
312  std::shared_ptr<NinePatch> node = std::make_shared<NinePatch>();
313  return (node->initWithTexture(texture,interior) ? node : nullptr);
314  }
315 
335  static std::shared_ptr<Node> allocWithData(const SceneLoader* loader,
336  const std::shared_ptr<JsonValue>& data) {
337  std::shared_ptr<NinePatch> result = std::make_shared<NinePatch>();
338  if (!result->initWithData(loader,data)) { result = nullptr; }
339  return std::dynamic_pointer_cast<Node>(result);
340  }
341 
342 
343 #pragma mark -
344 #pragma mark Attributes
345 
358  virtual void setContentSize(const Size& size) override;
359 
369  void setTexture(const std::string &filename) {
370  std::shared_ptr<Texture> texture = Texture::allocWithFile(filename);
371  setTexture(texture);
372  }
373 
383  void setTexture(const std::shared_ptr<Texture>& texture);
384 
390  std::shared_ptr<Texture>& getTexture() { return _texture; }
391 
397  const std::shared_ptr<Texture>& getTexture() const { return _texture; }
398 
412  void setInterior(const Rect& interior);
413 
427  const Rect& getInterior() const { return _interior; }
428 
447  void setBlendFunc(GLenum srcFactor, GLenum dstFactor) { _srcFactor = srcFactor; _dstFactor = dstFactor; }
448 
461  GLenum getSourceBlendFactor() const { return _srcFactor; }
462 
475  GLenum getDestinationBlendFactor() const { return _srcFactor; }
476 
492  void setBlendEquation(GLenum equation) { _blendEquation = equation; }
493 
506  GLenum getBlendEquation() const { return _blendEquation; }
507 
508 
509 #pragma mark -
510 #pragma mark Rendering
511 
534  virtual void draw(const std::shared_ptr<SpriteBatch>& batch,
535  const Mat4& transform, Color4 tint) override;
536 
541 
552  virtual std::string toString(bool verbose=false) const override;
553 
554 #pragma mark -
555 #pragma mark Internal Helpers
556 protected:
560  virtual void generateRenderData();
561 
565  void clearRenderData();
566 
585  unsigned short generatePatch(const Rect& src, const Rect& dst, unsigned short offset);
586 
589 };
590 
591 }
592 #endif /* __CU_NINEPATCH_H__ */
cugl::NinePatch
Definition: CUNinePatch.h:70
cugl::NinePatch::getTexture
std::shared_ptr< Texture > & getTexture()
Definition: CUNinePatch.h:390
cugl::NinePatch::_interior
Rect _interior
Definition: CUNinePatch.h:77
cugl::NinePatch::initWithData
virtual bool initWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data) override
cugl::NinePatch::allocWithTexture
static std::shared_ptr< NinePatch > allocWithTexture(const std::shared_ptr< Texture > &texture)
Definition: CUNinePatch.h:289
cugl::NinePatch::alloc
static std::shared_ptr< NinePatch > alloc()
Definition: CUNinePatch.h:233
cugl::NinePatch::CU_DISALLOW_COPY_AND_ASSIGN
CU_DISALLOW_COPY_AND_ASSIGN(NinePatch)
cugl::NinePatch::getDestinationBlendFactor
GLenum getDestinationBlendFactor() const
Definition: CUNinePatch.h:475
cugl::SpriteBatch::getBlankTexture
static const std::shared_ptr< Texture > & getBlankTexture()
cugl::NinePatch::_srcFactor
GLenum _srcFactor
Definition: CUNinePatch.h:89
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::NinePatch::toString
virtual std::string toString(bool verbose=false) const override
cugl::NinePatch::_dstFactor
GLenum _dstFactor
Definition: CUNinePatch.h:91
cugl::NinePatch::_vertices
std::vector< Vertex2 > _vertices
Definition: CUNinePatch.h:82
cugl::NinePatch::NinePatch
NinePatch()
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::NinePatch::generatePatch
unsigned short generatePatch(const Rect &src, const Rect &dst, unsigned short offset)
cugl::NinePatch::_rendered
bool _rendered
Definition: CUNinePatch.h:80
cugl::NinePatch::allocWithFile
static std::shared_ptr< NinePatch > allocWithFile(const std::string &filename, const Rect &interior)
Definition: CUNinePatch.h:273
cugl::NinePatch::setBlendFunc
void setBlendFunc(GLenum srcFactor, GLenum dstFactor)
Definition: CUNinePatch.h:447
cugl::SceneLoader
Definition: CUSceneLoader.h:77
cugl::NinePatch::setBlendEquation
void setBlendEquation(GLenum equation)
Definition: CUNinePatch.h:492
cugl::Node
Definition: CUNode.h:92
cugl::NinePatch::clearRenderData
void clearRenderData()
cugl::NinePatch::init
virtual bool init() override
Definition: CUNinePatch.h:133
cugl::NinePatch::initWithTexture
bool initWithTexture(const std::shared_ptr< Texture > &texture)
cugl::NinePatch::getTexture
const std::shared_ptr< Texture > & getTexture() const
Definition: CUNinePatch.h:397
cugl::NinePatch::allocWithFile
static std::shared_ptr< NinePatch > allocWithFile(const std::string &filename)
Definition: CUNinePatch.h:253
cugl::NinePatch::generateRenderData
virtual void generateRenderData()
cugl::NinePatch::draw
virtual void draw(const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
cugl::NinePatch::initWithFile
bool initWithFile(const std::string &filename)
cugl::NinePatch::setTexture
void setTexture(const std::string &filename)
Definition: CUNinePatch.h:369
cugl::NinePatch::setInterior
void setInterior(const Rect &interior)
cugl::NinePatch::getBlendEquation
GLenum getBlendEquation() const
Definition: CUNinePatch.h:506
cugl::NinePatch::_blendEquation
GLenum _blendEquation
Definition: CUNinePatch.h:87
cugl::NinePatch::dispose
virtual void dispose() override
cugl::Texture::allocWithFile
static std::shared_ptr< Texture > allocWithFile(const std::string &filename)
Definition: CUTexture.h:269
cugl::NinePatch::allocWithTexture
static std::shared_ptr< NinePatch > allocWithTexture(const std::shared_ptr< Texture > &texture, const Rect &interior)
Definition: CUNinePatch.h:310
cugl::NinePatch::~NinePatch
~NinePatch()
Definition: CUNinePatch.h:110
cugl::NinePatch::_texture
std::shared_ptr< Texture > _texture
Definition: CUNinePatch.h:74
cugl::NinePatch::setContentSize
virtual void setContentSize(const Size &size) override
cugl::NinePatch::refresh
void refresh()
Definition: CUNinePatch.h:540
cugl::NinePatch::getSourceBlendFactor
GLenum getSourceBlendFactor() const
Definition: CUNinePatch.h:461
cugl::NinePatch::_indices
std::vector< unsigned short > _indices
Definition: CUNinePatch.h:84
cugl::NinePatch::allocWithData
static std::shared_ptr< Node > allocWithData(const SceneLoader *loader, const std::shared_ptr< JsonValue > &data)
Definition: CUNinePatch.h:335
cugl::NinePatch::getInterior
const Rect & getInterior() const
Definition: CUNinePatch.h:427