YAPOG  0.0.1
Yet Another Pokemon Online Game
WidgetBorder.cpp
Go to the documentation of this file.
5 
6 namespace yap
7 {
9  : border_ (nullptr)
10  , file_ ("")
11  , textures_ ()
12  , tms_ ()
13  , tmTop_ (nullptr)
14  , tmBottom_ (nullptr)
15  , tmLeft_ (nullptr)
16  , tmRight_ (nullptr)
17  , width_ (0)
18  , isInit (false)
19  , basic_ (false)
20  , isScalable_ (true)
21  {
22  }
23 
25  {
26  return false;
27  }
28 
30  : border_ (nullptr)
31  , file_ (file)
32  , textures_ ()
33  , tms_ ()
34  , tmTop_ (nullptr)
35  , tmBottom_ (nullptr)
36  , tmLeft_ (nullptr)
37  , tmRight_ (nullptr)
38  , width_ (0)
39  , isInit (false)
40  , basic_ (true)
41  , isScalable_ (true)
42  {
43  }
44 
46  Texture& topRight,
47  Texture& right,
48  Texture& botRight,
49  Texture& bot,
50  Texture& botLeft,
51  Texture& left,
52  Texture& topLeft,
53  bool isScalable)
54  : border_ (nullptr)
55  , file_ ("NoImage")
56  , textures_ ()
57  , tms_ ()
58  , tmTop_ (nullptr)
59  , tmBottom_ (nullptr)
60  , tmLeft_ (nullptr)
61  , tmRight_ (nullptr)
62  , width_ (0)
63  , isInit (false)
64  , basic_ (false)
65  , isScalable_ (isScalable)
66  {
67  textures_.Add (&top);
68  textures_.Add (&topRight);
69  textures_.Add (&right);
70  textures_.Add (&botRight);
71  textures_.Add (&bot);
72  textures_.Add (&botLeft);
73  textures_.Add (&left);
74  textures_.Add (&topLeft);
75 
76  if (!isScalable_)
77  for (int i = 0; i < 8; i++)
78  tms_.Add (new TextureManager ());
79  }
80 
81 
83  {
84  }
85 
87  {
88  if (isInit && basic_ && !isScalable_)
89  {
90  tmTop_->Draw (context);
91  tmBottom_->Draw (context);
92  tmRight_->Draw (context);
93  tmLeft_->Draw (context);
94  }
95  else if (isInit && !isScalable_)
96  for (TextureManager* txtr : tms_)
97  txtr->Draw (context);
98  else if (isInit)
99  for (Texture* txtr : textures_)
100  txtr->Draw (context);
101  }
102 
103  void WidgetBorder::HandleShow (bool isVisible)
104  {
105  }
106 
107  void WidgetBorder::HandleMove (const Vector2& offset)
108  {
109  if (isInit && basic_ && !isScalable_)
110  {
111  tmTop_->Move (offset);
112  tmBottom_->Move (offset);
113  tmRight_->Move (offset);
114  tmLeft_->Move (offset);
115  }
116  else if (isInit && !isScalable_)
117  for (TextureManager* txtr : tms_)
118  txtr->Move (offset);
119  else if (isInit)
120  for (Texture* txtr : textures_)
121  txtr->Move (offset);
122  }
123 
124  void WidgetBorder::SetScalable (bool state)
125  {
126  isScalable_ = state;
127  }
128  void WidgetBorder::HandleScale (const Vector2& factor)
129  {
130  if (border_ != nullptr)
131  {
132  Vector2 base = border_->GetSize ();
133  Vector2 neo (base.x * factor.x, base.y * factor.y);
134 
135  if (isInit && basic_ && !isScalable_)
136  {
137  tmTop_->SetSize (neo);
138  tmBottom_->SetSize (neo);
139  tmRight_->SetSize (neo);
140  tmLeft_->SetSize (neo);
141  }
142  else if (isInit && !isScalable_)
143  for (TextureManager* txtr : tms_)
144  txtr->SetSize (neo);
145  else if (isInit)
146  for (Texture* txtr : textures_)
147  txtr->Scale (neo);
148  }
149  }
150 
152  {
153  }
154 
155  void WidgetBorder::HandleChangeColor (const sf::Color& color)
156  {
157  }
158 
160  {
161  if (border_ != nullptr)
162  return *border_;
163  else if (textures_.Count () >= 1)
164  return *textures_[0];
165 
166  YAPOG_THROW("No texture defined.");
167  }
168 
170  {
171  if (width_ > 0)
172  return width_;
173  else
174  {
175  return GetTextureWidth ();
176  }
177  }
179  {
180  if (basic_)
181  {
182  return Vector2 (GetWidth () * 2, GetWidth () * 2);
183  }
184  else
185  {
186  uint x = 0;
187  uint y = 0;
188 
189  if (textures_[0]->GetSize () != Vector2 (1, 1))
190  y += textures_[0]->GetSize ().y;
191  if (textures_[2]->GetSize () != Vector2 (1, 1))
192  x += textures_[2]->GetSize ().x;
193  if (textures_[4]->GetSize () != Vector2 (1, 1))
194  y += textures_[4]->GetSize ().y;
195  if (textures_[6]->GetSize () != Vector2 (1, 1))
196  x += textures_[6]->GetSize ().x;
197 
198  return Vector2 (x, y);
199  }
200  }
201 
203  {
204  uint maxWidth = 0;
205  uint currentSize = 0;
206  for (int i = 0; i < textures_.Count (); ++i)
207  {
208 
209  if (i == 0 || i == 4)
210  currentSize = textures_[i]->GetSize ().y ;
211  else
212  currentSize = textures_[i]->GetSize ().x ;
213  maxWidth = currentSize > maxWidth
214  ? currentSize : maxWidth;
215  }
216  return maxWidth;
217  }
218 
220  {
221  if (basic_)
222  {
223  border_ = new Texture (file_);
224  int width = border_->GetSize ().y;
225  delete border_;
226 
227  return SetBorder (size, width);
228  }
229  else if (!isScalable_)
230  {
231  width_ = GetTextureWidth ();
232  tms_[0] = new TextureManager (*textures_[0], size.x, width_);
233  tms_[0]->SetPosition (GetPosition () - Vector2 (0, width_));
234  tms_[0]->Init ();
235 
236  tms_[1] = new TextureManager (*textures_[1], width_, width_);
237  tms_[1]->SetPosition (GetPosition () - Vector2 (-size.x, width_));
238  tms_[1]->Init ();
239 
240  tms_[2] = new TextureManager (*textures_[2], width_, size.y);
241  tms_[2]->SetPosition (GetPosition () + Vector2 (size.x , 0));
242  tms_[2]->Init ();
243 
244  tms_[3] = new TextureManager (*textures_[3], width_, width_);
245  tms_[3]->SetPosition (GetPosition () + Vector2 (size.x, size.y));
246  tms_[3]->Init ();
247 
248  tms_[4] = new TextureManager (*textures_[4], size.x, width_);
249  tms_[4]->SetPosition (GetPosition () + Vector2 (0, size.y));
250  tms_[4]->Init ();
251 
252  tms_[5] = new TextureManager (*textures_[5], width_, width_);
253  tms_[5]->SetPosition (GetPosition () - Vector2 (width_, -size.y));
254  tms_[5]->Init ();
255 
256  tms_[6] = new TextureManager (*textures_[6], width_, size.y);
257  tms_[6]->SetPosition (GetPosition () - Vector2 (width_, 0));
258  tms_[6]->Init ();
259 
260  tms_[7] = new TextureManager (*textures_[7], width_, width_);
261  tms_[7]->SetPosition (GetPosition () - Vector2 (width_, width_));
262  tms_[7]->Init ();
263  }
264  else
265  {
266  width_ = GetTextureWidth ();
267  textures_[0]->SetSize (Vector2 (size.x, width_));
268  textures_[0]->SetPosition (GetPosition () - Vector2 (0, width_));
269 
270  textures_[1]->SetPosition (GetPosition () - Vector2 (-size.x, width_));
271 
272  textures_[2]->SetSize (Vector2 (width_, size.y));
273  textures_[2]->SetPosition (GetPosition () + Vector2 (size.x , 0));
274 
275  textures_[3]->SetPosition (GetPosition () + Vector2 (size.x, size.y));
276 
277  textures_[4]->SetSize (Vector2 (size.x, width_));
278  textures_[4]->SetPosition (GetPosition () + Vector2 (0, size.y));
279 
280  textures_[5]->SetPosition (GetPosition () - Vector2 (width_, -size.y));
281 
282  textures_[6]->SetSize (Vector2 ( width_, size.y));
283  textures_[6]->SetPosition (GetPosition () - Vector2 (width_, 0));
284 
285  textures_[7]->SetPosition (GetPosition () - Vector2 (width_, width_));
286  }
287 
288  OnBorderSet (*this, EventArgsTexture (*textures_[1]));
289  isInit = true;
290  }
291 
293  {
294  if (!basic_)
295  return;
296 
297  width_ = 0;
298 
299  border_ = new Texture (file_);
301 
302  if (tmTop_ != nullptr)
303  delete tmTop_;
304  if (tmBottom_ != nullptr)
305  delete tmBottom_;
306  if (tmLeft_ != nullptr)
307  delete tmLeft_;
308  if (tmRight_ != nullptr)
309  delete tmRight_;
310 
311  if (isScalable_)
312  {
313  textures_.Clear ();
314  Texture* top = new Texture (file_);
315  top->SetSize (Vector2(size.x + width, width));
316  top->SetPosition (GetPosition () - Vector2 (0, width));
317 
318  Texture* bottom = new Texture (file_);
319  bottom->SetSize (Vector2(size.x + width * 2, width));
320  bottom->SetPosition (GetPosition () - Vector2 (width, -size.y));
321 
322  Texture* left = new Texture (file_);
323  left->SetSize (Vector2(width, size.y + width));
324  left->SetPosition (GetPosition () - Vector2 (width, width));
325 
326  Texture* right = new Texture (file_);
327  right->SetSize (Vector2(width, size.y + width));
328  right->SetPosition (GetPosition () + Vector2 (size.x, 0));
329 
330  textures_.Add (top);
331  textures_.Add (bottom);
332  textures_.Add (right);
333  textures_.Add (left);
334 
335  }
336  else
337  {
338  tmTop_ = new TextureManager (*border_, size.x + width, width);
339  tmTop_->SetPosition (GetPosition () - Vector2 (0, width));
340  tmTop_->Init ();
341 
342  tmBottom_ = new TextureManager (*border_, size.x + width * 2, width);
343  tmBottom_->SetPosition (GetPosition () - Vector2 (width, -size.y));
344  tmBottom_->Init ();
345 
346  tmLeft_ = new TextureManager (*border_, width, size.y + width);
347  tmLeft_->SetPosition (GetPosition () - Vector2 (width, width));
348  tmLeft_->Init ();
349 
350  tmRight_ = new TextureManager (*border_, width, size.y + width);
351  tmRight_->SetPosition (GetPosition () + Vector2 (size.x, 0));
352  tmRight_->Init ();
353  }
354 
356  isInit = true;
357  width_ = width;
358  }
359 } //namespace yap