File: keyboardconfig.cpp

package info (click to toggle)
ppracer 0.3.1-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,900 kB
  • ctags: 3,046
  • sloc: cpp: 24,190; sh: 3,544; tcl: 2,450; makefile: 608; lisp: 87
file content (218 lines) | stat: -rw-r--r-- 6,013 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* 
 * PPRacer 
 * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
#include "keyboardconfig.h"

#include "game_config.h"
#include "game_mgr.h"
#include "ppgltk/ui_mgr.h"
	
struct KeyMap{
	SDLKey key;
	char string[10];
};

static KeyMap keymap[] = {
	{SDLK_LEFT,"left"},
	{SDLK_RIGHT,"right"},
	{SDLK_UP,"up"},
	{SDLK_DOWN,"down"},
	{SDLK_KP0,"keypad 0"},
	{SDLK_KP1,"keypad 1"},
	{SDLK_KP2,"keypad 2"},
	{SDLK_KP3,"keypad 3"},
	{SDLK_KP4,"keypad 4"},
	{SDLK_KP5,"keypad 5"},
	{SDLK_KP6,"keypad 6"},
	{SDLK_KP7,"keypad 7"},
	{SDLK_KP8,"keypad 8"},
	{SDLK_KP9,"keypad 9"},
	{SDLK_KP_PERIOD,"keypad ."},
	{SDLK_KP_DIVIDE,"keypad /"},
	{SDLK_KP_MULTIPLY,"keypad *"},
	{SDLK_KP_MINUS,"keypad m"},
	{SDLK_KP_PLUS,"keypad +"},
	{SDLK_KP_EQUALS,"keypad ="}	
};

	
	
KeyboardConfig::KeyboardConfig()
{
	setTitle(_("Keyboard Configuration"));
	
	pp::Vec2d pos(0,0);
	pp::Vec2d size(150,32);
	
	mp_leftEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_leftEntry->setEditable(false);
	mp_leftEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_leftEntry->setContent(getKey(SDLKey(getparam_turn_left_key())));
	
	mp_rightEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_rightEntry->setEditable(false);
	mp_rightEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_rightEntry->setContent(getKey(SDLKey(getparam_turn_right_key())));
	
	mp_paddleEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_paddleEntry->setEditable(false);
	mp_paddleEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_paddleEntry->setContent(getKey(SDLKey(getparam_paddle_key())));
	
	mp_brakeEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_brakeEntry->setEditable(false);
	mp_brakeEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_brakeEntry->setContent(getKey(SDLKey(getparam_brake_key())));
	
	mp_jumpEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_jumpEntry->setEditable(false);
	mp_jumpEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_jumpEntry->setContent(getKey(SDLKey(getparam_jump_key())));
	
	mp_trickEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_trickEntry->setEditable(false);
	mp_trickEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_trickEntry->setContent(getKey(SDLKey(getparam_trick_modifier_key())));
	
	mp_resetEntry = new pp::Entry(pos, size,
				     "listbox_item");
	mp_resetEntry->setEditable(false);
	mp_resetEntry->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
   	mp_resetEntry->setContent(getKey(SDLKey(getparam_reset_key())));	
}

KeyboardConfig::~KeyboardConfig()
{
	delete mp_leftEntry;
	delete mp_rightEntry;
	delete mp_paddleEntry;
	delete mp_brakeEntry;
	delete mp_jumpEntry;
	delete mp_trickEntry;
	delete mp_resetEntry;
}

void
KeyboardConfig::setWidgetPositions()
{
	int width = 500;
	int height = 240;
		
	pp::Vec2d pos(getparam_x_resolution()/2 - width/2,
				  getparam_y_resolution()/2 + height/2);
	
	pp::Font *font = pp::Font::get("button_label");
	
	font->draw(_("Turn left:"),pos);
	mp_leftEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
		
	pos.y-=40;
	font->draw(_("Turn right:"),pos);
	mp_rightEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
	
	pos.y-=40;
	font->draw(_("Paddle:"),pos);
	mp_paddleEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
	
	pos.y-=40;
	font->draw(_("Brake:"),pos);
	mp_brakeEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
	
	pos.y-=40;
	font->draw(_("Jump:"),pos);
	mp_jumpEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
	
	pos.y-=40;
	font->draw(_("Trick:"),pos);
	mp_trickEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));

	pos.y-=40;
	font->draw(_("Reset:"),pos);
	mp_resetEntry->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
}

void
KeyboardConfig::apply()
{
	setparam_turn_left_key(getKey(mp_leftEntry->getContent()));
	setparam_turn_right_key(getKey(mp_rightEntry->getContent()));		
	setparam_paddle_key(getKey(mp_paddleEntry->getContent()));		
	setparam_brake_key(getKey(mp_brakeEntry->getContent()));		
	setparam_jump_key(getKey(mp_jumpEntry->getContent()));		
	setparam_trick_modifier_key(getKey(mp_trickEntry->getContent()));		
	setparam_reset_key(getKey(mp_resetEntry->getContent()));		
	
	write_config_file();
	set_game_mode( GameMode::prevmode );
    UIMgr.setDirty();
}

void
KeyboardConfig::setKey(pp::Entry* widget, SDLKey key)
{
	std::string content="";
	
	if(key==SDLK_BACKSPACE){
		widget->setContent(content);
		return;
	}else{
		content = getKey(key);
	}
	
	if (content!=""){
		widget->setContent(content);
	}
}

std::string KeyboardConfig::getKey(SDLKey key)
{
	std::string content="";
	if(isalnum(key)){
		content=key;
	}else{
		for(unsigned int i=0;i<sizeof(keymap)/sizeof(KeyMap);i++){
			if(key==keymap[i].key){
				content=keymap[i].string;
				break;
			}
		}
	}
	return content;
}

SDLKey
KeyboardConfig::getKey(std::string& string)
{
	if(string!=""){
		for(unsigned int i=0;i<sizeof(keymap)/sizeof(KeyMap);i++){
			if(string==keymap[i].string){
				return keymap[i].key;
			}
		}
		return SDLKey(string.c_str()[0]);		
	}else{
		return SDLKey(0);
	}
}