1
2 /* Battle Tanks Game
3 * Copyright (C) 2006-2009 Battle Tanks team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Additional rights can be granted beyond the GNU General Public License
22 * on the terms provided in the Exception. If you modify this file,
23 * you may extend this exception to your version of the file,
24 * but you are not obligated to do so. If you do not wish to provide this
25 * exception without modification, you must delete this exception statement
26 * from your version and license this file solely under the GPL without exception.
27 */
28 #include "upper_box.h"
29 #include "resource_manager.h"
30 #include "config.h"
31 #include "sdlx/surface.h"
32 #include "sdlx/font.h"
33 #include "i18n.h"
34 #include "box.h"
35 #include "player_name_control.h"
36 #include "prompt.h"
37 #include "text_control.h"
38
update(const GameType game_type)39 void UpperBox::update(const GameType game_type) {
40 switch(game_type) {
41 case GameTypeDeathMatch:
42 value = "deathmatch"; break;
43 case GameTypeCooperative:
44 value = "cooperative"; break;
45 case GameTypeRacing:
46 value = "racing"; break;
47 default:
48 throw_ex(("invalid game_type value! (%d)", (int)game_type));
49 }
50 }
51
UpperBox(int _w,int _h,const bool server)52 UpperBox::UpperBox(int _w, int _h, const bool server) : value("deathmatch"), _server(server), _checkbox(NULL) {
53 add(0, 0, _box = new Box("menu/background_box.png", _w, _h));
54
55 int mx, my;
56 _box->getMargins(mx, my);
57
58 _medium = ResourceManager->loadFont("medium", true);
59 _big = ResourceManager->loadFont("big", true);
60
61 int w, h;
62 get_size(w, h);
63
64 int players_w = w / 5;
65
66 int cw1, ch1, cw2, ch2;
67 _player1_name = new PlayerNameControl(I18n->get("menu", "player-name-1"), "name", players_w);
68 _player1_name->get_size(cw1, ch1);
69
70 _player2_name = new PlayerNameControl(I18n->get("menu", "player-name-2"), "name-2", players_w);
71 _player2_name->get_size(cw2, ch2);
72
73 const int dh = 8;
74 add(w - players_w - mx, my + (h - (ch1 + ch2) - dh) / 2 - ch1, _player1_name);
75 add(w - players_w - mx, my + (h - (ch1 + ch2) + dh) / 2, _player2_name);
76
77 _name_prompt = new Prompt(320, 80, new TextControl("small", 32));
78 int nw, nh;
79 get_size(w, h);
80 _name_prompt->get_size(nw, nh);
81 add(w - nw, (h - nh) / 2, _name_prompt);
82 _name_prompt->hide();
83 }
84
render(sdlx::Surface & surface,const int x,const int y) const85 void UpperBox::render(sdlx::Surface &surface, const int x, const int y) const{
86 AUTOLOAD_SURFACE(_checkbox, "menu/radio.png");
87
88 Container::render(surface, x, y);
89
90 int font_dy = (_big->get_height() - _medium->get_height()) / 2;
91
92 int wt = 0;
93 int line1_y = 10;
94 wt = _big->render(surface, x + 16, y + line1_y, I18n->get("menu", "mode"));
95
96 int line2_y = 40;
97
98 int wt2 = _big->render(surface, x + 16, y + line2_y, I18n->get("menu", "split-screen"));
99 if (wt2 > wt)
100 wt = wt2;
101
102 wt += 48;
103
104 _medium->render(surface, x + wt, y + line1_y + font_dy, I18n->get("menu/modes", value));
105
106 int cw = _checkbox->get_width() / 2;
107
108 sdlx::Rect off(0, 0, cw, _checkbox->get_height());
109 sdlx::Rect on(cw, 0, _checkbox->get_width(), _checkbox->get_height());
110
111 bool split;
112 Config->get("multiplayer.split-screen-mode", split, false);
113
114 _off_area.x = wt;
115 _off_area.y = line2_y;
116 _off_area.w = wt;
117 _on_area.h = _off_area.h = 32;
118
119 surface.blit(*_checkbox, split?off:on, x + wt, y + line2_y + font_dy);
120 wt += cw;
121 wt += 16 + _medium->render(surface, x + wt, y + line2_y + font_dy - 2, I18n->get("menu", "off"));
122 _off_area.w = wt - _off_area.w + 1;
123
124 _on_area.x = wt;
125 _on_area.y = line2_y;
126 _on_area.w = wt;
127 surface.blit(*_checkbox, split?on:off, x + wt, y + line2_y + font_dy);
128 wt += cw;
129 wt += 16 + _medium->render(surface, x + wt, y + line2_y + font_dy - 2, I18n->get("menu", "on"));
130 _on_area.w = wt - _on_area.w + 1;
131 }
132
onMouse(const int button,const bool pressed,const int x,const int y)133 bool UpperBox::onMouse(const int button, const bool pressed, const int x, const int y) {
134 if (Container::onMouse(button, pressed, x, y))
135 return true;
136
137 if (!pressed)
138 return false;
139
140 if (_on_area.in(x, y)) {
141 //LOG_DEBUG(("split screen on!"));
142 Config->set("multiplayer.split-screen-mode", true);
143 invalidate();
144 return true;
145 } else if (_off_area.in(x, y)) {
146 //LOG_DEBUG(("split screen off!"));
147 Config->set("multiplayer.split-screen-mode", false);
148 invalidate();
149 return true;
150 }
151 return false;
152 }
153
tick(const float dt)154 void UpperBox::tick(const float dt) {
155 Container::tick(dt);
156 bool split;
157
158 Config->get("multiplayer.split-screen-mode", split, false);
159 if (split) {
160 if (_player2_name->hidden())
161 _player2_name->hide(false);
162 } else {
163 if (!_player2_name->hidden())
164 _player2_name->hide(true);
165 }
166
167 if (_player1_name->changed()) {
168 _player1_name->reset();
169 if (_player1_name->edit()) {
170 _edit_player1 = true;
171 _name_prompt->hide(false);
172 _name_prompt->set(_player1_name->get());
173 _name_prompt->reset();
174 }
175 }
176
177 //copypasteninja was here.
178 if (_player2_name->changed()) {
179 _player2_name->reset();
180 if (_player2_name->edit()) {
181 _edit_player1 = false;
182 _name_prompt->hide(false);
183 _name_prompt->set(_player2_name->get());
184 _name_prompt->reset();
185 }
186 }
187
188 if (_name_prompt->changed()) {
189 _name_prompt->reset();
190 _name_prompt->hide();
191 std::string name = _name_prompt->get();
192 if (!name.empty()) {
193 LOG_DEBUG(("setting name to %s", name.c_str()));
194 (_edit_player1?_player1_name:_player2_name)->set(name);
195 }
196 }
197 }
198