File: redefine_keys.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (258 lines) | stat: -rw-r--r-- 7,058 bytes parent folder | download | duplicates (5)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

/* Battle Tanks Game
 * Copyright (C) 2006-2009 Battle Tanks team
 *
 * 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.
 */

/* 
 * Additional rights can be granted beyond the GNU General Public License 
 * on the terms provided in the Exception. If you modify this file, 
 * you may extend this exception to your version of the file, 
 * but you are not obligated to do so. If you do not wish to provide this
 * exception without modification, you must delete this exception statement
 * from your version and license this file solely under the GPL without exception. 
*/
#include "redefine_keys.h"
#include "resource_manager.h"
#include "sdlx/surface.h"
#include "sdlx/font.h"
#include "sdlx/rect.h"
#include "config.h"
#include "i18n.h"
#include "button.h"

static const std::string variants[] = {"keys", "keys-1", "keys-2"};

void RedefineKeys::initDefaults() {
#include "controls/default_keys.cpp"

	memcpy(_keys, keys, sizeof(_keys));
}


RedefineKeys::RedefineKeys() : _active_row(-1), _active_col(-1) {
	_bg_table = ResourceManager->load_surface("menu/keys_table.png");
	_selection = ResourceManager->load_surface("menu/keys_selection.png");
	_font = ResourceManager->loadFont("medium", true);
	_small_font = ResourceManager->loadFont("small", true);

	_background.init("menu/background_box_dark.png", _bg_table->get_width() + 96, _bg_table->get_height() + 180, 24);
	
	initDefaults();
	
	_labels.push_back("up");
	_labels.push_back("down");
	_labels.push_back("left");
	_labels.push_back("right");
	_labels.push_back("fire");
	_labels.push_back("alt-fire");
	_labels.push_back("disembark");
	_labels.push_back("hint-ctrl");
	
	_b_ok = new Button("medium_dark", I18n->get("menu", "ok"));
	_b_default = new Button("medium_dark", I18n->get("menu", "default-keys"));
	
	int w, h;
	int mx, my;
	_background.getMargins(mx, my);
	
	_b_ok->get_size(w, h);
	int ym = 32;
	int yp = _background.h - h - ym;
	add (_background.w - 2 * mx - w, yp, _b_ok);

	_b_default->get_size(w, h);
	yp = _background.h - h - ym;
	add (mx * 2, yp, _b_default);
	
	load();
	_modal = true;
}

void RedefineKeys::tick(const float dt) {
	Container::tick(dt);

	if (_b_ok->changed()) {
		_b_ok->reset();
		save();
		hide();
	}
	
	if (_b_default->changed()) {
		_b_default->reset();
		initDefaults();
	}
}


void RedefineKeys::load() {
	std::string profile;
	Config->get("engine.profile", profile, std::string());
	if (profile.empty())
		throw_ex(("empty profile"));
	
	_actions.clear();
	for(size_t i = 0; i < _labels.size(); ++i) {
		_actions.push_back(Actions::value_type(I18n->get("menu", _labels[i]), sdlx::Rect()));
		for(size_t j = 0; j < 3; ++j) {
			Config->get("profile." + profile + ".controls." + variants[j] + "." + _labels[i], _keys[j][i], _keys[j][i]);
		}
	}
}

void RedefineKeys::revert_to_defaults() {
	std::string profile;
	Config->get("engine.profile", profile, std::string());
	if (profile.empty())
		throw_ex(("empty profile"));

	for(size_t i = 0; i < _labels.size(); ++i) {
		for(size_t j = 0; j < 3; ++j) {
			Config->remove("profile." + profile + ".controls." + variants[j] + "." + _labels[i]);
		}
	}
	load();
}

