Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1118


Ignore:
Timestamp:
Apr 20, 2008, 4:31:03 PM (16 years ago)
Author:
rgrieder
Message:
  • classed FloatParser to ExprParser class
Location:
code/branches/input
Files:
6 edited
2 moved

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/core/InputHandler.cc

    r1112 r1118  
    7474    this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "setInputMode " + getConvertedValue<int, std::string>(IM_KEYBOARD);
    7575    this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
     76    this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
    7677    return true;
    7778  }
     
    8384  bool InputHandlerGame::keyPressed(const OIS::KeyEvent &e)
    8485  {
     86    this->keysDown_.push_back(e.key);
    8587    // find the appropriate key binding
    8688    std::string cmdStr = bindingsKeyPress_[int(e.key)];
     
    99101  bool InputHandlerGame::keyReleased(const OIS::KeyEvent &e)
    100102  {
     103    // remove the key from the keysDown_ list
     104    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
     105    {
     106      if (*it == e.key)
     107      {
     108        keysDown_.erase(it);
     109        break;
     110      }
     111    }
     112
    101113    // find the appropriate key binding
    102114    std::string cmdStr = bindingsKeyRelease_[int(e.key)];
     
    136148  {
    137149    return true;
     150  }
     151
     152  /**
     153    @brief Tick method to do additional calculations.
     154    @param dt Delta time.
     155  */
     156  void InputHandlerGame::tick(float dt)
     157  {
     158    // iterate through all the pressed keys
     159    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
     160    {
     161      // find the appropriate key binding
     162      std::string cmdStr = bindingsKeyHold_[*it];
     163      if (cmdStr != "")
     164      {
     165        CommandExecutor::execute(cmdStr);
     166        COUT(3) << "Executing command: " << cmdStr << std::endl;
     167      }
     168    }
    138169  }
    139170
     
    225256  }
    226257
     258  /**
     259    @brief Tick method to do additional calculations.
     260    @param dt Delta time.
     261  */
     262  void InputHandlerGUI::tick(float dt)
     263  {
     264   
     265  }
     266
    227267}
  • code/branches/input/src/core/InputHandler.h

    r1112 r1118  
    3838
    3939#include <string>
     40#include <list>
    4041#include <OIS/OIS.h>
    4142
     
    4849    enum KeybindSetting
    4950    {
    50       none,
    51       onPress,
    52       onRelease,
    53       continuous,
     51      None,
     52      OnPress,
     53      OnRelease,
     54      Continuous,
    5455    };
    5556  }
     
    5859      : public OIS::KeyListener, public OIS::MouseListener
    5960  {
     61    virtual void tick(float dt) = 0;
    6062  };
    6163   
     
    8082                bool keyReleased  (const OIS::KeyEvent   &arg);
    8183
     84    void tick(float dt);
     85
    8286    // temporary hack
    8387    void callListeners(InputEvent &evt);
     88
     89    //! Stores all the keys that are down
     90    std::list<OIS::KeyCode> keysDown_;
    8491
    8592    /** denotes the maximum number of different keys there are in OIS.
     
    114121    ~InputHandlerGUI();
    115122
     123    void tick(float dt);
     124
    116125  private:
    117126    // input events
  • code/branches/input/src/core/InputManager.cc

    r1112 r1118  
    207207    if (mouse_)
    208208      mouse_->capture();
    209 
    210209    if (keyboard_)
    211210      keyboard_->capture();
     211
     212    // Give the listeners the chance to do additional calculations
    212213  }
    213214
  • code/branches/input/src/orxonox/Orxonox.cc

    r1112 r1118  
    5454//#include "util/Sleep.h"
    5555#include "util/ArgReader.h"
    56 #include "util/FloatParser.h"
     56#include "util/ExprParser.h"
    5757
    5858// core
     
    130130    static void calculate(std::string calculation)
    131131    {
    132       char** useless = 0;
    133       double result = 0.0;
    134       bool success = parse_float((char* const)calculation.c_str(), useless, &result);
    135       if (success)
    136       {
    137         if (result == 42.0)
     132      ExprParser expr(calculation);
     133      if (expr.getSuccess())
     134      {
     135        if (expr.getResult() == 42.0)
    138136          std::cout << "Greetings from the restaurant at the end of the universe." << std::endl;
    139         std::cout << "Result is: " << result << std::endl;
     137        // FIXME: insert modifier to display in full precision
     138        std::cout << "Result is: " << expr.getResult() << std::endl;
     139        if (expr.getRemains() != "")
     140          std::cout << "Warning: Expression could not be parsed to the end! Remains: '"
     141              << expr.getRemains() << "'" << std::endl;
    140142      }
    141143      else
  • code/branches/input/src/util/CMakeLists.txt

    r1114 r1118  
    77  Clipboard.cc
    88  SubString.cc
    9   FloatParser.cc
     9  ExprParser.cc
    1010  MultiTypePrimitive.cc
    1111  MultiTypeString.cc
  • code/branches/input/src/util/ExprParser.cc

    r1114 r1118  
    1 //FloatParser.cpp
    2 
    3 #include "FloatParser.h"
    4 #include <string>
     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 Declaration of FloatParser
     32*/
     33
     34#include "ExprParser.h"
    535#include <cmath>
    636#include <cstring>
    737
    8 using namespace std;
    9 
    10 //Makros, um Funktionen einfacher parser zu können
     38// macros for easier if, else statements
    1139#define CASE_1(var) if (!strcmp(SWITCH,var))
    1240#define CASE(var) else if (!strcmp(SWITCH,var))
    1341#define CASE_ELSE else
    1442
     43//! skip white spaces
    1544#define PARSE_BLANKS while (*reading_stream == ' ') ++reading_stream;
    1645
    17 //static enumerations and variables
    18 static enum binary_operator { b_plus, b_minus, mal, durch, modulo, hoch, undef, oder, und, gleich, b_nicht, kleiner, groesser, ungleich, kleinergleich, groessergleich};
    19 static enum unary_operator { u_plus, u_minus, u_nicht };
    20 static binary_operator op;
    21 static char* reading_stream;
    22 static bool parse_float_failed = false;
    23 
    24 //static funtions
    25 static double parse_expr_1();
    26 static double parse_expr_2();
    27 static double parse_expr_3();
    28 static double parse_expr_4();
    29 static double parse_expr_5();
    30 static double parse_expr_6();
    31 static double parse_expr_7();
    32 static double parse_expr_8();
    33 static inline char* parse_word(char* str);
    34 static inline binary_operator parse_binary_operator();
    35 static inline unary_operator parse_unary_operator();
    36 static double parse_argument();
    37 static double parse_last_argument();
    38 
    39 //Public functions:
    40 /******************/
    41 bool parse_float(char* const string, char **endptr, double* result)
    42 {
    43    parse_float_failed = false;
    44    reading_stream = string;
    45    double value = parse_expr_8();
    46    if ( !parse_float_failed && (
    47       *reading_stream == ')' || \
    48       *reading_stream == '}' || \
    49       *reading_stream == ']' || \
    50       *reading_stream == ',' || \
    51       *reading_stream == ';' || \
    52       *reading_stream == '_' || \
    53       *reading_stream == '\0' || \
    54       *reading_stream > 64 && *reading_stream < 91 || \
    55       *reading_stream > 96 && *reading_stream < 123))
    56    {
    57       endptr = &reading_stream;
    58       *result = value;
    59       return true;
    60    }
    61    else
    62    {
    63      *result = 0;
    64      return false;
    65    }
    66 }
    67 
    68 bool parse_float(char* const string, char** endptr, char delimiter, double* result)
    69 {
    70    parse_float_failed = false;
    71    reading_stream = string;
    72    double value = parse_expr_8();
    73    if (*reading_stream == delimiter && !parse_float_failed)
    74    {
    75       endptr = &reading_stream;
    76       *result = value;
    77       return true;
    78    }
    79    else
    80    {
    81      *result = 0;
    82      return false;
    83    }
    84 }
    85 
    86 bool parse_vector_float(char* const string, char** endptr, bool last_float, double* result)
    87 {
    88    parse_float_failed = false;
    89    reading_stream = string;
    90    double value = parse_expr_4();
    91    if (last_float)
    92    {
    93       if (*reading_stream == '>')
    94       {
    95          endptr = &reading_stream;
    96          *result = value;
    97       }
    98       else
    99         parse_float_failed = true;
    100    }
    101    else
    102    {
    103       if (*reading_stream == ',')
    104       {
    105          *endptr = reading_stream;
    106          *result = value;
    107       }
    108       else
    109         parse_float_failed = true;
    110    }
    111    if (parse_float_failed)
    112    {
    113      *result = 0;
    114      return false;
    115    }
    116    else
    117      return true;
     46ExprParser::ExprParser(std::string& str)
     47{
     48  this->failed_ = false;
     49  this->reading_stream = str.c_str();
     50  if (str.size() == 0 || *reading_stream == '\0')
     51  {
     52    this->failed_ = true;
     53    this->result_ = 0.0;
     54  }
     55  else
     56  {
     57    this->result_ = parse_expr_8();
     58    this->remains_ = reading_stream;
     59  }
    11860}
    11961
    12062//Private functions:
    12163/******************/
    122 static double parse_argument()
     64double ExprParser::parse_argument()
    12365{
    12466   double value = parse_expr_8();
     
    13072   else
    13173   {
    132      parse_float_failed = true;
     74     this->failed_ = true;
    13375     return 0;
    13476   }
    13577}
    13678
    137 static double parse_last_argument()
     79double ExprParser::parse_last_argument()
    13880{
    13981   double value = parse_expr_8();
     
    14587   else
    14688   {
    147      parse_float_failed = true;
     89     this->failed_ = true;
    14890     return 0;
    14991   }
    15092}
    15193
    152 static double parse_expr_8()
     94double ExprParser::parse_expr_8()
    15395{
    15496   double value = parse_expr_7();
     
    166108
    167109
    168 static double parse_expr_7()
     110double ExprParser::parse_expr_7()
    169111{
    170112   double value = parse_expr_6();
     
    181123}
    182124
    183 static double parse_expr_6()
     125double ExprParser::parse_expr_6()
    184126{
    185127   double value = parse_expr_5();
     
    200142}
    201143
    202 static double parse_expr_5()
     144double ExprParser::parse_expr_5()
    203145{
    204146   double value = parse_expr_4();
     
    225167}
    226168
    227 static double parse_expr_4()
     169double ExprParser::parse_expr_4()
    228170{
    229171   double value = parse_expr_3();
     
    244186}
    245187
    246 static double parse_expr_3()
     188double ExprParser::parse_expr_3()
    247189{
    248190   double value = parse_expr_2();
     
    269211}
    270212
    271 static double parse_expr_2()
     213double ExprParser::parse_expr_2()
    272214{
    273215   double value = parse_expr_1();
     
    288230}
    289231
    290 static double parse_expr_1()
     232double ExprParser::parse_expr_1()
    291233{
    292234   PARSE_BLANKS
     
    299241   {
    300242     // end of string
    301      parse_float_failed = true;
     243     this->failed_ = true;
    302244     return 0;
    303245   }
    304246   else if (*reading_stream > 47 && *reading_stream < 59 || *reading_stream == 46)
    305    {  //Zahl
    306       value = strtod(reading_stream,&reading_stream);
     247   {  // number
     248      value = strtod(reading_stream, const_cast<char**>(&reading_stream));
    307249   }
    308250   else if (*reading_stream > 64 && *reading_stream < 91 || *reading_stream > 96 && *reading_stream < 123 || *reading_stream == 46)
    309    {  //Variable oder Funktion
     251   {  // variable or function
    310252      char* word = new char[256];
    311253      parse_word(word);
     
    386328            value = floor(parse_argument()/parse_last_argument());
    387329         CASE("max")
    388             value = max(parse_argument(),parse_last_argument());
     330           value = std::max(parse_argument(),parse_last_argument());
    389331         CASE("min")
    390             value = min(parse_argument(),parse_last_argument());
     332           value = std::min(parse_argument(),parse_last_argument());
    391333         CASE_ELSE
    392334         {
    393            parse_float_failed = true;
     335           this->failed_ = true;
     336           delete[] word;
    394337           return 0;
    395338         }
     
    397340      else
    398341      {
    399          //TODO: Variablen-Liste durchsuchen
    400          //Momentaner Ersatzwert für jede Variable:
    401          //value = 0;
    402          parse_float_failed = true;
    403          return 0;
     342#define SWITCH word
     343         CASE_1("pi")
     344           value = 3.1415926535897932;
     345         CASE("e")
     346           value = 2.7182818284590452;
     347         CASE_ELSE
     348         {
     349           this->failed_ = true;
     350           delete[] word;
     351           return 0;
     352         }
    404353      }
    405354      delete[] word;
    406355   }
    407356   else if (*reading_stream == 40)
    408    {  //Audruck in Klammern
     357   {  // expresion in paranthesis
    409358      ++reading_stream;
    410359      value = parse_last_argument();
     
    412361   else
    413362   {
    414      parse_float_failed = true;
     363     this->failed_ = true;
    415364     return 0;
    416365   }
     
    424373      default:
    425374        {
    426           parse_float_failed = true;
     375          this->failed_ = true;
    427376          return 0;
    428377        }
     
    430379}
    431380
    432 static inline char* parse_word(char* str)
     381char* ExprParser::parse_word(char* str)
    433382{
    434383   char* word = str;
     
    440389      if (counter > 255)
    441390      {
    442         parse_float_failed = true;
     391        this->failed_ = true;
    443392        return '\0';
    444393      }
     
    448397}
    449398
    450 static inline binary_operator parse_binary_operator()
     399ExprParser::binary_operator ExprParser::parse_binary_operator()
    451400{
    452401   binary_operator op;
     
    484433}
    485434
    486 static inline unary_operator parse_unary_operator()
     435ExprParser::unary_operator ExprParser::parse_unary_operator()
    487436{
    488437   switch (*reading_stream)
  • code/branches/input/src/util/ExprParser.h

    r1113 r1118  
    1 //FloatParser.h
     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 Declaration of FloatParser
     32*/
    233
    334#ifndef _FloatParser_H__
     
    536
    637#include "UtilPrereqs.h"
     38#include <string>
     39
     40class _UtilExport ExprParser
     41{
     42public:
     43  enum binary_operator
     44  {
     45    b_plus,
     46    b_minus,
     47    mal,
     48    durch,
     49    modulo,
     50    hoch,
     51    undef,
     52    oder,
     53    und,
     54    gleich,
     55    b_nicht,
     56    kleiner,
     57    groesser,
     58    ungleich,
     59    kleinergleich,
     60    groessergleich
     61  };
     62
     63  enum unary_operator
     64  {
     65    u_plus,
     66    u_minus,
     67    u_nicht
     68  };
     69
     70
     71  ExprParser(std::string& str);
     72  std::string& getRemains() { return  this->remains_; }
     73  double       getResult()  { return  this->result_; }
     74  bool         getSuccess() { return !this->failed_; }
     75
     76private:
     77  double parse_expr_1();
     78  double parse_expr_2();
     79  double parse_expr_3();
     80  double parse_expr_4();
     81  double parse_expr_5();
     82  double parse_expr_6();
     83  double parse_expr_7();
     84  double parse_expr_8();
     85  char* parse_word(char* str);
     86  binary_operator parse_binary_operator();
     87  unary_operator parse_unary_operator();
     88
     89  double parse_argument();
     90  double parse_last_argument();
     91
     92  binary_operator op;
     93  const char* reading_stream;
     94  bool failed_;
     95  double result_;
     96  std::string remains_;
     97
     98};
    799
    8100//Endzeichen für float expression: ')', '}', ']', ',', ';'
  • code/branches/input/visual_studio/vc8/util.vcproj

    r1112 r1118  
    157157                        </File>
    158158                        <File
    159                                 RelativePath="..\..\src\util\FloatParser.cpp"
     159                                RelativePath="..\..\src\util\ExprParser.cc"
    160160                                >
    161161                        </File>
     
    203203                        </File>
    204204                        <File
    205                                 RelativePath="..\..\src\util\FloatParser.h"
     205                                RelativePath="..\..\src\util\ExprParser.h"
    206206                                >
    207207                        </File>
Note: See TracChangeset for help on using the changeset viewer.