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"
227d1f4e sago007 2015-12-05 07:34 25
#include "utf8.h"
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
078900fe sago007 2015-12-05 15:31 48
void ReadKeyboard::putchar(const std::string& thing) {
f6707482 sago007 2015-12-04 22:55 49
	if (text_string.length() < maxLength) {
1d2e2129 sago007 2015-12-23 15:41 50
//		text_string.insert(position, thing);
227d1f4e sago007 2015-12-05 07:34 51
		utf8::advance(position, 1, text_string.end());
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()) {
227d1f4e sago007 2015-12-05 07:34 58
		string::iterator endChar= position;
227d1f4e sago007 2015-12-05 07:34 59
		utf8::advance(endChar, 1, text_string.end());
227d1f4e sago007 2015-12-05 07:34 60
		text_string.erase(position, endChar);
acd79baf sago007 2015-11-22 19:37 61
	}
89c4a3a6 sago007 2008-08-29 14:32 62
}
89c4a3a6 sago007 2008-08-29 14:32 63
078900fe sago007 2015-12-05 15:31 64
bool ReadKeyboard::ReadKey(const SDL_Event& key) {
1d2e2129 sago007 2015-12-23 15:41 65
	return false;
1d2e2129 sago007 2015-12-23 15:41 66
	/*if ( 
9b99cf7a sago007 2015-12-04 20:22 67
			(key.unicode >= 'a' && key.unicode <= 'z') ||
9b99cf7a sago007 2015-12-04 20:22 68
			(key.unicode >= '0' && key.unicode <= '9') || 
9b99cf7a sago007 2015-12-04 20:22 69
			(key.unicode >= 'A' && key.unicode <= 'Z')) {
9b99cf7a sago007 2015-12-04 20:22 70
		ReadKeyboard::putchar(key.unicode);
9b99cf7a sago007 2015-12-04 20:22 71
		return true;
9b99cf7a sago007 2015-12-04 20:22 72
	}
9b99cf7a sago007 2015-12-04 20:22 73
	if (key.unicode == '.' || key.unicode == ',') {
9b99cf7a sago007 2015-12-04 20:22 74
		ReadKeyboard::putchar('.');
9b99cf7a sago007 2015-12-04 20:22 75
	}
1d2e2129 sago007 2015-12-23 15:41 76
	return ReadKey(key.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) {
1d2e2129 sago007 2015-12-23 15:41 80
	return false;
1d2e2129 sago007 2015-12-23 15:41 81
	/*
ecef5838 sago007 2015-11-24 20:01 82
	if (keyPressed == SDLK_SPACE) {
c53e6443 sago007 2012-04-17 11:04 83
		ReadKeyboard::putchar(' ');
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_DELETE) {
d26562ba sago007 2015-12-04 23:29 87
		if ((text_string.length()>0)&& (position<text_string.end())) {
acd79baf sago007 2015-11-22 19:37 88
			ReadKeyboard::removeChar();
acd79baf sago007 2015-11-22 19:37 89
		}
c53e6443 sago007 2012-04-17 11:04 90
		return true;
c53e6443 sago007 2012-04-17 11:04 91
	}
ecef5838 sago007 2015-11-24 20:01 92
	if (keyPressed == SDLK_BACKSPACE) {
d26562ba sago007 2015-12-04 23:29 93
		if (position>text_string.begin()) {
227d1f4e sago007 2015-12-05 07:34 94
			utf8::prior(position, text_string.begin());
c53e6443 sago007 2012-04-17 11:04 95
			ReadKeyboard::removeChar();
c53e6443 sago007 2012-04-17 11:04 96
			return true;
c53e6443 sago007 2012-04-17 11:04 97
		}
c53e6443 sago007 2012-04-17 11:04 98
		return false;
c53e6443 sago007 2012-04-17 11:04 99
	}
ecef5838 sago007 2015-11-24 20:01 100
	if (keyPressed == SDLK_HOME) {
d26562ba sago007 2015-12-04 23:29 101
		position = text_string.begin();
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_END) {
d26562ba sago007 2015-12-04 23:29 105
		position=text_string.end();
c53e6443 sago007 2012-04-17 11:04 106
		return true;
c53e6443 sago007 2012-04-17 11:04 107
	}
d26562ba sago007 2015-12-04 23:29 108
	if ((keyPressed == SDLK_LEFT) && (position>text_string.begin())) {
227d1f4e sago007 2015-12-05 07:34 109
		utf8::prior(position, text_string.begin());
c53e6443 sago007 2012-04-17 11:04 110
		return true;
c53e6443 sago007 2012-04-17 11:04 111
	}
d26562ba sago007 2015-12-04 23:29 112
	if ((keyPressed == SDLK_RIGHT) && (position<text_string.end())) {
227d1f4e sago007 2015-12-05 07:34 113
		utf8::next(position, text_string.end());
c53e6443 sago007 2012-04-17 11:04 114
		return true;
c53e6443 sago007 2012-04-17 11:04 115
	}
1d2e2129 sago007 2015-12-23 15:41 116
	return true;*/
89c4a3a6 sago007 2008-08-29 14:32 117
}
89c4a3a6 sago007 2008-08-29 14:32 118
ecef5838 sago007 2015-11-24 20:01 119
const char* ReadKeyboard::GetString() {
f6707482 sago007 2015-12-04 22:55 120
	return text_string.c_str();
89c4a3a6 sago007 2008-08-29 14:32 121
}
1970-01-01 00:00 122