YAPOG  0.0.1
Yet Another Pokemon Online Game
Label.cpp
Go to the documentation of this file.
1 #include <SFML/Graphics/Text.hpp>
2 
9 
10 namespace yap
11 {
12  const String Label::DEFAULT_FONT = "pkmnemn.ttf";
13 
15  : textContent_ ()
16  , drawableText_ ()
17  , isPosSet_ (false)
18  {
19  drawableText_.setFont
20  (ContentManager::Instance ().LoadFont (DEFAULT_FONT));
21  drawableText_.setColor (userColor_);
22  }
23 
24  Label::Label (String content)
25  : textContent_ (content)
26  , drawableText_ (content)
27  , isPosSet_ (false)
28  {
29  /*drawableText_.setPosition (Vector2 (GetPosition ().x + padding_.left,
30  GetPosition ().y + padding_.top));*/
31  drawableText_.setColor (userColor_);
32  drawableText_.setPosition (GetPosition ().x,
33  GetPosition ().y);
34 
35  isPosSet_ = true;
36 
37  drawableText_.setFont
38  (ContentManager::Instance ().LoadFont (DEFAULT_FONT));
39  }
40 
42  {
43  }
44  bool Label::IsFocusable () const
45  {
46  return false;
47  }
48 
49  float Label::GetCharWidth () const
50  {
51  return drawableText_.getCharacterSize () / 1.5;
52  }
53 
55  {
56  return drawableText_.findCharacterPos (pos);
57  }
58 
60  {
61  drawableText_.setPosition (
62  GetPosition ().x + padding_.left,
63  GetPosition ().y + padding_.top);
64  isPosSet_ = false;
65  }
66 
67  void Label::SetDefaultColor (const sf::Color& color)
68  {
69  if (!isChangeColorCall_)
70  drawableText_.setColor (color);
71  }
73  {
74  return textContent_.length ();
75  }
76 
78  {
79  return drawableText_.getCharacterSize ();
80  }
81 
83  {
84  if (userSize_ == Vector2 (0, 0))
85  return Vector2 (padding_.left
86  + drawableText_.getGlobalBounds ().width
87  + padding_.right,
89  + /*drawableText_.getGlobalBounds ().height*/drawableText_.getCharacterSize ()
90  + padding_.bottom)
91  + ((border_ != nullptr) ? Vector2 (border_->GetWidth ()
92  * 2, border_->GetWidth () * 2) : Vector2 ());
93  else
94  return GetUserSize ();
95  }
96 
98  {
99  if (isVisible_)
100  context.Draw (drawableText_);
101  }
102 
103  void Label::HandleShow (bool isVisible)
104  {
105  }
106 
107  void Label::HandleMove (const Vector2& offset)
108  {
109  drawableText_.move (offset);
110  //drawableText_.setPosition (GetPosition() + offset);
111  }
112 
114  {
115  drawableText_.setCharacterSize (size);
116  }
117  void Label::HandleScale (const Vector2& factor)
118  {
119  drawableText_.setScale (factor);
120  }
121 
122  void Label::HandleUpdate (const Time& dt)
123  {
124  }
125 
126  void Label::HandleChangeColor (const sf::Color& color)
127  {
128  drawableText_.setColor (color);
129  }
130 
132  {
133  return textContent_;
134  }
135 
137  {
138  background_= &background;
139  background_->SetPosition (GetPosition ()/* + Vector2 (0,
140  drawableText_.getGlobalBounds ().height
141  - drawableText_.getCharacterSize ())*/);
143  }
144 
145  void Label::SetText (const String& content)
146  {
147  if (content == textContent_)
148  return;
149 
150  if (!isChangeColorCall_)
151  drawableText_.setColor (userColor_);
152  textContent_ = content;
153  drawableText_.setString (content);
154  /*drawableText_.setPosition (Vector2 (GetPosition ().x + padding_.left,
155  GetPosition ().y + padding_.top));*/
156 
157  if (isPosSet_)
158  {
159  drawableText_.setPosition (
160  GetPosition ().x + padding_.left,
161  GetPosition ().y + padding_.top);
162  isPosSet_ = false;
163  }
164 
165  OnTextChanged (*this, EventArgsString (content));
166  }
167 } // namespace yap