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
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.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"
89c4a3a6 sago007 2008-08-29 14:32 25
cdb45e21 sago007 2015-11-22 21:35 26
cdb45e21 sago007 2015-11-22 21:35 27
using namespace std;
cdb45e21 sago007 2015-11-22 21:35 28
ecef5838 sago007 2015-11-24 20:01 29
ReadKeyboard::ReadKeyboard(void) {
c53e6443 sago007 2012-04-17 11:04 30
	maxLength = 16;
d26562ba sago007 2015-12-04 23:29 31
	position = text_string.begin();
89c4a3a6 sago007 2008-08-29 14:32 32
}
89c4a3a6 sago007 2008-08-29 14:32 33
ecef5838 sago007 2015-11-24 20:01 34
ReadKeyboard::~ReadKeyboard(void) {
89c4a3a6 sago007 2008-08-29 14:32 35
}
89c4a3a6 sago007 2008-08-29 14:32 36
ecef5838 sago007 2015-11-24 20:01 37
Uint8 ReadKeyboard::CharsBeforeCursor() {
d26562ba sago007 2015-12-04 23:29 38
	return std::distance(text_string.begin(), position);
89c4a3a6 sago007 2008-08-29 14:32 39
}
89c4a3a6 sago007 2008-08-29 14:32 40
ecef5838 sago007 2015-11-24 20:01 41
ReadKeyboard::ReadKeyboard(const char* oldName) {
c53e6443 sago007 2012-04-17 11:04 42
	maxLength = 16;
f6707482 sago007 2015-12-04 22:55 43
	text_string = oldName;
d26562ba sago007 2015-12-04 23:29 44
	position = text_string.end();
89c4a3a6 sago007 2008-08-29 14:32 45
}
89c4a3a6 sago007 2008-08-29 14:32 46
89c4a3a6 sago007 2008-08-29 14:32 47
ecef5838 sago007 2015-11-24 20:01 48
void ReadKeyboard::putchar(char thing) {
f6707482 sago007 2015-12-04 22:55 49
	if (text_string.length() < maxLength) {
d26562ba sago007 2015-12-04 23:29 50
		text_string.insert(position, thing);
c53e6443 sago007 2012-04-17 11:04 51
		position++;
c53e6443 sago007 2012-04-17 11:04 52
	}
89c4a3a6 sago007 2008-08-29 14:32 53
}
89c4a3a6 sago007 2008-08-29 14:32 54
89c4a3a6 sago007 2008-08-29 14:32 55
ecef5838 sago007 2015-11-24 20:01 56
void ReadKeyboard::removeChar() {
d26562ba sago007 2015-12-04 23:29 57
	if (position < text_string.end()) {
f6707482 sago007 2015-12-04 22:55 58
		text_string.erase(position);
acd79baf sago007 2015-11-22 19:37 59
	}
89c4a3a6 sago007 2008-08-29 14:32 60
}
89c4a3a6 sago007 2008-08-29 14:32 61
9b99cf7a sago007 2015-12-04 20:22 62
bool ReadKeyboard::ReadKey(const SDL_keysym& key) {
9b99cf7a sago007 2015-12-04 20:22 63
	if ( 
9b99cf7a sago007 2015-12-04 20:22 64
			(key.unicode >= 'a' && key.unicode <= 'z') ||
9b99cf7a sago007 2015-12-04 20:22 65
			(key.unicode >= '0' && key.unicode <= '9') || 
9b99cf7a sago007 2015-12-04 20:22 66
			(key.unicode >= 'A' && key.unicode <= 'Z')) {
9b99cf7a sago007 2015-12-04 20:22 67
		ReadKeyboard::putchar(key.unicode);
9b99cf7a sago007 2015-12-04 20:22 68
		return true;
9b99cf7a sago007 2015-12-04 20:22 69
	}
9b99cf7a sago007 2015-12-04 20:22 70
	if (key.unicode == '.' || key.unicode == ',') {
9b99cf7a sago007 2015-12-04 20:22 71
		ReadKeyboard::putchar('.');
9b99cf7a sago007 2015-12-04 20:22 72
	}
9b99cf7a sago007 2015-12-04 20:22 73
	return ReadKey(key.sym);
9b99cf7a sago007 2015-12-04 20:22 74
}
9b99cf7a sago007 2015-12-04 20:22 75
ecef5838 sago007 2015-11-24 20:01 76
bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
ecef5838 sago007 2015-11-24 20:01 77
	if (keyPressed == SDLK_SPACE) {
c53e6443 sago007 2012-04-17 11:04 78
		ReadKeyboard::putchar(' ');
c53e6443 sago007 2012-04-17 11:04 79
		return true;
c53e6443 sago007 2012-04-17 11:04 80
	}
