git repos / blockattack-game

blame: source/code/ReadKeyboard.cpp

normal view · raw

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
96eb7a16 sago007 2016-04-29 17:01 28
using std::string;
96eb7a16 sago007 2016-04-29 17:01 29
using std::cerr;
cdb45e21 sago007 2015-11-22 21:35 30
ecef5838 sago007 2015-11-24 20:01 31
ReadKeyboard::ReadKeyboard(void) {
c53e6443 sago007 2012-04-17 11:04 32
	maxLength = 16;
d26562ba sago007 2015-12-04 23:29 33
	position = text_string.begin();
89c4a3a6 sago007 2008-08-29 14:32 34
}
89c4a3a6 sago007 2008-08-29 14:32 35
ecef5838 sago007 2015-11-24 20:01 36
ReadKeyboard::~ReadKeyboard(void) {
89c4a3a6 sago007 2008-08-29 14:32 37
}
89c4a3a6 sago007 2008-08-29 14:32 38
0e1e83de sago007 2015-12-25 13:30 39
int ReadKeyboard::CharsBeforeCursor() {
d26562ba sago007 2015-12-04 23:29 40
	return std::distance(text_string.begin(), position);
89c4a3a6 sago007 2008-08-29 14:32 41
}
89c4a3a6 sago007 2008-08-29 14:32 42
ecef5838 sago007 2015-11-24 20:01 43
ReadKeyboard::ReadKeyboard(const char* oldName) {
c53e6443 sago007 2012-04-17 11:04 44
	maxLength = 16;
f6707482 sago007 2015-12-04 22:55 45
	text_string = oldName;
d26562ba sago007 2015-12-04 23:29 46
	position = text_string.end();
89c4a3a6 sago007 2008-08-29 14:32 47
}
89c4a3a6 sago007 2008-08-29 14:32 48
89c4a3a6 sago007 2008-08-29 14:32 49
078900fe sago007 2015-12-05 15:31 50
void ReadKeyboard::putchar(const std::string& thing) {
daa8f1cf sago007 2016-01-23 10:57 51
	if ( (int)text_string.length() < maxLength) {
0e1e83de sago007 2015-12-25 13:30 52
		int oldPostition = utf8::distance(text_string.begin(), position);
0e1e83de sago007 2015-12-25 13:30 53
		int lengthOfInsertString = utf8::distance(thing.begin(), thing.end());
0e1e83de sago007 2015-12-25 13:30 54
		text_string.insert(position, thing.begin(), thing.end());
0e1e83de sago007 2015-12-25 13:30 55
		position = text_string.begin();  //Inserting may destroy our old iterator
0e1e83de sago007 2015-12-25 13:30 56
		utf8::advance(position, oldPostition + lengthOfInsertString, text_string.end());
c53e6443 sago007 2012-04-17 11:04 57
	}
89c4a3a6 sago007 2008-08-29 14:32 58
}
89c4a3a6 sago007 2008-08-29 14:32 59
89c4a3a6 sago007 2008-08-29 14:32 60
ecef5838 sago007 2015-11-24 20:01 61
void ReadKeyboard::removeChar() {
d26562ba sago007 2015-12-04 23:29 62
	if (position < text_string.end()) {
227d1f4e sago007 2015-12-05 07:34 63
		string::iterator endChar= position;
227d1f4e sago007 2015-12-05 07:34 64
		utf8::advance(endChar, 1, text_string.end());
227d1f4e sago007 2015-12-05 07:34 65
		text_string.erase(position, endChar);
acd79baf sago007 2015-11-22 19:37 66
	}
89c4a3a6 sago007 2008-08-29 14:32 67
}
89c4a3a6 sago007 2008-08-29 14:32 68
078900fe sago007 2015-12-05 15:31 69
bool ReadKeyboard::ReadKey(const SDL_Event& key) {
0e1e83de sago007 2015-12-25 13:30 70
	if (key.type == SDL_TEXTINPUT) {
0e1e83de sago007 2015-12-25 13:30 71
		putchar(key.text.text);
0e1e83de sago007 2015-12-25 13:30 72
		if (key.text.text[0] != 0) {
0e1e83de sago007 2015-12-25 13:30 73
			return true;
0e1e83de sago007 2015-12-25 13:30 74
		}
9b99cf7a sago007 2015-12-04 20:22 75
	}
0e1e83de sago007 2015-12-25 13:30 76
	return ReadKey(key.key.keysym.sym);
9b99cf7a sago007 2015-12-04 20:22 77
}
9b99cf7a sago007 2015-12-04 20:22 78
078900fe sago007 2015-12-05 15:31 79
bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
ecef5838 sago007 2015-11-24 20:01 80
	if (keyPressed == SDLK_DELETE) {
d26562ba sago007 2015-12-04 23:29 81
		if ((text_string.length()>0)&& (position<text_string.end())) {
acd79baf sago007 2015-11-22 19:37 82
			ReadKeyboard::removeChar();
acd79baf sago007 2015-11-22 19:37 83
		}
c53e6443 sago007 2012-04-17 11:04 84
		return true;
c53e6443 sago007 2012-04-17 11:04 85
	}
ecef5838 sago007 2015-11-24 20:01 86
	if (keyPressed == SDLK_BACKSPACE) {
d26562ba sago007 2015-12-04 23:29 87
		if (position>text_string.begin()) {
227d1f4e sago007 2015-12-05 07:34 88
			utf8::prior(position, text_string.begin());
c53e6443 sago007 2012-04-17 11:04 89
			ReadKeyboard::removeChar();
c53e6443 sago007 2012-04-17 11:04 90
			return true;
c53e6443 sago007 2012-04-17 11:04 91
		}
c53e6443 sago007 2012-04-17 11:04 92
		return false;
c53e6443 sago007 2012-04-17 11:04 93
	}
ecef5838 sago007 2015-11-24 20:01 94
	if (keyPressed == SDLK_HOME) {
d26562ba sago007 2015-12-04 23:29 95
		position = text_string.begin();
c53e6443 sago007 2012-04-17 11:04 96
		return true;
c53e6443 sago007 2012-04-17 11:04 97
	}
ecef5838 sago007 2015-11-24 20:01 98
	if (keyPressed == SDLK_END) {
d26562ba sago007 2015-12-04 23:29 99
		position=text_string.end();
c53e6443 sago007 2012-04-17 11:04 100
		return true;
c53e6443 sago007 2012-04-17 11:04 101
	}
d26562ba sago007 2015-12-04 23:29 102
	if ((keyPressed == SDLK_LEFT) && (position>text_string.begin())) {
227d1f4e sago007 2015-12-05 07:34 103
		utf8::prior(position, text_string.begin());
c53e6443 sago007 2012-04-17 11:04 104
		return true;
c53e6443 sago007 2012-04-17 11:04 105
	}
d26562ba sago007 2015-12-04 23:29 106
	if ((keyPressed == SDLK_RIGHT) && (position<text_string.end())) {
227d1f4e sago007 2015-12-05 07:34 107
		utf8::next(position, text_string.end());
c53e6443 sago007 2012-04-17 11:04 108
		return true;
c53e6443 sago007 2012-04-17 11:04 109
	}
0e1e83de sago007 2015-12-25 13:30 110
	return true;
89c4a3a6 sago007 2008-08-29 14:32 111
}
89c4a3a6 sago007 2008-08-29 14:32 112
0e1e83de sago007 2015-12-25 13:30 113
const std::string& ReadKeyboard::GetString() const {
0e1e83de sago007 2015-12-25 13:30 114
	return text_string;
89c4a3a6 sago007 2008-08-29 14:32 115
}
1970-01-01 00:00 116