1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
* CTextInput.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CTextInput.h"
#include "Images.h"
#include "TextControls.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../render/IRenderHandler.h"
#include "../../lib/texts/TextOperations.h"
std::list<CFocusable *> CFocusable::focusables;
CFocusable * CFocusable::inputWithFocus;
CTextInput::CTextInput(const Rect & Pos)
:originalAlignment(ETextAlignment::CENTERLEFT)
{
pos += Pos.topLeft();
pos.h = Pos.h;
pos.w = Pos.w;
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
}
void CTextInput::createLabel(bool giveFocusToInput)
{
OBJECT_CONSTRUCTION;
label = std::make_shared<CLabel>();
label->pos = pos;
label->alignment = originalAlignment;
#if !defined(VCMI_MOBILE)
if(giveFocusToInput)
giveFocus();
#endif
}
CTextInput::CTextInput(const Rect & Pos, EFonts font, ETextAlignment alignment, bool giveFocusToInput)
: CTextInput(Pos)
{
originalAlignment = alignment;
setRedrawParent(true);
createLabel(giveFocusToInput);
setFont(font);
setAlignment(alignment);
}
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName)
: CTextInput(Pos)
{
OBJECT_CONSTRUCTION;
if (!bgName.empty())
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
else
setRedrawParent(true);
createLabel(true);
}
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
: CTextInput(Pos)
{
OBJECT_CONSTRUCTION;
background = std::make_shared<CPicture>(srf, Pos);
pos.w = background->pos.w;
pos.h = background->pos.h;
background->pos = pos;
createLabel(true);
}
void CTextInput::setFont(EFonts font)
{
label->font = font;
}
void CTextInput::setColor(const ColorRGBA & color)
{
label->color = color;
}
void CTextInput::setAlignment(ETextAlignment alignment)
{
originalAlignment = alignment;
label->alignment = alignment;
}
const std::string & CTextInput::getText() const
{
return currentText;
}
void CTextInput::setCallback(const TextEditedCallback & cb)
{
assert(!onTextEdited);
onTextEdited = cb;
}
void CTextInput::setPopupCallback(const std::function<void()> & cb)
{
callbackPopup = cb;
}
void CTextInput::setFilterFilename()
{
assert(!onTextFiltering);
onTextFiltering = std::bind(&CTextInput::filenameFilter, _1, _2);
}
void CTextInput::setFilterNumber(int minValue, int maxValue)
{
onTextFiltering = std::bind(&CTextInput::numberFilter, _1, _2, minValue, maxValue);
}
std::string CTextInput::getVisibleText() const
{
return hasFocus() ? currentText + composedText + "_" : currentText;
}
void CTextInput::showPopupWindow(const Point & cursorPosition)
{
if(callbackPopup)
callbackPopup();
}
void CTextInput::clickPressed(const Point & cursorPosition)
{
// attempt to give focus unconditionally, even if we already have it
// this forces on-screen keyboard to show up again, even if player have closed it before
giveFocus();
}
void CTextInput::keyPressed(EShortcut key)
{
if(!hasFocus())
return;
if(key == EShortcut::GLOBAL_MOVE_FOCUS)
{
moveFocus();
return;
}
bool redrawNeeded = false;
switch(key)
{
case EShortcut::GLOBAL_BACKSPACE:
if(!composedText.empty())
{
TextOperations::trimRightUnicode(composedText);
redrawNeeded = true;
}
else if(!currentText.empty())
{
TextOperations::trimRightUnicode(currentText);
redrawNeeded = true;
}
break;
default:
break;
}
if(redrawNeeded)
{
updateLabel();
if(onTextEdited)
onTextEdited(currentText);
}
}
void CTextInput::setText(const std::string & nText)
{
currentText = nText;
updateLabel();
}
void CTextInput::updateLabel()
{
std::string visibleText = getVisibleText();
label->alignment = originalAlignment;
const auto & font = GH.renderHandler().loadFont(label->font);
while (font->getStringWidth(visibleText) > pos.w)
{
label->alignment = ETextAlignment::CENTERRIGHT;
visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));
}
label->setText(visibleText);
}
void CTextInput::textInputted(const std::string & enteredText)
{
if(!hasFocus())
return;
std::string oldText = currentText;
setText(getText() + enteredText);
if(onTextFiltering)
onTextFiltering(currentText, oldText);
if(currentText != oldText)
{
updateLabel();
if(onTextEdited)
onTextEdited(currentText);
}
composedText.clear();
}
void CTextInput::textEdited(const std::string & enteredText)
{
if(!hasFocus())
return;
composedText = enteredText;
updateLabel();
}
void CTextInput::filenameFilter(std::string & text, const std::string &oldText)
{
static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
size_t pos;
while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
text.erase(pos, 1);
}
void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
{
assert(minValue < maxValue);
if(text.empty())
text = "0";
size_t pos = 0;
if(text[0] == '-') //allow '-' sign as first symbol only
pos++;
while(pos < text.size())
{
if(text[pos] < '0' || text[pos] > '9')
{
text = oldText;
return; //new text is not number.
}
pos++;
}
try
{
int value = boost::lexical_cast<int>(text);
if(value < minValue)
text = std::to_string(minValue);
else if(value > maxValue)
text = std::to_string(maxValue);
}
catch(boost::bad_lexical_cast &)
{
//Should never happen. Unless I missed some cases
logGlobal->warn("Warning: failed to convert %s to number!", text);
text = oldText;
}
}
void CTextInput::activate()
{
CFocusable::activate();
if (hasFocus())
{
#if defined(VCMI_MOBILE)
//giveFocus();
#else
GH.startTextInput(pos);
#endif
}
}
void CTextInput::deactivate()
{
CFocusable::deactivate();
if (hasFocus())
{
#if defined(VCMI_MOBILE)
removeFocus();
#else
GH.stopTextInput();
#endif
}
}
void CTextInput::onFocusGot()
{
updateLabel();
}
void CTextInput::onFocusLost()
{
updateLabel();
}
void CFocusable::focusGot()
{
if (isActive())
GH.startTextInput(pos);
onFocusGot();
}
void CFocusable::focusLost()
{
if (isActive())
GH.stopTextInput();
onFocusLost();
}
CFocusable::CFocusable()
{
focusables.push_back(this);
}
CFocusable::~CFocusable()
{
if(hasFocus())
inputWithFocus = nullptr;
focusables -= this;
}
bool CFocusable::hasFocus() const
{
return inputWithFocus == this;
}
void CFocusable::giveFocus()
{
auto previousInput = inputWithFocus;
inputWithFocus = this;
if(previousInput)
previousInput->focusLost();
focusGot();
}
void CFocusable::moveFocus()
{
auto i = vstd::find(focusables, this);
auto ourIt = i;
for(i++; i != ourIt; i++)
{
if(i == focusables.end())
i = focusables.begin();
if(*i == this)
return;
if((*i)->isActive())
{
(*i)->giveFocus();
break;
}
}
}
void CFocusable::removeFocus()
{
if(this == inputWithFocus)
{
inputWithFocus = nullptr;
focusLost();
}
}
|