Extended event infrastructure so that setting default events is determined

by the system-specific OSystemXXX class.  This is accomplished by overriding
certain methods in OSystem, and removes the need for numerous #ifdef's in
EventHandler.  In simpler terms, pressing 'Defaults' in the EventMapper
now uses defaults which are custom-defined to the specific port.

Added ability for joystick buttons to emulate axis events (and therefore
act as mouse events) for the internal GUI.  Some devices such as PSP and
GP2X don't have a directional pad, and send directions as button events
instead of axis events.  By overriding a method in OSystem, one can now
specify which buttons (if any) to treat as directional.

Added ability to remap the 'increase/decrease volume' events.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@936 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2006-01-05 18:53:23 +00:00
parent adc6354943
commit 98603c1e6c
9 changed files with 254 additions and 105 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: Event.hxx,v 1.18 2005-12-28 22:56:36 stephena Exp $
// $Id: Event.hxx,v 1.19 2006-01-05 18:53:22 stephena Exp $
//============================================================================
#ifndef EVENT_HXX
@ -26,7 +26,7 @@ class EventStreamer;
/**
@author Bradford W. Mott
@version $Id: Event.hxx,v 1.18 2005-12-28 22:56:36 stephena Exp $
@version $Id: Event.hxx,v 1.19 2006-01-05 18:53:22 stephena Exp $
*/
class Event
{
@ -78,6 +78,7 @@ class Event
ChangeState, LoadState, SaveState, TakeSnapshot, Pause, Quit,
MenuMode, CmdMenuMode, DebuggerMode, LauncherMode, Fry,
VolumeDecrease, VolumeIncrease,
LastType
};

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: EventHandler.cxx,v 1.139 2006-01-04 01:24:17 stephena Exp $
// $Id: EventHandler.cxx,v 1.140 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#include <sstream>
@ -870,7 +870,7 @@ void EventHandler::handleJoyAxisEvent(int stick, int axis, int value)
if(type == JA_NONE)
{
// TODO - will this always work??
if(value > 32667 - JOY_DEADZONE || value < -32767 + JOY_DEADZONE)
if(value > 32767 - JOY_DEADZONE || value < -32767 + JOY_DEADZONE)
type = myJoyAxisType[stick][axis] = JA_DIGITAL;
else
type = myJoyAxisType[stick][axis] = JA_ANALOG;
@ -992,6 +992,12 @@ void EventHandler::handleEvent(Event::Type event, int state)
myFryingFlag = bool(state);
return;
case Event::VolumeDecrease:
case Event::VolumeIncrease:
if(state && !myPauseFlag)
myOSystem->sound().adjustVolume(event == Event::VolumeIncrease ? 1 : -1);
return;
case Event::SaveState:
if(state && !myPauseFlag) saveState();
return;
@ -1265,35 +1271,56 @@ void EventHandler::addKeyMapping(Event::Type event, int key)
setActionMappings();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::setDefaultJoyMapping(Event::Type event, int stick, int button)
{
if(stick >= 0 && stick < kNumJoysticks &&
button >= 0 && button < kNumJoyButtons &&
event >= 0 && event < Event::LastType)
myJoyTable[stick][button] = event;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::addJoyMapping(Event::Type event, int stick, int button)
{
myJoyTable[stick][button] = event;
saveJoyMapping();
setDefaultJoyMapping(event, stick, button);
saveJoyMapping();
setActionMappings();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::setDefaultJoyAxisMapping(Event::Type event, int stick,
int axis, int value)
{
if(stick >= 0 && stick < kNumJoysticks &&
axis >= 0 && axis < kNumJoyAxis &&
event >= 0 && event < Event::LastType)
{
// This confusing code is because each axis has two associated values,
// but analog events only affect one of the axis.
if(eventIsAnalog(event))
myJoyAxisTable[stick][axis][0] = myJoyAxisTable[stick][axis][1] = event;
else
{
// Otherwise, turn off the analog event(s) for this axis
if(eventIsAnalog(myJoyAxisTable[stick][axis][0]))
myJoyAxisTable[stick][axis][0] = Event::NoType;
if(eventIsAnalog(myJoyAxisTable[stick][axis][1]))
myJoyAxisTable[stick][axis][1] = Event::NoType;
myJoyAxisTable[stick][axis][(value > 0)] = event;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::addJoyAxisMapping(Event::Type event, int stick, int axis,
int value)
{
// This confusing code is because each axis has two associated values,
// but analog events only affect one of the axis.
if(eventIsAnalog(event))
myJoyAxisTable[stick][axis][0] = myJoyAxisTable[stick][axis][1] = event;
else
{
// Otherwise, turn off the analog event(s) for this axis
if(eventIsAnalog(myJoyAxisTable[stick][axis][0]))
myJoyAxisTable[stick][axis][0] = Event::NoType;
if(eventIsAnalog(myJoyAxisTable[stick][axis][1]))
myJoyAxisTable[stick][axis][1] = Event::NoType;
myJoyAxisTable[stick][axis][(value > 0)] = event;
}
saveJoyAxisMapping();
setDefaultJoyAxisMapping(event, stick, axis, value);
saveJoyAxisMapping();
setActionMappings();
}
@ -1424,31 +1451,7 @@ void EventHandler::setDefaultJoymap()
for(int j = 0; j < kNumJoyButtons; ++j)
myJoyTable[i][j] = Event::NoType;
// Left joystick (assume joystick zero, button zero)
myJoyTable[0][0] = Event::JoystickZeroFire;
// Right joystick (assume joystick one, button zero)
myJoyTable[1][0] = Event::JoystickOneFire;
// FIXME - add call to OSystem (or some other class) to set default
// joy button mapping for the specific platform
#ifdef PSP
myJoyTable[0][0] = Event::TakeSnapshot; // Triangle
myJoyTable[0][1] = Event::LoadState; // Circle
myJoyTable[0][2] = Event::JoystickZeroFire; // Cross
myJoyTable[0][3] = Event::SaveState; // Square
myJoyTable[0][4] = Event::MenuMode; // Left trigger
myJoyTable[0][5] = Event::CmdMenuMode; // Right trigger
myJoyTable[0][6] = Event::JoystickZeroDown; // Down
myJoyTable[0][7] = Event::JoystickZeroLeft; // Left
myJoyTable[0][8] = Event::JoystickZeroUp; // Up
myJoyTable[0][9] = Event::JoystickZeroRight; // Right
myJoyTable[0][10] = Event::ConsoleSelect; // Select
myJoyTable[0][11] = Event::ConsoleReset; // Start
myJoyTable[0][12] = Event::NoType; // Home
myJoyTable[0][13] = Event::NoType; // Hold
#endif
myOSystem->setDefaultJoymap();
saveJoyMapping();
}
@ -1461,22 +1464,7 @@ void EventHandler::setDefaultJoyAxisMap()
for(int k = 0; k < 2; ++k)
myJoyAxisTable[i][j][k] = Event::NoType;
// Left joystick left/right directions (assume joystick zero)
myJoyAxisTable[0][0][0] = Event::JoystickZeroLeft;
myJoyAxisTable[0][0][1] = Event::JoystickZeroRight;
// Left joystick up/down directions (assume joystick zero)
myJoyAxisTable[0][1][0] = Event::JoystickZeroUp;
myJoyAxisTable[0][1][1] = Event::JoystickZeroDown;
// Right joystick left/right directions (assume joystick one)
myJoyAxisTable[1][0][0] = Event::JoystickOneLeft;
myJoyAxisTable[1][0][1] = Event::JoystickOneRight;
// Right joystick left/right directions (assume joystick one)
myJoyAxisTable[1][1][0] = Event::JoystickOneUp;
myJoyAxisTable[1][1][1] = Event::JoystickOneDown;
myOSystem->setDefaultJoyAxisMap();
saveJoyAxisMapping();
}
@ -2099,6 +2087,8 @@ ActionList EventHandler::ourActionList[kActionListSize] = {
{ Event::TakeSnapshot, "Snapshot", "" },
{ Event::Pause, "Pause", "" },
{ Event::Fry, "Fry cartridge", "" },
{ Event::VolumeDecrease, "Decrease volume", "" },
{ Event::VolumeIncrease, "Increase volume", "" },
{ Event::MenuMode, "Toggle options menu mode", "" },
{ Event::CmdMenuMode, "Toggle command menu mode", "" },
{ Event::DebuggerMode, "Toggle debugger mode", "" },

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: EventHandler.hxx,v 1.70 2005-12-28 22:56:36 stephena Exp $
// $Id: EventHandler.hxx,v 1.71 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#ifndef EVENTHANDLER_HXX
@ -51,7 +51,7 @@ struct ActionList {
};
enum {
kActionListSize = 79
kActionListSize = 81
};
// Joystick related items
@ -101,7 +101,7 @@ struct JoyMouse {
mapping can take place.
@author Stephen Anthony
@version $Id: EventHandler.hxx,v 1.70 2005-12-28 22:56:36 stephena Exp $
@version $Id: EventHandler.hxx,v 1.71 2006-01-05 18:53:23 stephena Exp $
*/
class EventHandler
{
@ -152,43 +152,24 @@ class EventHandler
void poll(uInt32 time);
/**
Bind a key to an event/action
Set the default action for a joystick button to the given event
@param event The event we are remapping
@param key The key to bind to this event
*/
void addKeyMapping(Event::Type event, int key);
/**
Bind a joystick button to an event/action
@param event The event we are remapping
@param event The event we are assigning
@param stick The joystick number
@param button The joystick button
*/
void addJoyMapping(Event::Type event, int stick, int button);
void setDefaultJoyMapping(Event::Type event, int stick, int button);
/**
Bind a joystick axis direction to an event/action
Set the default for a joystick axis to the given event
@param event The event we are remapping
@param event The event we are assigning
@param stick The joystick number
@param axis The joystick axis
@param value The value on the given axis
*/
void addJoyAxisMapping(Event::Type event, int stick, int axis, int value);
/**
Erase the specified mapping
@event The event for which we erase all mappings
*/
void eraseMapping(Event::Type event);
/**
Resets the event mappings to default values
*/
void setDefaultMapping();
void setDefaultJoyAxisMapping(Event::Type event, int stick, int axis,
int value);
/**
Returns the current state of the EventHandler
@ -314,6 +295,47 @@ class EventHandler
void createMouseButtonEvent(int x, int y, int state);
private:
/**
Bind a key to an event/action and regenerate the mapping array(s)
@param event The event we are remapping
@param key The key to bind to this event
*/
void addKeyMapping(Event::Type event, int key);
/**
Bind a joystick button to an event/action and regenerate the
mapping array(s)
@param event The event we are remapping
@param stick The joystick number
@param button The joystick button
*/
void addJoyMapping(Event::Type event, int stick, int button);
/**
Bind a joystick axis direction to an event/action and regenerate
the mapping array(s)
@param event The event we are remapping
@param stick The joystick number
@param axis The joystick axis
@param value The value on the given axis
*/
void addJoyAxisMapping(Event::Type event, int stick, int axis, int value);
/**
Erase the specified mapping
@event The event for which we erase all mappings
*/
void eraseMapping(Event::Type event);
/**
Resets the event mappings to default values
*/
void setDefaultMapping();
/**
Send a mouse motion event to the handler.

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystem.cxx,v 1.51 2005-12-23 20:48:50 stephena Exp $
// $Id: OSystem.cxx,v 1.52 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#include <cassert>
@ -445,6 +445,42 @@ bool OSystem::openROM(const string& rom, string& md5, uInt8** image, int* size)
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::getJoyButtonDirections(int& up, int& down, int& left, int& right)
{
up = down = left = right = -1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::setDefaultJoymap()
{
// Left joystick (assume joystick zero, button zero)
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroFire, 0, 0);
// Right joystick (assume joystick one, button zero)
myEventHandler->setDefaultJoyMapping(Event::JoystickOneFire, 1, 0);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::setDefaultJoyAxisMap()
{
// Left joystick left/right directions (assume joystick zero)
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickZeroLeft, 0, 0, 0);
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickZeroRight, 0, 0, 1);
// Left joystick up/down directions (assume joystick zero)
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickZeroUp, 0, 1, 0);
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickZeroDown, 0, 1, 1);
// Right joystick left/right directions (assume joystick one)
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickOneLeft, 1, 0, 0);
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickOneRight, 1, 0, 1);
// Right joystick left/right directions (assume joystick one)
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickOneUp, 1, 1, 0);
myEventHandler->setDefaultJoyAxisMapping(Event::JoystickOneDown, 1, 1, 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OSystem::OSystem(const OSystem& osystem)
{

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystem.hxx,v 1.32 2005-11-21 13:47:34 stephena Exp $
// $Id: OSystem.hxx,v 1.33 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#ifndef OSYSTEM_HXX
@ -44,7 +44,7 @@ class CheatManager;
other objects belong.
@author Stephen Anthony
@version $Id: OSystem.hxx,v 1.32 2005-11-21 13:47:34 stephena Exp $
@version $Id: OSystem.hxx,v 1.33 2006-01-05 18:53:23 stephena Exp $
*/
class OSystem
{
@ -313,6 +313,34 @@ class OSystem
*/
virtual uInt32 getTicks() = 0;
//////////////////////////////////////////////////////////////////////
// The following methods are system-specific and can be overrided in
// derived classes. Otherwise, the base methods will be used.
//////////////////////////////////////////////////////////////////////
/**
This method gives joystick button numbers representing the 'up', 'down',
'left' and 'right' directions for use in the internal GUI. A normal
joystick will use axes for this, but some hardware uses buttons instead.
@up Button number to assign to the 'up' direction
@down Button number to assign to the 'down' direction
@left Button number to assign to the 'left' direction
@right Button number to assign to the 'right' direction
*/
virtual void getJoyButtonDirections(int& up, int& down, int& left, int& right);
/**
This method determines the default mapping of joystick buttons to
Stella events for a specific system/platform.
*/
virtual void setDefaultJoymap();
/**
This method determines the default mapping of joystick axis to
Stella events for a specific system/platform.
*/
virtual void setDefaultJoyAxisMap();
protected:
/**
Set the base directory for all Stella files

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: DialogContainer.cxx,v 1.23 2006-01-04 01:24:17 stephena Exp $
// $Id: DialogContainer.cxx,v 1.24 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#include "OSystem.hxx"
@ -282,8 +282,24 @@ void DialogContainer::handleJoyEvent(int stick, int button, uInt8 state)
activeDialog->handleJoyUp(stick, button);
}
else
myOSystem->eventHandler().createMouseButtonEvent(
ourJoyMouse.x, ourJoyMouse.y, state);
{
// Some buttons act as directions. In those cases, translate them
// to axis events instead of mouse button events
int up, down, left, right = -1;
int value = state > 0 ? 32767 : 0;
myOSystem->getJoyButtonDirections(up, down, left, right);
if(button == up)
handleJoyAxisEvent(stick, 1, value); // axis 1, +value ==> UP
else if(button == down)
handleJoyAxisEvent(stick, 1, -value); // axis 1, -value ==> DOWN
else if(button == left)
handleJoyAxisEvent(stick, 0, value); // axis 0, +value ==> LEFT
else if(button == right)
handleJoyAxisEvent(stick, 0, -value); // axis 0, -value ==> RIGHT
else
myOSystem->eventHandler().createMouseButtonEvent(
ourJoyMouse.x, ourJoyMouse.y, state);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: LauncherDialog.cxx,v 1.35 2006-01-04 01:24:17 stephena Exp $
// $Id: LauncherDialog.cxx,v 1.36 2006-01-05 18:53:23 stephena Exp $
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
@ -91,6 +91,7 @@ LauncherDialog::LauncherDialog(OSystem* osystem, DialogContainer* parent,
myQuitButton->setEditable(true);
wid.push_back(myQuitButton);
xpos += space + width;
mySelectedItem = 0; // Highlight 'Play' button
#else
myQuitButton = new ButtonWidget(this, xpos, _h - 24, width, 16, "Quit", kQuitCmd, 'Q');
myQuitButton->setEditable(true);
@ -108,6 +109,7 @@ LauncherDialog::LauncherDialog(OSystem* osystem, DialogContainer* parent,
myStartButton->setEditable(true);
wid.push_back(myStartButton);
xpos += space + width;
mySelectedItem = 3; // Highlight 'Play' button
#endif
// Add list with game titles
@ -151,6 +153,8 @@ void LauncherDialog::loadConfig()
// has been called (and we should reload the list).
if(myList->getList().isEmpty())
updateListing();
Dialog::setFocus(getFocusList()[mySelectedItem]);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystemPSP.cxx,v 1.3 2005-09-18 14:31:36 optixx Exp $
// $Id: OSystemPSP.cxx,v 1.4 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#include <cstdlib>
@ -101,10 +101,12 @@ void OSystemPSP::mainLoop()
{
scePowerSetClockFrequency(333,333,166);
fprintf(stderr,"OSystemPSP::mainLoop overclock to 333\n");
} else
}
else
{
fprintf(stderr,"OSystemPSP::mainLoop NOT overclock\n");
}
// Main game loop
for(;;)
{
@ -138,3 +140,35 @@ uInt32 OSystemPSP::getTicks()
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystemPSP::getJoyButtonDirections(int& up, int& down, int& left, int& right)
{
up = 8;
down = 6;
left = 7;
right = 9;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystemPSP::setDefaultJoymap()
{
myEventHandler->setDefaultJoyMapping(Event::TakeSnapshot, 0, 0); // Triangle
myEventHandler->setDefaultJoyMapping(Event::LoadState, 0, 1); // Circle
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroFire, 0, 2); // Cross
myEventHandler->setDefaultJoyMapping(Event::SaveState, 0, 3); // Square
myEventHandler->setDefaultJoyMapping(Event::MenuMode, 0, 4); // Left trigger
myEventHandler->setDefaultJoyMapping(Event::CmdMenuMode, 0, 5); // Right trigger
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroDown, 0, 6); // Down
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroLeft, 0, 7); // Left
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroUp, 0, 8); // Up
myEventHandler->setDefaultJoyMapping(Event::JoystickZeroRight, 0, 9); // Right
myEventHandler->setDefaultJoyMapping(Event::ConsoleSelect, 0, 10); // Select
myEventHandler->setDefaultJoyMapping(Event::ConsoleReset, 0, 11); // Start
myEventHandler->setDefaultJoyMapping(Event::NoType, 0, 12); // Home
myEventHandler->setDefaultJoyMapping(Event::NoType, 0, 13); // Hold
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystemPSP::setDefaultJoyAxisMap()
{
}

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystemPSP.hxx,v 1.1 2005-08-25 15:19:17 stephena Exp $
// $Id: OSystemPSP.hxx,v 1.2 2006-01-05 18:53:23 stephena Exp $
//============================================================================
#ifndef OSYSTEM_PSP_HXX
@ -23,10 +23,10 @@
/**
This class defines PSP-like OS's (Linux) system specific settings.
This class defines PSP-specific settings.
@author Stephen Anthony
@version $Id: OSystemPSP.hxx,v 1.1 2005-08-25 15:19:17 stephena Exp $
@version $Id: OSystemPSP.hxx,v 1.2 2006-01-05 18:53:23 stephena Exp $
*/
class OSystemPSP : public OSystem
{
@ -47,14 +47,32 @@ class OSystemPSP : public OSystem
may use different timing methods and/or algorithms, this method has
been abstracted to each platform.
*/
virtual void mainLoop();
void mainLoop();
/**
This method returns number of ticks in microseconds.
@return Current time in microseconds.
*/
virtual uInt32 getTicks();
uInt32 getTicks();
/**
This method gives joystick button numbers representing the 'up', 'down',
'left' and 'right' directions for use in the internal GUI.
*/
void getJoyButtonDirections(int& up, int& down, int& left, int& right);
/**
This method determines the default mapping of joystick buttons to
Stella events for the PSP device.
*/
void setDefaultJoymap();
/**
This method determines the default mapping of joystick axis to
Stella events for for the PSP device.
*/
void setDefaultJoyAxisMap();
};
#endif