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
	length = 0;
c53e6443 sago007 2012-04-17 11:04 31
	maxLength = 16;
c53e6443 sago007 2012-04-17 11:04 32
	position = 0;
56241205 sago007 2012-08-05 11:15 33
	strcpy(textstring,"                             ");
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
ecef5838 sago007 2015-11-24 20:01 39
Uint8 ReadKeyboard::CharsBeforeCursor() {
c53e6443 sago007 2012-04-17 11:04 40
	return 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
	length = 0;
c53e6443 sago007 2012-04-17 11:04 45
	maxLength = 16;
c53e6443 sago007 2012-04-17 11:04 46
	position = 0;
56241205 sago007 2012-08-05 11:15 47
	strcpy(textstring,"                             ");
b45d8841 sago007 2012-04-23 19:30 48
	strncpy(textstring,oldName,strlen(oldName));
c53e6443 sago007 2012-04-17 11:04 49
	char charecter = textstring[maxLength+1];
c53e6443 sago007 2012-04-17 11:04 50
	int i = maxLength+1;
ecef5838 sago007 2015-11-24 20:01 51
	while ((charecter == ' ') && (i>0)) {
c53e6443 sago007 2012-04-17 11:04 52
		i--;
c53e6443 sago007 2012-04-17 11:04 53
		charecter = textstring[i];
c53e6443 sago007 2012-04-17 11:04 54
	}
acd79baf sago007 2015-11-22 19:37 55
	if (i>0) {
acd79baf sago007 2015-11-22 19:37 56
		length = i+1;
acd79baf sago007 2015-11-22 19:37 57
	}
acd79baf sago007 2015-11-22 19:37 58
	else if (charecter == ' ') {
acd79baf sago007 2015-11-22 19:37 59
		length = 0;
acd79baf sago007 2015-11-22 19:37 60
	}
acd79baf sago007 2015-11-22 19:37 61
	else {
acd79baf sago007 2015-11-22 19:37 62
		length = 1;
acd79baf sago007 2015-11-22 19:37 63
	}
c53e6443 sago007 2012-04-17 11:04 64
	position = length;
89c4a3a6 sago007 2008-08-29 14:32 65
89c4a3a6 sago007 2008-08-29 14:32 66
}
89c4a3a6 sago007 2008-08-29 14:32 67
89c4a3a6 sago007 2008-08-29 14:32 68
ecef5838 sago007 2015-11-24 20:01 69
void ReadKeyboard::putchar(char thing) {
ecef5838 sago007 2015-11-24 20:01 70
	if (length < maxLength) {
ecef5838 sago007 2015-11-24 20:01 71
		for (int i = 28; i>position; i--) {
c53e6443 sago007 2012-04-17 11:04 72
			textstring[i]=textstring[i-1];
c53e6443 sago007 2012-04-17 11:04 73
		}
c53e6443 sago007 2012-04-17 11:04 74
		textstring[position] = thing;
c53e6443 sago007 2012-04-17 11:04 75
		length++;
c53e6443 sago007 2012-04-17 11:04 76
		position++;
c53e6443 sago007 2012-04-17 11:04 77
	}
89c4a3a6 sago007 2008-08-29 14:32 78
}
89c4a3a6 sago007 2008-08-29 14:32 79
89c4a3a6 sago007 2008-08-29 14:32 80
ecef5838 sago007 2015-11-24 20:01 81
void ReadKeyboard::removeChar() {
ecef5838 sago007 2015-11-24 20:01 82
	for (int i = position; i<28; i++) {
c53e6443 sago007 2012-04-17 11:04 83
		textstring[i]=textstring[i+1];
c53e6443 sago007 2012-04-17 11:04 84
	}
c53e6443 sago007 2012-04-17 11:04 85
	textstring[28]=' ';
acd79baf sago007 2015-11-22 19:37 86
	if (length>0) {
acd79baf sago007 2015-11-22 19:37 87
		length--;
acd79baf sago007 2015-11-22 19:37 88
	}
89c4a3a6 sago007 2008-08-29 14:32 89
}
89c4a3a6 sago007 2008-08-29 14:32 90
9b99cf7a sago007 2015-12-04 20:22 91
bool ReadKeyboard::ReadKey(const SDL_keysym& key) {
9b99cf7a sago007 2015-12-04 20:22 92
	if ( 
9b99cf7a sago007 2015-12-04 20:22 93
			(key.unicode >= 'a' && key.unicode <= 'z') ||
9b99cf7a sago007 2015-12-04 20:22 94
			(key.unicode >= '0' && key.unicode <= '9') || 
9b99cf7a sago007 2015-12-04 20:22 95
			(key.unicode >= 'A' && key.unicode <= 'Z')) {
9b99cf7a sago007 2015-12-04 20:22 96
		ReadKeyboard::putchar(key.unicode);
9b99cf7a sago007 2015-12-04 20:22 97
		return true;
9b99cf7a sago007 2015-12-04 20:22 98
	}
9b99cf7a sago007 2015-12-04 20:22 99
	if (key.unicode == '.' || key.unicode == ',') {
9b99cf7a sago007 2015-12-04 20:22 100
		ReadKeyboard::putchar('.');
9b99cf7a sago007 2015-12-04 20:22 101
	}
9b99cf7a sago007 2015-12-04 20:22 102
	return ReadKey(key.sym);
9b99cf7a sago007 2015-12-04 20:22 103
}
9b99cf7a sago007 2015-12-04 20:22 104
ecef5838 sago007 2015-11-24 20:01 105
bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
ecef5838 sago007 2015-11-24 20:01 106
	if (keyPressed == SDLK_SPACE) {
c53e6443 sago007 2012-04-17 11:04 107
		ReadKeyboard::putchar(' ');
c53e6443 sago007 2012-04-17 11:04 108
		return true;
c53e6443 sago007 2012-04-17 11:04 109
	}
