1 // Controls.h - a class for handling control events. 2 // 3 // Vamos Automotive Simulator 4 // Copyright (C) 2003 Sam Varner 5 // 6 // This program is free software; you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation; either version 2 of the License, or 9 // (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 #ifndef _CONTROLS_H_ 21 #define _CONTROLS_H_ 22 23 #include "../geometry/Constants.h" 24 25 #include <vector> 26 #include <map> 27 #include <iostream> 28 29 namespace Vamos_World 30 { 31 class Control; 32 33 // The base class for classes that can set control callbacks 34 class Control_Handler 35 { 36 public: 37 virtual Control& joystick () = 0; 38 virtual Control& keyboard () = 0; 39 virtual Control& mouse () = 0; 40 }; 41 42 // The callback function pointer type 43 typedef bool (Control_Handler::* Callback_Function) (double, double); 44 45 struct Calibration 46 { 47 Calibration (bool neg = true, 48 bool pos = true, 49 double fact = 1.0, 50 double off = 0.0, 51 double dead = 0.0, 52 double upper = 0.0) negativeCalibration53 : negative (neg), 54 positive (pos), 55 factor (fact), 56 offset (off), 57 deadband (dead), 58 upper_deadband (upper) 59 {}; 60 61 bool negative; 62 bool positive; 63 double factor; 64 double offset; 65 double deadband; 66 double upper_deadband; 67 }; 68 69 // A class for managing callbacks 70 class Callback_List 71 { 72 public: 73 void add (int index, 74 Control_Handler* object, 75 Callback_Function function, 76 const Calibration& calibration, 77 double argument = 0.0); 78 void call (int index, double value); 79 80 private: 81 struct Callback 82 { 83 Callback (int i, 84 Control_Handler* obj, 85 Callback_Function func, 86 const Calibration& cal, 87 double arg = 0.0); 88 89 int index; 90 Control_Handler* object; 91 Callback_Function function; 92 Calibration calibration; 93 double argument; 94 95 double transform (double value) const; 96 }; 97 std::vector <Callback> m_callbacks; 98 }; 99 100 class Control 101 { 102 public: 103 void bind_action (int index, 104 Vamos_Geometry::Direction direction, 105 Control_Handler* object, 106 Callback_Function function, 107 double time); 108 109 void bind_motion (int axis, 110 Vamos_Geometry::Direction direction, 111 Control_Handler* object, 112 Callback_Function func, 113 double factor, 114 double offset, 115 double deadband, 116 double upper_deadband); 117 118 void move (int axis, int position); 119 void press (int index); 120 void release (int index); 121 122 void set_axis_range (int axis, int low_raw_value, int high_raw_value); 123 124 protected: 125 double transform (int axis, int value) const; 126 127 private: 128 Callback_List m_press_callbacks; 129 Callback_List m_release_callbacks; 130 Callback_List m_motion_callbacks; 131 std::map <int, std::pair <int, int> > m_ranges; 132 }; 133 } 134 135 #endif // not _CONTROLS_H_ 136