Debian Codegraph @

source/code/ReadKeyboard.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2018 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 https://blockattack.net
21 ===========================================================================
22 */
23
24 #include "ReadKeyboard.h"
25 #include "utf8.h"
26 #include <iostream>
27
28 ReadKeyboard::ReadKeyboard(void) {
29 maxLength = 16;
30 position = text_string.begin();
31 }
32
33 ReadKeyboard::~ReadKeyboard(void) {
34 }
35
36 int ReadKeyboard::CharsBeforeCursor() {
37 return std::distance(text_string.begin(), position);
38 }
39
40 ReadKeyboard::ReadKeyboard(const char* oldName) : maxLength(16), text_string(oldName) {
41 position = text_string.end();
42 }
43
44
45 void ReadKeyboard::putchar(const std::string& thing) {
46 if ( (int)text_string.length() < maxLength) {
47 int oldPostition = utf8::distance(text_string.begin(), position);
48 int lengthOfInsertString = utf8::distance(thing.begin(), thing.end());
49 text_string.insert(position, thing.begin(), thing.end());
50 position = text_string.begin(); //Inserting may destroy our old iterator
51 utf8::advance(position, oldPostition + lengthOfInsertString, text_string.end());
52 }
53 }
54
55
56 void ReadKeyboard::removeChar() {
57 if (position < text_string.end()) {
58 std::string::iterator endChar= position;
59 utf8::advance(endChar, 1, text_string.end());
60 text_string.erase(position, endChar);
61 }
62 }
63
64 bool ReadKeyboard::ReadKey(const SDL_Event& key) {
65 if (key.type == SDL_TEXTINPUT) {
66 putchar(key.text.text);
67 if (key.text.text[0] != 0) {
68 return true;
69 }
70 }
71 return ReadKey(key.key.keysym.sym);
72 }
73
74 bool ReadKeyboard::cursorLeft() {
75 if (position>text_string.begin()) {
76 utf8::prior(position, text_string.begin());
77 return true;
78 }
79 return false;
80 }
81 bool ReadKeyboard::cursorRight() {
82 if (position<text_string.end()) {
83 utf8::next(position, text_string.end());
84 return true;
85 }
86 return false;
87 }
88
89 bool ReadKeyboard::emulateBackspace() {
90 if (position>text_string.begin()) {
91 utf8::prior(position, text_string.begin());
92 ReadKeyboard::removeChar();
93 return true;
94 }
95 return false;
96 }
97
98 bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
99 if (keyPressed == SDLK_DELETE) {
100 if ((text_string.length()>0)&& (position<text_string.end())) {
101 ReadKeyboard::removeChar();
102 }
103 return true;
104 }
105 if (keyPressed == SDLK_BACKSPACE) {
106 return emulateBackspace();
107 }
108 if (keyPressed == SDLK_HOME) {
109 position = text_string.begin();
110 return true;
111 }
112 if (keyPressed == SDLK_END) {
113 position=text_string.end();
114 return true;
115 }
116 if (keyPressed == SDLK_LEFT) {
117 cursorLeft();
118 return true;
119 }
120 if ((keyPressed == SDLK_RIGHT)) {
121 cursorRight();
122 return true;
123 }
124 return true;
125 }
126
127 const std::string& ReadKeyboard::GetString() const {
128 return text_string;
129 }