YAPOG  0.0.1
Yet Another Pokemon Online Game
MultiLabelWidget.cpp
Go to the documentation of this file.
1 #include <SFML/Graphics/Text.hpp>
10 
11 namespace yap
12 {
14  : labels_ ()
15  , currentText_ (0)
16  , layout_ (nullptr)
17  , layoutManager_ (nullptr)
18  , showText_ (true)
19  , currentSelec_ (0)
20  {
21  layout_ = new VerticalLayout (ext, in, Extendable);
24  }
25 
27  {
28  }
29 
31  {
32  showText_ = state;
33  }
34 
36  {
37  return true;
38  }
39 
41  {
43  }
44 
46  {
47  return GetUserSize ()
48  + ((border_ != nullptr) ? border_->GetSize () : Vector2 ());
49  }
50 
52  {
53  }
54 
55  void MultiLabelWidget::HandleShow (bool isVisible)
56  {
57  }
58 
60  {
61  }
62 
64  {
65  }
66 
68  {
69  }
70 
71  void MultiLabelWidget::HandleChangeColor (const sf::Color& color)
72  {
73  }
74 
76  {
77  return content_;
78  }
79 
81  {
82  if (guiEvent.type == sf::Event::KeyPressed)
83  {
84  if (guiEvent.key.code == sf::Keyboard::PageUp)
85  {
86  if (currentSelec_ <= 0)
87  return true;
88 
89  currentSelec_--;
91  return true;
92  }
93  if (guiEvent.key.code == sf::Keyboard::PageDown)
94  {
95  if (labels_.Count () == 0 || currentSelec_ >= labels_.Count () - 1)
96  return true;
97 
98  currentSelec_++;
100  return true;
101  }
102  }
103  return false;
104  }
105 
107  const String& contentArg,
108  uint charSize,
109  sf::Color color,
110  LayoutBox::Align align)
111  {
112  if (contentArg.empty())
113  return;
114 
115  String txt = contentArg;
116  Label charWidth ("Test");
117  charWidth.SetTextSize (charSize);
118  uint LabelMaxSize = GetUserSize ().x - padding_.left - padding_.right;
119  uint charNumb = (LabelMaxSize / charWidth.GetCharWidth () * 1.5);
120  uint previousPos = 0;
121  uint subPos = charNumb;
122  sf::Text width (txt.substr (0, subPos));
123 
125  pos.Add (0);
126  while (true)
127  {
128  while (width.getLocalBounds ().width < LabelMaxSize)
129  {
130  if (previousPos + subPos + 1 >= txt.length ())
131  break;
132 
133  width.setString (txt.substr (previousPos, ++subPos));
134  }
135 
136  if (previousPos + subPos + 1 >= txt.length ())
137  break;
138 
139  pos.Add (previousPos + subPos - 1);
140  txt.insert (previousPos + subPos - 1, "\n");
141  previousPos += subPos;
142  subPos = 1;
143  width.setString (txt.substr (previousPos, subPos));
144  }
145 
146  for (int i = 0; i < pos.Count (); ++i)
147  {
148  Label* lb = new Label ();
149  lb->ChangeColor (color);
150  lb->SetTextSize (charSize);
151 
152  if (i + 1 < pos.Count ())
153  lb->SetText (contentArg.substr (pos[i], pos[i + 1] - pos[i]));
154  else
155  lb->SetText (contentArg.substr (pos[i]));
156 
157  labels_.Add (lb);
158  layout_->AddChild (*lb, align);
159  layoutManager_->AddItem (lb);
161 
162  //Auto Scroll;
163  currentSelec_++;
165  }
166  layout_->SetPosition (GetPosition () - Vector2 (0, charWidth.GetCharHeight () / 2));
167  Refresh ();
168  }
169 
171  {
172  content_ = "";
173  labels_.Clear ();
174  currentSelec_ = 0;
175  layout_->Clear ();
176  currentText_ = 0;
177  layoutManager_->Clear ();
178  }
179 } // namespace yap