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