initial changes

This commit is contained in:
thrust26 2019-06-20 14:13:59 +02:00
parent d7e9c95fb0
commit 7176d291b3
11 changed files with 390 additions and 7 deletions

View File

@ -0,0 +1,223 @@
//============================================================================
//
// 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 "ControllerMap.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ControllerMap::ControllerMap(void)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::add(const Event::Type event, const ControllerMapping& 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)
{
add(event, ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::erase(const ControllerMapping& 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)
{
erase(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Event::Type ControllerMap::get(const ControllerMapping& mapping) const
{
auto find = myMap.find(mapping);
if (find != myMap.end())
return find->second;
return Event::Type::NoType;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
{
return get(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ControllerMap::check(const ControllerMapping & mapping) const
{
auto find = myMap.find(mapping);
return (find != myMap.end());
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
{
return check(ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::getDesc(const Event::Type event, const ControllerMapping& mapping) const
{
ostringstream buf;
buf << "J" << mapping.stick;
// button description
if (mapping.button != CTRL_NONE)
buf << "/B" << mapping.button;
// axis description
if (int(mapping.axis) != CTRL_NONE)
{
buf << "/A" << mapping.hat;
switch (mapping.axis)
{
case JoyAxis::X: buf << "X"; break;
case JoyAxis::Y: buf << "Y"; break;
default: break;
}
if (Event::isAnalog(event))
buf << "+|-";
else if (mapping.adir == JoyDir::NEG)
buf << "-";
else
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
{
return getDesc(event, ControllerMapping(mode, stick, button, axis, adir, hat, hdir));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::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();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ControllerMap::ControllerMappingArray ControllerMap::getEventMapping(const Event::Type event, const EventMode mode) const
{
ControllerMappingArray map;
for (auto item : myMap)
if (item.second == event && item.first.mode == mode)
map.push_back(item.first);
return map;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ControllerMap::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.button << ","
<< int(item.first.axis) << "," << int(item.first.adir) << "," << item.first.hat << "," << int(item.first.hdir);
}
}
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int ControllerMap::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, button, axis, adir, hat, hdir, 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));
return i;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ControllerMap::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 ControllerMap::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++;
}

View File

@ -0,0 +1,129 @@
//============================================================================
//
// 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

View File

@ -161,9 +161,9 @@ string KeyMap::getEventMappingDesc(const Event::Type event, const int mode) cons
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::vector<KeyMap::Mapping> KeyMap::getEventMapping(const Event::Type event, const int mode) const
KeyMap::MappingArray KeyMap::getEventMapping(const Event::Type event, const int mode) const
{
std::vector<KeyMap::Mapping> map;
MappingArray map;
for (auto item : myMap)
if (item.second == event && item.first.mode == mode)

View File

@ -55,6 +55,7 @@ class KeyMap
);
}
};
using MappingArray = std::vector<Mapping>;
KeyMap();
virtual ~KeyMap() = default;
@ -82,7 +83,7 @@ class KeyMap
/** Get the mapping description(s) for given event and mode */
string getEventMappingDesc(const Event::Type event, const int mode) const;
std::vector<Mapping> getEventMapping(const Event::Type event, const int mode) const;
MappingArray getEventMapping(const Event::Type event, const int mode) const;
string saveMapping(const int mode) const;
int loadMapping(string& list, const int mode);

View File

@ -426,7 +426,7 @@ string PhysicalJoystickHandler::getMappingDesc(Event::Type event, EventMode mode
dir = NUM_JOY_DIRS; // Immediately exit the inner loop after this iteration
buf << "/+|-";
}
else if(dir == 0)
else if(dir == int(JoyDir::NEG))
buf << "/-";
else
buf << "/+";
@ -521,6 +521,15 @@ bool PhysicalJoystickHandler::addHatMapping(Event::Type event, EventMode mode,
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::addMapping(Event::Type event, EventMode mode, int stick,
int button, JoyAxis axis, JoyDir adir, int hat, JoyHat hdir)
{
myControllerMap.add(event, mode, stick, button, axis, adir, hat, hdir);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::handleAxisEvent(int stick, int axis, int value)
{

View File

@ -28,6 +28,7 @@ class Event;
#include "EventHandlerConstants.hxx"
#include "PhysicalJoystick.hxx"
#include "Variant.hxx"
#include "ControllerMap.hxx"
using PhysicalJoystickPtr = shared_ptr<PhysicalJoystick>;
@ -78,6 +79,12 @@ class PhysicalJoystickHandler
bool addBtnMapping(Event::Type event, EventMode mode, int stick, int button);
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);
/** Handle a physical joystick event. */
void handleAxisEvent(int stick, int axis, int value);
void handleBtnEvent(int stick, int button, bool pressed);
@ -133,6 +140,9 @@ 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

@ -236,7 +236,7 @@ void PhysicalKeyboardHandler::enableMappings(const EventSet events, EventMode mo
void PhysicalKeyboardHandler::enableMapping(const Event::Type event, EventMode mode)
{
// copy from controller mode into emulation mode
std::vector<KeyMap::Mapping> mappings = myKeyMap.getEventMapping(event, mode);
KeyMap::MappingArray mappings = myKeyMap.getEventMapping(event, mode);
for (const auto& mapping : mappings)
myKeyMap.add(event, kEmulationMode, mapping.key, mapping.mod);

View File

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

View File

@ -38,6 +38,8 @@ enum class MouseButton {
NONE
};
static constexpr int CTRL_NONE = -1;
enum class JoyAxis {
X = 0,
Y = 1,

View File

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

View File

@ -978,6 +978,9 @@
<ClCompile Include="..\gui\R77HelpDialog.cxx">
<Filter>Source Files\gui</Filter>
</ClCompile>
<ClCompile Include="..\common\ControllerMap.cxx">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\bspf.hxx">
@ -2000,6 +2003,9 @@
<ClInclude Include="..\common\KeyMap.hxx">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\common\ControllerMap.hxx">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="stella.ico">