~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/input/controller.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: controller.cxx,v 1.26 2002/12/20 01:22:32 grumbel Exp $
 
2
// 
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
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.
 
10
//
 
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.
 
15
// 
 
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.
 
19
 
 
20
#include "../debug.hxx"
 
21
#include "../xml_helper.hxx"
 
22
#include "../pingus_error.hxx"
 
23
 
 
24
/*#include "axis_event.hxx"
 
25
#include "axis_factory.hxx"
 
26
#include "button_factory.hxx"
 
27
#include "pointer_event.hxx"
 
28
*/
 
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"
 
38
 
 
39
namespace Input {
 
40
 
 
41
using namespace Axes;
 
42
using namespace Buttons;
 
43
using namespace Pointers;
 
44
using namespace Scrollers;
 
45
 
 
46
Controller::Controller (const std::string& configfile) 
 
47
  : standard_pointer(0), 
 
48
    scroller(0),
 
49
    std_pointer_x(0),
 
50
    std_pointer_y(0)
 
51
{
 
52
  xmlDocPtr doc = xmlParseFile(configfile.c_str());
 
53
    
 
54
  if (!doc)
 
55
    PingusError::raise("Controller: config file <" + configfile + "> not found");
 
56
      
 
57
  xmlNodePtr cur = doc->ROOT;
 
58
    
 
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);
 
62
 
 
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);
 
66
               
 
67
  while (cur) 
 
68
    {
 
69
      if (xmlIsBlankNode(cur)) // explicit check cause we need the continue to check for cur again
 
70
        {
 
71
          cur = cur->next;
 
72
          continue;
 
73
        }
 
74
 
 
75
      else if (XMLhelper::equal_str(cur->name, "standard-pointer"))
 
76
        standard_pointer = PointerFactory::create(XMLhelper::skip_blank(cur->children));
 
77
          
 
78
      else if (XMLhelper::equal_str(cur->name, "scroller"))
 
79
        scroller = ScrollerFactory::create(XMLhelper::skip_blank(cur->children));
 
80
          
 
81
      else if (XMLhelper::equal_str(cur->name, "primary-button"))
 
82
        buttons[primary].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
83
          
 
84
      else if (XMLhelper::equal_str(cur->name, "secondary-button"))
 
85
        buttons[secondary].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
86
          
 
87
      else if (XMLhelper::equal_str(cur->name, "pause-button"))
 
88
        buttons[pause].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
89
 
 
90
      else if (XMLhelper::equal_str(cur->name, "fast-forward-button"))
 
91
        buttons[fast_forward].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
92
        
 
93
      else if (XMLhelper::equal_str(cur->name, "armageddon-button"))
 
94
        buttons[armageddon].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
95
        
 
96
      else if (XMLhelper::equal_str(cur->name, "escape-button"))
 
97
        buttons[escape].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
98
        
 
99
      else if (XMLhelper::equal_str(cur->name, "action-buttons"))
 
100
        create_action_buttons(XMLhelper::skip_blank(cur->children));
 
101
        
 
102
      else if (XMLhelper::equal_str(cur->name, "action-up"))
 
103
        buttons[action_up].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
104
        
 
105
      else if (XMLhelper::equal_str(cur->name, "action-down"))
 
106
        buttons[action_down].first = ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
107
          
 
108
      else
 
109
        PingusError::raise(std::string("Unkown Element in Controller Config: ") + ((cur->name) ? reinterpret_cast<const char*>(cur->name) : ""));
 
110
          
 
111
      cur = cur->next;
 
112
    }
 
113
 
 
114
  if (!standard_pointer)
 
115
    {
 
116
      standard_pointer = new DummyPointer;
 
117
      pwarn << "Controller: No standard pointer - inserting dummy" << std::endl;
 
118
    }
 
119
  else
 
120
    {
 
121
      std_pointer_x = standard_pointer->get_x_pos();
 
122
      std_pointer_y = standard_pointer->get_y_pos();
 
123
    }
 
124
 
 
125
  if (!scroller)
 
126
    {
 
127
      scroller = new DummyScroller;
 
128
      pwarn << "Controller: No scroller - inserting dummy" << std::endl;
 
129
    }
 
130
      
 
131
  if (!buttons.count(primary))
 
132
    {
 
133
      buttons[primary].first = new DummyButton;
 
134
      pwarn << "Controller: No primary button - inserting dummy" << std::endl;
 
135
    }
 
