ManaPlus
pincode.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "gui/widgets/pincode.h"
22 
23 #include "gui/gui.h"
24 #include "gui/skin.h"
25 
26 #include "gui/widgets/button.h"
27 #include "gui/widgets/textfield.h"
28 
29 #include "utils/gettext.h"
30 #include "utils/stringutils.h"
31 
32 #include "debug.h"
33 
34 Skin *Pincode::mSkin = nullptr;
35 int Pincode::mInstances = 0;
36 
37 Pincode::Pincode(const Widget2 *const widget,
38  TextField *const textField) :
39  Container(widget),
42  mButtons(),
43  mText(),
44  mTextField(textField),
45  mPadding(0),
46  mSpacing(2),
47  mButtonWidth(0),
48  mButtonHeight(0)
49 {
50  mAllowLogic = false;
52 
53  if (mInstances == 0)
54  {
55  if (theme != nullptr)
56  {
57  mSkin = theme->load("pin.xml",
58  "",
59  true,
61  }
62  }
63  mInstances ++;
64 
65  if (mSkin != nullptr)
66  {
68  mSpacing = mSkin->getOption("spacing", 2);
69  }
70  setSelectable(false);
71 
72  addButtons();
73  adjustSize();
74 }
75 
77 {
78  if (mWindow != nullptr)
80 
81  if (gui != nullptr)
82  gui->removeDragged(this);
83 
84  mInstances --;
85  if (mInstances == 0)
86  {
87  if (theme != nullptr)
88  theme->unload(mSkin);
89  }
90 }
91 
93 {
94  int buttonWidth = 0;
95  int buttonHeight = 0;
96  for (int f = 0; f < 10; f ++)
97  {
98  const std::string str = toString(f);
99  mButtons[f] = new Button(this,
100  str,
101  str,
103  this);
104  mButtons[f]->adjustSize();
105  const Rect &rect = mButtons[f]->getDimension();
106  if (rect.width > buttonWidth)
107  buttonWidth = rect.width;
108  if (rect.height > buttonHeight)
109  buttonHeight = rect.height;
110  add(mButtons[f]);
111  }
112  int x = 0;
113  int y = 0;
114  const int xSize = buttonWidth + mSpacing;
115  const int ySize = buttonHeight + mSpacing;
116  for (int f = 0; f < 10; f ++)
117  {
118  mButtons[f]->setPosition(mPadding + x * xSize,
119  mPadding + y * ySize);
120  x ++;
121  if (x > 2)
122  {
123  x = 0;
124  y ++;
125  }
126  }
127  mButtons[10] = new Button(this,
128  // TRANSLATORS: clear pin code button
129  _("Clear"),
130  "clear",
132  this);
133  mButtons[10]->adjustSize();
134  add(mButtons[10]);
135  mButtons[10]->setPosition(mPadding + xSize,
136  mPadding + 3 * ySize);
137 
139  mButtonHeight = buttonHeight;
140 }
141 
143 {
144  const int pad2 = mPadding * 2;
145  setSize(pad2 + 3 * mButtonWidth + 2 * mSpacing,
146  pad2 + 4 * mButtonHeight + 3 * mSpacing);
147 }
148 
150 {
151  mSkin = nullptr;
152 }
153 
154 void Pincode::action(const ActionEvent &event)
155 {
156  if (event.getId() == "clear")
157  {
158  mText.clear();
161  return;
162  }
163  if (mText.size() >= 4)
164  return;
165  const Widget *const eventSrc = event.getSource();
166  for (int f = 0; f < 10; f ++)
167  {
168  if (mButtons[f] == eventSrc)
169  {
170  const std::string str = toString(f);
171  mText.append(str);
174  return;
175  }
176  }
177 }
178 
179 void Pincode::shuffle(uint32_t seed) const
180 {
181  int tab[10];
182  const uint32_t multiplier = 0x3498;
183  const uint32_t baseSeed = 0x881234;
184  int k = 2;
185 
186  for (size_t f = 0; f < 10; f ++)
187  tab[f] = CAST_S32(f);
188  for (size_t f = 1; f < 10; f ++)
189  {
190  seed = baseSeed + seed * multiplier;
191  const size_t pos = seed % k;
192  if (f != pos)
193  {
194  const int tmp = tab[f];
195  tab[f] = tab[pos];
196  tab[pos] = tmp;
197  }
198  k = k + 1;
199  }
200  for (size_t f = 0; f < 10; f ++)
201  {
202  const std::string str = toString(tab[f]);
203  mButtons[f]->setCaption(str);
204  }
205 }
static Skin * mSkin
Definition: pincode.h:53
void setSize(const int width, const int height)
Definition: widget.cpp:366
Pincode(const Widget2 *const widget, TextField *const textField)
Definition: pincode.cpp:37
static const int buttonWidth
Definition: sliderlist.cpp:34
#define _(s)
Definition: gettext.h:34
int width
Definition: rect.h:218
const std::string BUTTON_PIN_SKIN
Definition: button.h:89
Definition: skin.h:35
void unload(Skin *const skin)
Definition: theme.cpp:249
Gui * gui
Definition: gui.cpp:110
Definition: rect.h:72
int mButtonWidth
Definition: pincode.h:68
Definition: button.h:96
void adjustSize()
Definition: pincode.cpp:142
static std::string getThemePath()
Definition: theme.h:66
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
int getOption(const std::string &name) const
Definition: skin.h:105
void setText(const std::string &text)
Definition: textfield.cpp:802
void adjustSize()
Definition: button.cpp:798
TextField * mTextField
Definition: pincode.h:65
void setSelectable(const bool selectable)
Definition: widget.h:947
int getPadding() const
Definition: skin.h:99
void setCaption(const std::string &caption)
Definition: button.h:213
static int mInstances
Definition: pincode.h:55
void shuffle(uint32_t seed) const
Definition: pincode.cpp:179
#define CAST_S32
Definition: cast.h:29
Widget * mWindow
Definition: widget2.h:111
std::string mText
Definition: pincode.h:64
virtual void add(Widget *const widget)
const std::string & getId() const
Definition: actionevent.h:121
bool mAllowLogic
Definition: widget.h:1159
const bool Opaque_false
Definition: opaque.h:29
int mButtonHeight
Definition: pincode.h:69
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
static void finalCleanup()
Definition: pincode.cpp:149
const Rect & getDimension() const
Definition: widget.h:316
void setOpaque(Opaque opaque)
void action(const ActionEvent &event)
Definition: pincode.cpp:154
~Pincode()
Definition: pincode.cpp:76
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
Theme * theme
Definition: theme.cpp:61
Button * mButtons[11]
Definition: pincode.h:63
int mSpacing
Definition: pincode.h:67
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1160
Definition: widget.h:97
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306
int mPadding
Definition: pincode.h:66
void signalEvent()
Definition: textfield.cpp:842
void addButtons()
Definition: pincode.cpp:92