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
e373cd75 Poul Sander 2018-11-07 19:21 4
Copyright (C) 2005-2018 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
e373cd75 Poul Sander 2018-11-07 19:21 20
https://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
ecef5838 sago007 2015-11-24 20:01 28
ReadKeyboard::ReadKeyboard(void) {
c53e6443 sago007 2012-04-17 11:04 29
	maxLength = 16;
d26562ba sago007 2015-12-04 23:29 30
	position = text_string.begin();
89c4a3a6 sago007 2008-08-29 14:32 31
}
89c4a3a6 sago007 2008-08-29 14:32 32
ecef5838 sago007 2015-11-24 20:01 33
ReadKeyboard::~ReadKeyboard(void) {
89c4a3a6 sago007 2008-08-29 14:32 34
}
89c4a3a6 sago007 2008-08-29 14:32 35
0e1e83de sago007 2015-12-25 13:30 36
int ReadKeyboard::CharsBeforeCursor() {
d26562ba sago007 2015-12-04 23:29 37
	return std::distance(text_string.begin(), position);
89c4a3a6 sago007 2008-08-29 14:32 38
}
89c4a3a6 sago007 2008-08-29 14:32 39
c0e47392 Poul Sander 2018-12-23 16:39 40
ReadKeyboard::ReadKeyboard(const char* oldName) : maxLength(16), text_string(oldName) {
d26562ba sago007 2015-12-04 23:29 41
	position = text_string.end();
89c4a3a6 sago007 2008-08-29 14:32 42
}
89c4a3a6 sago007 2008-08-29 14:32 43
89c4a3a6 sago007 2008-08-29 14:32 44
078900fe sago007 2015-12-05 15:31 45
void ReadKeyboard::putchar(const std::string& thing) {
daa8f1cf sago007 2016-01-23 10:57 46
	if ( (int)text_string.length() < maxLength) {
0e1e83de sago007 2015-12-25 13:30 47
		int oldPostition = utf8::distance(text_string.begin(), position);
0e1e83de sago007 2015-12-25 13:30 48
		int lengthOfInsertString = utf8::distance(thing.begin(), thing.end());
0e1e83de sago007 2015-12-25 13:30 49
		text_string.insert(position, thing.begin(), thing.end());
0e1e83de sago007 2015-12-25 13:30 50
		position = text_string.begin();  //Inserting may destroy our old iterator
0e1e83de sago007 2015-12-25 13:30 51
		utf8::advance(position, oldPostition + lengthOfInsertString, 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()) {
e373cd75 Poul Sander 2018-11-07 19:21 58
		std::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) {
0e1e83de sago007 2015-12-25 13:30 65
	if (key.type == SDL_TEXTINPUT) {
0e1e83de sago007 2015-12-25 13:30 66
		putchar(key.text.text);
0e1e83de sago007 2015-12-25 13:30 67
		if (key.text.text[0] != 0) {
0e1e83de sago007 2015-12-25 13:30 68
			return true;
0e1e83de sago007 2015-12-25 13:30 69
		}
9b99cf7a sago007 2015-12-04 20:22 70
	}
0e1e83de sago007 2015-12-25 13:30 71
	return ReadKey(key.key.keysym.sym);
9b99cf7a sago007 2015-12-04 20:22 72
}
9b99cf7a sago007 2015-12-04 20:22 73
c634a901 Poul Sander 2020-05-24 19:37 74
bool ReadKeyboard::cursorLeft() {
c634a901 Poul Sander 2020-05-24 19:37 75
	if (position>text_string.begin()) {
c634a901 Poul Sander 2020-05-24 19:37 76
		utf8::prior(position, text_string.begin());
c634a901 Poul Sander 2020-05-24 19:37 77
		return true;
c634a901 Poul Sander 2020-05-24 19:37 78
	}
c634a901 Poul Sander 2020-05-24 19:37 79
	return false;
c634a901 Poul Sander 2020-05-24 19:37 80
}
c634a901 Poul Sander 2020-05-24 19:37 81
bool ReadKeyboard::cursorRight() {
c634a901 Poul Sander 2020-05-24 19:37 82
	if (position<text_string.end()) {
c634a901 Poul Sander 2020-05-24 19:37 83
		utf8::next(position, text_string.end());
c634a901 Poul Sander 2020-05-24 19:37 84
		return true;
c634a901 Poul Sander 2020-05-24 19:37 85
	}
c634a901 Poul Sander 2020-05-24 19:37 86
	return false;
c634a901 Poul Sander 2020-05-24 19:37 87
}
c634a901 Poul Sander 2020-05-24 19:37 88
c634a901 Poul Sander 2020-05-24 19:37 89
bool ReadKeyboard::emulateBackspace() {
c634a901 Poul Sander 2020-05-24 19:37 90
	if (position>text_string.begin()) {
c634a901 Poul Sander 2020-05-24 19:37 91
		utf8::prior(position, text_string.begin());
c634a901 Poul Sander 2020-05-24 19:37 92
		ReadKeyboard::removeChar();
c634a901 Poul Sander 2020-05-24 19:37 93
		return true;
c634a901 Poul Sander 2020-05-24 19:37 94
	}
c634a901 Poul Sander 2020-05-24 19:37 95
	return false;
c634a901 Poul Sander 2020-05-24 19:37 96
}
c634a901 Poul Sander 2020-05-24 19:37 97
078900fe sago007 2015-12-05 15:31 98
bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
ecef5838 sago007 2015-11-24 20:01 99
	if (keyPressed == SDLK_DELETE) {
d26562ba sago007 2015-12-04 23:29 100
		if ((text_string.length()>0)&& (position<text_string.end())) {
acd79baf sago007 2015-11-22 19:37 101
			ReadKeyboard::removeChar();
acd79baf sago007 2015-11-22 19:37 102
		}
c53e6443 sago007 2012-04-17 11:04 103
		return true;
c53e6443 sago007 2012-04-17 11:04 104
	}
ecef5838 sago007 2015-11-24 20:01 105
	if (keyPressed == SDLK_BACKSPACE) {
c634a901 Poul Sander 2020-05-24 19:37 106
		return emulateBackspace();
c53e6443 sago007 2012-04-17 11:04 107
	}
ecef5838 sago007 2015-11-24 20:01 108
	if (keyPressed == SDLK_HOME) {
d26562ba sago007 2015-12-04 23:29 109
		position = text_string.begin();
c53e6443 sago007 2012-04-17 11:04 110
		return true;
c53e6443 sago007 2012-04-17 11:04 111
	}
ecef5838 sago007 2015-11-24 20:01 112
	if (keyPressed == SDLK_END) {
d26562ba sago007 2015-12-04 23:29 113
		position=text_string.end();
c53e6443 sago007 2012-04-17 11:04 114
		return true;
c53e6443 sago007 2012-04-17 11:04 115
	}
c634a901 Poul Sander 2020-05-24 19:37 116
	if (keyPressed == SDLK_LEFT) {
c634a901 Poul Sander 2020-05-24 19:37 117
		cursorLeft();
c53e6443 sago007 2012-04-17 11:04 118
		return true;
c53e6443 sago007 2012-04-17 11:04 119
	}
c634a901 Poul Sander 2020-05-24 19:37 120
	if ((keyPressed == SDLK_RIGHT)) {
c634a901 Poul Sander 2020-05-24 19:37 121
		cursorRight();
c53e6443 sago007 2012-04-17 11:04 122
		return true;
c53e6443 sago007 2012-04-17 11:04 123
	}
0e1e83de sago007 2015-12-25 13:30 124
	return true;
89c4a3a6 sago007 2008-08-29 14:32 125
}
89c4a3a6 sago007 2008-08-29 14:32 126
0e1e83de sago007 2015-12-25 13:30 127
const std::string& ReadKeyboard::GetString() const {
0e1e83de sago007 2015-12-25 13:30 128
	return text_string;
89c4a3a6 sago007 2008-08-29 14:32 129
}
1970-01-01 00:00 130