From caccceb02b7de1812190114373f69e574e570308 Mon Sep 17 00:00:00 2001 From: Bastien Bouclet Date: Sat, 22 Jan 2011 17:26:09 +0000 Subject: MOHAWK: Dragging resources / levers code cleanup svn-id: r55439 --- engines/mohawk/myst.cpp | 9 ++-- engines/mohawk/myst.h | 1 - engines/mohawk/myst_areas.cpp | 78 ++++++++++++++++++++++-------- engines/mohawk/myst_areas.h | 28 ++++++----- engines/mohawk/myst_stacks/channelwood.cpp | 61 +++++++---------------- engines/mohawk/myst_stacks/myst.cpp | 61 +++++------------------ 6 files changed, 106 insertions(+), 132 deletions(-) (limited to 'engines/mohawk') diff --git a/engines/mohawk/myst.cpp b/engines/mohawk/myst.cpp index c7fa89e16a..fe2ffd5879 100644 --- a/engines/mohawk/myst.cpp +++ b/engines/mohawk/myst.cpp @@ -302,7 +302,6 @@ Common::Error MohawkEngine_Myst::run() { switch (event.type) { case Common::EVENT_MOUSEMOVE: { _needsUpdate = true; - _mouse = event.mouse; bool mouseClicked = _system->getEventManager()->getButtonState() & 1; // Keep the same resource when dragging @@ -311,23 +310,21 @@ Common::Error MohawkEngine_Myst::run() { } if (_curResource >= 0 && _resources[_curResource]->isEnabled() && mouseClicked) { debug(2, "Sending mouse move event to resource %d", _curResource); - _resources[_curResource]->handleMouseDrag(event.mouse); + _resources[_curResource]->handleMouseDrag(); } break; } case Common::EVENT_LBUTTONUP: - _mouse = event.mouse; if (_curResource >= 0 && _resources[_curResource]->isEnabled()) { debug(2, "Sending mouse up event to resource %d", _curResource); - _resources[_curResource]->handleMouseUp(event.mouse); + _resources[_curResource]->handleMouseUp(); } checkCurrentResource(); break; case Common::EVENT_LBUTTONDOWN: - _mouse = event.mouse; if (_curResource >= 0 && _resources[_curResource]->isEnabled()) { debug(2, "Sending mouse up event to resource %d", _curResource); - _resources[_curResource]->handleMouseDown(event.mouse); + _resources[_curResource]->handleMouseDown(); } break; case Common::EVENT_KEYDOWN: diff --git a/engines/mohawk/myst.h b/engines/mohawk/myst.h index 7e9a1bd37d..5fd405434d 100644 --- a/engines/mohawk/myst.h +++ b/engines/mohawk/myst.h @@ -180,7 +180,6 @@ public: MystScriptParser *_scriptParser; Common::Array _resources; MystResource *_dragResource; - Common::Point _mouse; Common::RandomSource *_rnd; bool _showResourceRects; diff --git a/engines/mohawk/myst_areas.cpp b/engines/mohawk/myst_areas.cpp index a571a4905f..8198e3279c 100644 --- a/engines/mohawk/myst_areas.cpp +++ b/engines/mohawk/myst_areas.cpp @@ -68,7 +68,7 @@ MystResource::MystResource(MohawkEngine_Myst *vm, Common::SeekableReadStream *rl MystResource::~MystResource() { } -void MystResource::handleMouseUp(const Common::Point &mouse) { +void MystResource::handleMouseUp() { if (_dest != 0) _vm->changeToCard(_dest, true); else @@ -122,7 +122,7 @@ MystResourceType5::MystResourceType5(MohawkEngine_Myst *vm, Common::SeekableRead _script = vm->_scriptParser->readScript(rlstStream, kMystScriptNormal); } -void MystResourceType5::handleMouseUp(const Common::Point &mouse) { +void MystResourceType5::handleMouseUp() { _vm->_scriptParser->runScript(_script, this); } @@ -285,40 +285,40 @@ void MystResourceType7::handleCardChange() { } } -void MystResourceType7::handleMouseUp(const Common::Point &mouse) { +void MystResourceType7::handleMouseUp() { if (_var7 == 0xFFFF) { if (_numSubResources == 1) - _subResources[0]->handleMouseUp(mouse); + _subResources[0]->handleMouseUp(); else if (_numSubResources != 0) warning("Type 7 Resource with _numSubResources of %d, but no control variable", _numSubResources); } else { uint16 varValue = _vm->_scriptParser->getVar(_var7); if (_numSubResources == 1 && varValue != 0) - _subResources[0]->handleMouseUp(mouse); + _subResources[0]->handleMouseUp(); else if (_numSubResources != 0) { if (varValue < _numSubResources) - _subResources[varValue]->handleMouseUp(mouse); + _subResources[varValue]->handleMouseUp(); else warning("Type 7 Resource Var %d: %d exceeds number of sub resources %d", _var7, varValue, _numSubResources); } } } -void MystResourceType7::handleMouseDown(const Common::Point &mouse) { +void MystResourceType7::handleMouseDown() { if (_var7 == 0xFFFF) { if (_numSubResources == 1) - _subResources[0]->handleMouseDown(mouse); + _subResources[0]->handleMouseDown(); else if (_numSubResources != 0) warning("Type 7 Resource with _numSubResources of %d, but no control variable", _numSubResources); } else { uint16 varValue = _vm->_scriptParser->getVar(_var7); if (_numSubResources == 1 && varValue != 0) - _subResources[0]->handleMouseDown(mouse); + _subResources[0]->handleMouseDown(); else if (_numSubResources != 0) { if (varValue < _numSubResources) - _subResources[varValue]->handleMouseDown(mouse); + _subResources[varValue]->handleMouseDown(); else warning("Type 7 Resource Var %d: %d exceeds number of sub resources %d", _var7, varValue, _numSubResources); } @@ -515,13 +515,14 @@ void MystResourceType10::restoreBackground() { _vm->_gfx->copyImageSectionToScreen(_vm->getCardBackgroundId(), src, dest); } -void MystResourceType10::handleMouseDown(const Common::Point &mouse) { +void MystResourceType10::handleMouseDown() { // Tell the engine we are dragging a resource _vm->_dragResource = this; + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); updatePosition(mouse); - MystResourceType11::handleMouseDown(mouse); + MystResourceType11::handleMouseDown(); // Restore background restoreBackground(); @@ -530,7 +531,8 @@ void MystResourceType10::handleMouseDown(const Common::Point &mouse) { drawConditionalDataToScreen(2); } -void MystResourceType10::handleMouseUp(const Common::Point &mouse) { +void MystResourceType10::handleMouseUp() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); updatePosition(mouse); // Restore background @@ -555,16 +557,17 @@ void MystResourceType10::handleMouseUp(const Common::Point &mouse) { _vm->_scriptParser->setVarValue(_var8, value); - MystResourceType11::handleMouseUp(mouse); + MystResourceType11::handleMouseUp(); // No longer in drag mode _vm->_dragResource = 0; } -void MystResourceType10::handleMouseDrag(const Common::Point &mouse) { +void MystResourceType10::handleMouseDrag() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); updatePosition(mouse); - MystResourceType11::handleMouseDrag(mouse); + MystResourceType11::handleMouseDrag(); // Restore background restoreBackground(); @@ -681,21 +684,24 @@ MystResourceType11::~MystResourceType11() { delete[] _lists[i].list; } -void MystResourceType11::handleMouseDown(const Common::Point &mouse) { +void MystResourceType11::handleMouseDown() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); setPositionClipping(mouse, _pos); _vm->_scriptParser->setInvokingResource(this); _vm->_scriptParser->runOpcode(_mouseDownOpcode, _var8); } -void MystResourceType11::handleMouseUp(const Common::Point &mouse) { +void MystResourceType11::handleMouseUp() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); setPositionClipping(mouse, _pos); _vm->_scriptParser->setInvokingResource(this); _vm->_scriptParser->runOpcode(_mouseUpOpcode, _var8); } -void MystResourceType11::handleMouseDrag(const Common::Point &mouse) { +void MystResourceType11::handleMouseDrag() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); setPositionClipping(mouse, _pos); _vm->_scriptParser->setInvokingResource(this); @@ -760,6 +766,38 @@ void MystResourceType12::drawFrame(uint16 frame) { _vm->_system->updateScreen(); } +bool MystResourceType12::pullLeverV() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); + + // Make the handle follow the mouse + int16 maxStep = getStepsV() - 1; + Common::Rect rect = getRect(); + int16 step = ((mouse.y - rect.top) * getStepsV()) / rect.height(); + step = CLIP(step, 0, maxStep); + + // Draw current frame + drawFrame(step); + + // Return true if lever fully pulled + return step == maxStep; +} + +void MystResourceType12::releaseLeverV() { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); + + // Get current lever frame + int16 maxStep = getStepsV() - 1; + Common::Rect rect = getRect(); + int16 step = ((mouse.y - rect.top) * getStepsV()) / rect.height(); + step = CLIP(step, 0, maxStep); + + // Release lever + for (int i = step; i >= 0; i--) { + drawFrame(i); + _vm->_system->delayMillis(10); + } +} + MystResourceType13::MystResourceType13(MohawkEngine_Myst *vm, Common::SeekableReadStream *rlstStream, MystResource *parent) : MystResource(vm, rlstStream, parent) { _enterOpcode = rlstStream->readUint16LE(); _leaveOpcode = rlstStream->readUint16LE(); @@ -780,7 +818,7 @@ void MystResourceType13::handleMouseLeave() { _vm->_scriptParser->runOpcode(_leaveOpcode, _dest); } -void MystResourceType13::handleMouseUp(const Common::Point &mouse) { +void MystResourceType13::handleMouseUp() { // Type 13 Resources do nothing on Mouse Clicks. // This is required to override the inherited default // i.e. MystResource::handleMouseUp diff --git a/engines/mohawk/myst_areas.h b/engines/mohawk/myst_areas.h index 43af2a73f7..b7e8358d17 100644 --- a/engines/mohawk/myst_areas.h +++ b/engines/mohawk/myst_areas.h @@ -80,9 +80,9 @@ public: bool canBecomeActive(); // Mouse interface - virtual void handleMouseUp(const Common::Point &mouse); - virtual void handleMouseDown(const Common::Point &mouse) {} - virtual void handleMouseDrag(const Common::Point &mouse) {} + virtual void handleMouseUp(); + virtual void handleMouseDown() {} + virtual void handleMouseDrag() {} protected: MohawkEngine_Myst *_vm; @@ -95,7 +95,7 @@ protected: class MystResourceType5 : public MystResource { public: MystResourceType5(MohawkEngine_Myst *vm, Common::SeekableReadStream *rlstStream, MystResource *parent); - void handleMouseUp(const Common::Point &mouse); + void handleMouseUp(); const Common::String describe(); protected: @@ -130,8 +130,8 @@ public: virtual void drawDataToScreen(); virtual void handleCardChange(); - virtual void handleMouseUp(const Common::Point &mouse); - virtual void handleMouseDown(const Common::Point &mouse); + virtual void handleMouseUp(); + virtual void handleMouseDown(); MystResource *getSubResource(uint16 index) { return _subResources[index]; } protected: @@ -168,9 +168,9 @@ public: virtual ~MystResourceType11(); const Common::String describe(); - void handleMouseDown(const Common::Point &mouse); - void handleMouseUp(const Common::Point &mouse); - void handleMouseDrag(const Common::Point &mouse); + void handleMouseDown(); + void handleMouseUp(); + void handleMouseDrag(); uint16 getList1(uint16 index); uint16 getList2(uint16 index); @@ -207,9 +207,9 @@ public: MystResourceType10(MohawkEngine_Myst *vm, Common::SeekableReadStream *rlstStream, MystResource *parent); virtual ~MystResourceType10(); - void handleMouseDown(const Common::Point &mousee); - void handleMouseUp(const Common::Point &mouse); - void handleMouseDrag(const Common::Point &mouse); + void handleMouseDown(); + void handleMouseUp(); + void handleMouseDrag(); void setStep(uint16 step); void setPosition(uint16 pos); void restoreBackground(); @@ -228,6 +228,8 @@ public: MystResourceType12(MohawkEngine_Myst *vm, Common::SeekableReadStream *rlstStream, MystResource *parent); virtual ~MystResourceType12(); void drawFrame(uint16 frame); + bool pullLeverV(); + void releaseLeverV(); protected: uint16 _numFrames; @@ -243,7 +245,7 @@ public: MystResourceType13(MohawkEngine_Myst *vm, Common::SeekableReadStream *rlstStream, MystResource *parent); const Common::String describe(); - void handleMouseUp(const Common::Point &mouse); + void handleMouseUp(); void handleMouseEnter(); void handleMouseLeave(); diff --git a/engines/mohawk/myst_stacks/channelwood.cpp b/engines/mohawk/myst_stacks/channelwood.cpp index 7943c62662..b456d3c2d4 100644 --- a/engines/mohawk/myst_stacks/channelwood.cpp +++ b/engines/mohawk/myst_stacks/channelwood.cpp @@ -368,18 +368,10 @@ void MystScriptParser_Channelwood::o_leverMove(uint16 op, uint16 var, uint16 arg MystResourceType12 *lever = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - lever->drawFrame(step); - - if (step == maxStep) { + if (lever->pullLeverV()) { if (!_leverPulled) { _leverPulled = true; - _leverAction->handleMouseUp(_vm->_system->getEventManager()->getMousePos()); + _leverAction->handleMouseUp(); } } else { _leverPulled = false; @@ -391,15 +383,7 @@ void MystScriptParser_Channelwood::o_leverMoveFail(uint16 op, uint16 var, uint16 MystResourceType12 *lever = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - lever->drawFrame(step); - - if (step == maxStep) { + if (lever->pullLeverV()) { if (!_leverPulled) { _leverPulled = true; uint16 soundId = lever->getList2(0); @@ -416,16 +400,9 @@ void MystScriptParser_Channelwood::o_leverEndMove(uint16 op, uint16 var, uint16 // Get current lever frame MystResourceType12 *lever = static_cast(_invokingResource); - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); // Release lever - for (int i = step; i >= 0; i--) { - lever->drawFrame(i); - _vm->_system->delayMillis(10); - } + lever->releaseLeverV(); uint16 soundId = lever->getList3(0); if (soundId) @@ -466,15 +443,7 @@ void MystScriptParser_Channelwood::o_pumpLeverMove(uint16 op, uint16 var, uint16 MystResourceType12 *lever = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - lever->drawFrame(step); - - if (step == maxStep) { + if (lever->pullLeverV()) { uint16 soundId = lever->getList2(0); _vm->_sound->replaceBackgroundMyst(soundId, 38400); } else { @@ -509,9 +478,11 @@ void MystScriptParser_Channelwood::o_valveHandleMove1(uint16 op, uint16 var, uin debugC(kDebugScript, "Opcode %d: Valve handle move", op); MystResourceType12 *handle = static_cast(_invokingResource); - if (handle->getRect().contains(_vm->_mouse)) { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); + + if (handle->getRect().contains(mouse)) { // Compute frame to draw - _tempVar = (_vm->_mouse.x - 250) / 4; + _tempVar = (mouse.x - 250) / 4; _tempVar = CLIP(_tempVar, 1, handle->getStepsH() - 2); // Draw frame @@ -558,9 +529,11 @@ void MystScriptParser_Channelwood::o_valveHandleMove2(uint16 op, uint16 var, uin debugC(kDebugScript, "Opcode %d: Valve handle move", op); MystResourceType12 *handle = static_cast(_invokingResource); - if (handle->getRect().contains(_vm->_mouse)) { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); + + if (handle->getRect().contains(mouse)) { // Compute frame to draw - _tempVar = handle->getStepsH() - (_vm->_mouse.x - 234) / 4; + _tempVar = handle->getStepsH() - (mouse.x - 234) / 4; _tempVar = CLIP(_tempVar, 1, handle->getStepsH() - 2); // Draw frame @@ -584,9 +557,11 @@ void MystScriptParser_Channelwood::o_valveHandleMove3(uint16 op, uint16 var, uin debugC(kDebugScript, "Opcode %d: Valve handle move", op); MystResourceType12 *handle = static_cast(_invokingResource); - if (handle->getRect().contains(_vm->_mouse)) { + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); + + if (handle->getRect().contains(mouse)) { // Compute frame to draw - _tempVar = handle->getStepsH() - (_vm->_mouse.x - 250) / 4; + _tempVar = handle->getStepsH() - (mouse.x - 250) / 4; _tempVar = CLIP(_tempVar, 1, handle->getStepsH() - 2); // Draw frame @@ -677,7 +652,7 @@ void MystScriptParser_Channelwood::o_executeMouseUp(uint16 op, uint16 var, uint1 debugC(kDebugScript, "Opcode %d: Execute mouse up", op); MystResourceType5 *resource = static_cast(_vm->_resources[argv[0]]); - resource->handleMouseUp(_vm->_system->getEventManager()->getMousePos()); + resource->handleMouseUp(); } void MystScriptParser_Channelwood::o_waterTankValveClose(uint16 op, uint16 var, uint16 argc, uint16 *argv) { diff --git a/engines/mohawk/myst_stacks/myst.cpp b/engines/mohawk/myst_stacks/myst.cpp index 9f813c5106..d63cf77987 100644 --- a/engines/mohawk/myst_stacks/myst.cpp +++ b/engines/mohawk/myst_stacks/myst.cpp @@ -1460,15 +1460,7 @@ void MystScriptParser_Myst::o_cabinSafeHandleMove(uint16 op, uint16 var, uint16 // Used on Card 4100 MystResourceType12 *handle = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = handle->getStepsV() - 1; - Common::Rect rect = handle->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * handle->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - handle->drawFrame(step); - - if (step == maxStep) { + if (handle->pullLeverV()) { // Sound not played yet if (_tempVar == 0) { uint16 soundId = handle->getList2(0); @@ -1804,9 +1796,10 @@ void MystScriptParser_Myst::o_circuitBreakerMove(uint16 op, uint16 var, uint16 a debugC(kDebugScript, "Opcode %d: Circuit breaker move", op); MystResourceType12 *breaker = static_cast(_invokingResource); + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); int16 maxStep = breaker->getStepsV() - 1; - int16 step = ((_vm->_mouse.y - 80) * breaker->getStepsV()) / 65; + int16 step = ((mouse.y - 80) * breaker->getStepsV()) / 65; step = CLIP(step, 0, maxStep); breaker->drawFrame(step); @@ -2329,11 +2322,12 @@ void MystScriptParser_Myst::o_rocketLeverMove(uint16 op, uint16 var, uint16 argc debugC(kDebugScript, "Opcode %d: Rocket lever move", op); MystResourceType12 *lever = static_cast(_invokingResource); + const Common::Point &mouse = _vm->_system->getEventManager()->getMousePos(); // Make the lever follow the mouse int16 maxStep = lever->getStepsV() - 1; Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); + int16 step = ((mouse.y - rect.top) * lever->getStepsV()) / rect.height(); step = CLIP(step, 0, maxStep); lever->drawFrame(step); @@ -2808,15 +2802,8 @@ void MystScriptParser_Myst::o_clockLeverMove(uint16 op, uint16 var, uint16 argc, if (!_clockLeverPulled) { MystResourceType12 *lever = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - lever->drawFrame(step); - - if (step == maxStep) { + // If lever pulled + if (lever->pullLeverV()) { // Start videos for first step if (_clockWeightPosition < 2214) { _vm->_sound->replaceSoundMyst(5113); @@ -2890,18 +2877,9 @@ void MystScriptParser_Myst::o_clockLeverEndMove(uint16 op, uint16 var, uint16 ar if (_clockMiddleGearMovedAlone) _vm->_sound->replaceSoundMyst(8113); - // Get current lever frame - MystResourceType12 *lever = static_cast(_invokingResource); - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - // Release lever - for (int i = step; i >= 0; i--) { - lever->drawFrame(i); - _vm->_system->delayMillis(10); - } + MystResourceType12 *lever = static_cast(_invokingResource); + lever->releaseLeverV(); // Check if puzzle is solved clockGearsCheckSolution(); @@ -2950,15 +2928,8 @@ void MystScriptParser_Myst::o_clockResetLeverMove(uint16 op, uint16 var, uint16 MystResourceType12 *lever = static_cast(_invokingResource); - // Make the handle follow the mouse - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - - lever->drawFrame(step); - - if (step == maxStep && _clockWeightPosition != 0) + // If pulled + if (lever->pullLeverV() && _clockWeightPosition != 0) clockReset(); } @@ -3040,16 +3011,8 @@ void MystScriptParser_Myst::o_clockResetLeverEndMove(uint16 op, uint16 var, uint // Get current lever frame MystResourceType12 *lever = static_cast(_invokingResource); - int16 maxStep = lever->getStepsV() - 1; - Common::Rect rect = lever->getRect(); - int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height(); - step = CLIP(step, 0, maxStep); - // Release lever - for (int i = step; i >= 0; i--) { - lever->drawFrame(i); - _vm->_system->delayMillis(10); - } + lever->releaseLeverV(); _vm->checkCursorHints(); } -- cgit v1.2.3