| 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 "remap_widget.hpp" |
| 24 |
|
| 25 |
class RemapWidgetColumns : public Gtk::TreeModel::ColumnRecord |
| 26 |
{ |
| 27 |
public: |
| 28 |
static RemapWidgetColumns* instance_; |
| 29 |
|
| 30 |
public: |
| 31 |
static RemapWidgetColumns& instance() { |
| 32 |
if (instance_) |
| 33 |
return *instance_; |
| 34 |
else |
| 35 |
return *(instance_ = new RemapWidgetColumns()); |
| 36 |
} |
| 37 |
|
| 38 |
Gtk::TreeModelColumn<int> id; |
| 39 |
Gtk::TreeModelColumn<std::string> name; |
| 40 |
|
| 41 |
private: |
| 42 |
RemapWidgetColumns() { |
| 43 |
add(id); |
| 44 |
add(name); |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
RemapWidgetColumns* RemapWidgetColumns::instance_ = 0; |
| 49 |
|
| 50 |
RemapWidget::RemapWidget(Joystick& joystick_, Mode mode_) |
| 51 |
: joystick(joystick_), |
| 52 |
mode(mode_) |
| 53 |
{ |
| 54 |
map_list = Gtk::ListStore::create(RemapWidgetColumns::instance()); |
| 55 |
treeview.set_model(map_list); |
| 56 |
if (mode == REMAP_AXIS) |
| 57 |
treeview.append_column("Axes", RemapWidgetColumns::instance().name); |
| 58 |
else |
| 59 |
treeview.append_column("Buttons", RemapWidgetColumns::instance().name); |
| 60 |
|
| 61 |
set_border_width(5); |
| 62 |
treeview.set_border_width(5); |
| 63 |
|
| 64 |
scroll.add(treeview); |
| 65 |
scroll.set_size_request(-1, 300); |
| 66 |
scroll.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
| 67 |
pack_start(scroll, Gtk::PACK_EXPAND_WIDGET); |
| 68 |
|
| 69 |
treeview.set_reorderable(); |
| 70 |
|
| 71 |
map_list->signal_row_inserted().connect(sigc::mem_fun(this, &RemapWidget::on_my_row_inserted)); |
| 72 |
map_list->signal_row_deleted().connect(sigc::mem_fun(this, &RemapWidget::on_my_row_deleted)); |
| 73 |
map_list->signal_rows_reordered().connect(sigc::mem_fun(this, &RemapWidget::on_my_rows_reordered)); |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
RemapWidget::add_entry(int id, const std::string& str) |
| 78 |
{ |
| 79 |
Gtk::ListStore::iterator it = map_list->append(); |
| 80 |
(*it)[RemapWidgetColumns::instance().id] = id; |
| 81 |
(*it)[RemapWidgetColumns::instance().name] = str; |
| 82 |
} |
| 83 |
|
| 84 |
struct RemapEntry { |
| 85 |
int id; |
| 86 |
std::string name; |
| 87 |
|
| 88 |
bool operator<(const RemapEntry& rhs) const { |
| 89 |
return id < rhs.id; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
void |
| 94 |
RemapWidget::on_clear() |
| 95 |
{ |
| 96 |
// We simple sort the list here by 'id' |
| 97 |
|
| 98 |
// Convert the ListStore into a vector |
| 99 |
std::vector<RemapEntry> rows; |
| 100 |
for(Gtk::TreeIter i = map_list->children().begin(); i != map_list->children().end(); ++i) |
| 101 |
{ |
| 102 |
RemapEntry entry; |
| 103 |
entry.id = (*i)[RemapWidgetColumns::instance().id]; |
| 104 |
entry.name = (*i)[RemapWidgetColumns::instance().name]; |
| 105 |
rows.push_back(entry); |
| 106 |
} |
| 107 |
|
| 108 |
// Sort the vector |
| 109 |
std::sort(rows.begin(), rows.end()); |
| 110 |
|
| 111 |
// Renter the vector into the liststore |
| 112 |
map_list->clear(); |
| 113 |
for(std::vector<RemapEntry>::iterator i = rows.begin(); i != rows.end(); ++i) |
| 114 |
{ |
| 115 |
add_entry(i->id, i->name); |
| 116 |
} |
| 117 |
|
| 118 |
on_apply(); |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
RemapWidget::on_apply() |
| 123 |
{ |
| 124 |
std::vector<int> mapping; |
| 125 |
std::vector<int> mapping_old; |
| 126 |
for(Gtk::TreeIter i = map_list->children().begin(); i != map_list->children().end(); ++i) |
| 127 |
{ |
| 128 |
mapping.push_back((*i)[RemapWidgetColumns::instance().id]); |
| 129 |
} |
| 130 |
|
| 131 |
if (mode == REMAP_AXIS) |
| 132 |
{ |
| 133 |
mapping_old = joystick.get_axis_mapping(); |
| 134 |
joystick.set_axis_mapping(mapping); |
| 135 |
joystick.correct_calibration(mapping_old, mapping); |
| 136 |
} |
| 137 |
else if (mode == REMAP_BUTTON) |
| 138 |
{ |
| 139 |
joystick.set_button_mapping(mapping); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
RemapWidget::on_my_rows_reordered(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter, int* new_order) |
| 145 |
{ |
| 146 |
// std::cout << "on_my_rows_reordered" << std::endl; |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
RemapWidget::on_my_row_inserted(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter) |
| 151 |
{ |
| 152 |
// std::cout << "on_my_rows_inserted" << std::endl; |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
RemapWidget::on_my_row_deleted(const Gtk::TreeModel::Path& path) |
| 157 |
{ |
| 158 |
// std::cout << "on_my_rows_deleted" << std::endl; |
| 159 |
|
| 160 |
if (mode == REMAP_AXIS) |
| 161 |
{ |
| 162 |
if (joystick.get_axis_count() == (int)map_list->children().size()) |
| 163 |
{ |
| 164 |
on_apply(); |
| 165 |
} |
| 166 |
} |
| 167 |
else if (mode == REMAP_BUTTON) |
| 168 |
{ |
| 169 |
if (joystick.get_button_count() == (int)map_list->children().size()) |
| 170 |
{ |
| 171 |
on_apply(); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* EOF */ |