[go: up one dir, main page]

Menu

[2d94f2]: / world / Controls.cpp  Maximize  Restore  History

Download this file

124 lines (107 with data), 4.3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Controls.cc - 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/>.
#include "Controls.hpp"
#include "../geometry/Numeric.hpp"
using namespace Vamos_Geometry;
using namespace Vamos_World;
const int RAW_MOTION_RANGE = 32767;
// SDL reports joystick positions from -32768 to 32767. We'll pretend
// it's -32767 to 32767.
//-----------------------------------------------------------------------------
Callback_List::Callback::Callback(int i, Control_Handler *obj, Callback_Function func,
const Calibration &cal, double arg)
: index(i),
object(obj),
function(func),
calibration(cal),
argument(arg)
{
}
double Callback_List::Callback::transform(double value) const
{
// Handle half-range controls.
if (!calibration.negative)
value = std::max(value, 0.0);
if (!calibration.positive)
value = std::min(value, 0.0);
// Handle deadbands.
if (std::abs(value) < calibration.deadband)
return calibration.offset;
if (value < -1.0 + calibration.upper_deadband)
return -calibration.factor + calibration.offset;
if (value > 1.0 - calibration.upper_deadband)
return calibration.factor + calibration.offset;
double m = calibration.factor / (1.0 - calibration.upper_deadband - calibration.deadband);
double b = calibration.offset - sign(value) * m * calibration.deadband;
return m * value + b;
}
void Callback_List::add(int index, Control_Handler *object, Callback_Function function,
const Calibration &calibration, double argument)
{
m_callbacks.push_back(Callback(index, object, function, calibration, argument));
}
void Callback_List::call(int index, double value)
{
for (const auto& cb : m_callbacks)
if (index == cb.index)
if (((cb.object)->*(cb.function))(cb.transform(value), cb.argument))
break;
}
//-----------------------------------------------------------------------------
void Control::bind_action(int index, Direction direction, Control_Handler *object,
Callback_Function function, double time)
{
if (direction == UP)
m_release_callbacks.add(index, object, function, Calibration(), time);
else
m_press_callbacks.add(index, object, function, Calibration(), time);
}
void Control::bind_motion(int axis, Direction direction, Control_Handler *object,
Callback_Function function, double factor, double offset,
double deadband, double upper_deadband)
{
bool negative = direction != FORWARD && direction != RIGHT;
bool positive = direction != BACKWARD && direction != LEFT;
m_motion_callbacks.add(
axis, object, function,
Calibration(negative, positive, factor, offset, deadband, upper_deadband));
if (m_ranges.find(axis) == m_ranges.end())
set_axis_range(axis, -RAW_MOTION_RANGE, RAW_MOTION_RANGE);
}
void Control::press(int index)
{
m_press_callbacks.call(index, 1.0);
}
void Control::release(int index)
{
m_release_callbacks.call(index, 0.0);
}
void Control::move(int axis, int position)
{
m_motion_callbacks.call(axis, transform(axis, position));
}
void Control::set_axis_range(int axis, int low_raw_value, int high_raw_value)
{
m_ranges[static_cast<size_t>(axis)] = std::pair<int, int>(low_raw_value, high_raw_value);
}
double Control::transform(int axis, int value) const
{
std::pair<int, int> range = m_ranges.find(axis)->second;
return 1.0 - 2.0 * (value - range.first) / (range.second - range.first);
}