Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/InputHandler.cc @ 1398

Last change on this file since 1398 was 1398, checked in by rgrieder, 16 years ago
  • removed the mouseHandler in SpaceShip
File size: 25.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30 @file
31 @brief Implementation of the different input handlers.
32 */
33
34#include "InputHandler.h"
35#include <fstream>
36#include "util/Convert.h"
37#include "util/SubString.h"
38#include "util/String.h"
39#include "Debug.h"
40#include "ConfigValueIncludes.h"
41#include "CoreIncludes.h"
42#include "CommandExecutor.h"
43#include "Executor.h"
44
45namespace orxonox
46{
47  // ###############################
48  // ######      Button       ######
49  // ###############################
50
51  bool BufferedParamCommand::execute()
52  {
53    if (nValuesAdded_)
54    {
55      BufferedParamCommand& cmd = *this;
56      cmd.evaluation_.setEvaluatedParameter(cmd.paramIndex_, cmd.value_);
57      // reset
58      cmd.nValuesAdded_ = 0;
59      cmd.value_ = 0;
60      return CommandExecutor::execute(cmd.evaluation_);
61    }
62    else
63      return true;
64  }
65
66  bool SimpleCommand::execute(float abs, float rel)
67  {
68    return CommandExecutor::execute(evaluation_);
69  }
70
71  bool ParamCommand::execute(float abs, float rel)
72  {
73    BufferedParamCommand& paramCommand = *paramCommand_;
74    // command has an additional parameter
75    if (bRelative_ && (rel > 0 || rel < 0))
76    {
77      // we have to calculate a relative movement.
78      // paramModifier_ says how much one keystroke is
79      paramCommand.value_ += paramModifier_ * rel;
80    }
81    else if (abs > 0 || abs < 0)
82    {
83      // we have to calculate absolute position of the axis.
84      // Since there might be another axis that is affected, we have to wait and
85      // store the result in a temporary place
86      paramCommand.value_ = (paramCommand.value_ * paramCommand.nValuesAdded_ + paramModifier_ * abs)
87                            /++paramCommand.nValuesAdded_;
88    }
89    return true;
90  }
91
92  void Button::clear()
93  {
94    for (unsigned int j = 0; j < 3; j++)
95    {
96      if (nCommands_[j])
97      {
98        // delete all commands and the command pointer array
99        for (unsigned int i = 0; i < nCommands_[j]; i++)
100          delete commands_[j][i];
101        delete[] commands_[j];
102        commands_[j] = 0;
103        nCommands_[j] = 0;
104      }
105      else
106      {
107        commands_[j] = 0;
108      }
109    }
110  }
111
112  void Button::parse(std::vector<BufferedParamCommand*>& paramCommandBuffer)
113  {
114    if (isEmpty(bindingString_))
115    {
116      clear();
117      return;
118    }
119
120    // use std::vector for a temporary dynamic array
121    std::vector<BaseCommand*> commands[3];
122
123
124    // separate the commands
125    SubString commandStrings(bindingString_, "|", SubString::WhiteSpaces, false,
126        '\\', false, '"', false, '(', ')', false, '\0');
127
128    for (unsigned int iCommand = 0; iCommand < commandStrings.size(); iCommand++)
129    {
130      if (commandStrings[iCommand] != "")
131      {
132        SubString tokens(commandStrings[iCommand], " ", SubString::WhiteSpaces, false,
133            '\\', false, '"', false, '(', ')', false, '\0');
134       
135        unsigned int iToken = 0;
136
137        // for real axes, we can feed a ButtonThreshold argument as entire command
138        if (getLowercase(tokens[0]) == "buttonthreshold")
139        {
140          if (tokens.size() == 1)
141            continue;
142          // may fail, but doesn't matter
143          convertValue(&buttonThreshold_, tokens[1]);
144          continue;
145        }
146
147        // first argument can be OnPress, OnHold OnRelease or nothing
148        KeybindMode::Enum mode = KeybindMode::None;
149        if (getLowercase(tokens[iToken]) == "onpress")
150          mode = KeybindMode::OnPress,   iToken++;
151        if (getLowercase(tokens[iToken]) == "onrelease")
152          mode = KeybindMode::OnRelease, iToken++;
153        if (getLowercase(tokens[iToken]) == "onhold")
154          mode = KeybindMode::OnHold,    iToken++;
155
156        if (iToken == tokens.size())
157          continue;
158
159        // second argument can be the amplitude for the case it as an axis command
160        // default amplitude is 1.0f
161        float paramModifier = 1.0f;
162        if (getLowercase(tokens[iToken]) == "scale")
163        {
164          iToken++;
165          if (iToken == tokens.size() || !convertValue(&paramModifier, tokens[iToken]))
166          {
167            COUT(2) << "Error while parsing key binding " << name_
168                << ". Numeric expression expected afer 'AxisAmp', switching to default value" << std::endl;
169            if (iToken == tokens.size())
170              continue;
171          }
172          iToken++;
173        }
174
175        // no more arguments expected except for the actual command
176        if (iToken == tokens.size())
177          continue;
178
179        std::string commandStr;
180        while (iToken != tokens.size())
181          commandStr += tokens[iToken++] + " ";
182
183        // evaluate the command
184        CommandEvaluation eval = CommandExecutor::evaluate(commandStr);
185        if (!eval.isValid())
186          continue;
187
188        // check for param command
189        int paramIndex = eval.getEvaluatedExecutor()->getAxisParamIndex();
190        // TODO: check in Executor for correct paramIndex
191        if (paramIndex >= 0)
192        {
193          // parameter supported command
194          ParamCommand* cmd = new ParamCommand();
195          cmd->paramModifier_ = paramModifier;
196          cmd->bRelative_ = eval.getEvaluatedExecutor()->getIsAxisRelative();
197
198          // add command to the buffer if not yet existing
199          for (unsigned int iParamCmd = 0; iParamCmd < paramCommandBuffer.size(); iParamCmd++)
200          {
201            if (getLowercase(paramCommandBuffer[iParamCmd]->evaluation_.getCommandString())
202                == getLowercase(commandStr))
203            {
204              // already in list
205              cmd->paramCommand_ = paramCommandBuffer[iParamCmd];
206              break;
207            }
208          }
209          if (cmd->paramCommand_ == 0)
210          {
211            cmd->paramCommand_ = new BufferedParamCommand();
212            paramCommandBuffer.push_back(cmd->paramCommand_);
213            cmd->paramCommand_->evaluation_ = eval;
214            cmd->paramCommand_->paramIndex_ = paramIndex;
215          }
216
217
218          // we don't know whether this is an actual axis or just a button
219          if (mode == KeybindMode::None)
220          {
221            if (!addParamCommand(cmd))
222            {
223              mode = eval.getEvaluatedExecutor()->getKeybindMode();
224              commands[mode].push_back(cmd);
225            }
226          }
227        }
228        else
229        {
230          SimpleCommand* cmd = new SimpleCommand();
231          cmd->evaluation_ = eval;
232
233          //TODO: check CommandEvaluation for correct KeybindMode
234          if (mode == KeybindMode::None)
235            mode = eval.getEvaluatedExecutor()->getKeybindMode();
236
237          commands[mode].push_back(cmd);
238        }
239      }
240    }
241
242    for (unsigned int j = 0; j < 3; j++)
243    {
244      nCommands_[j] = commands[j].size();
245      if (nCommands_[j])
246      {
247        commands_[j] = new BaseCommand*[nCommands_[j]];
248        for (unsigned int i = 0; i < commands[j].size(); i++)
249          commands_[j][i] = commands[j][i];
250      }
251      else
252        commands_[j] = 0;
253    }
254  }
255
256  bool Button::execute(KeybindMode::Enum mode, float abs, float rel)
257  {
258    // execute all the parsed commands in the string
259    for (unsigned int iCommand = 0; iCommand < nCommands_[mode]; iCommand++)
260      commands_[mode][iCommand]->execute(abs, rel);
261    return true;
262  }
263
264  void HalfAxis::clear()
265  {
266    Button::clear();
267    if (nParamCommands_)
268    {
269      // delete all commands and the command pointer array
270      for (unsigned int i = 0; i < nParamCommands_; i++)
271        delete paramCommands_[i];
272      delete[] paramCommands_;
273    }
274    else
275    {
276      nParamCommands_ = 0; nParamCommands_ = 0;
277    }
278  }
279 
280  bool HalfAxis::addParamCommand(ParamCommand* command)
281  {
282    ParamCommand** cmds = paramCommands_;
283    paramCommands_ = new ParamCommand*[++nParamCommands_];
284    unsigned int i;
285    for (i = 0; i < nParamCommands_ - 1; i++)
286      paramCommands_[i] = cmds[i];
287    paramCommands_[i] = command;
288    delete[] cmds;
289    return true;
290  }
291
292  bool HalfAxis::execute()
293  {
294    bool success = true;
295    for (unsigned int i = 0; i < nParamCommands_; i++)
296      success = success && paramCommands_[i]->execute(absVal_, relVal_);
297    return success;
298  }
299
300
301  // ###############################
302  // ######     KeyBinder     ######
303  // ###############################
304
305  /**
306    @brief Constructor that does as little as necessary.
307  */
308  KeyBinder::KeyBinder() : deriveTime_(0.0f)
309  {
310    mouseRelative_[0] = 0;
311    mouseRelative_[1] = 0;
312    mousePosition_[0] = 0;
313    mousePosition_[1] = 0;
314
315    RegisterObject(KeyBinder);
316
317    // keys
318    std::string keyNames[] = {
319      "UNASSIGNED",
320      "ESCAPE",
321      "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
322      "MINUS", "EQUALS", "BACK", "TAB",
323      "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
324      "LBRACKET", "RBRACKET",
325      "RETURN", "LCONTROL",
326      "A", "S", "D", "F", "G", "H", "J", "K", "L",
327      "SEMICOLON", "APOSTROPHE", "GRAVE",
328      "LSHIFT", "BACKSLASH",
329      "Z", "X", "C", "V", "B", "N", "M",
330      "COMMA", "PERIOD", "SLASH",
331      "RSHIFT",
332      "MULTIPLY",
333      "LMENU",
334      "SPACE",
335      "CAPITAL",
336      "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
337      "NUMLOCK", "SCROLL",
338      "NUMPAD7", "NUMPAD8", "NUMPAD9",
339      "SUBTRACT",
340      "NUMPAD4", "NUMPAD5", "NUMPAD6",
341      "ADD",
342      "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0",
343      "DECIMAL",
344      "","",
345      "OEM_102",
346      "F11", "F12",
347      "","","","","","","","","","","",
348      "F13", "F14", "F15",
349      "","","","","","","","","","",
350      "KANA",
351      "","",
352      "ABNT_C1",
353      "","","","","",
354      "CONVERT",
355      "",
356      "NOCONVERT",
357      "",
358      "YEN",
359      "ABNT_C2",
360      "","","","","","","","","","","","","","",
361      "NUMPADEQUALS",
362      "","",
363      "PREVTRACK",
364      "AT",
365      "COLON", "UNDERLINE",
366      "KANJI",
367      "STOP",
368      "AX",
369      "UNLABELED",
370      "NEXTTRACK",
371      "","",
372      "NUMPADENTER",
373      "RCONTROL",
374      "","",
375      "MUTE",
376      "CALCULATOR",
377      "PLAYPAUSE",
378      "",
379      "MEDIASTOP",
380      "","","","","","","","","",
381      "VOLUMEDOWN",
382      "",
383      "VOLUMEUP",
384      "",
385      "WEBHOME",
386      "NUMPADCOMMA",
387      "",
388      "DIVIDE",
389      "",
390      "SYSRQ",
391      "RMENU",
392      "","","","","","","","","","","","",
393      "PAUSE",
394      "",
395      "HOME",
396      "UP",
397      "PGUP",
398      "",
399      "LEFT",
400      "",
401      "RIGHT",
402      "",
403      "END", "DOWN", "PGDOWN", "INSERT", "DELETE",
404      "","","","","","","",
405      "LWIN", "RWIN", "APPS",
406      "POWER", "SLEEP",
407      "","","",
408      "WAKE",
409      "",
410      "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK",
411      "MYCOMPUTER", "MAIL", "MEDIASELECT"
412    };
413    for (unsigned int i = 0; i < nKeys_s; i++)
414      keys_[i].name_ = "Key" + keyNames[i];
415
416    // mouse buttons
417    std::string mouseButtonNames[] = {
418      "MouseLeft", "MouseRight", "MouseMiddle",
419      "MouseButton3", "MouseButton4", "MouseButton5",
420      "MouseButton6", "MouseButton7",
421      "MouseWheel1Up", "MouseWheel1Down",
422      "MouseWheel2Up", "MouseWheel2Down" };
423    for (unsigned int i = 0; i < nMouseButtons_s; i++)
424      mouseButtons_[i].name_ = mouseButtonNames[i];
425
426    // joy stick buttons
427    for (unsigned int i = 0; i < 32; i++)
428      joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i);
429    for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4)
430    {
431                  joyStickButtons_[i + 0].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North";
432                  joyStickButtons_[i + 1].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South";
433                  joyStickButtons_[i + 2].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East";
434                  joyStickButtons_[i + 3].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West";
435    }
436
437    // half axes
438    std::string rawNames[nHalfAxes_s/2];
439    rawNames[0] = "MouseX";
440    rawNames[1] = "MouseY";
441    rawNames[2] = "Empty1";
442    rawNames[3] = "Empty2";
443    for (unsigned int i = 4; i < nHalfAxes_s/2; i++)
444      rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3);
445    for (unsigned int i = 0; i < nHalfAxes_s/2; i++)
446    {
447      halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos";
448      halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg";
449    }
450
451    for (unsigned int i = 0; i < this->nHalfAxes_s; i++)
452      halfAxes_[i].buttonThreshold_ = buttonThreshold_;
453  }
454
455  /**
456    @brief Destructor
457  */
458  KeyBinder::~KeyBinder()
459  {
460    // almost no destructors required because most of the arrays are static.
461    clearBindings(); // does some destruction work
462  }
463
464  /**
465    @brief Loads the key and button bindings.
466    @return True if loading succeeded.
467  */
468  void KeyBinder::loadBindings()
469  {
470    COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
471
472    clearBindings();
473
474    /*std::ifstream infile;
475    infile.open("keybindings.ini");
476    if (!infile.is_open())
477    {
478      ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings_def.ini");
479      setConfigValues();
480    }
481    infile.close();*/
482    ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
483    setConfigValues();
484
485    COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
486  }
487
488  /**
489    @brief Loader for the key bindings, managed by config values.
490  */
491  void KeyBinder::setConfigValues()
492  {
493    SetConfigValue(analogThreshold_, 0.01f)  .description("Threshold for analog axes until which the state is 0.");
494    SetConfigValue(mouseSensitivity_, 1.0f)  .description("Mouse sensitivity.");
495    SetConfigValue(bDeriveMouseInput_, false).description("Whether or not to derive moues movement for the absolute value.");
496    SetConfigValue(derivePeriod_, 0.1f).description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
497    SetConfigValue(mouseSensitivityDerived_, 1.0f).description("Mouse sensitivity if mouse input is derived.");
498    SetConfigValue(keybindingsDefault_, "keybindings_def.ini").description("Default ini file for the keybindings.");
499
500    float oldThresh = buttonThreshold_;
501    SetConfigValue(buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed.");
502    if (oldThresh != buttonThreshold_)
503      for (unsigned int i = 0; i < nHalfAxes_s; i++)
504        if (halfAxes_[i].buttonThreshold_ == oldThresh)
505          halfAxes_[i].buttonThreshold_ = buttonThreshold_;
506
507    // keys
508    for (unsigned int i = 0; i < nKeys_s; i++)
509      readTrigger(keys_[i]);
510    // mouse buttons
511    for (unsigned int i = 0; i < nMouseButtons_s; i++)
512      readTrigger(mouseButtons_[i]);
513    // joy stick buttons
514    for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
515      readTrigger(joyStickButtons_[i]);
516    // half axes
517    for (unsigned int i = 0; i < nHalfAxes_s; i++)
518      readTrigger(halfAxes_[i]);
519  }
520
521  void KeyBinder::readTrigger(Button& button)
522  {
523    // config value stuff
524    ConfigValueContainer* cont = getIdentifier()->getConfigValueContainer(button.name_);
525    if (!cont)
526    {
527      cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), button.name_, "");
528      getIdentifier()->addConfigValueContainer(button.name_, cont);
529    }
530    std::string old = button.bindingString_;
531    cont->getValue(&button.bindingString_);
532
533    // keybinder stuff
534    if (old != button.bindingString_)
535    {
536      // binding has changed
537      button.parse(paramCommandBuffer_);
538    }
539  }
540
541  /**
542    @brief Overwrites all bindings with ""
543  */
544  void KeyBinder::clearBindings(bool bInit)
545  {
546    for (unsigned int i = 0; i < nKeys_s; i++)
547      keys_[i].clear();
548
549    for (unsigned int i = 0; i < nMouseButtons_s; i++)
550      mouseButtons_[i].clear();
551
552    for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
553      joyStickButtons_[i].clear();
554
555    for (unsigned int i = 0; i < nHalfAxes_s; i++)
556      halfAxes_[i].clear();
557
558    for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
559      delete paramCommandBuffer_[i];
560    paramCommandBuffer_.clear();
561  }
562
563  void KeyBinder::tick(float dt)
564  {
565    // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event.
566    for (unsigned int i = 0; i < nHalfAxes_s; i++)
567    {
568      if (halfAxes_[i].hasChanged_)
569      {
570        if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_)
571        {
572          halfAxes_[i].wasDown_ = true;
573          if (halfAxes_[i].nCommands_[KeybindMode::OnPress])
574            halfAxes_[i].execute(KeybindMode::OnPress);
575        }
576        else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_)
577        {
578          halfAxes_[i].wasDown_ = false;
579          if (halfAxes_[i].nCommands_[KeybindMode::OnRelease])
580            halfAxes_[i].execute(KeybindMode::OnRelease);
581        }
582        if (halfAxes_[i].wasDown_)
583        {
584          if (halfAxes_[i].nCommands_[KeybindMode::OnHold])
585            halfAxes_[i].execute(KeybindMode::OnHold);
586        }
587        halfAxes_[i].hasChanged_ = false;
588      }
589
590      // these are the actually useful axis bindings for analog input AND output
591      if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_)
592      {
593        //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl;
594        halfAxes_[i].execute();
595      }
596    }
597
598    if (bDeriveMouseInput_)
599    {
600      if (deriveTime_ > derivePeriod_)
601      {
602        deriveTime_ = 0.0f;
603        //CCOUT(3) << "mouse abs: ";
604        for (int i = 0; i < 2; i++)
605        {
606          if (mouseRelative_[i] > 0)
607          {
608            halfAxes_[2*i + 0].absVal_ =  mouseRelative_[i] / derivePeriod_ / 5000 * mouseSensitivityDerived_;
609            halfAxes_[2*i + 1].absVal_ = 0.0f;
610          }
611          else if (mouseRelative_[i] < 0)
612          {
613            halfAxes_[2*i + 0].absVal_ = 0.0f;
614            halfAxes_[2*i + 1].absVal_ = -mouseRelative_[i] / derivePeriod_ / 5000 * mouseSensitivityDerived_;
615          }
616          else
617          {
618            halfAxes_[2*i + 0].absVal_ = 0.0f;
619            halfAxes_[2*i + 1].absVal_ = 0.0f;
620          }
621          //COUT(3) << mouseRelative_[i] << " | ";
622          mouseRelative_[i] = 0;
623        }
624        //COUT(3) << std::endl;
625      }
626      else
627        deriveTime_ += dt;
628    }
629
630    // execute all buffered bindings (addional parameter)
631    for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
632      paramCommandBuffer_[i]->execute();
633
634    // always reset the relative movement of the mouse
635    for (unsigned int i = 0; i < 8; i++)
636      halfAxes_[i].relVal_ = 0.0f;
637  }
638
639  void KeyBinder::keyPressed (const KeyEvent& evt)
640  { keys_[evt.key].execute(KeybindMode::OnPress); }
641
642  void KeyBinder::keyReleased(const KeyEvent& evt)
643  { keys_[evt.key].execute(KeybindMode::OnRelease); }
644
645  void KeyBinder::keyHeld    (const KeyEvent& evt)
646  { keys_[evt.key].execute(KeybindMode::OnHold); }
647
648
649  void KeyBinder::mouseButtonPressed (MouseButton::Enum id)
650  { mouseButtons_[id].execute(KeybindMode::OnPress); }
651
652  void KeyBinder::mouseButtonReleased(MouseButton::Enum id)
653  { mouseButtons_[id].execute(KeybindMode::OnRelease); }
654
655  void KeyBinder::mouseButtonHeld    (MouseButton::Enum id)
656  { mouseButtons_[id].execute(KeybindMode::OnHold); }
657
658
659  void KeyBinder::joyStickButtonPressed (int joyStickID, int button)
660  { joyStickButtons_[button].execute(KeybindMode::OnPress); }
661
662  void KeyBinder::joyStickButtonReleased(int joyStickID, int button)
663  { joyStickButtons_[button].execute(KeybindMode::OnRelease); }
664
665  void KeyBinder::joyStickButtonHeld    (int joyStickID, int button)
666  { joyStickButtons_[button].execute(KeybindMode::OnHold); }
667
668  /**
669    @brief Event handler for the mouseMoved Event.
670    @param e Mouse state information
671  */
672  void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
673  {
674    if (!bDeriveMouseInput_)
675    {
676      // y axis of mouse input is inverted
677      int rel[] = { rel_.x, -rel_.y };
678
679      //COUT(3) << rel[0] << " | " << rel[1] << std::endl;
680
681      for (int i = 0; i < 2; i++)
682      {
683        if (rel[i])
684        {
685          // absolute
686          if (mousePosition_[i] >= 0)
687          {
688            mousePosition_[i] += rel[i];
689            halfAxes_[0 + 2*i].hasChanged_ = true;
690            if (mousePosition_[i] < 0)
691            {
692              halfAxes_[1 + 2*i].hasChanged_ = true;
693              halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024 * mouseSensitivity_;
694              halfAxes_[0 + 2*i].absVal_ =  0.0f;
695            }
696            else
697              halfAxes_[0 + 2*i].absVal_ =  ((float)mousePosition_[i])/1024 * mouseSensitivity_;
698          }
699          else
700          {
701            mousePosition_[i] += rel[i];
702            halfAxes_[1 + 2*i].hasChanged_ = true;
703            if (mousePosition_[i] > 0)
704            {
705              halfAxes_[0 + 2*i].hasChanged_ = true;
706              halfAxes_[0 + 2*i].absVal_ =  ((float)mousePosition_[i])/1024 * mouseSensitivity_;
707              halfAxes_[1 + 2*i].absVal_ =  0.0f;
708            }
709            else
710              halfAxes_[1 + 2*i].absVal_ = -((float)mousePosition_[i])/1024 * mouseSensitivity_;
711          }
712          //COUT(3) << "half axis 0: " << halfAxes_[0].absVal_ << std::endl;
713          //COUT(3) << "half axis 1: " << halfAxes_[1].absVal_ << std::endl;
714          //COUT(3) << "half axis 2: " << halfAxes_[2].absVal_ << std::endl;
715          //COUT(3) << "half axis 3: " << halfAxes_[3].absVal_ << std::endl;
716
717          // relative
718          if (rel[i] > 0)
719            halfAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/1024 * mouseSensitivity_;
720          else
721            halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_;
722        }
723      }
724    }
725    else
726    {
727      mouseRelative_[0] += rel_.x;
728      mouseRelative_[1] -= rel_.y;
729    }
730  }
731
732  /**
733    @brief Event handler for the mouseScrolled Event.
734    @param e Mouse state information
735  */
736  void KeyBinder::mouseScrolled(int abs, int rel)
737  {
738    //COUT(3) << mouseButtons_[8].name_ << "   " << abs << " | " << rel << std::endl;
739
740    if (rel > 0)
741      for (int i = 0; i < rel/120; i++)
742        mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
743    else
744      for (int i = 0; i < -rel/120; i++)
745        mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
746  }
747
748  void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, int value)
749  {
750    // TODO: check whether 16 bit integer as general axis value is a good idea (works under windows)
751    int i = 8 + axis * 2;
752    if (value >= 0)
753    {
754      //if (value > 10000)
755      //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; }
756
757      halfAxes_[i].absVal_ = ((float)value)/0x8000;
758      halfAxes_[i].relVal_ = ((float)value)/0x8000;
759      halfAxes_[i].hasChanged_ = true;
760      if (halfAxes_[i + 1].absVal_ > 0)
761      {
762        halfAxes_[i + 1].absVal_ = -0.0f;
763        halfAxes_[i + 1].relVal_ = -0.0f;
764        halfAxes_[i + 1].hasChanged_ = true;
765      }
766    }
767    else
768    {
769      //if (value < -10000)
770      //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; }
771
772      halfAxes_[i + 1].absVal_ = -((float)value)/0x8000;
773      halfAxes_[i + 1].relVal_ = -((float)value)/0x8000;
774      halfAxes_[i + 1].hasChanged_ = true;
775      if (halfAxes_[i].absVal_ > 0)
776      {
777        halfAxes_[i].absVal_ = -0.0f;
778        halfAxes_[i].relVal_ = -0.0f;
779        halfAxes_[i].hasChanged_ = true;
780      }
781    }
782  }
783
784
785  // ###############################
786  // ###     GUIInputHandler     ###
787  // ###############################
788
789  ///**
790  //  @brief standard constructor
791  //*/
792  //GUIInputHandler::GUIInputHandler()
793  //{
794  //}
795
796  ///**
797  //  @brief Destructor
798  //*/
799  //GUIInputHandler::~GUIInputHandler()
800  //{
801  //}
802
803  ///**
804  //  @brief Event handler for the keyPressed Event.
805  //  @param e Event information
806  //*/
807  //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e)
808  //{
809    ////CEGUI::System::getSingleton().injectKeyDown( arg.key );
810    ////CEGUI::System::getSingleton().injectChar( arg.text );
811  //  return true;
812  //}
813
814  ///**
815  //  @brief Event handler for the keyReleased Event.
816  //  @param e Event information
817  //*/
818  //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e)
819  //{
820    ////CEGUI::System::getSingleton().injectKeyUp( arg.key );
821  //  return true;
822  //}
823
824  ///**
825  //  @brief Event handler for the mouseMoved Event.
826  //  @param e Event information
827  //*/
828  //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e)
829  //{
830    ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
831  //  return true;
832  //}
833
834  ///**
835  //  @brief Event handler for the mousePressed Event.
836  //  @param e Event information
837  //  @param id The ID of the mouse button
838  //*/
839  //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButton id)
840  //{
841    ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
842  //  return true;
843  //}
844
845  ///**
846  //  @brief Event handler for the mouseReleased Event.
847  //  @param e Event information
848  //  @param id The ID of the mouse button
849  //*/
850  //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButton id)
851  //{
852    ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
853  //  return true;
854  //}
855
856}
Note: See TracBrowser for help on using the repository browser.