void RedefineKeys::render(sdlx::Surface &surface, const int x, const int y) const {
	_background.render(surface, x, y);
	int dx = (_background.w - _bg_table->get_width()) / 2, dy = (_background.h - _bg_table->get_height()) / 2;
	surface.blit(*_bg_table, x + dx, y + dy);
	
	int yp = y + dy + 50;
	for(size_t i = 0; i < _actions.size(); ++i) {
		sdlx::Rect &rect = _actions[i].second;
		rect.x = 0;
		rect.y = yp - 15 - y;
		rect.h = _font->get_height() + 30;
		rect.w = _background.w;
		
		if (_active_row == (int)i) {
			_background.renderHL(surface, x, yp + _font->get_height() / 2 + 1);
		}

		if (_active_row == (int)i && _active_col != -1) {
			surface.blit(*_selection, x + 205 + 110 * _active_col, yp - 6);
		}

		_font->render(surface, x + 66, yp, _actions[i].first);
		
		for(size_t j = 0; j < 3; ++j) {
			const char *cname = _keys[j][i] ? SDL_GetKeyName((SDLKey)_keys[j][i]): NULL;
			std::string name = (cname)?cname:"???";
			_small_font->render(surface, x + dx + 155 + 110 * j, yp + (_font->get_height() - _small_font->get_height()) / 2, name);
		}
		
		yp += 30;
	}
	Container::render(surface, x, y);
}

void RedefineKeys::get_size(int &w, int &h) const {
	Container::get_size(w, h);
	
	if (_background.w > w)
		w = _background.w;
	
	if (_background.h > h)
		h = _background.h;
}

bool RedefineKeys::onKey(const SDL_keysym sym) {
	switch(sym.sym) {

	case SDLK_RETURN:
	case SDLK_ESCAPE: 
		hide(true);
		return true;
	
	case SDLK_TAB:
	case SDLK_F12:
	case SDLK_m:
	case SDLK_KP_ENTER:
		return true;
		
	default: ;
	}
	
	if (_active_row == -1 || _active_col == -1) 
		return true;
	
	//LOG_DEBUG(("key: %s", SDL_GetKeyName(sym.sym)));
	int old_key = _keys[_active_col][_active_row];
	_keys[_active_col][_active_row] = sym.sym;
	//validation
	if (_active_col == 0) {
		for(int j = 0; j < 7; ++j) {
			if (j == _active_row)
				continue;
			if (_keys[0][j] == sym.sym)
				_keys[0][j] = old_key;
		}
	} else {
		for(int i = 1; i < 3; ++i) 
			for(int j = 0; j < 7; ++j) {
				if (i == _active_col && j == _active_row)
					continue;
				if (_keys[i][j] == sym.sym)
					_keys[i][j] = old_key;
			}
	}
	return true;
}

void RedefineKeys::save() {
	std::string profile;
	Config->get("engine.profile", profile, std::string());
	if (profile.empty())
		throw_ex(("empty profile"));

	for(int i = 0; i < 3; ++i) 
		for(int j = 0; j < 7; ++j) {
			if (_keys[i][j] == 0)
				throw_ex(("invalid key code. (0)"));
	}

	for(size_t i = 0; i < _labels.size(); ++i) {
		for(size_t j = 0; j < 3; ++j) {
			Config->set("profile." + profile + ".controls." + variants[j] + "." + _labels[i], _keys[j][i]);
		}
	}	
}

bool RedefineKeys::onMouse(const int button, const bool pressed, const int x, const int y) {
	Container::onMouse(button, pressed, x, y);
	return true;
}


bool RedefineKeys::onMouseMotion(const int state, const int x, const int y, const int xrel, const int yrel) {
	_active_col = _active_row = -1;
	int dx = (_background.w - _bg_table->get_width()) / 2;
	for(size_t i = 0; i < _actions.size(); ++i) {
		const sdlx::Rect &rect = _actions[i].second;
		if (rect.in(x, y)) 
			_active_row = i;
		int col = (x - dx - 148);
		if (col >= 0) {
			col /=110;
			if (col >= 0 && col < 3)
				_active_col = col;
			//LOG_DEBUG(("col %d", _active_col));
		}
	}
	return true;
}