Skip to content

Commit

Permalink
CGE: Rewrite keymapper and add joystick support - bug 15187
Browse files Browse the repository at this point in the history
Also, clean up the keyboard input code
  • Loading branch information
bluegr committed Aug 9, 2024
1 parent 81bc533 commit 0a8d1e0
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 104 deletions.
23 changes: 23 additions & 0 deletions engines/cge/cge.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ class Talk;

#define kSayTheEnd 41

enum CGEAction {
kActionNone,
kActionInfo,
kActionEscape,
kActionSave,
kActionLoad,
kActionQuit,
kActionInv1,
kActionInv2,
kActionInv3,
kActionInv4,
kActionInv5,
kActionInv6,
kActionInv7,
kActionInv8,
kActionAltDice,
kActionLevel0,
kActionLevel1,
kActionLevel2,
kActionLevel3,
kActionLevel4
};

// our engine debug channels
enum {
kCGEDebugBitmap = 1 << 0,
Expand Down
24 changes: 11 additions & 13 deletions engines/cge/cge_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ Square::Square(CGEEngine *vm) : Sprite(vm, nullptr), _vm(vm) {
setShapeList(MB);
}

void Square::touch(uint16 mask, int x, int y, Common::KeyCode keyCode) {
Sprite::touch(mask, x, y, keyCode);
void Square::touch(uint16 mask, int x, int y) {
Sprite::touch(mask, x, y);
if (mask & kMouseLeftUp) {
_vm->XZ(_x + x, _y + y).cell() = 0;
_vm->_commandHandlerTurbo->addCommand(kCmdKill, -1, 0, this);
Expand Down Expand Up @@ -772,18 +772,16 @@ void System::funTouch() {
_funDel = n;
}

void System::touch(uint16 mask, int x, int y, Common::KeyCode keyCode) {
void System::touch(uint16 mask, int x, int y) {
funTouch();

if (mask & kEventKeyb) {
if (keyCode == Common::KEYCODE_ESCAPE) {
// The original was calling keyClick()
// The sound is uselessly annoying and noisy, so it has been removed
_vm->killText();
if (_vm->_startupMode == 1) {
_vm->_commandHandler->addCommand(kCmdClear, -1, 0, nullptr);
return;
}
if (mask & kEventEsc) {
// The original was calling keyClick()
// The sound is uselessly annoying and noisy, so it has been removed
_vm->killText();
if (_vm->_startupMode == 1) {
_vm->_commandHandler->addCommand(kCmdClear, -1, 0, nullptr);
return;
}
} else {
if (_vm->_startupMode)
Expand Down Expand Up @@ -942,7 +940,7 @@ void CGEEngine::optionTouch(int opt, uint16 mask) {
}

#pragma argsused
void Sprite::touch(uint16 mask, int x, int y, Common::KeyCode keyCode) {
void Sprite::touch(uint16 mask, int x, int y) {
_vm->_sys->funTouch();

if ((mask & kEventAttn) != 0)
Expand Down
4 changes: 2 additions & 2 deletions engines/cge/cge_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class System : public Sprite {

void setPal();
void funTouch();
void touch(uint16 mask, int x, int y, Common::KeyCode keyCode) override;
void touch(uint16 mask, int x, int y) override;
void tick() override;
private:
CGEEngine *_vm;
Expand All @@ -100,7 +100,7 @@ class System : public Sprite {
class Square : public Sprite {
public:
Square(CGEEngine *vm);
void touch(uint16 mask, int x, int y, Common::KeyCode keyCode) override;
void touch(uint16 mask, int x, int y) override;
private:
CGEEngine *_vm;
};
Expand Down
107 changes: 42 additions & 65 deletions engines/cge/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,72 +49,54 @@ Sprite *Keyboard::setClient(Sprite *spr) {
return spr;
}

bool Keyboard::getKey(Common::Event &event) {
Common::KeyCode keycode = event.kbd.keycode;

if (((keycode == Common::KEYCODE_LALT) || (keycode == Common::KEYCODE_RALT)) && event.type == Common::EVENT_KEYDOWN)
_keyAlt = true;
else
_keyAlt = false;

switch (keycode) {
case Common::KEYCODE_F1:
if (event.type == Common::EVENT_KEYUP)
return false;
void Keyboard::handleAction(Common::Event &event) {
switch (event.customType) {
case kActionInfo:
// Display ScummVM version and translation strings
for (int i = 0; i < 5; i++)
_vm->_commandHandler->addCommand(kCmdInf, 1, kShowScummVMVersion + i, nullptr);
return false;
case Common::KEYCODE_F5:
break;
case kActionSave:
_vm->saveGameDialog();
return false;
case Common::KEYCODE_F7:
break;
case kActionLoad:
_vm->loadGameDialog();
return false;
case Common::KEYCODE_x:
if (event.type == Common::EVENT_KEYDOWN && (event.kbd.flags & Common::KBD_ALT)) {
_vm->quit();
return false;
}
break;
case Common::KEYCODE_0:
case Common::KEYCODE_1:
case Common::KEYCODE_2:
case Common::KEYCODE_3:
case Common::KEYCODE_4:
if (event.kbd.flags & Common::KBD_ALT) {
_vm->_commandHandler->addCommand(kCmdLevel, -1, keycode - Common::KEYCODE_0, nullptr);
return false;
}
// fall through
case Common::KEYCODE_5:
case Common::KEYCODE_6:
case Common::KEYCODE_7:
case Common::KEYCODE_8:
if (event.type == Common::EVENT_KEYDOWN && !(event.kbd.flags & Common::KBD_ALT) && keycode != Common::KEYCODE_0) {
_vm->selectPocket(keycode - Common::KEYCODE_1);
return false;
case kActionQuit:
_vm->quit();
break;
case kActionEscape:
if (_client) {
CGEEvent &evt = _vm->_eventManager->getNextEvent();
evt._x = 0;
evt._y = 0;
evt._mask = kEventEsc; // Event mask
evt._spritePtr = _client; // Sprite pointer
}
break;
case kActionAltDice:
_keyAlt = true;
break;
case kActionInv1:
case kActionInv2:
case kActionInv3:
case kActionInv4:
case kActionInv5:
case kActionInv6:
case kActionInv7:
case kActionInv8:
_vm->selectPocket(event.customType - kActionInv1);
break;
case kActionLevel0:
case kActionLevel1:
case kActionLevel2:
case kActionLevel3:
case kActionLevel4:
_vm->_commandHandler->addCommand(kCmdLevel, -1, event.customType - kActionLevel0, nullptr);
break;
default:
break;
}

return true;
}

void Keyboard::newKeyboard(Common::Event &event) {
if (!getKey(event))
return;

if ((event.type == Common::EVENT_KEYDOWN) && (_client)) {
CGEEvent &evt = _vm->_eventManager->getNextEvent();
evt._x = 0;
evt._y = 0;
evt._keyCode = event.kbd.keycode; // Keycode
evt._mask = kEventKeyb; // Event mask
evt._spritePtr = _client; // Sprite pointer
}
}

/*----------------- MOUSE interface -----------------*/
Expand Down Expand Up @@ -180,7 +162,6 @@ void Mouse::newMouse(Common::Event &event) {
CGEEvent &evt = _vm->_eventManager->getNextEvent();
evt._x = event.mouse.x;
evt._y = event.mouse.y;
evt._keyCode = Common::KEYCODE_INVALID;
evt._spritePtr = _vm->spriteAt(evt._x, evt._y);

switch (event.type) {
Expand Down Expand Up @@ -227,10 +208,9 @@ EventManager::EventManager(CGEEngine *vm) : _vm(vm){
void EventManager::poll() {
while (g_system->getEventManager()->pollEvent(_event)) {
switch (_event.type) {
case Common::EVENT_KEYDOWN:
case Common::EVENT_KEYUP:
case Common::EVENT_CUSTOM_ENGINE_ACTION_START:
// Handle keyboard events
_vm->_keyboard->newKeyboard(_event);
_vm->_keyboard->handleAction(_event);
handleEvents();
break;
case Common::EVENT_MOUSEMOVE:
Expand All @@ -253,20 +233,17 @@ void EventManager::handleEvents() {
CGEEvent e = _eventQueue[_eventQueueTail];
if (e._mask) {
if (_vm->_mouse->_hold && e._spritePtr != _vm->_mouse->_hold)
_vm->_mouse->_hold->touch(e._mask | kEventAttn, e._x - _vm->_mouse->_hold->_x, e._y - _vm->_mouse->_hold->_y, e._keyCode);
_vm->_mouse->_hold->touch(e._mask | kEventAttn, e._x - _vm->_mouse->_hold->_x, e._y - _vm->_mouse->_hold->_y);

// update mouse cursor position
if (e._mask & kMouseRoll)
_vm->_mouse->gotoxy(e._x, e._y);

// activate current touched SPRITE
if (e._spritePtr) {
if (e._mask & kEventKeyb)
e._spritePtr->touch(e._mask, e._x, e._y, e._keyCode);
else
e._spritePtr->touch(e._mask, e._x - e._spritePtr->_x, e._y - e._spritePtr->_y, e._keyCode);
e._spritePtr->touch(e._mask, e._x - e._spritePtr->_x, e._y - e._spritePtr->_y);
} else if (_vm->_sys)
_vm->_sys->touch(e._mask, e._x, e._y, e._keyCode);
_vm->_sys->touch(e._mask, e._x, e._y);

if (e._mask & kMouseLeftDown) {
_vm->_mouse->_hold = e._spritePtr;
Expand Down
6 changes: 2 additions & 4 deletions engines/cge/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@ enum EventMask {
kMouseRightDown = 1 << 3,
kMouseRightUp = 1 << 4,
kEventAttn = 1 << 5,
kEventKeyb = 1 << 7
kEventEsc = 1 << 7
};

class Keyboard {
private:
bool getKey(Common::Event &event);
CGEEngine *_vm;
public:
Sprite *_client;
bool _keyAlt;

void newKeyboard(Common::Event &event);
void handleAction(Common::Event &event);
Sprite *setClient(Sprite *spr);

Keyboard(CGEEngine *vm);
Expand All @@ -69,7 +68,6 @@ struct CGEEvent {
uint16 _mask;
uint16 _x;
uint16 _y;
Common::KeyCode _keyCode;
Sprite *_spritePtr;
};

Expand Down
Loading

0 comments on commit 0a8d1e0

Please sign in to comment.