Gorgon Game Engine
Line.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Animations.h"
4 #include "TextureAnimation.h"
5 #include "EmptyImage.h"
6 #include "Bitmap.h"
7 
8 namespace Gorgon { namespace Graphics {
9  class Line;
10 
13  public:
14  ILineProvider(Orientation orientation) : orientation(orientation)
15  { }
16 
17  virtual ILineProvider &MoveOutProvider() override = 0;
18 
20  virtual RectangularAnimation &CreateStart() const = 0;
21 
23  virtual RectangularAnimation &CreateMiddle() const = 0;
24 
26  virtual RectangularAnimation &CreateEnd() const = 0;
27 
30  virtual void SetOrientation(Orientation value) {
31  orientation = value;
32  }
33 
35  virtual Orientation GetOrientation() const {
36  return orientation;
37  }
38 
42  virtual void SetTiling(bool value) {
43  tiling = value;
44  }
45 
47  virtual bool GetTiling() const {
48  return tiling;
49  }
50 
51  private:
52  Orientation orientation;
53  bool tiling = true;
54  };
55 
61  class Line : public RectangularAnimation {
62  public:
64 
65  Line(const ILineProvider &prov, bool create = true);
66 
67  virtual ~Line() {
68  start.DeleteAnimation();
69  middle.DeleteAnimation();
70  end.DeleteAnimation();
71  }
72 
73  virtual bool Progress(unsigned &) override {
74  return true; //individual parts will work automatically
75  }
76 
77  int GetDuration() const override {
78  auto dur = start.GetDuration();
79  if(dur > 0)
80  return dur;
81 
82  dur = middle.GetDuration();
83  if(dur > 0)
84  return dur;
85 
86  dur = end.GetDuration();
87 
88  return dur;
89  }
90 
91  protected:
92  virtual void drawin(TextureTarget &target, const Geometry::Rectanglef &r, RGBAf color) const override;
93 
94  virtual void drawin(TextureTarget &target, const SizeController &controller, const Geometry::Rectanglef &r, RGBAf color) const override;
95 
96  virtual Geometry::Size calculatesize(const Geometry::Size &area) const override {
98  return{area.Width, getfixedsize()};
99  }
100  else {
101  return{getfixedsize(), area.Height};
102  }
103  }
104 
105  virtual Geometry::Size getsize() const override {
107  return{start.GetWidth()+middle.GetWidth()+end.GetWidth(), getfixedsize()};
108  }
109  else {
110  return{getfixedsize(), start.GetHeight()+middle.GetHeight()+end.GetHeight()};
111  }
112  }
113 
114  virtual Geometry::Size calculatesize(const SizeController &controller, const Geometry::Size &s) const override {
116  return controller.CalculateSize({middle.GetWidth(), getfixedsize()}, {start.GetWidth()+end.GetWidth(), 0}, s);
117  }
118  else {
119  return controller.CalculateSize({getfixedsize(), middle.GetHeight()}, {0, start.GetHeight()+end.GetHeight()}, s);
120  }
121  }
122 
124  int getfixedsize() const {
126  return std::max(std::max(start.GetHeight(), middle.GetHeight()), end.GetHeight());
127  }
128  else {
129  return std::max(std::max(start.GetWidth(), middle.GetWidth()), end.GetWidth());
130  }
131  }
132 
133  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
134  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
135  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
136  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color) const override;
137 
138 
139  virtual void draw(TextureTarget &target, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color) const override;
140 
141 
142  virtual void draw(TextureTarget &target, const Geometry::Pointf &p, RGBAf color) const override;
143 
144  private:
145  RectangularAnimation &start;
146  RectangularAnimation &middle;
148 
149  const ILineProvider &prov;
150  };
151 
161  template<class A_>
163  public:
165 
168  { }
169 
171  basic_LineProvider(Orientation orientation, A_ &start, A_ &middle, A_ &end) : ILineProvider(orientation),
172  start(&start), middle(&middle), end(&end) {}
173 
175  basic_LineProvider(Orientation orientation, A_ &&start, A_ &&middle, A_ &&end) : ILineProvider(orientation),
176  start(new A_(std::move(start))), middle(new A_(std::move(middle))), end(new A_(std::move(end))),
177  own(true)
178  { }
179 
182  basic_LineProvider(Orientation orientation, A_ *start, A_ *middle, A_ *end) : ILineProvider(orientation),
183  start(start), middle(middle), end(end) {}
184 
187  start(other.start), middle(other.middle), end(other.end), own(other.own)
188  {
189  other.own = false;
190  other.start = nullptr;
191  other.middle = nullptr;
192  other.end = nullptr;
193  SetTiling(other.GetTiling());
194  }
195 
197 
198  //types are derived not to type the same code for every class
199  virtual auto MoveOutProvider() -> decltype(*this) override {
200  auto ret = new typename std::remove_reference<decltype(*this)>::type(std::move(*this));
201 
202  return *ret;
203  }
204 
206  if(own) {
207  delete start;
208  delete middle;
209  delete end;
210  }
211  }
212 
214  return *new Line(*this, timer);
215  }
216 
217  Line &CreateAnimation(bool create = true) const override {
218  return *new Line(*this, create);
219  }
220 
221  virtual RectangularAnimation &CreateStart() const override {
222  if(start)
223  return start->CreateAnimation(false);
224  else
225  return EmptyImage::Instance();
226  }
227 
228  virtual RectangularAnimation &CreateMiddle() const override {
229  if(middle)
230  return middle->CreateAnimation(false);
231  else
232  return EmptyImage::Instance();
233  }
234 
235  virtual RectangularAnimation &CreateEnd() const override {
236  if(end)
237  return end->CreateAnimation(false);
238  else
239  return EmptyImage::Instance();
240  }
241 
243  A_ *GetStart() const {
244  return start;
245  }
246 
248  A_ *GetMiddle() const {
249  return middle;
250  }
251 
253  A_ *GetEnd() const {
254  return end;
255  }
256 
258  void SetStart(A_ *value) {
259  if(own)
260  delete start;
261 
262  start = value;
263  }
264 
266  void SetMiddle(A_ *value) {
267  if(own)
268  delete middle;
269 
270  middle = value;
271  }
272 
274  void SetEnd(A_ *value) {
275  if(own)
276  delete end;
277 
278  end = value;
279  }
280 
282  void Prepare() {
283  if(start)
284  start->Prepare();
285  if(middle)
286  middle->Prepare();
287  if(end)
288  end->Prepare();
289  }
290 
293  void OwnProviders() {
294  own = true;
295  }
296 
300  own = false;
301  start = nullptr;
302  middle = nullptr;
303  end = nullptr;
304  }
305 
306  Geometry::Size GetSize() const override {
307  int w = 0;
308  int h = 0;
309 
311  if(start) {
312  auto sz = start->GetSize();
313  w += sz.Width;
314  if(h < sz.Height)
315  h = sz.Height;
316  }
317  if(middle) {
318  auto sz = middle->GetSize();
319  w += sz.Width;
320  if(h < sz.Height)
321  h = sz.Height;
322  }
323  if(end) {
324  auto sz = end->GetSize();
325  w += sz.Width;
326  if(h < sz.Height)
327  h = sz.Height;
328  }
329  }
330  else {
331  if(start) {
332  auto sz = start->GetSize();
333  h += sz.Height;
334  if(w < sz.Width)
335  w = sz.Width;
336  }
337  if(middle) {
338  auto sz = middle->GetSize();
339  h += sz.Height;
340  if(w < sz.Width)
341  w = sz.Width;
342  }
343  if(end) {
344  auto sz = end->GetSize();
345  h += sz.Height;
346  if(w < sz.Width)
347  w = sz.Width;
348  }
349  }
350 
351  return {w, h};
352  }
353 
354  private:
355  A_ *start = nullptr;
356  A_ *middle = nullptr;
357  A_ *end = nullptr;
358 
359  bool own = false;
360  };
361 
365 }}
Gorgon::Graphics::ILineProvider::CreateEnd
virtual RectangularAnimation & CreateEnd() const =0
Creates a start animation without controller. This function should always return an animation.
Gorgon::Animation::Base::GetDuration
virtual int GetDuration() const =0
Returns the duration of the animation if it is a known apriori.
Gorgon::Graphics::basic_LineProvider::Prepare
void Prepare()
Prepares all animation providers if the they support Prepare function.
Definition: Line.h:282
Gorgon::Graphics::Orientation
Orientation
2D orientation constants
Definition: Graphics.h:39
Gorgon::Graphics::ILineProvider::CreateStart
virtual RectangularAnimation & CreateStart() const =0
Creates a start animation without controller. This function should always return an animation.
Gorgon::Graphics::basic_LineProvider::OwnProviders
void OwnProviders()
Issuing this function will make this line to own its providers destroying them along with itself.
Definition: Line.h:293
Gorgon::Graphics::SizeController
This class allows control over a sizable object.
Definition: Graphics.h:161
Gorgon::Graphics::Line::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: Line.cpp:65
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_LineProvider::CreateAnimation
Line & CreateAnimation(bool create=true) const override
This function should create and animation and depending on the create parameter, it should create its...
Definition: Line.h:217
Gorgon::Graphics::ILineProvider::MoveOutProvider
virtual ILineProvider & MoveOutProvider() override=0
This function moves this animation provider into a new provider.
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(Orientation orientation, A_ &start, A_ &middle, A_ &end)
Filling constructor.
Definition: Line.h:171
Gorgon::Graphics::Line::~Line
virtual ~Line()
Definition: Line.h:67
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(basic_LineProvider &&other)
Move constructor.
Definition: Line.h:186
Gorgon::Resource::GID::Line
constexpr Type Line
Definition: GID.h:234
Gorgon::Graphics::RGBAf
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
Gorgon::Graphics::Line::GetDuration
int GetDuration() const override
Returns the duration of the animation if it is a known apriori.
Definition: Line.h:77
EmptyImage.h
Gorgon::Graphics::Line::getsize
virtual Geometry::Size getsize() const override
Should return the exact size of this object.
Definition: Line.h:105
Gorgon::Graphics::basic_LineProvider::SetEnd
void SetEnd(A_ *value)
Changes the end animation, ownership semantics will not change.
Definition: Line.h:274
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(const basic_LineProvider &)=delete
Gorgon::Graphics::ILineProvider::CreateMiddle
virtual RectangularAnimation & CreateMiddle() const =0
Creates a start animation without controller. This function should always return an animation.
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::Line
This class allows drawing a line like image that is made out of three parts.
Definition: Line.h:61
Gorgon::Animation::Base
This is the base class for all animations.
Definition: Animation.h:306
Gorgon::Graphics::Line::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: Line.h:96
Gorgon::Graphics::basic_LineProvider::CreateEnd
virtual RectangularAnimation & CreateEnd() const override
Creates a start animation without controller. This function should always return an animation.
Definition: Line.h:235
Gorgon::Graphics::RectangularAnimation
Rectangular drawable animation.
Definition: Animations.h:19
Bitmap.h
Gorgon::Graphics::ILineProvider
Interface for LineProviders.
Definition: Line.h:12
Gorgon::Graphics::basic_LineProvider
This class allows instancing of a line like image that is made out of three parts.
Definition: Line.h:162
Gorgon::Graphics::basic_LineProvider::CreateAnimation
Line & CreateAnimation(Gorgon::Animation::ControllerBase &timer) const override
This function should create a new animation with the given controller and if owner parameter is set t...
Definition: Line.h:213
Gorgon::Graphics::ILineProvider::GetTiling
virtual bool GetTiling() const
Returns if the middle part will be tiled.
Definition: Line.h:47
Gorgon::Graphics::Line::Line
Line(const ILineProvider &prov, Gorgon::Animation::ControllerBase &timer)
Definition: Line.cpp:17
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::basic_LineProvider::GetEnd
A_ * GetEnd() const
Returns the end animation, might return nullptr.
Definition: Line.h:253
Gorgon::Graphics::Line::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: Line.cpp:29
Gorgon::Animation::Base::DeleteAnimation
virtual void DeleteAnimation() const
Deletes this animation.
Definition: Animation.h:379
Gorgon::Graphics::ILineProvider::SetOrientation
virtual void SetOrientation(Orientation value)
Changes the orientation of the provider.
Definition: Line.h:30
Gorgon::Graphics::Line::Progress
virtual bool Progress(unsigned &) override
This function should progress the animation.
Definition: Line.h:73
Gorgon::Graphics::Orientation::Horizontal
@ Horizontal
Gorgon::Graphics::basic_LineProvider::SetMiddle
void SetMiddle(A_ *value)
Changes the middle animation, ownership semantics will not change.
Definition: Line.h:266
Gorgon::Graphics::basic_LineProvider::GetStart
A_ * GetStart() const
Returns the start animation, might return nullptr.
Definition: Line.h:243
Animations.h
Gorgon::Graphics::RectangularAnimationProvider
This class provides rectangular animations.
Definition: Animations.h:48
Gorgon::UI::Graphics
@ Graphics
Definition: Template.h:164
Gorgon::Graphics::ILineProvider::GetOrientation
virtual Orientation GetOrientation() const
Returns the orientation of the line provider.
Definition: Line.h:35
Gorgon::Graphics::basic_LineProvider::GetSize
Geometry::Size GetSize() const override
Definition: Line.h:306
Gorgon::Graphics::ILineProvider::SetTiling
virtual void SetTiling(bool value)
Sets whether the middle part would be tiled.
Definition: Line.h:42
Gorgon::Graphics::RectangularDrawable::GetHeight
int GetHeight() const
Returns the height of the drawable.
Definition: Drawables.h:451
Gorgon::Graphics::basic_LineProvider::ReleaseProviders
void ReleaseProviders()
Issuing this function will make this line to disown its providers and set them to nullptr.
Definition: Line.h:299
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::Graphics::basic_LineProvider::CreateStart
virtual RectangularAnimation & CreateStart() const override
Creates a start animation without controller. This function should always return an animation.
Definition: Line.h:221
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Graphics::Line::getfixedsize
int getfixedsize() const
Returns the largest size in the non-scalable direction.
Definition: Line.h:124
Gorgon::Graphics::RectangularDrawable::GetWidth
int GetWidth() const
Returns the width of the drawable.
Definition: Drawables.h:448
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(Orientation orientation=Orientation::Horizontal)
Empty constructor, line can be instanced even if it is completely empty.
Definition: Line.h:167
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_LineProvider::CreateMiddle
virtual RectangularAnimation & CreateMiddle() const override
Creates a start animation without controller. This function should always return an animation.
Definition: Line.h:228
Gorgon::Graphics::ILineProvider::ILineProvider
ILineProvider(Orientation orientation)
Definition: Line.h:14
Gorgon::Graphics::Line::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: Line.h:114
Gorgon::Graphics::basic_LineProvider::MoveOutProvider
virtual auto MoveOutProvider() -> decltype(*this) override
This function moves this animation provider into a new provider.
Definition: Line.h:199
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(Orientation orientation, A_ *start, A_ *middle, A_ *end)
Filling constructor, nullptr is acceptable, however, it is not advised to use only one side,...
Definition: Line.h:182
Gorgon::Graphics::basic_LineProvider::~basic_LineProvider
~basic_LineProvider()
Definition: Line.h:205
Gorgon::Graphics::basic_LineProvider::basic_LineProvider
basic_LineProvider(Orientation orientation, A_ &&start, A_ &&middle, A_ &&end)
Filling constructor. This variant will move in the animations, freeing them with this item.
Definition: Line.h:175
Gorgon::Graphics::basic_LineProvider::SetStart
void SetStart(A_ *value)
Changes the start animation, ownership semantics will not change.
Definition: Line.h:258
Gorgon::Graphics::basic_LineProvider::GetMiddle
A_ * GetMiddle() const
Returns the middle animation, might return nullptr.
Definition: Line.h:248