Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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