Gorgon Game Engine
TintedObject.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Graphics.h"
4 #include "EmptyImage.h"
5 #include "Animations.h"
6 #include "TextureAnimation.h"
7 
8 namespace Gorgon { namespace Graphics {
9 
12  public:
13  virtual RectangularAnimation &CreateBase() const = 0;
14 
15  virtual RGBAf GetColor() const = 0;
16 
17  virtual void SetColor(const RGBAf &value) = 0;
18 
19  virtual ITintedObjectProvider &MoveOutProvider() override = 0;
20  };
21 
22  template<class A_>
24 
25  template<class A_>
26  class basic_TintedObject : public virtual RectangularAnimation {
27  public:
28  basic_TintedObject(const basic_TintedObjectProvider<A_> &parent, bool create = true);
29 
31 
35  basic_TintedObject(RectangularAnimation &base, const RGBAf &color, bool create = true) :
36  Gorgon::Animation::Base(create), base(base), color(color)
37  {
38  if(this->HasController()) {
39  base.SetController(this->GetColor());
40  }
41  else {
42  base.RemoveController();
43  }
44  }
45 
50  Gorgon::Animation::Base(timer), base(base), color(color)
51  {
52  if(this->HasController()) {
53  base.SetController(this->GetColor());
54  }
55  else {
56  base.RemoveController();
57  }
58  }
59 
61  base.DeleteAnimation();
62  }
63 
65  void SetColor(RGBAf value) {
66  color = value;
67  }
68 
70  RGBAf GetColor() const {
71  return color;
72  }
73 
74  virtual bool Progress(unsigned &) override {
75  return true; //individual parts will work automatically
76  }
77 
78  int GetDuration() const override {
79  return base.GetDuration();
80  }
81 
82  protected:
83  virtual void drawin(TextureTarget &target, const Geometry::Rectanglef &r, RGBAf color) const override;
84 
85  virtual void drawin(TextureTarget &target, const SizeController &controller, const Geometry::Rectanglef &r, RGBAf color) const override;
86 
87  virtual Geometry::Size calculatesize(const Geometry::Size &area) const override {
88  return base.CalculateSize(area);
89  }
90 
91  virtual Geometry::Size calculatesize(const SizeController &controller, const Geometry::Size &s) const override {
92  return base.CalculateSize(controller, s);
93  }
94 
95  using Drawable::draw;
96 
97  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
98  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
99  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
100  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const override;
101 
102 
103  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
104  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color) const override;
105 
106 
107 
108 
109  virtual Geometry::Size getsize() const override;
110 
111  private:
112  RectangularAnimation &base;
113  RGBAf color;
114  };
115 
123  template<class A_>
125  public:
126  using AnimationType = typename A_::AnimationType;
127 
130 
132  explicit basic_TintedObjectProvider(A_ &base, const RGBAf &color = 1.f) :
133  base(&base), color(color)
134  { }
135 
136  explicit basic_TintedObjectProvider(A_ &&base, const RGBAf &color = 1.f) :
137  base(new A_(std::move(base))), color(color), own(true)
138  { }
139 
141  base(other.base), color(other.color)
142  {
143  other.base = nullptr;
144  }
145 
147  if(own) {
148  delete this->base;
149  }
150  }
151 
152  //types are derived not to type the same code for every class
153  virtual auto MoveOutProvider() -> decltype(*this) override {
154  auto ret = new typename std::remove_reference<decltype(*this)>::type(std::move(*this));
155 
156  return *ret;
157  }
158 
159  basic_TintedObject<A_> &CreateAnimation(bool create = true) const override {
160  return *new basic_TintedObject<A_>(*this, create);
161  }
162 
164  return *new basic_TintedObject<A_>(*this, timer);
165  }
166 
168  RectangularAnimation &CreateBase() const override {
169  if(base) {
170  return base->CreateAnimation(false);
171  }
172  else {
173  return EmptyImage::Instance();
174  }
175  }
176 
178  A_ *GetBase() const {
179  return base;
180  }
181 
183  RGBAf GetColor() const override {
184  return color;
185  }
186 
188  void SetColor(const RGBAf &value) override {
189  color = value;
190  }
191 
193  void SetBase(A_ *value) {
194  if(own)
195  delete base;
196 
197  base = value;
198  }
199 
201  void OwnProvider() {
202  own = true;
203  }
204 
207  void Prepare() {
208  if(base) base->Prepare();
209  }
210 
211  Geometry::Size GetSize() const override {
212  return base ? base->GetSize() : Geometry::Size{0, 0};
213  }
214 
215  private:
216  A_ *base = nullptr;
217  RGBAf color = 1.0f;
218 
219  bool own = false;
220  };
221 
222  template<class A_>
224  Gorgon::Animation::Base(create),
225  base(parent.CreateBase()), color(parent.GetColor())
226  {
227  if(this->HasController()) {
228  base.SetController(this->GetController());
229  }
230  }
231 
232  template<class A_>
234  Gorgon::Animation::Base(timer),
235  base(parent.CreateBase()), color(parent.GetColor())
236  {
237  if(this->HasController()) {
238  base.SetController(this->GetController());
239  }
240  }
241 
242  template<class A_>
244  base.DrawIn(target, r, this->color*color);
245  }
246 
247  template<class A_>
248  void basic_TintedObject<A_>::drawin(TextureTarget &target, const SizeController &controller, const Geometry::Rectanglef &r, RGBAf color) const {
249  base.DrawIn(target, controller, r, this->color*color);
250  }
251 
252 
253 
254  template<class A_>
256  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
257  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
258  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const
259  {
260  base.Draw(target, p1, p2, p3, p4, tex1, tex2, tex3, tex4, this->color*color);
261  }
262 
263 
264  template<class A_>
266  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color) const
267  {
268  base.Draw(target, p1, p2, p3, p4, this->color*color);
269  }
270 
271  template<class A_>
273  return base.GetSize();
274  }
275 
278 
281 
284 
285 } }
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::EmptyImage::Instance
static EmptyImage & Instance()
Returns the instance for empty image. Only one instance is enough.
Definition: EmptyImage.h:50
Gorgon::Graphics::basic_TintedObjectProvider::CreateAnimation
basic_TintedObject< A_ > & CreateAnimation(Gorgon::Animation::ControllerBase &timer) const override
Definition: TintedObject.h:163
Gorgon::Graphics::basic_TintedObject::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: TintedObject.h:243
Gorgon::Graphics::basic_TintedObjectProvider::basic_TintedObjectProvider
basic_TintedObjectProvider(A_ &&base, const RGBAf &color=1.f)
Definition: TintedObject.h:136
Gorgon::Graphics::basic_TintedObject::Progress
virtual bool Progress(unsigned &) override
This function should progress the animation.
Definition: TintedObject.h:74
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Graphics::basic_TintedObjectProvider::basic_TintedObjectProvider
basic_TintedObjectProvider(A_ &base, const RGBAf &color=1.f)
Filling constructor.
Definition: TintedObject.h:132
Gorgon::Graphics::ITintedObjectProvider::SetColor
virtual void SetColor(const RGBAf &value)=0
Gorgon::Graphics::basic_TintedObjectProvider::AnimationType
typename A_::AnimationType AnimationType
Definition: TintedObject.h:126
Gorgon::Graphics::RGBAf
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
EmptyImage.h
Gorgon::Graphics::basic_TintedObjectProvider
This object creates a scalable object from a graphic object.
Definition: TintedObject.h:124
Gorgon::Graphics::basic_TintedObject::draw
virtual void draw(TextureTarget &target, const Geometry::Pointf &p, RGBAf color) const=0
This is the only function that needs to implemented for a drawable.
Gorgon::Graphics::ITintedObjectProvider
For ease of use in resource system.
Definition: TintedObject.h:11
Gorgon::Graphics::basic_TintedObjectProvider::SetColor
void SetColor(const RGBAf &value) override
Sets the tint color of the object.
Definition: TintedObject.h:188
Gorgon::Graphics::basic_TintedObject::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: TintedObject.h:87
Gorgon::Graphics::ITintedObjectProvider::CreateBase
virtual RectangularAnimation & CreateBase() const =0
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_TintedObject::basic_TintedObject
basic_TintedObject(RectangularAnimation &base, const RGBAf &color, Gorgon::Animation::ControllerBase &timer)
Creates a scalable object from two animations, these animations should not have controllers attached ...
Definition: TintedObject.h:49
Gorgon::Graphics::basic_TintedObjectProvider::CreateBase
RectangularAnimation & CreateBase() const override
Creates a base animation without controller.
Definition: TintedObject.h:168
Gorgon::Graphics::RectangularAnimation
Rectangular drawable animation.
Definition: Animations.h:19
Gorgon::Graphics::basic_TintedObject::SetColor
void SetColor(RGBAf value)
Changes the size controller used in this scalable object.
Definition: TintedObject.h:65
Gorgon::Graphics::basic_TintedObjectProvider::~basic_TintedObjectProvider
~basic_TintedObjectProvider()
Definition: TintedObject.h:146
Gorgon::Graphics::basic_TintedObjectProvider::basic_TintedObjectProvider
basic_TintedObjectProvider()=default
Empty constructor.
Gorgon::Graphics::basic_TintedObjectProvider::MoveOutProvider
virtual auto MoveOutProvider() -> decltype(*this) override
Definition: TintedObject.h:153
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::Animation::Base::DeleteAnimation
virtual void DeleteAnimation() const
Deletes this animation.
Definition: Animation.h:379
Gorgon::Graphics::basic_TintedObject
Definition: TintedObject.h:26
Gorgon::Graphics::Animation
A regular drawable animation.
Definition: Animations.h:14
Animations.h
Gorgon::Graphics::basic_TintedObjectProvider::GetSize
Geometry::Size GetSize() const override
Definition: TintedObject.h:211
Gorgon::Graphics::basic_TintedObjectProvider::GetColor
RGBAf GetColor() const override
Returns the tint color.
Definition: TintedObject.h:183
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::Animation::Base::RemoveController
virtual void RemoveController()
Removes the controller of this animation.
Definition: Animation.h:357
Gorgon::Graphics::basic_TintedObjectProvider::GetBase
A_ * GetBase() const
Returns the base component. Could return nullptr.
Definition: TintedObject.h:178
Gorgon::Graphics::basic_TintedObjectProvider::SetBase
void SetBase(A_ *value)
Sets the base provider, ownership semantics will not be changed.
Definition: TintedObject.h:193
Gorgon::Graphics::basic_TintedObjectProvider::OwnProvider
void OwnProvider()
Assumes the ownership of the providers.
Definition: TintedObject.h:201
Gorgon::Graphics::basic_TintedObjectProvider::basic_TintedObjectProvider
basic_TintedObjectProvider(basic_TintedObjectProvider &&other)
Definition: TintedObject.h:140
Gorgon::Graphics::basic_TintedObject::basic_TintedObject
basic_TintedObject(const basic_TintedObjectProvider< A_ > &parent, bool create=true)
Definition: TintedObject.h:223
Gorgon::Animation::Base::SetController
virtual void SetController(ControllerBase &controller)
Sets the controller to the given controller.
Definition: Animation.h:334
Gorgon::Graphics::basic_TintedObject::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: TintedObject.h:91
Gorgon::Graphics::Drawable::draw
virtual void draw(TextureTarget &target, const Geometry::Pointf &p, RGBAf color) const =0
This is the only function that needs to implemented for a drawable.
Gorgon::Graphics::basic_TintedObject::basic_TintedObject
basic_TintedObject(RectangularAnimation &base, const RGBAf &color, bool create=true)
Creates a scalable object from two animations, these animations should not have controllers attached ...
Definition: TintedObject.h:35
Gorgon::Graphics::basic_TintedObjectProvider::Prepare
void Prepare()
Prepares the providers.
Definition: TintedObject.h:207
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::Graphics::basic_TintedObjectProvider::CreateAnimation
basic_TintedObject< A_ > & CreateAnimation(bool create=true) const override
Definition: TintedObject.h:159
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
Gorgon::Graphics::ITintedObjectProvider::GetColor
virtual RGBAf GetColor() const =0
TextureAnimation.h
Gorgon::Graphics::basic_TintedObject::getsize
virtual Geometry::Size getsize() const override
Should return the exact size of this object.
Definition: TintedObject.h:272
Gorgon::Graphics::basic_TintedObject::GetColor
RGBAf GetColor() const
Returns the size controller used in this scalable object.
Definition: TintedObject.h:70
Gorgon::Graphics::ITintedObjectProvider::MoveOutProvider
virtual ITintedObjectProvider & MoveOutProvider() override=0
This function moves this animation provider into a new provider.
Gorgon::Graphics::basic_TintedObject::~basic_TintedObject
~basic_TintedObject()
Definition: TintedObject.h:60
Gorgon::Animation::Base::GetController
virtual ControllerBase & GetController() const
Returns the controller of this animation.
Definition: Animation.h:347
Gorgon::Graphics::basic_TintedObject::GetDuration
int GetDuration() const override
Returns the duration of the animation if it is a known apriori.
Definition: TintedObject.h:78