Gorgon Game Engine
StackedObject.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "EmptyImage.h"
4 #include "Animations.h"
5 #include "TextureAnimation.h"
6 
7 namespace Gorgon { namespace Graphics {
8 
11  public:
12  virtual RectangularAnimation &CreateTop() const = 0;
13  virtual RectangularAnimation &CreateBottom() const = 0;
14 
15  virtual Geometry::Point GetOffset() const = 0;
16  virtual void SetOffset(const Geometry::Point&) = 0;
17 
18  virtual IStackedObjectProvider &MoveOutProvider() override = 0;
19  };
20 
21  template<class A_>
23 
24  template<class A_>
25  class basic_StackedObject : public virtual RectangularAnimation {
26  public:
27  basic_StackedObject(const basic_StackedObjectProvider<A_> &parent, bool create = true);
28 
30 
34  basic_StackedObject(RectangularAnimation &top, RectangularAnimation &bottom, const Geometry::Point &offset, bool create = true) :
35  Gorgon::Animation::Base(create), top(top), offset(offset), bottom(bottom)
36  {
37  if(this->HasController()) {
38  top.SetController(this->GetController());
39  bottom.SetController(this->GetController());
40  }
41  else {
42  top.RemoveController();
43  bottom.RemoveController();
44  }
45  }
46 
51  Gorgon::Animation::Base(timer), top(top), offset(offset), bottom(bottom)
52  {
53  if(this->HasController()) {
54  top.SetController(this->GetController());
55  bottom.SetController(this->GetController());
56  }
57  else {
58  top.RemoveController();
59  bottom.RemoveController();
60  }
61  }
62 
64  top.DeleteAnimation();
65  bottom.DeleteAnimation();
66  }
67 
68  virtual bool Progress(unsigned &) override {
69  return true; //individual parts will work automatically
70  }
71 
72  int GetDuration() const override {
73  auto dur = top.GetDuration();
74  if(dur > 0)
75  return dur;
76 
77 
78  return bottom.GetDuration();
79  }
80 
81  protected:
82  virtual void drawin(TextureTarget &target, const Geometry::Rectanglef &r, RGBAf color) const override;
83 
84  virtual void drawin(TextureTarget &target, const SizeController &controller, const Geometry::Rectanglef &r, RGBAf color) const override;
85 
86  virtual Geometry::Size calculatesize(const Geometry::Size &area) const override {
87  return Union(top.CalculateSize(area), bottom.CalculateSize(area));
88  }
89 
90  virtual Geometry::Size calculatesize(const SizeController &controller, const Geometry::Size &s) const override {
91  return Union(top.CalculateSize(controller, s), bottom.CalculateSize(controller, s));
92  }
93 
94  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
95  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
96  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
97  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const override;
98 
99 
100  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
101  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color) const override;
102 
103 
104  virtual Geometry::Size getsize() const override;
105 
106  private:
107  RectangularAnimation &top, &bottom;
108  Geometry::Point offset;
109  };
110 
115  template<class A_>
117  public:
118  using AnimationType = typename A_::AnimationType;
119 
122 
124  basic_StackedObjectProvider(A_ &bottom, A_ &top, const Geometry::Point &offset ={0,0}) :
125  top(&top), bottom(&bottom), offset(offset) {}
126 
128  basic_StackedObjectProvider(A_ *bottom, A_ *top, const Geometry::Point &offset ={0,0}) :
129  top(top), bottom(bottom), offset(offset) {}
130 
132  basic_StackedObjectProvider(const AssumeOwnershipTag &, A_ &bottom, A_ &top, const Geometry::Point &offset ={0,0}) :
133  top(&top), bottom(&bottom), offset(offset) { own = true; }
134 
136  basic_StackedObjectProvider(const AssumeOwnershipTag &, A_ *bottom, A_ *top, const Geometry::Point &offset ={0,0}) :
137  top(top), bottom(bottom), offset(offset) { own = true; }
138 
139  basic_StackedObjectProvider(A_ &&bottom, A_ &&top, const Geometry::Point &offset = {0,0}) :
140  top(new A_(std::move(top))), bottom(new A_(std::move(bottom))), offset(offset), own(true)
141  { }
142 
144  top(other.top), bottom(other.bottom), offset(other.offset)
145  {
146  other.top = nullptr;
147  other.bottom = nullptr;
148  other.offset = {0, 0};
149  other.own = false;
150  }
151 
153  if(own) {
154  delete this->top;
155  delete this->bottom;
156  }
157  }
158 
160  if(own) {
161  delete this->top;
162  delete this->bottom;
163  }
164 
165  top = other.top;
166  bottom = other.bottom;
167  offset = other.offset;
168  own = other.own;
169 
170  other.top = nullptr;
171  other.bottom = nullptr;
172  other.offset ={0, 0};
173  other.own = false;
174 
175  return *this;
176  }
177 
178 
179  //types are derived not to type the same code for every class
180  virtual auto MoveOutProvider() -> decltype(*this) override {
181  auto ret = new typename std::remove_reference<decltype(*this)>::type(std::move(*this));
182 
183  return *ret;
184  }
185 
186  basic_StackedObject<A_> &CreateAnimation(bool create = true) const override {
187  return *new basic_StackedObject<A_>(*this, create);
188  }
189 
191  return *new basic_StackedObject<A_>(*this, timer);
192  }
193 
195  RectangularAnimation &CreateTop() const override {
196  if(top) {
197  return top->CreateAnimation(false);
198  }
199  else {
200  return EmptyImage::Instance();
201  }
202  }
203 
205  RectangularAnimation &CreateBottom() const override {
206  if(bottom) {
207  return bottom->CreateAnimation(false);
208  }
209  else {
210  return EmptyImage::Instance();
211  }
212  }
213 
215  A_ *GetTop() const {
216  return top;
217  }
218 
220  A_ *GetBottom() const {
221  return bottom;
222  }
223 
225  void SetTop(A_ *value) {
226  if(own)
227  delete top;
228 
229  top = value;
230  }
231 
233  void SetBottom(A_ *value) {
234  if(own)
235  delete bottom;
236 
237  bottom = value;
238  }
239 
241  Geometry::Point GetOffset() const override {
242  return offset;
243  }
244 
246  void SetOffset(const Geometry::Point &value) override {
247  offset = value;
248  }
249 
251  void SetProviders(A_ &top, A_ &bottom) {
252  if(own) {
253  delete this->top;
254  delete this->bottom;
255  }
256  this->top = &top;
257  this->bottom = &bottom;
258  }
259 
261  void OwnProviders() {
262  own = true;
263  }
264 
267  void Prepare() {
268  if(top) top->Prepare();
269  if(bottom) bottom->Prepare();
270  }
271 
272  Geometry::Size GetSize() const override {
273  return top ? top->GetSize() : Geometry::Size{0, 0};
274  }
275 
276  private:
277  A_ *top = nullptr;
278  A_ *bottom = nullptr;
279  Geometry::Point offset = {0,0};
280 
281  bool own = false;
282  };
283 
284  template<class A_>
286  Gorgon::Animation::Base(create),
287  top(parent.CreateTop()), bottom(parent.CreateBottom()),
288  offset(parent.GetOffset())
289  {
290  if(this->HasController()) {
291  top.SetController(this->GetController());
292  bottom.SetController(this->GetController());
293  }
294  }
295 
296  template<class A_>
298  Gorgon::Animation::Base(timer),
299  top(parent.CreateTop()), bottom(parent.CreateBottom()),
300  offset(parent.GetOffset())
301  {
302  if(this->HasController()) {
303  top.SetController(this->GetController());
304  bottom.SetController(this->GetController());
305  }
306  }
307 
308  template<class A_>
310  bottom.DrawIn(target, r, color);
311  top.DrawIn(target, r+offset, color);
312  }
313 
314  template<class A_>
315  void basic_StackedObject<A_>::drawin(TextureTarget &target, const SizeController &controller, const Geometry::Rectanglef &r, RGBAf color) const {
316  bottom.DrawIn(target, controller, r, color);
317  top.DrawIn(target, controller, r+offset, color);
318  }
319 
320 
321 
322  template<class A_>
324  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
325  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
326  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const
327  {
328  auto f = Geometry::Sizef((float)bottom.GetSize().Width/top.GetSize().Width, (float)bottom.GetSize().Height/top.GetSize().Height);
329  auto bp2 = p1 + (p2-p1) * f;
330  auto bp3 = p1 + (p3-p1) * f;
331  auto bp4 = p1 + (p4-p1) * f;
332 
333  bottom.Draw(target, p1, bp2, bp3, bp4, tex1, tex2, tex3, tex4, color);
334  top.Draw(target, p1+offset, p2+offset, p3+offset, p4+offset, tex1, tex2, tex3, tex4, color);
335  }
336 
337 
338  template<class A_>
340  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color) const
341  {
342  auto f = Geometry::Sizef((float)bottom.GetSize().Width/top.GetSize().Width, (float)bottom.GetSize().Height/top.GetSize().Height);
343  auto bp2 = p1 + (p2-p1) * f;
344  auto bp3 = p1 + (p3-p1) * f;
345  auto bp4 = p1 + (p4-p1) * f;
346  bottom.Draw(target, p1, bp2, bp3, bp4, color);
347  top.Draw(target, p1+offset, p2+offset, p3+offset, p4+offset, color);
348  }
349 
350 
351  template<class A_>
353  return top.GetSize();
354  }
355 
358 
361 
364 
365 } }
Gorgon::Graphics::basic_StackedObjectProvider::SetOffset
void SetOffset(const Geometry::Point &value) override
Sets the offset of the top image.
Definition: StackedObject.h:246
Gorgon::Animation::Base::GetDuration
virtual int GetDuration() const =0
Returns the duration of the animation if it is a known apriori.
Gorgon::Animation::Base::Base
Base(ControllerBase &controller)
Sets the controller for this animation to the given controller.
Definition: Animation.h:310
Gorgon::Graphics::SizeController
This class allows control over a sizable object.
Definition: Graphics.h:161
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(A_ *bottom, A_ *top, const Geometry::Point &offset={0, 0})
Filling constructor, nullptr is allowed but not recommended.
Definition: StackedObject.h:128
Gorgon::Graphics::basic_StackedObjectProvider::operator=
basic_StackedObjectProvider & operator=(basic_StackedObjectProvider &&other)
Definition: StackedObject.h:159
Gorgon::Graphics::EmptyImage::Instance
static EmptyImage & Instance()
Returns the instance for empty image. Only one instance is enough.
Definition: EmptyImage.h:50
Gorgon::Graphics::basic_StackedObjectProvider
This object creates an object that has two subobjects drawn on top of each other.
Definition: StackedObject.h:116
Gorgon::Graphics::basic_StackedObject::basic_StackedObject
basic_StackedObject(const basic_StackedObjectProvider< A_ > &parent, bool create=true)
Definition: StackedObject.h:285
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(const AssumeOwnershipTag &, A_ &bottom, A_ &top, const Geometry::Point &offset={0, 0})
Filling constructor.
Definition: StackedObject.h:132
Gorgon::Graphics::basic_StackedObjectProvider::SetTop
void SetTop(A_ *value)
Sets the top provider, ownership semantics will not be changed.
Definition: StackedObject.h:225
Gorgon::Graphics::basic_StackedObject::calculatesize
virtual Geometry::Size calculatesize(const Geometry::Size &area) const override
This function should return the size of the object when it is requested to be drawn in the given area...
Definition: StackedObject.h:86
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Graphics::basic_StackedObjectProvider::CreateAnimation
basic_StackedObject< A_ > & CreateAnimation(bool create=true) const override
Definition: StackedObject.h:186
Gorgon::Graphics::basic_StackedObject::drawin
virtual void drawin(TextureTarget &target, const Geometry::Rectanglef &r, RGBAf color) const override
This function should draw the object to the target area.
Definition: StackedObject.h:309
Gorgon::Graphics::basic_StackedObjectProvider::AnimationType
typename A_::AnimationType AnimationType
Definition: StackedObject.h:118
Gorgon::Graphics::basic_StackedObjectProvider::SetBottom
void SetBottom(A_ *value)
Sets the bottom provider, ownership semantics will not be changed.
Definition: StackedObject.h:233
Gorgon::Graphics::IStackedObjectProvider::SetOffset
virtual void SetOffset(const Geometry::Point &)=0
Gorgon::Graphics::basic_StackedObject
Definition: StackedObject.h:25
Gorgon::Graphics::basic_StackedObject::~basic_StackedObject
~basic_StackedObject()
Definition: StackedObject.h:63
Gorgon::Graphics::basic_StackedObject::basic_StackedObject
basic_StackedObject(RectangularAnimation &top, RectangularAnimation &bottom, const Geometry::Point &offset, Gorgon::Animation::ControllerBase &timer)
Creates a bottomed object from two animations, these animations should not have controllers attached ...
Definition: StackedObject.h:50
Gorgon::Graphics::RGBAf
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
Gorgon::Graphics::basic_StackedObjectProvider::MoveOutProvider
virtual auto MoveOutProvider() -> decltype(*this) override
Definition: StackedObject.h:180
EmptyImage.h
Gorgon::Graphics::basic_StackedObject::GetDuration
int GetDuration() const override
Returns the duration of the animation if it is a known apriori.
Definition: StackedObject.h:72
Gorgon::Graphics::basic_StackedObjectProvider::CreateTop
RectangularAnimation & CreateTop() const override
Creates a top animation without controller.
Definition: StackedObject.h:195
Gorgon::Graphics::IStackedObjectProvider::CreateTop
virtual RectangularAnimation & CreateTop() const =0
Gorgon::Graphics::basic_StackedObjectProvider::GetTop
A_ * GetTop() const
Returns the top component. Could return nullptr.
Definition: StackedObject.h:215
Gorgon::Geometry::Union
basic_Bounds< T_ > Union(const basic_Bounds< T_ > &l, const basic_Bounds< T_ > &r)
Returns the smallest bounds that contains given bounds.
Definition: Bounds.h:488
Gorgon::Graphics::basic_StackedObjectProvider::GetBottom
A_ * GetBottom() const
Returns the bottom component. Could return nullptr.
Definition: StackedObject.h:220
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(A_ &&bottom, A_ &&top, const Geometry::Point &offset={0, 0})
Definition: StackedObject.h:139
Gorgon::Graphics::IStackedObjectProvider::CreateBottom
virtual RectangularAnimation & CreateBottom() const =0
Gorgon::Graphics::RectangularAnimation
Rectangular drawable animation.
Definition: Animations.h:19
Gorgon::Graphics::basic_StackedObjectProvider::GetSize
Geometry::Size GetSize() const override
Definition: StackedObject.h:272
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(basic_StackedObjectProvider &&other)
Definition: StackedObject.h:143
Gorgon::Graphics::basic_StackedObjectProvider::~basic_StackedObjectProvider
~basic_StackedObjectProvider()
Definition: StackedObject.h:152
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(const AssumeOwnershipTag &, A_ *bottom, A_ *top, const Geometry::Point &offset={0, 0})
Filling constructor, nullptr is allowed but not recommended.
Definition: StackedObject.h:136
Gorgon::Graphics::IStackedObjectProvider
For ease of use in resource system.
Definition: StackedObject.h:10
Gorgon::Graphics::basic_StackedObjectProvider::SetProviders
void SetProviders(A_ &top, A_ &bottom)
Sets the providers in this object.
Definition: StackedObject.h:251
Gorgon::Geometry::Sizef
basic_Size< Float > Sizef
Definition: Size.h:388
Gorgon::Graphics::TextureTarget
This interface defines a class that can be used as a common target for texture based drawing.
Definition: TextureTargets.h:12
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Graphics::IStackedObjectProvider::MoveOutProvider
virtual IStackedObjectProvider & MoveOutProvider() override=0
This function moves this animation provider into a new provider.
Gorgon::Animation::Base::DeleteAnimation
virtual void DeleteAnimation() const
Deletes this animation.
Definition: Animation.h:379
Gorgon::Graphics::basic_StackedObjectProvider::CreateBottom
RectangularAnimation & CreateBottom() const override
Creates a bottom animation without controller.
Definition: StackedObject.h:205
Gorgon::Graphics::Animation
A regular drawable animation.
Definition: Animations.h:14
Animations.h
Gorgon::Graphics::RectangularAnimationProvider
This class provides rectangular animations.
Definition: Animations.h:48
Gorgon::Animation::Base::HasController
virtual bool HasController() const
Returns whether this animation has a controller.
Definition: Animation.h:344
Gorgon::UI::Graphics
@ Graphics
Definition: Template.h:164
Gorgon::Graphics::basic_StackedObjectProvider::GetOffset
Geometry::Point GetOffset() const override
Returns the offset of the top image.
Definition: StackedObject.h:241
Gorgon::Animation::Base::RemoveController
virtual void RemoveController()
Removes the controller of this animation.
Definition: Animation.h:357
Gorgon::Graphics::basic_StackedObject::getsize
virtual Geometry::Size getsize() const override
Should return the exact size of this object.
Definition: StackedObject.h:352
Gorgon::Graphics::basic_StackedObjectProvider::Prepare
void Prepare()
Prepares the providers.
Definition: StackedObject.h:267
Gorgon::Graphics::basic_StackedObjectProvider::OwnProviders
void OwnProviders()
Assumes the ownership of the providers.
Definition: StackedObject.h:261
Gorgon::Animation::Base::SetController
virtual void SetController(ControllerBase &controller)
Sets the controller to the given controller.
Definition: Animation.h:334
Gorgon::Graphics::basic_StackedObjectProvider::CreateAnimation
basic_StackedObject< A_ > & CreateAnimation(Gorgon::Animation::ControllerBase &timer) const override
Definition: StackedObject.h:190
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider(A_ &bottom, A_ &top, const Geometry::Point &offset={0, 0})
Filling constructor.
Definition: StackedObject.h:124
Gorgon::Graphics::RectangularDrawable::CalculateSize
const Geometry::Size CalculateSize(const Geometry::Size &area) const
Calculates the adjusted size of this drawable depending on the given area.
Definition: Drawables.h:322
Gorgon::Geometry::basic_Rectangle
Represents a rectangle in a 2D space.
Definition: Rectangle.h:19
Gorgon::Animation::Base::controller
ControllerBase * controller
Controller of this animation.
Definition: Animation.h:388
TextureAnimation.h
Gorgon::Graphics::basic_StackedObjectProvider::basic_StackedObjectProvider
basic_StackedObjectProvider()=default
Empty constructor.
Gorgon::Graphics::IStackedObjectProvider::GetOffset
virtual Geometry::Point GetOffset() const =0
Gorgon::AssumeOwnershipTag
Where acceptable, denotes that the object will assume the ownership.
Definition: Types.h:136
Gorgon::Graphics::basic_StackedObject::basic_StackedObject
basic_StackedObject(RectangularAnimation &top, RectangularAnimation &bottom, const Geometry::Point &offset, bool create=true)
Creates a stacked object from two animations, these animations should not have controllers attached t...
Definition: StackedObject.h:34
Gorgon::Graphics::basic_StackedObject::Progress
virtual bool Progress(unsigned &) override
This function should progress the animation.
Definition: StackedObject.h:68
Gorgon::Animation::Base::GetController
virtual ControllerBase & GetController() const
Returns the controller of this animation.
Definition: Animation.h:347
Gorgon::Graphics::basic_StackedObject::calculatesize
virtual Geometry::Size calculatesize(const SizeController &controller, const Geometry::Size &s) const override
This function should return the size of the object when it is requested to be drawn in the given area...
Definition: StackedObject.h:90
Gorgon::Graphics::basic_StackedObject::draw
virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, const Geometry::Pointf &tex1, const Geometry::Pointf &tex2, const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const override
This method should draw to object inside the given quad with the given texture coordinates.
Definition: StackedObject.h:323