| 1 |
/* |
| 2 |
** jstest-gtk - A graphical joystick tester |
| 3 |
** Copyright (C) 2009 Ingo Ruhnke <[email protected]> |
| 4 |
** |
| 5 |
** This program is free software: you can redistribute it and/or modify |
| 6 |
** it under the terms of the GNU General Public License as published by |
| 7 |
** the Free Software Foundation, either version 3 of the License, or |
| 8 |
** (at your option) any later version. |
| 9 |
** |
| 10 |
** This program is distributed in the hope that it will be useful, |
| 11 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
** GNU General Public License for more details. |
| 14 |
** |
| 15 |
** You should have received a copy of the GNU General Public License |
| 16 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <gtkmm/stock.h> |
| 21 |
|
| 22 |
#include "joystick.hpp" |
| 23 |
#include "calibrate_maximum_dialog.hpp" |
| 24 |
|
| 25 |
CalibrateMaximumDialog::CalibrateMaximumDialog(Joystick& joystick_) |
| 26 |
: Gtk::Dialog("CalibrationWizard: " + joystick_.get_name()), |
| 27 |
joystick(joystick_), |
| 28 |
orig_data(joystick.get_calibration()), |
| 29 |
label("1) Rotate your joystick around to move all axis into their extreme positions at least once\n" |
| 30 |
"2) Move all axis back to the center\n" |
| 31 |
"3) Press ok\n") |
| 32 |
{ |
| 33 |
joystick.clear_calibration(); |
| 34 |
|
| 35 |
set_border_width(5); |
| 36 |
label.set_line_wrap(); |
| 37 |
get_vbox()->add(label); |
| 38 |
add_button(Gtk::Stock::CANCEL, 1); |
| 39 |
add_button(Gtk::Stock::OK, 0); |
| 40 |
|
| 41 |
connection = joystick.axis_move.connect(sigc::mem_fun(this, &CalibrateMaximumDialog::on_axis_move)); |
| 42 |
|
| 43 |
is_init_axis_state.resize(joystick.get_axis_count(), false); |
| 44 |
min_axis_state.resize(joystick.get_axis_count()); |
| 45 |
max_axis_state.resize(joystick.get_axis_count()); |
| 46 |
|
| 47 |
for(int i = 0; i < joystick.get_axis_count(); ++i) |
| 48 |
{ |
| 49 |
min_axis_state[i] = joystick.get_axis_state(i); |
| 50 |
max_axis_state[i] = joystick.get_axis_state(i); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
CalibrateMaximumDialog::on_response(int v) |
| 56 |
{ |
| 57 |
if (v == 0) |
| 58 |
{ |
| 59 |
// Calculate CalibrationData |
| 60 |
std::vector<Joystick::CalibrationData> data; |
| 61 |
|
| 62 |
for(int i = 0; i < joystick.get_axis_count(); ++i) |
| 63 |
{ |
| 64 |
Joystick::CalibrationData axis; |
| 65 |
axis.calibrate = true; |
| 66 |
axis.invert = false; |
| 67 |
axis.center_min = axis.center_max = joystick.get_axis_state(i); |
| 68 |
axis.range_min = min_axis_state[i]; |
| 69 |
axis.range_max = max_axis_state[i]; |
| 70 |
|
| 71 |
// When the center is the same as the outer edge of an axis, |
| 72 |
// we assume its a throttle control or analog button and |
| 73 |
// calculate the center on our own |
| 74 |
if (axis.center_min == axis.range_min || |
| 75 |
axis.center_max == axis.range_max) |
| 76 |
{ |
| 77 |
axis.center_min = axis.center_max = (axis.range_min + axis.range_max)/2; |
| 78 |
} |
| 79 |
|
| 80 |
data.push_back(axis); |
| 81 |
} |
| 82 |
|
| 83 |
joystick.set_calibration(data); |
| 84 |
|
| 85 |
hide(); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
joystick.set_calibration(orig_data); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
CalibrateMaximumDialog::on_axis_move(int id, int value) |
| 95 |
{ |
| 96 |
// std::cout << "AxisMove: " << id << " " << value << std::endl; |
| 97 |
if (!is_init_axis_state[id]) |
| 98 |
{ |
| 99 |
min_axis_state[id] = value; |
| 100 |
max_axis_state[id] = value; |
| 101 |
is_init_axis_state[id] = true; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
min_axis_state[id] = std::min(value, min_axis_state[id]); |
| 106 |
max_axis_state[id] = std::max(value, max_axis_state[id]); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* EOF */ |