MYST3: Implement dragging using the gamepad

Used in the Xbox version menus
This commit is contained in:
Bastien Bouclet 2014-09-14 20:18:54 +02:00
parent 402517fbee
commit ea3fcdd96f
3 changed files with 57 additions and 32 deletions

View file

@ -1886,54 +1886,77 @@ void Script::runScriptWhileDragging(Context &c, const Opcode &cmd) {
_vm->_cursor->changeCursor(2); _vm->_cursor->changeCursor(2);
bool mousePressed = true; bool dragWithDirectionKeys = _vm->_state->hasVarDragWithDirectionKeys()
&& _vm->_state->getDragWithDirectionKeys();
bool dragging = true;
do { do {
mousePressed = _vm->getEventManager()->getButtonState() & Common::EventManager::LBUTTON; dragging = _vm->getEventManager()->getButtonState() & Common::EventManager::LBUTTON;
_vm->_state->setDragEnded(!mousePressed); dragging |= _vm->_state->hasVarGamePadActionPressed() && _vm->_state->getGamePadActionPressed();
_vm->_state->setDragEnded(!dragging);
_vm->processInput(true); _vm->processInput(true);
_vm->drawFrame(); _vm->drawFrame();
// Distance between the mouse and the lever if (!dragWithDirectionKeys) {
Common::Point mouse = _vm->_cursor->getPosition(); // Distance between the mouse and the lever
int16 distanceX = mouse.x - leverWidth / 2 - _vm->_state->getVar(cmd.args[0]); Common::Point mouse = _vm->_cursor->getPosition();
int16 distanceY = mouse.y - leverHeight / 2 - _vm->_state->getVar(cmd.args[1]) - topOffset; int16 distanceX = mouse.x - leverWidth / 2 - _vm->_state->getVar(cmd.args[0]);
float distance = sqrt((float) distanceX * distanceX + distanceY * distanceY); int16 distanceY = mouse.y - leverHeight / 2 - _vm->_state->getVar(cmd.args[1]) - topOffset;
float distance = sqrt((float) distanceX * distanceX + distanceY * distanceY);
uint16 bestPosition = lastLeverPosition; uint16 bestPosition = lastLeverPosition;
if (distance > maxDistance) { if (distance > maxDistance) {
_vm->_state->setDragLeverPositionChanged(false); _vm->_state->setDragLeverPositionChanged(false);
} else { } else {
// Find the lever position where the distance between the lever // Find the lever position where the distance between the lever
// and the mouse is minimal, by trying every possible position. // and the mouse is minimal, by trying every possible position.
float minDistance = 1000; float minDistance = 1000;
for (uint i = 0; i < maxLeverPosition; i++) { for (uint i = 0; i < maxLeverPosition; i++) {
_vm->_state->setDragPositionFound(false); _vm->_state->setDragPositionFound(false);
_vm->_state->setVar(cmd.args[4], i); _vm->_state->setVar(cmd.args[4], i);
_vm->runScriptsFromNode(script); _vm->runScriptsFromNode(script);
mouse = _vm->_cursor->getPosition(); mouse = _vm->_cursor->getPosition();
distanceX = mouse.x - leverWidth / 2 - _vm->_state->getVar(cmd.args[0]); distanceX = mouse.x - leverWidth / 2 - _vm->_state->getVar(cmd.args[0]);
distanceY = mouse.y - leverHeight / 2 - _vm->_state->getVar(cmd.args[1]) - topOffset; distanceY = mouse.y - leverHeight / 2 - _vm->_state->getVar(cmd.args[1]) - topOffset;
distance = sqrt((float) distanceX * distanceX + distanceY * distanceY); distance = sqrt((float) distanceX * distanceX + distanceY * distanceY);
if (distance < minDistance) { if (distance < minDistance) {
minDistance = distance; minDistance = distance;
bestPosition = i; bestPosition = i;
}
} }
_vm->_state->setDragLeverPositionChanged(bestPosition != lastLeverPosition);
} }
_vm->_state->setDragLeverPositionChanged(bestPosition != lastLeverPosition);
}
// Set the lever position to the best position // Set the lever position to the best position
_vm->_state->setDragPositionFound(true); _vm->_state->setDragPositionFound(true);
_vm->_state->setVar(cmd.args[4], bestPosition); _vm->_state->setVar(cmd.args[4], bestPosition);
} else {
uint16 previousPosition = _vm->_state->getVar(cmd.args[4]);
uint16 position = previousPosition;
if (_vm->_state->getGamePadLeftPressed()) {
position--;
} else if (_vm->_state->getGamePadRightPressed()) {
position++;
}
position = CLIP<int16>(position, 0, maxLeverPosition);
_vm->_state->setVar(cmd.args[4], position);
_vm->_state->setDragLeverPositionChanged(position != previousPosition);
}
_vm->runScriptsFromNode(script); _vm->runScriptsFromNode(script);
_vm->processInput(true); _vm->processInput(true);
_vm->drawFrame(); _vm->drawFrame();
} while (mousePressed); } while (dragging);
if (dragWithDirectionKeys) {
_vm->_state->setDragWithDirectionKeys(false);
}
} }
void Script::chooseNextNode(Context &c, const Opcode &cmd) { void Script::chooseNextNode(Context &c, const Opcode &cmd) {

View file

@ -373,6 +373,7 @@ GameState::GameState(Myst3Engine *vm):
VAR(1434, GamePadRightPressed, false) VAR(1434, GamePadRightPressed, false)
VAR(1435, GamePadCancelPressed, false) VAR(1435, GamePadCancelPressed, false)
VAR(1437, DragWithDirectionKeys, false)
VAR(1438, MenuAttractCountDown, false) VAR(1438, MenuAttractCountDown, false)
VAR(1439, ShieldEffectActive, false) VAR(1439, ShieldEffectActive, false)

View file

@ -297,6 +297,7 @@ public:
DECLARE_VAR(GamePadRightPressed) DECLARE_VAR(GamePadRightPressed)
DECLARE_VAR(GamePadCancelPressed) DECLARE_VAR(GamePadCancelPressed)
DECLARE_VAR(DragWithDirectionKeys)
DECLARE_VAR(MenuSavesAvailable) DECLARE_VAR(MenuSavesAvailable)
DECLARE_VAR(MenuSelectedSave) DECLARE_VAR(MenuSelectedSave)
DECLARE_VAR(MenuAttractCountDown) DECLARE_VAR(MenuAttractCountDown)