Convert some raw enum to C++11 'enum class' in EventHandlerConstants.

This commit is contained in:
Stephen Anthony 2017-12-14 21:04:19 -03:30
parent fd5c026034
commit 31da99f48b
16 changed files with 90 additions and 84 deletions

View File

@ -71,11 +71,11 @@ void EventHandlerSDL2::pollEvent()
switch(myEvent.button.button)
{
case SDL_BUTTON_LEFT:
handleMouseButtonEvent(pressed ? EVENT_LBUTTONDOWN : EVENT_LBUTTONUP,
handleMouseButtonEvent(pressed ? MouseButton::LBUTTONDOWN : MouseButton::LBUTTONUP,
myEvent.button.x, myEvent.button.y);
break;
case SDL_BUTTON_RIGHT:
handleMouseButtonEvent(pressed ? EVENT_RBUTTONDOWN : EVENT_RBUTTONUP,
handleMouseButtonEvent(pressed ? MouseButton::RBUTTONDOWN : MouseButton::RBUTTONUP,
myEvent.button.x, myEvent.button.y);
break;
}
@ -87,9 +87,9 @@ void EventHandlerSDL2::pollEvent()
int x, y;
SDL_GetMouseState(&x, &y); // we need mouse position too
if(myEvent.wheel.y < 0)
handleMouseButtonEvent(EVENT_WHEELDOWN, x, y);
handleMouseButtonEvent(MouseButton::WHEELDOWN, x, y);
else if(myEvent.wheel.y > 0)
handleMouseButtonEvent(EVENT_WHEELUP, x, y);
handleMouseButtonEvent(MouseButton::WHEELUP, x, y);
break;
}
@ -112,11 +112,15 @@ void EventHandlerSDL2::pollEvent()
case SDL_JOYHATMOTION:
{
int v = myEvent.jhat.value, value = 0;
if(v & SDL_HAT_UP) value |= EVENT_HATUP_M;
if(v & SDL_HAT_DOWN) value |= EVENT_HATDOWN_M;
if(v & SDL_HAT_LEFT) value |= EVENT_HATLEFT_M;
if(v & SDL_HAT_RIGHT) value |= EVENT_HATRIGHT_M;
if(v == SDL_HAT_CENTERED) value = EVENT_HATCENTER_M;
if(v & SDL_HAT_CENTERED)
value = EVENT_HATCENTER_M;
else
{
if(v & SDL_HAT_UP) value |= EVENT_HATUP_M;
if(v & SDL_HAT_DOWN) value |= EVENT_HATDOWN_M;
if(v & SDL_HAT_LEFT) value |= EVENT_HATLEFT_M;
if(v & SDL_HAT_RIGHT) value |= EVENT_HATRIGHT_M;
}
handleJoyHatEvent(myEvent.jhat.which, myEvent.jhat.hat, value);
break; // SDL_JOYHATMOTION

View File

@ -671,16 +671,16 @@ void EventHandler::handleMouseButtonEvent(MouseButton b, int x, int y)
{
switch(b)
{
case EVENT_LBUTTONDOWN:
case MouseButton::LBUTTONDOWN:
myEvent.set(Event::MouseButtonLeftValue, 1);
break;
case EVENT_LBUTTONUP:
case MouseButton::LBUTTONUP:
myEvent.set(Event::MouseButtonLeftValue, 0);
break;
case EVENT_RBUTTONDOWN:
case MouseButton::RBUTTONDOWN:
myEvent.set(Event::MouseButtonRightValue, 1);
break;
case EVENT_RBUTTONUP:
case MouseButton::RBUTTONUP:
myEvent.set(Event::MouseButtonRightValue, 0);
break;
default:
@ -869,29 +869,29 @@ void EventHandler::handleJoyHatEvent(int stick, int hat, int value)
// when we get a diagonal hat event
if(myState == S_EMULATE)
{
handleEvent(joy->hatTable[hat][EVENT_HATUP][kEmulationMode],
handleEvent(joy->hatTable[hat][int(JoyHat::UP)][kEmulationMode],
value & EVENT_HATUP_M);
handleEvent(joy->hatTable[hat][EVENT_HATRIGHT][kEmulationMode],
handleEvent(joy->hatTable[hat][int(JoyHat::RIGHT)][kEmulationMode],
value & EVENT_HATRIGHT_M);
handleEvent(joy->hatTable[hat][EVENT_HATDOWN][kEmulationMode],
handleEvent(joy->hatTable[hat][int(JoyHat::DOWN)][kEmulationMode],
value & EVENT_HATDOWN_M);
handleEvent(joy->hatTable[hat][EVENT_HATLEFT][kEmulationMode],
handleEvent(joy->hatTable[hat][int(JoyHat::LEFT)][kEmulationMode],
value & EVENT_HATLEFT_M);
}
else if(myOverlay)
{
if(value == EVENT_HATCENTER_M)
myOverlay->handleJoyHatEvent(stick, hat, EVENT_HATCENTER);
myOverlay->handleJoyHatEvent(stick, hat, JoyHat::CENTER);
else
{
if(value & EVENT_HATUP_M)
myOverlay->handleJoyHatEvent(stick, hat, EVENT_HATUP);
myOverlay->handleJoyHatEvent(stick, hat, JoyHat::UP);
if(value & EVENT_HATRIGHT_M)
myOverlay->handleJoyHatEvent(stick, hat, EVENT_HATRIGHT);
myOverlay->handleJoyHatEvent(stick, hat, JoyHat::RIGHT);
if(value & EVENT_HATDOWN_M)
myOverlay->handleJoyHatEvent(stick, hat, EVENT_HATDOWN);
myOverlay->handleJoyHatEvent(stick, hat, JoyHat::DOWN);
if(value & EVENT_HATLEFT_M)
myOverlay->handleJoyHatEvent(stick, hat, EVENT_HATLEFT);
myOverlay->handleJoyHatEvent(stick, hat, JoyHat::LEFT);
}
}
}
@ -1354,12 +1354,13 @@ void EventHandler::setActionMappings(EventMode mode)
{
buf.str("");
buf << "J" << stick << "/H" << hat;
switch(dir)
switch(JoyHat(dir))
{
case EVENT_HATUP: buf << "/up"; break;
case EVENT_HATDOWN: buf << "/down"; break;
case EVENT_HATLEFT: buf << "/left"; break;
case EVENT_HATRIGHT: buf << "/right"; break;
case JoyHat::UP: buf << "/up"; break;
case JoyHat::DOWN: buf << "/down"; break;
case JoyHat::LEFT: buf << "/left"; break;
case JoyHat::RIGHT: buf << "/right"; break;
case JoyHat::CENTER: break;
}
if(key == "")
key = key + buf.str();
@ -1563,7 +1564,7 @@ bool EventHandler::addJoyButtonMapping(Event::Type event, EventMode mode,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EventHandler::addJoyHatMapping(Event::Type event, EventMode mode,
int stick, int hat, int value,
int stick, int hat, JoyHat value,
bool updateMenus)
{
#ifdef JOYSTICK_SUPPORT
@ -1571,9 +1572,9 @@ bool EventHandler::addJoyHatMapping(Event::Type event, EventMode mode,
if(joy)
{
if(hat >= 0 && hat < joy->numHats && event < Event::LastType &&
value != EVENT_HATCENTER)
value != JoyHat::CENTER)
{
joy->hatTable[hat][value][mode] = event;
joy->hatTable[hat][int(value)][mode] = event;
if(updateMenus)
setActionMappings(mode);
return true;

View File

@ -203,9 +203,9 @@ class EventHandler
const StellaJoystick* joy = myJoyHandler->joy(stick);
return joy ? joy->btnTable[button][mode] : Event::NoType;
}
Event::Type eventForJoyHat(int stick, int hat, int value, EventMode mode) const {
Event::Type eventForJoyHat(int stick, int hat, JoyHat value, EventMode mode) const {
const StellaJoystick* joy = myJoyHandler->joy(stick);
return joy ? joy->hatTable[hat][value][mode] : Event::NoType;
return joy ? joy->hatTable[hat][int(value)][mode] : Event::NoType;
}
Event::Type eventAtIndex(int idx, EventMode mode) const;
@ -267,7 +267,7 @@ class EventHandler
'adds', in which case it's delayed until the end
*/
bool addJoyHatMapping(Event::Type event, EventMode mode,
int stick, int hat, int value,
int stick, int hat, JoyHat value,
bool updateMenus = true);
/**

View File

@ -15,28 +15,27 @@
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
// FIXME - perhaps add to namespace or something
#ifndef EVENTHANDLER_CONSTANTS_HXX
#define EVENTHANDLER_CONSTANTS_HXX
enum MouseButton {
EVENT_LBUTTONDOWN,
EVENT_LBUTTONUP,
EVENT_RBUTTONDOWN,
EVENT_RBUTTONUP,
EVENT_WHEELDOWN,
EVENT_WHEELUP
enum class MouseButton {
LBUTTONDOWN,
LBUTTONUP,
RBUTTONDOWN,
RBUTTONUP,
WHEELDOWN,
WHEELUP
};
enum JoyHat {
EVENT_HATUP = 0, // make sure these are set correctly,
EVENT_HATDOWN = 1, // since they'll be used as array indices
EVENT_HATLEFT = 2,
EVENT_HATRIGHT = 3,
EVENT_HATCENTER = 4
enum class JoyHat {
UP = 0, // make sure these are set correctly,
DOWN = 1, // since they'll be used as array indices
LEFT = 2,
RIGHT = 3,
CENTER = 4
};
// TODO - add bitmask class for 'enum class' and convert this
enum JoyHatMask {
EVENT_HATUP_M = 1<<0,
EVENT_HATDOWN_M = 1<<1,
@ -45,6 +44,7 @@ enum JoyHatMask {
EVENT_HATCENTER_M = 1<<4
};
// TODO - make this 'enum class' somehow
enum EventMode {
kEmulationMode = 0, // make sure these are set correctly,
kMenuMode = 1, // since they'll be used as array indices

View File

@ -472,7 +472,7 @@ void EventHandler::JoystickHandler::setStickDefaultMapping(int stick,
if(eraseAll || b_event == event)
handler.addJoyButtonMapping(b_event, mode, b_stick, b_button, false);
};
auto setDefaultHat = [&](int h_stick, int h_hat, int h_dir, Event::Type h_event)
auto setDefaultHat = [&](int h_stick, int h_hat, JoyHat h_dir, Event::Type h_event)
{
if(eraseAll || h_event == event)
handler.addJoyHatMapping(h_event, mode, h_stick, h_hat, h_dir, false);
@ -492,11 +492,11 @@ void EventHandler::JoystickHandler::setStickDefaultMapping(int stick,
// Left joystick (assume joystick zero, button zero)
setDefaultBtn( 0, 0, Event::JoystickZeroFire );
// Left joystick left/right directions (assume joystick zero and hat 0)
setDefaultHat( 0, 0, EVENT_HATLEFT, Event::JoystickZeroLeft );
setDefaultHat( 0, 0, EVENT_HATRIGHT, Event::JoystickZeroRight );
setDefaultHat( 0, 0, JoyHat::LEFT, Event::JoystickZeroLeft );
setDefaultHat( 0, 0, JoyHat::RIGHT, Event::JoystickZeroRight );
// Left joystick up/down directions (assume joystick zero and hat 0)
setDefaultHat( 0, 0, EVENT_HATUP, Event::JoystickZeroUp );
setDefaultHat( 0, 0, EVENT_HATDOWN, Event::JoystickZeroDown );
setDefaultHat( 0, 0, JoyHat::UP, Event::JoystickZeroUp );
setDefaultHat( 0, 0, JoyHat::DOWN, Event::JoystickZeroDown );
}
else if(stick == 1)
{
@ -509,11 +509,11 @@ void EventHandler::JoystickHandler::setStickDefaultMapping(int stick,
// Right joystick (assume joystick one, button zero)
setDefaultBtn( 1, 0, Event::JoystickOneFire );
// Right joystick left/right directions (assume joystick one and hat 0)
setDefaultHat( 1, 0, EVENT_HATLEFT, Event::JoystickOneLeft );
setDefaultHat( 1, 0, EVENT_HATRIGHT, Event::JoystickOneRight );
setDefaultHat( 1, 0, JoyHat::LEFT, Event::JoystickOneLeft );
setDefaultHat( 1, 0, JoyHat::RIGHT, Event::JoystickOneRight );
// Right joystick up/down directions (assume joystick one and hat 0)
setDefaultHat( 1, 0, EVENT_HATUP, Event::JoystickOneUp );
setDefaultHat( 1, 0, EVENT_HATDOWN, Event::JoystickOneDown );
setDefaultHat( 1, 0, JoyHat::UP, Event::JoystickOneUp );
setDefaultHat( 1, 0, JoyHat::DOWN, Event::JoystickOneDown );
}
break;
@ -528,10 +528,10 @@ void EventHandler::JoystickHandler::setStickDefaultMapping(int stick,
// Left joystick (assume joystick zero, button zero)
setDefaultBtn( 0, 0, Event::UISelect );
setDefaultHat( 0, 0, EVENT_HATLEFT, Event::UILeft );
setDefaultHat( 0, 0, EVENT_HATRIGHT, Event::UIRight );
setDefaultHat( 0, 0, EVENT_HATUP, Event::UIUp );
setDefaultHat( 0, 0, EVENT_HATDOWN, Event::UIDown );
setDefaultHat( 0, 0, JoyHat::LEFT, Event::UILeft );
setDefaultHat( 0, 0, JoyHat::RIGHT, Event::UIRight );
setDefaultHat( 0, 0, JoyHat::UP, Event::UIUp );
setDefaultHat( 0, 0, JoyHat::DOWN, Event::UIDown );
}
break;

View File

@ -297,7 +297,7 @@ void ContextMenu::handleJoyAxis(int stick, int axis, int value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ContextMenu::handleJoyHat(int stick, int hat, int value)
bool ContextMenu::handleJoyHat(int stick, int hat, JoyHat value)
{
handleEvent(instance().eventHandler().eventForJoyHat(stick, hat, value, kMenuMode));
return true;

View File

@ -89,7 +89,7 @@ class ContextMenu : public Dialog, public CommandSender
void handleKeyDown(StellaKey key, StellaMod mod) override;
void handleJoyDown(int stick, int button) override;
void handleJoyAxis(int stick, int axis, int value) override;
bool handleJoyHat(int stick, int hat, int value) override;
bool handleJoyHat(int stick, int hat, JoyHat value) override;
void handleEvent(Event::Type e);
void drawDialog() override;

View File

@ -522,7 +522,7 @@ void Dialog::handleJoyAxis(int stick, int axis, int value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Dialog::handleJoyHat(int stick, int hat, int value)
bool Dialog::handleJoyHat(int stick, int hat, JoyHat value)
{
Event::Type e =
instance().eventHandler().eventForJoyHat(stick, hat, value, kMenuMode);

View File

@ -31,6 +31,7 @@ class CommandSender;
#include "Widget.hxx"
#include "GuiObject.hxx"
#include "StellaKeys.hxx"
#include "EventHandlerConstants.hxx"
#include "bspf.hxx"
/**
@ -93,7 +94,7 @@ class Dialog : public GuiObject
virtual void handleJoyDown(int stick, int button);
virtual void handleJoyUp(int stick, int button);
virtual void handleJoyAxis(int stick, int axis, int value);
virtual bool handleJoyHat(int stick, int hat, int value);
virtual bool handleJoyHat(int stick, int hat, JoyHat value);
virtual void handleCommand(CommandSender* sender, int cmd, int data, int id) override;
Widget* findWidget(int x, int y) const; // Find the widget at pos x,y if any

View File

@ -205,11 +205,11 @@ void DialogContainer::handleMouseButtonEvent(MouseButton b, int x, int y)
Dialog* activeDialog = myDialogStack.top();
activeDialog->surface().translateCoords(x, y);
int button = (b == EVENT_LBUTTONDOWN || b == EVENT_LBUTTONUP) ? 1 : 2;
int button = (b == MouseButton::LBUTTONDOWN || b == MouseButton::LBUTTONUP) ? 1 : 2;
switch(b)
{
case EVENT_LBUTTONDOWN:
case EVENT_RBUTTONDOWN:
case MouseButton::LBUTTONDOWN:
case MouseButton::RBUTTONDOWN:
// If more than two clicks have been recorded, we start over
if(myLastClick.count == 2)
{
@ -249,8 +249,8 @@ void DialogContainer::handleMouseButtonEvent(MouseButton b, int x, int y)
button, myLastClick.count);
break;
case EVENT_LBUTTONUP:
case EVENT_RBUTTONUP:
case MouseButton::LBUTTONUP:
case MouseButton::RBUTTONUP:
activeDialog->handleMouseUp(x - activeDialog->_x, y - activeDialog->_y,
button, myLastClick.count);
@ -258,11 +258,11 @@ void DialogContainer::handleMouseButtonEvent(MouseButton b, int x, int y)
myCurrentMouseDown.button = -1;
break;
case EVENT_WHEELUP:
case MouseButton::WHEELUP:
activeDialog->handleMouseWheel(x - activeDialog->_x, y - activeDialog->_y, -1);
break;
case EVENT_WHEELDOWN:
case MouseButton::WHEELDOWN:
activeDialog->handleMouseWheel(x - activeDialog->_x, y - activeDialog->_y, 1);
break;
}
@ -324,11 +324,11 @@ void DialogContainer::handleJoyHatEvent(int stick, int hat, JoyHat value)
return;
// Only stop firing events if it's the current stick
if(myCurrentHatDown.stick == stick && value == EVENT_HATCENTER)
if(myCurrentHatDown.stick == stick && value == JoyHat::CENTER)
{
myCurrentHatDown.stick = myCurrentHatDown.hat = -1;
}
else if(value != EVENT_HATCENTER) // never repeat the 'center' direction
else if(value != JoyHat::CENTER) // never repeat the 'center' direction
{
// Now account for repeated hat events (press and hold)
myCurrentHatDown.stick = stick;

View File

@ -196,7 +196,7 @@ class DialogContainer
struct {
int stick;
int hat;
int value;
JoyHat value;
} myCurrentHatDown;
uInt64 myHatRepeatTime;

View File

@ -303,7 +303,7 @@ void EventMappingWidget::handleJoyAxis(int stick, int axis, int value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EventMappingWidget::handleJoyHat(int stick, int hat, int value)
bool EventMappingWidget::handleJoyHat(int stick, int hat, JoyHat value)
{
// Remap joystick hats in remap mode
// There are two phases to detection:
@ -312,19 +312,19 @@ bool EventMappingWidget::handleJoyHat(int stick, int hat, int value)
if(myRemapStatus && myActionSelected >= 0)
{
// Detect the first hat event that represents a valid direction
if(myLastStick == -1 && myLastHat == -1 && value != EVENT_HATCENTER)
if(myLastStick == -1 && myLastHat == -1 && value != JoyHat::CENTER)
{
myLastStick = stick;
myLastHat = hat;
myLastValue = value;
myLastValue = int(value);
return true;
}
// Detect the first hat event that matches a previously set
// stick and hat, but centers the hat
else if(myLastStick == stick && hat == myLastHat && value == EVENT_HATCENTER)
else if(myLastStick == stick && hat == myLastHat && value == JoyHat::CENTER)
{
value = myLastValue;
value = JoyHat(myLastValue);
Event::Type event =
instance().eventHandler().eventAtIndex(myActionSelected, myEventMode);

View File

@ -60,7 +60,7 @@ class EventMappingWidget : public Widget, public CommandSender
bool handleKeyDown(StellaKey key, StellaMod mod) override;
void handleJoyDown(int stick, int button) override;
void handleJoyAxis(int stick, int axis, int value) override;
bool handleJoyHat(int stick, int hat, int value) override;
bool handleJoyHat(int stick, int hat, JoyHat value) override;
void loadConfig() override;
void saveConfig();

View File

@ -453,7 +453,7 @@ void InputDialog::handleJoyAxis(int stick, int axis, int value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool InputDialog::handleJoyHat(int stick, int hat, int value)
bool InputDialog::handleJoyHat(int stick, int hat, JoyHat value)
{
// Remap joystick hat in remap mode, otherwise pass to parent dialog
if(myEmulEventMapper->remapMode())

View File

@ -46,7 +46,7 @@ class InputDialog : public Dialog
void handleKeyDown(StellaKey key, StellaMod mod) override;
void handleJoyDown(int stick, int button) override;
void handleJoyAxis(int stick, int axis, int value) override;
bool handleJoyHat(int stick, int hat, int value) override;
bool handleJoyHat(int stick, int hat, JoyHat value) override;
void handleCommand(CommandSender* sender, int cmd, int data, int id) override;
void loadConfig() override;

View File

@ -78,7 +78,7 @@ class Widget : public GuiObject
virtual void handleJoyDown(int stick, int button) { }
virtual void handleJoyUp(int stick, int button) { }
virtual void handleJoyAxis(int stick, int axis, int value) { }
virtual bool handleJoyHat(int stick, int hat, int value) { return false; }
virtual bool handleJoyHat(int stick, int hat, JoyHat value) { return false; }
virtual bool handleEvent(Event::Type event) { return false; }
void draw() override;