YAPOG  0.0.1
Yet Another Pokemon Online Game
DialogBoxWidget.cpp
Go to the documentation of this file.
1 #include <SFML/Graphics/Text.hpp>
8 
9 namespace yap
10 {
12  : labels_ ()
13  , currentText_ (0)
14  , textSpeed_ ()
15  {
16  }
17 
19  : labels_ ()
20  , currentText_ (0)
21  {
22  AddText (content);
23  }
24 
26  {
27  }
28 
29  void DialogBoxWidget::SetShowText (bool state)
30  {
31  showText_ = state;
32  }
33 
35  {
36  return true;
37  }
38 
40  {
41  for (Label* text : labels_)
42  {
43  text->SetPosition (Vector2 (GetPosition ().x + padding_.left,
44  GetPosition ().y + padding_.top/*+ label_->GetCharHeight () / 2*/));
45  }
46 
48  }
49 
51  {
52  return GetUserSize ()
53  + ((border_ != nullptr) ? border_->GetSize () : Vector2 ());
54  }
55 
57  {
58  if (showText_)
59  labels_[currentText_]->Draw (context);
60 
61  }
62 
63  void DialogBoxWidget::HandleShow (bool isVisible)
64  {
65  for (Label* text : labels_)
66  text->Show (isVisible);
67  }
68 
69  void DialogBoxWidget::HandleMove (const Vector2& offset)
70  {
71  for (Label* text : labels_)
72  text->Move (offset);
73  }
74 
76  {
77  for (Label* text : labels_)
78  text->Scale (factor);
79  }
80 
82  {
83  for (Label* text : labels_)
84  text->Update (dt);
85  }
86 
87  void DialogBoxWidget::HandleChangeColor (const sf::Color& color)
88  {
89  for (Label* text : labels_)
90  text->ChangeColor (color);
91  }
92 
94  {
95  return content_;
96  }
97 
99  {
100  if (!isVisible_ || !showText_)
101  return false;
102  if (guiEvent.type == sf::Event::KeyPressed)
103  {
104  if (guiEvent.key.code == sf::Keyboard::Return)
105  {
106  SkipText ();
107  return true;
108  }
109  }
110  return false;
111  }
112 
113  void DialogBoxWidget::AddText (const String& contentArg)
114  {
115  if (contentArg.empty())
116  return;
117 
118  String txt = contentArg;
119  Label* lb = new Label ();
120 
121  uint LabelMaxSize = GetUserSize ().x - padding_.left - padding_.right;
122  uint charNumb = (LabelMaxSize / lb->GetCharWidth ());
123 
124  uint previousPos = 0;
125  uint subPos = charNumb;
126  sf::Text width (txt.substr (0, subPos));
127 
128  while (true)
129  {
130  while (width.getLocalBounds ().width < LabelMaxSize)
131  {
132  if (previousPos + subPos + 1 >= txt.length ())
133  break;
134  width.setString (txt.substr (previousPos, ++subPos));
135  }
136  if (previousPos + subPos + 1 >= txt.length ())
137  break;
138  txt.insert (previousPos + subPos - 1, "\n");
139  previousPos += subPos;
140  subPos = 1;
141  width.setString (txt.substr (previousPos, subPos));
142  }
143 
145  lb->SetText (txt);
146  labels_.Add (lb);
147  Refresh ();
148  }
149 
151  {
152  currentText_++;
153  OnTextChanged (*this, EmptyEventArgs ());
154  if (currentText_ == labels_.Count ())
155  {
156  isVisible_ = false;
157  }
158  else
159  {
160  isVisible_ = true;
161  }
162  }
163 
164 } // namespace yap