ecef5838 sago007 2015-11-24 20:01 81
	if (keyPressed == SDLK_DELETE) {
d26562ba sago007 2015-12-04 23:29 82
		if ((text_string.length()>0)&& (position<text_string.end())) {
acd79baf sago007 2015-11-22 19:37 83
			ReadKeyboard::removeChar();
acd79baf sago007 2015-11-22 19:37 84
		}
c53e6443 sago007 2012-04-17 11:04 85
		return true;
c53e6443 sago007 2012-04-17 11:04 86
	}
ecef5838 sago007 2015-11-24 20:01 87
	if (keyPressed == SDLK_BACKSPACE) {
d26562ba sago007 2015-12-04 23:29 88
		if (position>text_string.begin()) {
c53e6443 sago007 2012-04-17 11:04 89
			position--;
c53e6443 sago007 2012-04-17 11:04 90
			ReadKeyboard::removeChar();
c53e6443 sago007 2012-04-17 11:04 91
			return true;
c53e6443 sago007 2012-04-17 11:04 92
		}
c53e6443 sago007 2012-04-17 11:04 93
		return false;
c53e6443 sago007 2012-04-17 11:04 94
	}
ecef5838 sago007 2015-11-24 20:01 95
	if (keyPressed == SDLK_HOME) {
d26562ba sago007 2015-12-04 23:29 96
		position = text_string.begin();
c53e6443 sago007 2012-04-17 11:04 97
		return true;
c53e6443 sago007 2012-04-17 11:04 98
	}
ecef5838 sago007 2015-11-24 20:01 99
	if (keyPressed == SDLK_END) {
d26562ba sago007 2015-12-04 23:29 100
		position=text_string.end();
c53e6443 sago007 2012-04-17 11:04 101
		return true;
c53e6443 sago007 2012-04-17 11:04 102
	}
d26562ba sago007 2015-12-04 23:29 103
	if ((keyPressed == SDLK_LEFT) && (position>text_string.begin())) {
c53e6443 sago007 2012-04-17 11:04 104
		position--;
c53e6443 sago007 2012-04-17 11:04 105
		return true;
c53e6443 sago007 2012-04-17 11:04 106
	}
d26562ba sago007 2015-12-04 23:29 107
	if ((keyPressed == SDLK_RIGHT) && (position<text_string.end())) {
c53e6443 sago007 2012-04-17 11:04 108
		position++;
c53e6443 sago007 2012-04-17 11:04 109
		return true;
c53e6443 sago007 2012-04-17 11:04 110
	}
c53e6443 sago007 2012-04-17 11:04 111
	return true;
89c4a3a6 sago007 2008-08-29 14:32 112
}
89c4a3a6 sago007 2008-08-29 14:32 113
ecef5838 sago007 2015-11-24 20:01 114
const char* ReadKeyboard::GetString() {
f6707482 sago007 2015-12-04 22:55 115
	return text_string.c_str();
89c4a3a6 sago007 2008-08-29 14:32 116
}
1970-01-01 00:00 117