YAPOG  0.0.1
Yet Another Pokemon Online Game
Texture.cpp
Go to the documentation of this file.
4 
5 namespace yap
6 {
7  const bool Texture::DEFAULT_VISIBLE_STATE = true;
8 
10  : spatialInfo_ ()
11  , innerTexture_ (nullptr)
12  , sprite_ ()
13  , isVisible_ (DEFAULT_VISIBLE_STATE)
14  , id_ ()
15  {
16  }
17 
18  Texture::Texture (const ID& id)
19  : spatialInfo_ ()
20  , innerTexture_ (nullptr)
21  , sprite_ ()
22  , isVisible_ (DEFAULT_VISIBLE_STATE)
23  , id_ (id)
24  {
25  }
26 
27  Texture::Texture (const String& name)
28  : spatialInfo_ ()
29  , innerTexture_ (nullptr)
30  , sprite_ ()
31  , isVisible_ (DEFAULT_VISIBLE_STATE)
32  , id_ ()
33  {
34  LoadFromFile (name);
35  }
36 
37  Texture::Texture (const String& name, const sf::IntRect& rect)
38  : spatialInfo_ ()
39  , innerTexture_ (nullptr)
40  , sprite_ ()
41  , isVisible_ (DEFAULT_VISIBLE_STATE)
42  , id_ ()
43  {
44  LoadFromFile (name);
45  SetRect (rect);
46  }
47 
49  {
50  }
51 
52  void Texture::LoadFromFile (const String& name)
53  {
55  sprite_.setTexture (*innerTexture_);
56 
58  }
59 
60  void Texture::SetRect (const sf::IntRect& rect)
61  {
62  spatialInfo_.SetSize (Vector2 (rect.width, rect.height));
63 
64  sprite_.setTextureRect (rect);
65  }
66 
67  const ID& Texture::GetID () const
68  {
69  return id_;
70  }
71 
72  void Texture::SetID (const ID& id)
73  {
74  id_ = id;
75  }
76 
77  const Vector2& Texture::GetPosition () const
78  {
79  return spatialInfo_.GetPosition ();
80  }
81 
82  const Vector2& Texture::GetSize () const
83  {
84  return spatialInfo_.GetSize ();
85  }
86 
87  const Vector2& Texture::GetTopLeft () const
88  {
89  return spatialInfo_.GetTopLeft ();
90  }
91 
93  {
94  return spatialInfo_.GetBottomRight ();
95  }
96 
97  const Vector2& Texture::GetCenter () const
98  {
99  return spatialInfo_.GetCenter ();
100  }
101 
103  {
104  return spatialInfo_.GetRectangle ();
105  }
106 
107  void Texture::Move (const Vector2& offset)
108  {
109  spatialInfo_.SetPosition (GetPosition () + offset);
110 
111  sprite_.move (offset);
112  }
113 
114  void Texture::Scale (const Vector2& factor)
115  {
117  Vector2 (
118  GetSize ().x * factor.x,
119  GetSize ().y * factor.y));
120 
121  sprite_.scale (factor);
122  }
123 
124  void Texture::SetPosition (const Vector2& position)
125  {
126  Move (position - GetPosition ());
127  }
128 
129  void Texture::SetSize (const Vector2& size)
130  {
131  Scale (
132  Vector2 (
133  size.x / GetSize ().x,
134  size.y / GetSize ().y));
135  }
136 
138  {
139  if (!isVisible_)
140  return;
141 
142  context.Draw (sprite_);
143  }
144 
145  bool Texture::IsVisible () const
146  {
147  return isVisible_;
148  }
149 
150  void Texture::Show (bool isVisible)
151  {
152  isVisible_ = isVisible;
153  }
154 
155  void Texture::ChangeColor (const sf::Color& color)
156  {
157  sprite_.setColor (color);
158  }
159 
161  {
162  return new Texture (*this);
163  }
164 
165  Texture::Texture (const Texture& copy)
166  : spatialInfo_ (copy.spatialInfo_)
167  , innerTexture_ (copy.innerTexture_)
168  , sprite_ (copy.sprite_)
169  , isVisible_ (copy.isVisible_)
170  , id_ (copy.id_)
171  {
172  }
173 } // namespace yap