Added StaticModifier to count number of button presses
This commit is contained in:
parent
bfe45b0497
commit
e22f72e97b
4 changed files with 144 additions and 1 deletions
6
NEWS
6
NEWS
|
@ -1,3 +1,9 @@
|
|||
xboxdrv 0.7.2 - (??/Feb/2011)
|
||||
=============================
|
||||
|
||||
* added statistic modifier that counts how often buttons are pressed
|
||||
|
||||
|
||||
xboxdrv 0.7.1 - (30/Jan/2011)
|
||||
=============================
|
||||
|
||||
|
|
|
@ -22,8 +22,9 @@
|
|||
|
||||
#include "modifier/dpad_rotation_modifier.hpp"
|
||||
#include "modifier/four_way_restrictor_modifier.hpp"
|
||||
#include "modifier/square_axis_modifier.hpp"
|
||||
#include "modifier/rotate_axis_modifier.hpp"
|
||||
#include "modifier/square_axis_modifier.hpp"
|
||||
#include "modifier/statistic_modifier.hpp"
|
||||
|
||||
Modifier*
|
||||
Modifier::from_string(const std::string& name, const std::string& value)
|
||||
|
@ -61,6 +62,10 @@ Modifier::from_string(const std::string& name, const std::string& value)
|
|||
{
|
||||
return RotateAxisModifier::from_string(args);
|
||||
}
|
||||
else if (name == "stat" || name == "statistic")
|
||||
{
|
||||
return StatisticModifier::from_string(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("unknown modifier: " + name);
|
||||
|
|
82
src/modifier/statistic_modifier.cpp
Normal file
82
src/modifier/statistic_modifier.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
** Xbox360 USB Gamepad Userspace Driver
|
||||
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
|
||||
**
|
||||
** This program 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.
|
||||
**
|
||||
** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "statistic_modifier.hpp"
|
||||
|
||||
#include <boost/format.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "../xboxmsg.hpp"
|
||||
|
||||
StatisticModifier*
|
||||
StatisticModifier::from_string(const std::vector<std::string>& args)
|
||||
{
|
||||
return new StatisticModifier;
|
||||
}
|
||||
|
||||
StatisticModifier::StatisticModifier() :
|
||||
m_button_state(XBOX_BTN_MAX),
|
||||
m_press_count(XBOX_BTN_MAX)
|
||||
{
|
||||
}
|
||||
|
||||
StatisticModifier::~StatisticModifier()
|
||||
{
|
||||
print_stats();
|
||||
}
|
||||
|
||||
void
|
||||
StatisticModifier::print_stats()
|
||||
{
|
||||
std::cout << "Button Press Statistics\n"
|
||||
<< "=======================\n\n";
|
||||
|
||||
std::cout << boost::format("%12s | %5d") % "Name" % "Count" << std::endl;
|
||||
std::cout << "-------------+---------" << std::endl;
|
||||
for(int btn = 1; btn < XBOX_BTN_MAX; ++btn)
|
||||
{
|
||||
std::cout << boost::format("%12s : %5d")
|
||||
% btn2string(static_cast<XboxButton>(btn)) % m_press_count[btn]
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
StatisticModifier::update(int msec_delta, XboxGenericMsg& msg)
|
||||
{
|
||||
for(int btn = 1; btn < static_cast<int>(XBOX_BTN_MAX); ++btn)
|
||||
{
|
||||
bool state = get_button(msg, static_cast<XboxButton>(btn));
|
||||
|
||||
// state changed and button is pressed
|
||||
if (state != m_button_state[btn] && state)
|
||||
{
|
||||
m_press_count[btn] += 1;
|
||||
}
|
||||
|
||||
m_button_state[btn] = state;
|
||||
}
|
||||
}
|
||||
|
||||
std::string
|
||||
StatisticModifier::str() const
|
||||
{
|
||||
return "stat";
|
||||
}
|
||||
|
||||
/* EOF */
|
50
src/modifier/statistic_modifier.hpp
Normal file
50
src/modifier/statistic_modifier.hpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
** Xbox360 USB Gamepad Userspace Driver
|
||||
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
|
||||
**
|
||||
** This program 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.
|
||||
**
|
||||
** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef HEADER_XBOXDRV_MODIFIER_STATISTIC_MODIFIER_HPP
|
||||
#define HEADER_XBOXDRV_MODIFIER_STATISTIC_MODIFIER_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "modifier.hpp"
|
||||
|
||||
class StatisticModifier : public Modifier
|
||||
{
|
||||
public:
|
||||
static StatisticModifier* from_string(const std::vector<std::string>& args);
|
||||
|
||||
public:
|
||||
StatisticModifier();
|
||||
~StatisticModifier();
|
||||
|
||||
void update(int msec_delta, XboxGenericMsg& msg);
|
||||
void print_stats();
|
||||
std::string str() const;
|
||||
|
||||
private:
|
||||
std::vector<int> m_button_state;
|
||||
std::vector<int> m_press_count;
|
||||
|
||||
private:
|
||||
StatisticModifier(const StatisticModifier&);
|
||||
StatisticModifier& operator=(const StatisticModifier&);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/* EOF */
|
Loading…
Add table
Reference in a new issue