1
// $Id: controller.cxx,v 1.26 2002/12/20 01:22:32 grumbel Exp $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "../debug.hxx"
21
#include "../xml_helper.hxx"
22
#include "../pingus_error.hxx"
24
/*#include "axis_event.hxx"
25
#include "axis_factory.hxx"
26
#include "button_factory.hxx"
27
#include "pointer_event.hxx"
29
#include "controller.hxx"
30
#include "axes/dummy_axis.hxx"
31
#include "buttons/dummy_button.hxx"
32
#include "pointers/dummy_pointer.hxx"
33
#include "scrollers/dummy_scroller.hxx"
34
#include "pointer_factory.hxx"
35
#include "scroller_factory.hxx"
36
#include "button_factory.hxx"
37
//#include "scroll_event.hxx"
42
using namespace Buttons;
43
using namespace Pointers;
44
using namespace Scrollers;
46
Controller::Controller (const std::string& configfile)
47
: standard_pointer(0),
52
xmlDocPtr doc = xmlParseFile(configfile.c_str());
55
PingusError::raise("Controller: config file <" + configfile + "> not found");
57
xmlNodePtr cur = doc->ROOT;
59
if (!cur || !XMLhelper::equal_str(cur->name, "pingus-controller"))
60
PingusError::raise("Controller: invalid config file <" + configfile + ">");
61
cur = XMLhelper::skip_blank(cur->children);
63
if (!cur || !XMLhelper::equal_str(cur->name, "controller-config"))
64
PingusError::raise("Controller: invalid config file <" + configfile + ">");
65
cur = XMLhelper::skip_blank(cur->children);
69
if (xmlIsBlankNode(cur)) // explicit check cause we need the continue to check for cur again
75
else if (XMLhelper::equal_str(cur->name, "standard-pointer"))
76
standard_pointer = PointerFactory::create(XMLhelper::skip_blank(cur->children));
78
else if (XMLhelper::equal_str(cur->name, "scroller"))
79
scroller = ScrollerFactory::create(XMLhelper::skip_blank(cur->children));
81
else if (XMLhelper::equal_str(cur->name, "primary-button"))
82
buttons[primary].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
84
else if (XMLhelper::equal_str(cur->name, "secondary-button"))
85
buttons[secondary].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
87
else if (XMLhelper::equal_str(cur->name, "pause-button"))
88
buttons[pause].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
90
else if (XMLhelper::equal_str(cur->name, "fast-forward-button"))
91
buttons[fast_forward].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
93
else if (XMLhelper::equal_str(cur->name, "armageddon-button"))
94
buttons[armageddon].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
96
else if (XMLhelper::equal_str(cur->name, "escape-button"))
97
buttons[escape].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
99
else if (XMLhelper::equal_str(cur->name, "action-buttons"))
100
create_action_buttons(XMLhelper::skip_blank(cur->children));
102
else if (XMLhelper::equal_str(cur->name, "action-up"))
103
buttons[action_up].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
105
else if (XMLhelper::equal_str(cur->name, "action-down"))
106
buttons[action_down].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
109
PingusError::raise(std::string("Unkown Element in Controller Config: ") + ((cur->name) ? reinterpret_cast<const char*>(cur->name) : ""));
114
if (!standard_pointer)
116
standard_pointer = new DummyPointer;
117
pwarn << "Controller: No standard pointer - inserting dummy" << std::endl;
121
std_pointer_x = standard_pointer->get_x_pos();
122
std_pointer_y = standard_pointer->get_y_pos();
127
scroller = new DummyScroller;
128
pwarn << "Controller: No scroller - inserting dummy" << std::endl;
131
if (!buttons.count(primary))
133
buttons[primary].first = new DummyButton;
134
pwarn << "Controller: No primary button - inserting dummy" << std::endl;
137
if (!buttons.count(secondary))
139
buttons[secondary].first = new DummyButton;
140
pwarn << "Controller: No secondary button - inserting dummy" << std::endl;
143
if (!buttons.count(pause))
145
buttons[pause].first = new DummyButton;
146
pwarn << "Controller: No pause button - inserting dummy" << std::endl;
149
if (!buttons.count(fast_forward))
151
buttons[fast_forward].first = new DummyButton;
152
pwarn << "Controller: No fast_forward button - inserting dummy" << std::endl;
155
if (!buttons.count(armageddon))
157
buttons[armageddon].first = new DummyButton;
158
pwarn << "Controller: No armageddon button - inserting dummy" << std::endl;
161
if (!buttons.count(escape))
163
buttons[escape].first = new DummyButton;
164
pwarn << "Controller: No escape button - inserting dummy" << std::endl;
167
if (!buttons.count(action_up))
169
buttons[action_up].first = new DummyButton;
170
pwarn << "Controller: No action up button - inserting dummy" << std::endl;
173
if (!buttons.count(action_down))
175
buttons[action_down].first = new DummyButton;
176
pwarn << "Controller: No action down button - inserting dummy" << std::endl;
179
for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
180
it->second.second = it->second.first->is_pressed();
183
Controller::~Controller ()
186
delete standard_pointer;
188
for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
189
delete it->second.first;
193
Controller::create_action_buttons (xmlNodePtr cur)
199
if (xmlIsBlankNode(cur))
205
if (XMLhelper::equal_str(cur->name, "action-button"))
206
buttons[static_cast<ButtonName>(action_1 + count)].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
208
PingusError::raise(std::string("Wrong Element in Controller Config (action-buttons): ") + reinterpret_cast<const char*>(cur->name));
216
Controller::update (float delta)
220
scroller ->update(delta);
221
standard_pointer->update(delta);
223
for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
224
it->second.first->update(delta);
226
if (std_pointer_x != standard_pointer->get_x_pos() || std_pointer_y != standard_pointer->get_y_pos())
228
std_pointer_x = standard_pointer->get_x_pos();
229
std_pointer_y = standard_pointer->get_y_pos();
231
events.push_back(makePointerEvent(standard, std_pointer_x, std_pointer_y));
234
if (scroller->get_x_delta() || scroller->get_y_delta())
235
events.push_back(makeScrollEvent(scroller->get_x_delta(), scroller->get_y_delta()));
237
for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
238
if (it->second.first->is_pressed() != it->second.second)
240
it->second.second = ! it->second.second;
241
if (it->second.second)
242
events.push_back(makeButtonEvent(it->first, pressed));
244
events.push_back(makeButtonEvent(it->first, released));
249
Controller::get_button (ButtonName name)
251
if (buttons.count(name))
252
return buttons[name].first;