[go: up one dir, main page]

Menu

[8286ad]: / world / Controls.hpp  Maximize  Restore  History

Download this file

108 lines (91 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
// Controls.h - a class for handling control events.
//
// Copyright (C) 2003 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#ifndef _CONTROLS_H_
#define _CONTROLS_H_
#include "../geometry/Constants.hpp"
#include <map>
#include <vector>
namespace Vamos_World
{
struct Calibration
{
Calibration(bool neg = true, bool pos = true, double fact = 1.0, double off = 0.0,
double dead = 0.0, double upper = 0.0)
: negative(neg), positive(pos), factor(fact), offset(off), deadband(dead),
upper_deadband(upper){};
bool negative;
bool positive;
double factor;
double offset;
double deadband;
double upper_deadband;
};
class Control_Handler;
/// The callback function pointer type
using Callback_Function = bool (Control_Handler::*)(double, double);
// A class for managing callbacks
class Callback_List
{
public:
void add(int index, Control_Handler* object, Callback_Function function,
const Calibration& calibration, double argument = 0.0);
void call(int index, double value);
private:
struct Callback
{
Callback(int i, Control_Handler* obj, Callback_Function func, const Calibration& cal,
double arg = 0.0);
int index;
Control_Handler* object;
Callback_Function function;
Calibration calibration;
double argument;
double transform(double value) const;
};
std::vector<Callback> m_callbacks;
};
class Control
{
public:
void bind_action(int index, Vamos_Geometry::Direction direction, Control_Handler* object,
Callback_Function function, double time);
void bind_motion(int axis, Vamos_Geometry::Direction direction,
Control_Handler* object, Callback_Function func, double factor,
double offset, double deadband, double upper_deadband);
void move(int axis, int position);
void press(int index);
void release(int index);
void set_axis_range(int axis, int low_raw_value, int high_raw_value);
protected:
double transform(int axis, int value) const;
private:
Callback_List m_press_callbacks;
Callback_List m_release_callbacks;
Callback_List m_motion_callbacks;
std::map<int, std::pair<int, int>> m_ranges;
};
/// The base class for classes that can set control callbacks
struct Control_Handler
{
Control joystick;
Control keyboard;
Control mouse;
};
} // namespace Vamos_World
#endif // not _CONTROLS_H_