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