refactor into two separate maps for stick and hats

This commit is contained in:
thrust26 2019-06-21 10:35:45 +02:00
parent 7176d291b3
commit 529c1fe7de
11 changed files with 509 additions and 197 deletions

View File

@ -1,129 +0,0 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef CONTROLLERMAP_HXX
#define CONTROLLERMAP_HXX
#include <unordered_map>
#include "Event.hxx"
#include "EventHandlerConstants.hxx"
/**
This class handles controller mappings in Stella.
@author Thomas Jentzsch
*/
class ControllerMap
{
public:
struct ControllerMapping
{
EventMode mode;
int stick; // stick number
int button; // button number
JoyAxis axis; // horizontal/vertical
JoyDir adir; // axis direction (neg/pos)
int hat; // hat number
JoyHat hdir; // hat direction (left/right/up/down)
ControllerMapping()
: mode(EventMode(0)), stick(0), button(0),
axis(JoyAxis(0)), adir(JoyDir(0)), hat(0), hdir(JoyHat(0)) { }
ControllerMapping(const ControllerMapping& m)
: mode(m.mode), stick(m.stick), button(m.button),
axis(m.axis), adir(m.adir), hat(m.hat), hdir(m.hdir) { }
explicit ControllerMapping(EventMode c_mode, int c_stick, int c_button,
JoyAxis c_axis, JoyDir c_adir, int c_hat, JoyHat c_hdir)
: mode(c_mode), stick(c_stick), button(c_button),
axis(c_axis), adir(c_adir), hat(c_hat), hdir(c_hdir) { }
bool operator==(const ControllerMapping& other) const
{
return (mode == other.mode
&& stick == other.stick
&& button == other.button
&& axis == other.axis
&& adir == other.adir
&& hat == other.hat
&& hdir == other.hdir
);
}
};
using ControllerMappingArray = std::vector<ControllerMapping>;
ControllerMap();
virtual ~ControllerMap() = default;
/** Add new mapping for given event */
void add(const Event::Type event, const ControllerMapping& mapping);
void add(const Event::Type event, const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir);
/** Erase mapping */
void erase(const ControllerMapping& mapping);
void erase(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir);
/** Get event for mapping */
Event::Type get(const ControllerMapping& mapping) const;
Event::Type get(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const;
/** Check if a mapping exists */
bool check(const ControllerMapping& mapping) const;
bool check(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const;
/** Get mapping description */
string getDesc(const Event::Type event, const ControllerMapping& mapping) const;
string getDesc(const Event::Type event, const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const;
/** Get the mapping description(s) for given event and mode */
string getEventMappingDesc(const Event::Type event, const EventMode mode) const;
ControllerMappingArray getEventMapping(const Event::Type event, const EventMode mode) const;
string saveMapping(const EventMode mode) const;
int loadMapping(string& list, const EventMode mode);
/** Erase all mappings for given mode */
void eraseMode(const EventMode mode);
/** Erase given event's mapping for given mode */
void eraseEvent(const Event::Type event, const EventMode mode);
/** clear all mappings for a modes */
// void clear() { myMap.clear(); }
size_t size() { return myMap.size(); }
private:
struct ControllerHash {
size_t operator()(const ControllerMapping& m)const {
return std::hash<uInt64>()((uInt64(m.mode)) // 3 bit
^ ((uInt64(m.stick)) << 3) // 2 bits
^ ((uInt64(m.button)) << 5) // 2 bits
^ ((uInt64(m.axis)) << 7) // 1 bit
^ ((uInt64(m.adir)) << 8) // 1 bit
^ ((uInt64(m.hat)) << 9) // 1 bit
^ ((uInt64(m.hdir)) << 10)); // 2 bits
}
};
std::unordered_map<ControllerMapping, Event::Type, ControllerHash> myMap;
};
#endif

200
src/common/JoyHatMap.cxx Normal file
View File

@ -0,0 +1,200 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "JoyHatMap.hxx"
// TODOs
// - two maps per controller (joydirs, hatdirs)
// - both maps combined with buttons
// - directions can work alone and with a button combination
// - buttons can work without a direction (mapped in joydir)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
JoyHatMap::JoyHatMap(void)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::add(const Event::Type event, const JoyHatMapping& mapping)
{
myMap[mapping] = event;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::add(const Event::Type event, const EventMode mode, const int hat, const JoyHat hdir)
{
add(event, JoyHatMapping(mode, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::erase(const JoyHatMapping& mapping)
{
myMap.erase(mapping);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::erase(const EventMode mode, const int hat, const JoyHat hdir)
{
erase(JoyHatMapping(mode, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type JoyHatMap::get(const JoyHatMapping& mapping) const
{
auto find = myMap.find(mapping);
if (find != myMap.end())
return find->second;
return Event::Type::NoType;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type JoyHatMap::get(const EventMode mode, const int hat, const JoyHat hdir) const
{
return get(JoyHatMapping(mode, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool JoyHatMap::check(const JoyHatMapping & mapping) const
{
auto find = myMap.find(mapping);
return (find != myMap.end());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool JoyHatMap::check(const EventMode mode, const int hat, const JoyHat hdir) const
{
return check(JoyHatMapping(mode, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string JoyHatMap::getDesc(const Event::Type event, const JoyHatMapping & mapping) const
{
ostringstream buf;
//buf << "J" << mapping.stick;
// hat description
if (mapping.hat != CTRL_NONE)
{
buf << "/H" << mapping.hat;
switch (mapping.hdir)
{
case JoyHat::UP: buf << "/up"; break;
case JoyHat::DOWN: buf << "/down"; break;
case JoyHat::LEFT: buf << "/left"; break;
case JoyHat::RIGHT: buf << "/right"; break;
default: break;
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string JoyHatMap::getDesc(const Event::Type event, const EventMode mode, const int hat, const JoyHat hdir) const
{
return getDesc(event, JoyHatMapping(mode, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string JoyHatMap::getEventMappingDesc(const Event::Type event, const EventMode mode) const
{
ostringstream buf;
for (auto item : myMap)
{
if (item.second == event && item.first.mode == mode)
{
if (buf.str() != "")
buf << ", ";
buf << getDesc(event, item.first);
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
JoyHatMap::JoyHatMappingArray JoyHatMap::getEventMapping(const Event::Type event, const EventMode mode) const
{
JoyHatMappingArray map;
for (auto item : myMap)
if (item.second == event && item.first.mode == mode)
map.push_back(item.first);
return map;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string JoyHatMap::saveMapping(const EventMode mode) const
{
ostringstream buf;
for (auto item : myMap)
{
if (item.first.mode == mode)
{
if (buf.str() != "")
buf << "|";
buf << item.second << ":" /*<< item.first.stick*/ << "," <<
item.first.hat << "," << int(item.first.hdir);
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int JoyHatMap::loadMapping(string & list, const EventMode mode)
{
// Since istringstream swallows whitespace, we have to make the
// delimiters be spaces
std::replace(list.begin(), list.end(), '|', ' ');
std::replace(list.begin(), list.end(), ':', ' ');
std::replace(list.begin(), list.end(), ',', ' ');
istringstream buf(list);
int event, stick, hat, hdir, i = 0;
while (buf >> event && buf >> stick && buf >> hat && buf >> hdir && ++i)
add(Event::Type(event), EventMode(mode), hat, JoyHat(hdir));
return i;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::eraseMode(const EventMode mode)
{
for (auto item = myMap.begin(); item != myMap.end();)
if (item->first.mode == mode) {
auto _item = item++;
erase(_item->first);
}
else item++;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JoyHatMap::eraseEvent(const Event::Type event, const EventMode mode)
{
for (auto item = myMap.begin(); item != myMap.end();)
if (item->second == event && item->first.mode == mode) {
auto _item = item++;
erase(_item->first);
}
else item++;
}

109
src/common/JoyHatMap.hxx Normal file
View File

@ -0,0 +1,109 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef JOYHATMAP_HXX
#define JOYHATMAP_HXX
#include <unordered_map>
#include "Event.hxx"
#include "EventHandlerConstants.hxx"
/**
This class handles controller mappings in Stella.
@author Thomas Jentzsch
*/
class JoyHatMap
{
public:
struct JoyHatMapping
{
EventMode mode;
int hat; // hat number
JoyHat hdir; // hat direction (left/right/up/down)
JoyHatMapping()
: mode(EventMode(0)), hat(0), hdir(JoyHat(0)) { }
JoyHatMapping(const JoyHatMapping& m)
: mode(m.mode), hat(m.hat), hdir(m.hdir) { }
explicit JoyHatMapping(EventMode c_mode, int c_hat, JoyHat c_hdir)
: mode(c_mode), hat(c_hat), hdir(c_hdir) { }
bool operator==(const JoyHatMapping& other) const
{
return (mode == other.mode
&& hat == other.hat
&& hdir == other.hdir
);
}
};
using JoyHatMappingArray = std::vector<JoyHatMapping>;
JoyHatMap();
virtual ~JoyHatMap() = default;
/** Add new mapping for given event */
void add(const Event::Type event, const JoyHatMapping& mapping);
void add(const Event::Type event, const EventMode mode, const int hat, const JoyHat hdir);
/** Erase mapping */
void erase(const JoyHatMapping& mapping);
void erase(const EventMode mode, const int hat, const JoyHat hdir);
/** Get event for mapping */
Event::Type get(const JoyHatMapping& mapping) const;
Event::Type get(const EventMode mode, const int hat, const JoyHat hdir) const;
/** Check if a mapping exists */
bool check(const JoyHatMapping& mapping) const;
bool check(const EventMode mode, const int hat, const JoyHat hdir) const;
/** Get mapping description */
string getDesc(const Event::Type event, const JoyHatMapping& mapping) const;
string getDesc(const Event::Type event, const EventMode mode, const int hat, const JoyHat hdir) const;
/** Get the mapping description(s) for given event and mode */
string getEventMappingDesc(const Event::Type event, const EventMode mode) const;
JoyHatMappingArray getEventMapping(const Event::Type event, const EventMode mode) const;
string saveMapping(const EventMode mode) const;
int loadMapping(string& list, const EventMode mode);
/** Erase all mappings for given mode */
void eraseMode(const EventMode mode);
/** Erase given event's mapping for given mode */
void eraseEvent(const Event::Type event, const EventMode mode);
/** clear all mappings for a modes */
// void clear() { myMap.clear(); }
size_t size() { return myMap.size(); }
private:
struct JoyHatHash {
size_t operator()(const JoyHatMapping& m)const {
return std::hash<uInt64>()((uInt64(m.mode)) // 3 bit
^ ((uInt64(m.hat)) << 3) // 1 bit
^ ((uInt64(m.hdir)) << 4) // 2 bits
);
}
};
std::unordered_map<JoyHatMapping, Event::Type, JoyHatHash> myMap;
};
#endif

View File

@ -15,41 +15,47 @@
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "ControllerMap.hxx"
#include "JoyMap.hxx"
// TODOs
// - two maps per controller (joydirs, hatdirs)
// - both maps combined with buttons
// - directions can work alone and with a button combination
// - buttons can work without a direction (mapped in joydir)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ControllerMap::ControllerMap(void)
JoyMap::JoyMap(void)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::add(const Event::Type event, const ControllerMapping& mapping)
void JoyMap::add(const Event::Type event, const JoyMapping& mapping)
{
myMap[mapping] = event;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::add(const Event::Type event, const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir)
void JoyMap::add(const Event::Type event, const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir)
{
add(event, ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
add(event, JoyMapping(mode, button, axis, adir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::erase(const ControllerMapping& mapping)
void JoyMap::erase(const JoyMapping& mapping)
{
myMap.erase(mapping);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::erase(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir)
void JoyMap::erase(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir)
{
erase(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
erase(JoyMapping(mode, button, axis, adir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type ControllerMap::get(const ControllerMapping& mapping) const
Event::Type JoyMap::get(const JoyMapping& mapping) const
{
auto find = myMap.find(mapping);
if (find != myMap.end())
@ -59,14 +65,14 @@ Event::Type ControllerMap::get(const ControllerMapping& mapping) const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type ControllerMap::get(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const
Event::Type JoyMap::get(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const
{
return get(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
return get(JoyMapping(mode, button, axis, adir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ControllerMap::check(const ControllerMapping & mapping) const
bool JoyMap::check(const JoyMapping & mapping) const
{
auto find = myMap.find(mapping);
@ -74,18 +80,18 @@ bool ControllerMap::check(const ControllerMapping & mapping) const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ControllerMap::check(const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const
bool JoyMap::check(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const
{
return check(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
return check(JoyMapping(mode, button, axis, adir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::getDesc(const Event::Type event, const ControllerMapping& mapping) const
string JoyMap::getDesc(const Event::Type event, const JoyMapping& mapping) const
{
ostringstream buf;
buf << "J" << mapping.stick;
//buf << "J" << mapping.stick;
// button description
if (mapping.button != CTRL_NONE)
@ -94,7 +100,7 @@ string ControllerMap::getDesc(const Event::Type event, const ControllerMapping&
// axis description
if (int(mapping.axis) != CTRL_NONE)
{
buf << "/A" << mapping.hat;
buf << "/A";
switch (mapping.axis)
{
case JoyAxis::X: buf << "X"; break;
@ -110,33 +116,18 @@ string ControllerMap::getDesc(const Event::Type event, const ControllerMapping&
buf << "+";
}
// hat description
if (mapping.hat != CTRL_NONE)
{
buf << "/H" << mapping.hat;
switch (mapping.hdir)
{
case JoyHat::UP: buf << "/up"; break;
case JoyHat::DOWN: buf << "/down"; break;
case JoyHat::LEFT: buf << "/left"; break;
case JoyHat::RIGHT: buf << "/right"; break;
default: break;
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::getDesc(const Event::Type event, const EventMode mode, const int stick, const int button,
const JoyAxis axis, const JoyDir adir, const int hat, const JoyHat hdir) const
string JoyMap::getDesc(const Event::Type event, const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const
{
return getDesc(event, ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
return getDesc(event, JoyMapping(mode, button, axis, adir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::getEventMappingDesc(const Event::Type event, const EventMode mode) const
string JoyMap::getEventMappingDesc(const Event::Type event, const EventMode mode) const
{
ostringstream buf;
@ -153,9 +144,9 @@ string ControllerMap::getEventMappingDesc(const Event::Type event, const EventMo
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ControllerMap::ControllerMappingArray ControllerMap::getEventMapping(const Event::Type event, const EventMode mode) const
JoyMap::JoyMappingArray JoyMap::getEventMapping(const Event::Type event, const EventMode mode) const
{
ControllerMappingArray map;
JoyMappingArray map;
for (auto item : myMap)
if (item.second == event && item.first.mode == mode)
@ -165,7 +156,7 @@ ControllerMap::ControllerMappingArray ControllerMap::getEventMapping(const Event
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::saveMapping(const EventMode mode) const
string JoyMap::saveMapping(const EventMode mode) const
{
ostringstream buf;
@ -175,15 +166,15 @@ string ControllerMap::saveMapping(const EventMode mode) const
{
if (buf.str() != "")
buf << "|";
buf << item.second << ":" << item.first.stick << "," << item.first.button << ","
<< int(item.first.axis) << "," << int(item.first.adir) << "," << item.first.hat << "," << int(item.first.hdir);
buf << item.second << ":" /*<< item.first.stick*/ << "," << item.first.button << ","
<< int(item.first.axis) << "," << int(item.first.adir);
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int ControllerMap::loadMapping(string& list, const EventMode mode)
int JoyMap::loadMapping(string& list, const EventMode mode)
{
// Since istringstream swallows whitespace, we have to make the
// delimiters be spaces
@ -191,17 +182,17 @@ int ControllerMap::loadMapping(string& list, const EventMode mode)
std::replace(list.begin(), list.end(), ':', ' ');
std::replace(list.begin(), list.end(), ',', ' ');
istringstream buf(list);
int event, stick, button, axis, adir, hat, hdir, i = 0;
int event, stick, button, axis, adir, i = 0;
while (buf >> event && buf >> stick && buf >> button
&& buf >> axis && buf >> adir && buf >> hat && buf >> hdir && ++i)
add(Event::Type(event), EventMode(mode), stick, button, JoyAxis(axis), JoyDir(adir), hat, JoyHat(hdir));
&& buf >> axis && buf >> adir && ++i)
add(Event::Type(event), EventMode(mode), button, JoyAxis(axis), JoyDir(adir));
return i;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::eraseMode(const EventMode mode)
void JoyMap::eraseMode(const EventMode mode)
{
for (auto item = myMap.begin(); item != myMap.end();)
if (item->first.mode == mode) {
@ -212,7 +203,7 @@ void ControllerMap::eraseMode(const EventMode mode)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::eraseEvent(const Event::Type event, const EventMode mode)
void JoyMap::eraseEvent(const Event::Type event, const EventMode mode)
{
for (auto item = myMap.begin(); item != myMap.end();)
if (item->second == event && item->first.mode == mode) {

121
src/common/JoyMap.hxx Normal file
View File

@ -0,0 +1,121 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#ifndef CONTROLLERMAP_HXX
#define CONTROLLERMAP_HXX
#include <unordered_map>
#include "Event.hxx"
#include "EventHandlerConstants.hxx"
/**
This class handles controller mappings in Stella.
@author Thomas Jentzsch
*/
class JoyMap
{
public:
struct JoyMapping
{
EventMode mode;
int button; // button number
JoyAxis axis; // horizontal/vertical
JoyDir adir; // axis direction (neg/pos)
JoyMapping()
: mode(EventMode(0)), button(0),
axis(JoyAxis(0)), adir(JoyDir(0)) { }
JoyMapping(const JoyMapping& m)
: mode(m.mode), button(m.button),
axis(m.axis), adir(m.adir) { }
explicit JoyMapping(EventMode c_mode, int c_button,
JoyAxis c_axis, JoyDir c_adir)
: mode(c_mode), button(c_button),
axis(c_axis), adir(c_adir) { }
bool operator==(const JoyMapping& other) const
{
return (mode == other.mode
&& button == other.button
&& axis == other.axis
&& adir == other.adir
);
}
};
using JoyMappingArray = std::vector<JoyMapping>;
JoyMap();
virtual ~JoyMap() = default;
/** Add new mapping for given event */
void add(const Event::Type event, const JoyMapping& mapping);
void add(const Event::Type event, const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir);
/** Erase mapping */
void erase(const JoyMapping& mapping);
void erase(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir);
/** Get event for mapping */
Event::Type get(const JoyMapping& mapping) const;
Event::Type get(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const;
/** Check if a mapping exists */
bool check(const JoyMapping& mapping) const;
bool check(const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const;
/** Get mapping description */
string getDesc(const Event::Type event, const JoyMapping& mapping) const;
string getDesc(const Event::Type event, const EventMode mode, const int button,
const JoyAxis axis, const JoyDir adir) const;
/** Get the mapping description(s) for given event and mode */
string getEventMappingDesc(const Event::Type event, const EventMode mode) const;
JoyMappingArray getEventMapping(const Event::Type event, const EventMode mode) const;
string saveMapping(const EventMode mode) const;
int loadMapping(string& list, const EventMode mode);
/** Erase all mappings for given mode */
void eraseMode(const EventMode mode);
/** Erase given event's mapping for given mode */
void eraseEvent(const Event::Type event, const EventMode mode);
/** clear all mappings for a modes */
// void clear() { myMap.clear(); }
size_t size() { return myMap.size(); }
private:
struct JoyHash {
size_t operator()(const JoyMapping& m)const {
return std::hash<uInt64>()((uInt64(m.mode)) // 3 bit
^ ((uInt64(m.button)) << 2) // 2 bits
^ ((uInt64(m.axis)) << 4) // 1 bit
^ ((uInt64(m.adir)) << 5) // 1 bit
);
}
};
std::unordered_map<JoyMapping, Event::Type, JoyHash> myMap;
};
#endif

View File

@ -480,7 +480,7 @@ bool PhysicalJoystickHandler::addAxisMapping(Event::Type event, EventMode mode,
if(Event::isAnalog(j->axisTable[axis][int(JoyDir::POS)][mode]))
j->axisTable[axis][int(JoyDir::POS)][mode] = Event::NoType;
j->axisTable[axis][(value > int(JoyDir::NEG))][mode] = event;
j->axisTable[axis][int(value > 0 ? JoyDir::POS : JoyDir::NEG)][mode] = event;
}
return true;
}
@ -522,13 +522,22 @@ bool PhysicalJoystickHandler::addHatMapping(Event::Type event, EventMode mode,
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::addMapping(Event::Type event, EventMode mode, int stick,
int button, JoyAxis axis, JoyDir adir, int hat, JoyHat hdir)
void PhysicalJoystickHandler::addJoyMapping(Event::Type event, EventMode mode, int stick,
int button, JoyAxis axis, JoyDir adir)
{
myControllerMap.add(event, mode, stick, button, axis, adir, hat, hdir);
const PhysicalJoystickPtr j = joy(stick);
j->joyMap.add(event, mode, button, axis, adir);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::addJoyHatMapping(Event::Type event, EventMode mode, int stick,
int hat, JoyHat hdir)
{
const PhysicalJoystickPtr j = joy(stick);
j->joyHatMap.add(event, mode, hat, hdir);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::handleAxisEvent(int stick, int axis, int value)

View File

@ -28,7 +28,6 @@ class Event;
#include "EventHandlerConstants.hxx"
#include "PhysicalJoystick.hxx"
#include "Variant.hxx"
#include "ControllerMap.hxx"
using PhysicalJoystickPtr = shared_ptr<PhysicalJoystick>;
@ -80,10 +79,10 @@ class PhysicalJoystickHandler
bool addHatMapping(Event::Type event, EventMode mode, int stick, int hat, JoyHat value);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void addMapping(Event::Type event, EventMode mode, int stick,
int button, JoyAxis axis, JoyDir adir, int hat, JoyHat hdir);
void addJoyMapping(Event::Type event, EventMode mode, int stick,
int button, JoyAxis axis, JoyDir adir);
void addJoyHatMapping(Event::Type event, EventMode mode, int stick,
int hat, JoyHat hdir);
/** Handle a physical joystick event. */
void handleAxisEvent(int stick, int axis, int value);
@ -140,9 +139,6 @@ class PhysicalJoystickHandler
static const Event::Type SA_Axis[NUM_PORTS][NUM_JOY_AXIS];
static const Event::Type SA_Button[NUM_PORTS][NUM_JOY_BTN];
static const Event::Type SA_Key[NUM_PORTS][NUM_KEY_BTN];
// Hashmap of controller events
ControllerMap myControllerMap;
};
#endif

View File

@ -20,6 +20,8 @@
#include "Event.hxx"
#include "EventHandlerConstants.hxx"
#include "JoyMap.hxx"
#include "JoyHatMap.hxx"
/**
An abstraction of a physical (real) joystick in Stella.
@ -70,6 +72,10 @@ class PhysicalJoystick
Event::Type (*hatTable)[NUM_JOY_HAT_DIRS][kNumModes];
int* axisLastValue;
// Hashmaps of controller events
JoyMap joyMap;
JoyHatMap joyHatMap;
private:
void getValues(const string& list, IntArray& map) const;

View File

@ -6,7 +6,8 @@ MODULE_OBJS := \
src/common/FBSurfaceSDL2.o \
src/common/FrameBufferSDL2.o \
src/common/FSNodeZIP.o \
src/common/ControllerMap.o \
src/common/JoyMap.o \
src/common/JoyHatMap.o \
src/common/KeyMap.o \
src/common/Logger.o \
src/common/main.o \

View File

@ -376,7 +376,8 @@
<ClCompile Include="..\common\FpsMeter.cxx" />
<ClCompile Include="..\common\FrameBufferSDL2.cxx" />
<ClCompile Include="..\common\FSNodeZIP.cxx" />
<ClCompile Include="..\common\ControllerMap.cxx" />
<ClCompile Include="..\common\JoyMap.cxx" />
<ClCompile Include="..\common\JoyHatMap.cxx" />
<ClCompile Include="..\common\KeyMap.cxx" />
<ClCompile Include="..\common\Logger.cxx" />
<ClCompile Include="..\common\main.cxx" />
@ -1074,7 +1075,8 @@
<ClInclude Include="..\common\FrameBufferSDL2.hxx" />
<ClInclude Include="..\common\FSNodeFactory.hxx" />
<ClInclude Include="..\common\FSNodeZIP.hxx" />
<ClInclude Include="..\common\ControllerMap.hxx" />
<ClInclude Include="..\common\JoyMap.hxx" />
<ClInclude Include="..\common\JoyHatMap.hxx" />
<ClInclude Include="..\common\KeyMap.hxx" />
<ClInclude Include="..\common\LinkedObjectPool.hxx" />
<ClInclude Include="..\common\Logger.hxx" />

View File

@ -978,7 +978,10 @@
<ClCompile Include="..\gui\R77HelpDialog.cxx">
<Filter>Source Files\gui</Filter>
</ClCompile>
<ClCompile Include="..\common\ControllerMap.cxx">
<ClCompile Include="..\common\JoyHatMap.cxx">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\common\JoyMap.cxx">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
@ -2003,7 +2006,10 @@
<ClInclude Include="..\common\KeyMap.hxx">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common\ControllerMap.hxx">
<ClInclude Include="..\common\JoyHatMap.hxx">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common\JoyMap.hxx">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>