ecef5838 sago007 2015-11-24 20:01 110
	if (keyPressed == SDLK_DELETE) {
acd79baf sago007 2015-11-22 19:37 111
		if ((length>0)&& (position<length)) {
acd79baf sago007 2015-11-22 19:37 112
			ReadKeyboard::removeChar();
acd79baf sago007 2015-11-22 19:37 113
		}
c53e6443 sago007 2012-04-17 11:04 114
		return true;
c53e6443 sago007 2012-04-17 11:04 115
	}
ecef5838 sago007 2015-11-24 20:01 116
	if (keyPressed == SDLK_BACKSPACE) {
ecef5838 sago007 2015-11-24 20:01 117
		if (position>0) {
c53e6443 sago007 2012-04-17 11:04 118
			position--;
c53e6443 sago007 2012-04-17 11:04 119
			ReadKeyboard::removeChar();
c53e6443 sago007 2012-04-17 11:04 120
			return true;
c53e6443 sago007 2012-04-17 11:04 121
		}
c53e6443 sago007 2012-04-17 11:04 122
		return false;
c53e6443 sago007 2012-04-17 11:04 123
	}
ecef5838 sago007 2015-11-24 20:01 124
	if (keyPressed == SDLK_HOME) {
c53e6443 sago007 2012-04-17 11:04 125
		position=0;
c53e6443 sago007 2012-04-17 11:04 126
		return true;
c53e6443 sago007 2012-04-17 11:04 127
	}
ecef5838 sago007 2015-11-24 20:01 128
	if (keyPressed == SDLK_END) {
c53e6443 sago007 2012-04-17 11:04 129
		position=length;
c53e6443 sago007 2012-04-17 11:04 130
		return true;
c53e6443 sago007 2012-04-17 11:04 131
	}
ecef5838 sago007 2015-11-24 20:01 132
	if ((keyPressed == SDLK_LEFT) && (position>0)) {
c53e6443 sago007 2012-04-17 11:04 133
		position--;
c53e6443 sago007 2012-04-17 11:04 134
		return true;
c53e6443 sago007 2012-04-17 11:04 135
	}
ecef5838 sago007 2015-11-24 20:01 136
	if ((keyPressed == SDLK_RIGHT) && (position<length)) {
c53e6443 sago007 2012-04-17 11:04 137
		position++;
c53e6443 sago007 2012-04-17 11:04 138
		return true;
c53e6443 sago007 2012-04-17 11:04 139
	}
c53e6443 sago007 2012-04-17 11:04 140
	return true;
89c4a3a6 sago007 2008-08-29 14:32 141
}
89c4a3a6 sago007 2008-08-29 14:32 142
ecef5838 sago007 2015-11-24 20:01 143
const char* ReadKeyboard::GetString() {
c53e6443 sago007 2012-04-17 11:04 144
	textstring[29]='\0';
c53e6443 sago007 2012-04-17 11:04 145
	return &textstring[0];
89c4a3a6 sago007 2008-08-29 14:32 146
}
1970-01-01 00:00 147