MOHAWK: Dragging resources / levers code cleanup

svn-id: r55439
This commit is contained in:
Bastien Bouclet 2011-01-22 17:26:09 +00:00
parent 5b4183fa27
commit caccceb02b
6 changed files with 106 additions and 132 deletions

View File

@ -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:

View File

@ -180,7 +180,6 @@ public:
MystScriptParser *_scriptParser;
Common::Array<MystResource*> _resources;
MystResource *_dragResource;
Common::Point _mouse;
Common::RandomSource *_rnd;
bool _showResourceRects;

View File

@ -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<int16>(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<int16>(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

View File

@ -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();

View File

@ -368,18 +368,10 @@ void MystScriptParser_Channelwood::o_leverMove(uint16 op, uint16 var, uint16 arg
MystResourceType12 *lever = static_cast<MystResourceType12 *>(_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<int16>(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<MystResourceType12 *>(_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<int16>(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<MystResourceType12 *>(_invokingResource);
int16 maxStep = lever->getStepsV() - 1;
Common::Rect rect = lever->getRect();
int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height();
step = CLIP<int16>(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<MystResourceType12 *>(_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<int16>(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<MystResourceType12 *>(_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<int16>(_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<MystResourceType12 *>(_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<int16>(_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<MystResourceType12 *>(_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<int16>(_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<MystResourceType5 *>(_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) {

View File

@ -1460,15 +1460,7 @@ void MystScriptParser_Myst::o_cabinSafeHandleMove(uint16 op, uint16 var, uint16
// Used on Card 4100
MystResourceType12 *handle = static_cast<MystResourceType12 *>(_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<uint16>(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<MystResourceType12 *>(_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<uint16>(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<MystResourceType12 *>(_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<uint16>(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<MystResourceType12 *>(_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<int16>(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<MystResourceType12 *>(_invokingResource);
int16 maxStep = lever->getStepsV() - 1;
Common::Rect rect = lever->getRect();
int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height();
step = CLIP<int16>(step, 0, maxStep);
// Release lever
for (int i = step; i >= 0; i--) {
lever->drawFrame(i);
_vm->_system->delayMillis(10);
}
MystResourceType12 *lever = static_cast<MystResourceType12 *>(_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<MystResourceType12 *>(_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<int16>(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<MystResourceType12 *>(_invokingResource);
int16 maxStep = lever->getStepsV() - 1;
Common::Rect rect = lever->getRect();
int16 step = ((_vm->_mouse.y - rect.top) * lever->getStepsV()) / rect.height();
step = CLIP<int16>(step, 0, maxStep);
// Release lever
for (int i = step; i >= 0; i--) {
lever->drawFrame(i);
_vm->_system->delayMillis(10);
}
lever->releaseLeverV();
_vm->checkCursorHints();
}