neoGFX
C++ GPU-oriented GUI library and app/game creation framework.
shape.hpp
Go to the documentation of this file.
1 // shape.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2015 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #pragma once
20 
21 #include <neogfx/neogfx.hpp>
22 #include <neogfx/core/colour.hpp>
23 #include <neogfx/gfx/texture.hpp>
24 #include <neogfx/gfx/i_image.hpp>
25 #include "i_shape.hpp"
26 
27 namespace neogfx
28 {
29  class shape_frame : public i_shape_frame
30  {
31  public:
32  shape_frame(const colour_or_gradient& aColour) : iColour(aColour) {}
33  shape_frame(const colour_or_gradient& aColour, const mat33& aTransformation) : iColour(aColour), iTransformation(aTransformation) {}
34  shape_frame(const i_texture& aTexture) : iTextures{ to_texture_list_pointer(aTexture) } {}
35  shape_frame(const i_texture& aTexture, const mat33& aTransformation) : iTextures{ to_texture_list_pointer(aTexture) }, iTransformation(aTransformation) {}
36  shape_frame(const i_texture& aTexture, const optional_rect& aTextureRect) : iTextures{ to_texture_list_pointer(aTexture, aTextureRect) } {}
37  shape_frame(const i_texture& aTexture, const optional_rect& aTextureRect, const mat33& aTransformation) : iTextures{ to_texture_list_pointer(aTexture, aTextureRect) }, iTransformation(aTransformation) {}
38  shape_frame(texture_list_pointer aTextures) : iTextures{ aTextures } {}
39  shape_frame(texture_list_pointer aTextures, const mat33& aTransformation) : iTextures{ aTextures }, iTransformation(aTransformation) {}
40  public:
41  bool has_extents() const override { return iTextures != nullptr && !iTextures->empty(); }
42  size extents() const override { return has_extents() ? (*iTextures)[0].second != boost::none ? *(*iTextures)[0].second : (*iTextures)[0].first->extents() : size{}; }
43  const optional_colour_or_gradient& colour() const override { return iColour; }
44  void set_colour(const optional_colour_or_gradient& aColour) override { iColour = aColour; }
45  texture_list_pointer textures() const override { return iTextures; }
46  void set_textures(texture_list_pointer aTextures) override { iTextures = aTextures; }
47  const optional_mat33& transformation() const override { return iTransformation; }
48  void set_transformation(const optional_mat33& aTransformation) override { iTransformation = aTransformation; }
49  private:
51  texture_list_pointer iTextures;
52  optional_mat33 iTransformation;
53  };
54 
55  class shape : public virtual i_shape
56  {
57  private:
58  typedef std::vector<std::shared_ptr<i_shape_frame>> frame_list;
59  public:
66  public:
68  {
70  {
73  };
74  std::vector<texture_sheet_item> items;
75  uint32_t count;
76  time_interval time;
77  bool repeat;
78  };
79  typedef boost::optional<animation_info> optional_animation_info;
80  public:
81  shape();
82  shape(const colour& aColour);
83  shape(const i_texture& aTexture, const optional_animation_info& aAnimationInfo = optional_animation_info());
84  shape(const i_image& aImage, const optional_animation_info& aAnimationInfo = optional_animation_info());
85  shape(const i_texture& aTexture, const rect& aTextureRect, const optional_animation_info& aAnimationInfo = optional_animation_info());
86  shape(const i_image& aImage, const rect& aTextureRect, const optional_animation_info& aAnimationInfo = optional_animation_info());
87  shape(i_shape_container& aContainer);
88  shape(i_shape_container& aContainer, const colour& aColour);
89  shape(i_shape_container& aContainer, const i_texture& aTexture, const optional_animation_info& aAnimationInfo = optional_animation_info());
90  shape(i_shape_container& aContainer, const i_image& aImage, const optional_animation_info& aAnimationInfo = optional_animation_info());
91  shape(i_shape_container& aContainer, const i_texture& aTexture, const rect& aTextureRect, const optional_animation_info& aAnimationInfo = optional_animation_info());
92  shape(i_shape_container& aContainer, const i_image& aImage, const rect& aTextureRect, const optional_animation_info& aAnimationInfo = optional_animation_info());
93  shape(const shape& aOther);
94  // object
95  public:
96  object_category category() const override;
97  const i_shape& as_shape() const override;
98  i_shape& as_shape() override;
99  const i_collidable_object& as_collidable_object() const override;
100  i_collidable_object& as_collidable_object() override;
101  const i_physical_object& as_physical_object() const override;
102  i_physical_object& as_physical_object() override;
103  bool killed() const override;
104  void kill() override;
105  // mesh
106  public:
107  vertex_list_pointer vertices() const override;
108  texture_list_pointer textures() const override;
109  face_list faces() const override;
110  face_list active_faces() const override;
111  void activate_faces(face_list aActiveFaces) const override;
112  mat44 transformation_matrix() const override;
113  const vertex_list& transformed_vertices() const override;
114  public:
115  void set_vertices(vertex_list_pointer aVertices) override;
116  void set_textures(texture_list_pointer aTextures) override;
117  void set_faces(face_list aFaces) override;
118  public:
119  // container
120  public:
121  const i_shape_container& container() const override;
122  i_shape_container& container() override;
123  // tag
124  public:
125  bool is_tag() const override;
126  i_shape& tag_of() const override;
127  void set_tag_of(i_shape& aTagOf, const vec3& aOffset = vec3{}) override;
128  const vec3& tag_offset() const override;
129  void set_tag_offset(const vec3& aOffset) override;
130  void unset_tag_of() override;
131  // animation
132  public:
133  frame_index frame_count() const override;
134  const i_shape_frame& shape_frame(frame_index aFrameIndex) const override;
135  i_shape_frame& shape_frame(frame_index aFrameIndex) override;
136  void add_frame(i_shape_frame& aFrame) override;
137  void add_frame(std::shared_ptr<i_shape_frame> aFrame) override;
138  void replace_frame(frame_index aFrameIndex, i_shape_frame& aFrame) override;
139  void replace_frame(frame_index aFrameIndex, std::shared_ptr<i_shape_frame> aFrame) override;
140  void remove_frame(frame_index aFrameIndex) override;
141  // geometry
142  public:
143  const animation_frames& animation() const override;
144  bool repeat_animation() const override;
145  const animation_frame& current_animation_frame() const override;
146  bool has_animation_finished() const override;
147  void animation_finished() override;
148  frame_index current_frame_index() const override;
149  const i_shape_frame& current_frame() const override;
150  i_shape_frame& current_frame();
151  vec3 origin() const override;
152  vec3 position() const override;
153  vec3 extents() const override;
154  rect bounding_box_2d(bool aWithPosition = true) const override;
155  void set_animation(const animation_frames& aAnimation) override;
156  void set_current_frame(frame_index aFrameIndex) override;
157  void set_origin(const vec3& aOrigin) override;
158  void set_position(const vec3& aPosition) override;
159  void clear_extents() override;
160  void set_extents(const vec3& aExtents) override;
161  bool has_transformation_matrix() const override;
162  void clear_transformation_matrix() override;
163  void set_transformation_matrix(const mat33& aTransformationMatrix) override;
164  void set_transformation_matrix(const mat44& aTransformationMatrix) override;
165  // rendering
166  public:
167  bool update(time_interval aNow) override;
168  void paint(graphics_context& aGraphicsContext) const override;
169  // udates
170  public:
171  virtual void clear_vertices_cache();
172  // implementation
173  private:
174  void init_frames(const i_texture& aTexture, const optional_rect& aTextureRect, const optional_animation_info& aAnimationInfo);
175  void init_frames(texture_list_pointer aTextures, const optional_animation_info& aAnimationInfo);
176  // attributes
177  private:
178  i_shape_container* iContainer;
179  frame_list iFrames;
180  animation_frames iAnimation;
181  bool iRepeatAnimation;
182  frame_index iAnimationFrame;
183  frame_index iCurrentFrame;
184  optional_time_interval iTimeOfLastUpdate;
185  vec3 iOrigin;
186  vec3 iPosition;
187  optional_vec3 iExtents;
188  optional_mat44 iTransformationMatrix;
189  std::pair<i_shape*, vec3> iTagOf;
190  mutable vertex_list_pointer iDefaultVertices;
191  mutable face_list iDefaultFaces;
192  mutable vertex_list_pointer iVertices;
193  mutable face_list iFaces;
194  mutable vertex_list iTransformedVertices;
195  mutable face_list iActiveFaces;
196  bool iKilled;
197  };
198 }
199 
200 #include "rectangle.hpp"
const optional_mat33 & transformation() const override
Definition: shape.hpp:47
std::shared_ptr< vertex_list > vertex_list_pointer
Definition: i_mesh.hpp:38
void set_transformation(const optional_mat33 &aTransformation) override
Definition: shape.hpp:48
size extents() const override
Definition: shape.hpp:42
shape_frame(const colour_or_gradient &aColour)
Definition: shape.hpp:32
std::size_t frame_index
Definition: i_shape.hpp:62
shape_frame(const i_texture &aTexture, const optional_rect &aTextureRect)
Definition: shape.hpp:36
texture_list_pointer textures() const override
Definition: shape.hpp:45
boost::optional< mat33 > optional_mat33
Definition: numerical.hpp:638
boost::optional< colour_or_gradient > optional_colour_or_gradient
Definition: colour.hpp:929
boost::optional< vec3 > optional_vec3
Definition: numerical.hpp:294
texture_list_pointer to_texture_list_pointer(const i_texture &aTexture, const optional_rect &aTextureRect=optional_rect{})
Definition: texture.hpp:76
boost::optional< rect > optional_rect
void set_textures(texture_list_pointer aTextures) override
Definition: shape.hpp:46
scalar time_interval
Definition: i_shape.hpp:63
i_shape::optional_time_interval optional_time_interval
Definition: shape.hpp:65
i_shape::animation_frames animation_frames
Definition: shape.hpp:63
boost::optional< animation_info > optional_animation_info
Definition: shape.hpp:79
bool has_extents() const override
Definition: shape.hpp:41
shape_frame(const i_texture &aTexture, const mat33 &aTransformation)
Definition: shape.hpp:35
std::vector< animation_frame > animation_frames
Definition: i_shape.hpp:65
std::vector< vertex > vertex_list
Definition: i_mesh.hpp:35
shape_frame(texture_list_pointer aTextures)
Definition: shape.hpp:38
void set_colour(const optional_colour_or_gradient &aColour) override
Definition: shape.hpp:44
std::vector< texture_sheet_item > items
Definition: shape.hpp:74
boost::optional< mat44 > optional_mat44
Definition: numerical.hpp:643
i_shape::time_interval time_interval
Definition: shape.hpp:61
i_shape::animation_frame animation_frame
Definition: shape.hpp:62
std::shared_ptr< texture_list > texture_list_pointer
Definition: texture.hpp:69
std::pair< frame_index, time_interval > animation_frame
Definition: i_shape.hpp:64
const optional_colour_or_gradient & colour() const override
Definition: shape.hpp:43
shape_frame(const i_texture &aTexture, const optional_rect &aTextureRect, const mat33 &aTransformation)
Definition: shape.hpp:37
shape_frame(const colour_or_gradient &aColour, const mat33 &aTransformation)
Definition: shape.hpp:33
shape_frame(const i_texture &aTexture)
Definition: shape.hpp:34
shape_frame(texture_list_pointer aTextures, const mat33 &aTransformation)
Definition: shape.hpp:39
i_shape::frame_index frame_index
Definition: shape.hpp:60
boost::optional< time_interval > optional_time_interval
Definition: i_shape.hpp:66