1 #include "mode_panel.h"
2 #include "i18n.h"
3 #include "map_desc.h"
4 #include "chooser.h"
5 #include "checkbox.h"
6 #include "box.h"
7 #include "config.h"
8 #include "label.h"
9 #include "grid.h"
10
ModePanel(const int width)11 ModePanel::ModePanel(const int width) : mode(-1) {
12 _time_limits.insert(std::pair<const int, std::string>(0, "-:--"));
13 _time_limits.insert(std::pair<const int, std::string>(60, "1:00"));
14 _time_limits.insert(std::pair<const int, std::string>(90, "1:30"));
15 _time_limits.insert(std::pair<const int, std::string>(120, "2:00"));
16 _time_limits.insert(std::pair<const int, std::string>(180, "3:00"));
17 _time_limits.insert(std::pair<const int, std::string>(300, "5:00"));
18 _time_limits.insert(std::pair<const int, std::string>(420, "7:00"));
19 _time_limits.insert(std::pair<const int, std::string>(600, "9:99"));
20
21 add(0, 0, _background = new Box("menu/background_box.png", width, 48));
22
23 int w, h;
24 get_size(w, h);
25 int mx, my;
26 _background->getMargins(mx, my);
27
28 std::vector<std::string> values;
29
30 int tl, pos = 0, idx = 0;
31 Config->get("multiplayer.time-limit", tl, 300);
32
33 for(TimeLimits::const_iterator i = _time_limits.begin(); i != _time_limits.end(); ++i, ++idx) {
34 values.push_back(i->second);
35 if (i->first <= tl)
36 pos = idx;
37 }
38
39 Grid *grid = new Grid(6, 1);
40 add(mx, my, grid);
41
42 _time_limit = new Chooser("big", values);
43 _time_limit->set(pos);
44 grid->set(0, 0, _time_limit, Grid::Middle | Grid::Center);
45 grid->set(0, 1, _tl_label = new Label("small", I18n->get("menu", "time-limit")), Grid::Middle);
46
47 bool rr;
48 Config->get("multiplayer.random-respawn", rr, false);
49
50 grid->set(0, 2, _random_respawn = new Checkbox(rr), Grid::Middle | Grid::Center);
51 grid->set(0, 3, _rr_label = new Label("small", I18n->get("menu", "random-respawn")), Grid::Middle);
52
53 std::vector<std::string> teams;
54 teams.push_back("2");
55 teams.push_back("3");
56 teams.push_back("4");
57 grid->set(0, 4, _teams = new Chooser("big", teams, "menu/teams.png"), Grid::Middle | Grid::Center);
58 grid->set(0, 5, _teams_label = new Label("small", I18n->get("menu", "teams")), Grid::Middle);
59
60 grid->set_spacing(5);
61 grid->recalculate(0, h - 2 * my);
62
63 validate();
64 }
65
set(const MapDesc & map,const int mode)66 void ModePanel::set(const MapDesc &map, const int mode) {
67 hide(map.game_type != GameTypeDeathMatch);
68 this->mode = mode;
69 //LOG_DEBUG(("ctf supported: %s", enable_ctf?"yes":"no"));
70 validate();
71 }
72
tick(const float dt)73 void ModePanel::tick(const float dt) {
74 //LOG_DEBUG(("tick(%g)", dt));
75 Container::tick(dt);
76 if (_time_limit->changed()) {
77 _time_limit->reset();
78 int idx = _time_limit->get();
79 if (idx >= 0) {
80 assert(idx < (int)_time_limits.size());
81 TimeLimits::const_iterator i;
82 for (i = _time_limits.begin(); idx-- && i != _time_limits.end(); ++i);
83 assert(i != _time_limits.end());
84 Config->set("multiplayer.time-limit", i->first);
85 }
86 }
87 if (_random_respawn->changed()) {
88 _random_respawn->reset();
89 Config->set("multiplayer.random-respawn", _random_respawn->get());
90 }
91
92 if (_teams->changed()) {
93 _teams->reset();
94 Config->set("multiplayer.teams", (int)atoi(_teams->getValue().c_str()));
95 }
96 }
97
validate()98 void ModePanel::validate() {
99 //_random_respawn->hide(ctf);
100 //_rr_label->hide(ctf);
101
102 bool ctf = mode == 3, tdm = mode == 1;
103
104 _teams->hide(!tdm);
105 _teams_label->hide(!tdm);
106 _random_respawn->hide(ctf);
107 _rr_label->hide(ctf);
108
109 if (tdm) {
110 int t;
111 Config->get("multiplayer.teams", t, 0);
112 for(int i = 0; i < _teams->size(); ++i)
113 _teams->disable(i, false);
114 try {
115 _teams->set(mrt::format_string("%d", t));
116 } CATCH("set", {});
117 }
118 }
119