136
      
 
137
  if (!buttons.count(secondary))
 
138
    {
 
139
      buttons[secondary].first = new DummyButton;
 
140
      pwarn << "Controller: No secondary button - inserting dummy" << std::endl;
 
141
    }
 
142
 
 
143
  if (!buttons.count(pause))
 
144
    {
 
145
      buttons[pause].first = new DummyButton;
 
146
      pwarn << "Controller: No pause button - inserting dummy" << std::endl;
 
147
    }
 
148
 
 
149
  if (!buttons.count(fast_forward))
 
150
    {
 
151
      buttons[fast_forward].first = new DummyButton;
 
152
      pwarn << "Controller: No fast_forward button - inserting dummy" << std::endl;
 
153
    }
 
154
      
 
155
  if (!buttons.count(armageddon))
 
156
    {
 
157
      buttons[armageddon].first = new DummyButton;
 
158
      pwarn << "Controller: No armageddon button - inserting dummy" << std::endl;
 
159
    }
 
160
 
 
161
  if (!buttons.count(escape))
 
162
    {
 
163
      buttons[escape].first = new DummyButton;
 
164
      pwarn << "Controller: No escape button - inserting dummy" << std::endl;
 
165
    }
 
166
      
 
167
  if (!buttons.count(action_up))
 
168
    {
 
169
      buttons[action_up].first = new DummyButton;
 
170
      pwarn << "Controller: No action up button - inserting dummy" << std::endl;
 
171
    }
 
172
      
 
173
  if (!buttons.count(action_down))
 
174
    {
 
175
      buttons[action_down].first = new DummyButton;
 
176
      pwarn << "Controller: No action down button - inserting dummy" << std::endl;
 
177
    }
 
178
 
 
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();
 
181
}
 
182
 
 
183
Controller::~Controller ()
 
184
{
 
185
  delete scroller;
 
186
  delete standard_pointer;
 
187
 
 
188
  for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
 
189
    delete it->second.first;
 
190
}
 
191
 
 
192
void
 
193
Controller::create_action_buttons (xmlNodePtr cur)
 
194
{
 
195
  int count = 0;
 
196
    
 
197
  while (cur)
 
198
    {
 
199
      if (xmlIsBlankNode(cur))
 
200
        {
 
201
          cur = cur->next;
 
202
          continue;
 
203
        }
 
204
          
 
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));
 
207
      else
 
208
        PingusError::raise(std::string("Wrong Element in Controller Config (action-buttons): ") + reinterpret_cast<const char*>(cur->name));
 
209
        
 
210
      cur = cur->next;
 
211
      count++;
 
212
    }
 
213
}
 
214
 
 
215
void
 
216
Controller::update (float delta)
 
217
{
 
218
  events.clear ();
 
219
 
 
220
  scroller        ->update(delta);
 
221
  standard_pointer->update(delta);
 
222
 
 
223
  for (std::map<ButtonName, std::pair<Button*, bool> >::iterator it = buttons.begin(); it != buttons.end(); ++it)
 
224
    it->second.first->update(delta);
 
225
 
 
226
  if (std_pointer_x != standard_pointer->get_x_pos() || std_pointer_y != standard_pointer->get_y_pos())
 
227
    {
 
228
      std_pointer_x = standard_pointer->get_x_pos();
 
229
      std_pointer_y = standard_pointer->get_y_pos();
 
230
        
 
231
      events.push_back(makePointerEvent(standard, std_pointer_x, std_pointer_y));
 
232
    }
 
233
      
 
234
  if (scroller->get_x_delta() || scroller->get_y_delta())
 
235
    events.push_back(makeScrollEvent(scroller->get_x_delta(), scroller->get_y_delta()));
 
236
    
 
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)
 
239
      {
 
240
        it->second.second = ! it->second.second;
 
241
        if (it->second.second)
 
242
          events.push_back(makeButtonEvent(it->first, pressed));
 
243
        else
 
244
          events.push_back(makeButtonEvent(it->first, released));
 
245
      }
 
246
}
 
247
 
 
248
const Button*
 
249
Controller::get_button (ButtonName name)
 
250
{
 
251
  if (buttons.count(name))
 
252
    return buttons[name].first;
 
253
 
 
254
  return 0;
 
255
}
 
256
 
 
257
}
 
258
 
 
259
/* EOF */