ManaPlus
inttextfield.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #ifdef USE_SDL2
27 #include "enums/input/keyvalue.h"
28 #endif // USE_SDL2
29 
30 #include "utils/stringutils.h"
31 
32 #include <sstream>
33 
34 #include "debug.h"
35 
36 IntTextField::IntTextField(const Widget2 *const widget,
37  const int def,
38  const int min,
39  const int max,
40  const Enable enabled,
41  const int width) :
42  TextField(widget, toString(def),
43  LoseFocusOnTab_true, nullptr, std::string(), false),
44  mMin(0),
45  mMax(0),
46  mDefault(def),
47  mValue(def)
48 {
49  if (min != 0 || max != 0)
50  setRange(min, max);
51 
52  setEnabled(enabled == Enable_true);
53  if (width != 0)
54  setWidth(width);
55 }
56 
58 {
59  const InputActionT action = event.getActionId();
60 
61  if (action == InputAction::GUI_DELETE
62  || action == InputAction::GUI_BACKSPACE)
63  {
64  setText(std::string());
67 
68  event.consume();
69  }
70 
71 #ifdef USE_SDL2
72  const int val = event.getKey().getValue();
73  if (val != KeyValue::TEXTINPUT)
74  return;
75 
76  const std::string str = event.getText();
77  if (str.empty())
78  return;
79  const size_t sz = str.size();
80  for (size_t f = 0; f < sz; f ++)
81  {
82  const char chr = str[f];
83  if (chr < '0' || chr > '9')
84  return;
85  }
86 #else // USE_SDL2
87 
88  if (!event.getKey().isNumber())
89  return;
90 #endif // USE_SDL2
91 
92  TextField::keyPressed(event);
93 
94  std::istringstream s(getText());
95  int i;
96  s >> i;
97  setValue(i);
100 }
101 
102 void IntTextField::setRange(const int min, const int max)
103 {
104  mMin = min;
105  mMax = max;
106 
107  if (mValue < mMin)
108  mValue = mMin;
109  else if (mValue > mMax)
110  mValue = mMax;
111 
112  if (mDefault < mMin)
113  mDefault = mMin;
114  else if (mDefault > mMax)
115  mDefault = mMax;
116 }
117 
119 {
120  return getText().empty() ? mMin : mValue;
121 }
122 
123 void IntTextField::setValue(const int i)
124 {
125  if (i < mMin)
126  mValue = mMin;
127  else if (i > mMax)
128  mValue = mMax;
129  else
130  mValue = i;
131 
132  const std::string valStr = toString(mValue);
133  setText(valStr);
134  setCaretPosition(CAST_U32(valStr.length()) + 1);
135 }
136 
137 void IntTextField::setDefaultValue(const int value)
138 {
139  if (value < mMin)
140  mDefault = mMin;
141  else if (value > mMax)
142  mDefault = mMax;
143  else
144  mDefault = value;
145 }
146 
148 {
150 }
#define CAST_U32
Definition: cast.h:31
IntTextField(const Widget2 *const widget, const int def, const int min, const int max, const Enable enabled, const int width)
void setValue(const int value)
void setDefaultValue(const int value)
void setRange(const int minimum, const int maximum)
void keyPressed(KeyEvent &event)
int getValue() const
const Key & getKey() const
Definition: keyevent.h:124
bool isNumber() const
Definition: key.cpp:85
void keyPressed(KeyEvent &event)
Definition: textfield.cpp:301
const std::string & getText() const
Definition: textfield.h:224
bool mSendAlwaysEvents
Definition: textfield.h:290
void setText(const std::string &text)
Definition: textfield.cpp:803
void setCaretPosition(unsigned int position)
Definition: textfield.cpp:752
void setWidth(const int width)
Definition: widget.cpp:133
void distributeActionEvent()
Definition: widget.cpp:493
void setEnabled(const bool enabled)
Definition: widget.h:352
bool Enable
Definition: enable.h:30
const bool Enable_true
Definition: enable.h:30
InputAction ::T InputActionT
Definition: inputaction.h:717
#define nullptr
Definition: localconsts.h:45
const bool LoseFocusOnTab_true
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
@ TEXTINPUT
Definition: keyvalue.h:128