2021-02-19 02:17:41 +02:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-04-16 17:20:47 +03:00
|
|
|
#include "common/system.h"
|
|
|
|
|
2021-02-19 02:17:41 +02:00
|
|
|
#include "engines/nancy/nancy.h"
|
|
|
|
#include "engines/nancy/graphics.h"
|
|
|
|
#include "engines/nancy/cursor.h"
|
2021-03-26 01:17:07 +02:00
|
|
|
#include "engines/nancy/input.h"
|
2021-04-01 21:04:38 +03:00
|
|
|
#include "engines/nancy/util.h"
|
|
|
|
|
|
|
|
#include "engines/nancy/state/scene.h"
|
|
|
|
|
|
|
|
#include "engines/nancy/ui/viewport.h"
|
2021-03-13 02:01:26 +02:00
|
|
|
|
2021-02-19 02:17:41 +02:00
|
|
|
namespace Nancy {
|
|
|
|
namespace UI {
|
|
|
|
|
|
|
|
// does NOT put the object in a valid state until loadVideo is called
|
|
|
|
void Viewport::init() {
|
2021-03-19 18:37:20 +02:00
|
|
|
Common::SeekableReadStream *viewChunk = g_nancy->getBootChunkStream("VIEW");
|
|
|
|
viewChunk->seek(0);
|
|
|
|
|
|
|
|
Common::Rect dest;
|
|
|
|
readRect(*viewChunk, dest);
|
|
|
|
viewChunk->skip(16); // skip viewport source rect
|
|
|
|
readRect(*viewChunk, _format1Bounds);
|
|
|
|
readRect(*viewChunk, _format2Bounds);
|
|
|
|
|
|
|
|
_screenPosition = dest;
|
|
|
|
|
|
|
|
_upHotspot = _downHotspot = _leftHotspot = _rightHotspot = _screenPosition;
|
|
|
|
_downHotspot.translate(0, _screenPosition.height());
|
|
|
|
_rightHotspot.translate(_screenPosition.width(), 0);
|
2021-04-22 00:15:25 +03:00
|
|
|
|
|
|
|
setEdgesSize(g_nancy->_verticalEdgesSize, g_nancy->_horizontalEdgesSize + 5, g_nancy->_horizontalEdgesSize, g_nancy->_horizontalEdgesSize);
|
2021-03-19 18:37:20 +02:00
|
|
|
|
|
|
|
RenderObject::init();
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::handleInput(NancyInput &input) {
|
2021-03-19 18:37:20 +02:00
|
|
|
Time playTime = g_nancy->getTotalPlayTime();
|
|
|
|
byte direction = 0;
|
|
|
|
|
2021-04-16 17:20:47 +03:00
|
|
|
// Make cursor sticky when scrolling the viewport
|
|
|
|
if (input.input & (NancyInput::kLeftMouseButton | NancyInput::kRightMouseButton) && _stickyCursorPos.x > -1) {
|
|
|
|
g_system->warpMouse(_stickyCursorPos.x, _stickyCursorPos.y);
|
|
|
|
input.mousePos = _stickyCursorPos;
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
if (_screenPosition.contains(input.mousePos)) {
|
|
|
|
g_nancy->_cursorManager->setCursorType(CursorManager::kNormal);
|
|
|
|
}
|
|
|
|
|
2021-04-16 17:20:47 +03:00
|
|
|
// Do not handle hotspots marked as inactive and ignore diagonals if intersecting hotspots are not active
|
2021-03-19 18:37:20 +02:00
|
|
|
if (_upHotspot.contains(input.mousePos) && (_edgesMask & kUp) == 0) {
|
|
|
|
if (_upHotspot.findIntersectingRect(_leftHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kLeft) == 0) {
|
|
|
|
direction |= kUp;
|
|
|
|
}
|
|
|
|
} else if (_upHotspot.findIntersectingRect(_rightHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kRight) == 0) {
|
|
|
|
direction |= kUp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
direction |= kUp;
|
|
|
|
}
|
|
|
|
} else if (_downHotspot.contains(input.mousePos) && (_edgesMask & kDown) == 0) {
|
|
|
|
if (_downHotspot.findIntersectingRect(_leftHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kLeft) == 0) {
|
|
|
|
direction |= kDown;
|
|
|
|
}
|
|
|
|
} else if (_downHotspot.findIntersectingRect(_rightHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kRight) == 0) {
|
|
|
|
direction |= kDown;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
direction |= kDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_leftHotspot.contains(input.mousePos) && (_edgesMask & kLeft) == 0) {
|
|
|
|
if (_leftHotspot.findIntersectingRect(_upHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kUp) == 0) {
|
|
|
|
direction |= kLeft;
|
|
|
|
}
|
|
|
|
} else if (_leftHotspot.findIntersectingRect(_downHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kDown) == 0) {
|
|
|
|
direction |= kLeft;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
direction |= kLeft;
|
|
|
|
}
|
|
|
|
} else if (_rightHotspot.contains(input.mousePos) && (_edgesMask & kRight) == 0) {
|
|
|
|
if (_rightHotspot.findIntersectingRect(_upHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kUp) == 0) {
|
|
|
|
direction |= kRight;
|
|
|
|
}
|
|
|
|
} else if (_rightHotspot.findIntersectingRect(_downHotspot).contains(input.mousePos)) {
|
|
|
|
if ((_edgesMask & kDown) == 0) {
|
|
|
|
direction |= kRight;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
direction |= kRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 17:20:47 +03:00
|
|
|
// Set sticky cursor
|
|
|
|
if (input.input & (NancyInput::kLeftMouseButton | NancyInput::kRightMouseButton) && direction) {
|
|
|
|
if (_stickyCursorPos.x <= -1) {
|
|
|
|
_stickyCursorPos = input.mousePos;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_stickyCursorPos.x = -1;
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
if (direction) {
|
|
|
|
g_nancy->_cursorManager->setCursorType(CursorManager::kMove);
|
|
|
|
|
|
|
|
if (input.input & NancyInput::kRightMouseButton) {
|
|
|
|
direction |= kMoveFast;
|
|
|
|
} else if ((input.input & NancyInput::kLeftMouseButton) == 0) {
|
|
|
|
direction = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we hover over an edge we don't want to click an element in the viewport underneath
|
|
|
|
// or to change the cursor, so we make the mouse input invalid
|
|
|
|
input.eatMouseInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!direction) {
|
|
|
|
if (input.input & NancyInput::kMoveUp) {
|
|
|
|
direction |= kUp;
|
|
|
|
}
|
|
|
|
if (input.input & NancyInput::kMoveDown) {
|
|
|
|
direction |= kDown;
|
|
|
|
}
|
|
|
|
if (input.input & NancyInput::kMoveLeft) {
|
|
|
|
direction |= kLeft;
|
|
|
|
}
|
|
|
|
if (input.input & NancyInput::kMoveRight) {
|
|
|
|
direction |= kRight;
|
|
|
|
}
|
|
|
|
if (input.input & NancyInput::kMoveFastModifier) {
|
|
|
|
direction |= kMoveFast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the movement
|
|
|
|
if (direction) {
|
|
|
|
const Nancy::State::Scene::SceneSummary &summary = NancySceneState.getSceneSummary();
|
|
|
|
Time movementDelta = NancySceneState.getMovementTimeDelta(direction & kMoveFast);
|
|
|
|
|
|
|
|
if (playTime > _nextMovementTime) {
|
|
|
|
if (direction & kLeft) {
|
|
|
|
setNextFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction & kRight) {
|
|
|
|
setPreviousFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction & kUp) {
|
|
|
|
scrollUp(summary.verticalScrollDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction & kDown) {
|
|
|
|
scrollDown(summary.verticalScrollDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
_nextMovementTime = playTime + movementDelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_movementLastFrame = direction;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 02:01:26 +02:00
|
|
|
void Viewport::loadVideo(const Common::String &filename, uint frameNr, uint verticalScroll, uint16 format, const Common::String &palette) {
|
2021-03-19 18:37:20 +02:00
|
|
|
if (_decoder.isVideoLoaded()) {
|
|
|
|
_decoder.close();
|
|
|
|
}
|
2021-03-25 00:33:23 +02:00
|
|
|
|
|
|
|
if (!_decoder.loadFile(filename + ".avf")) {
|
|
|
|
error("Couldn't load video file %s", filename.c_str());
|
|
|
|
}
|
2021-03-19 18:37:20 +02:00
|
|
|
|
|
|
|
_videoFormat = format;
|
|
|
|
|
|
|
|
enableEdges(kUp | kDown | kLeft | kRight);
|
|
|
|
|
|
|
|
setFrame(frameNr);
|
|
|
|
setVerticalScroll(verticalScroll);
|
|
|
|
|
|
|
|
if (palette.size()) {
|
|
|
|
GraphicsManager::loadSurfacePalette(_drawSurface, palette);
|
|
|
|
GraphicsManager::loadSurfacePalette(_fullFrame, palette);
|
|
|
|
}
|
|
|
|
|
|
|
|
_movementLastFrame = 0;
|
|
|
|
_nextMovementTime = 0;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::setFrame(uint frameNr) {
|
2021-03-19 18:37:20 +02:00
|
|
|
assert(frameNr < _decoder.getFrameCount());
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
const Graphics::Surface *newFrame = _decoder.decodeFrame(frameNr);
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
if (_videoFormat == 2) {
|
|
|
|
// Format 2 uses full-size images
|
|
|
|
GraphicsManager::copyToManaged(*newFrame, _fullFrame);
|
|
|
|
} else if (_videoFormat == 1) {
|
|
|
|
// Format 1 uses quarter-size, upside-down images
|
|
|
|
GraphicsManager::copyToManaged(*newFrame, _fullFrame, true, true);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
_needsRedraw = true;
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-03-19 18:37:20 +02:00
|
|
|
_currentFrame = frameNr;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::setNextFrame() {
|
2021-04-17 00:37:48 +03:00
|
|
|
uint newFrame = getCurFrame() + 1 >= getFrameCount() ? 0 : getCurFrame() + 1;
|
|
|
|
if (newFrame != _currentFrame) {
|
|
|
|
setFrame(newFrame);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::setPreviousFrame() {
|
2021-04-17 00:37:48 +03:00
|
|
|
uint newFrame = (int)getCurFrame() - 1 < 0 ? getFrameCount() - 1 : getCurFrame() - 1;
|
|
|
|
if (newFrame != _currentFrame) {
|
|
|
|
setFrame(newFrame);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::setVerticalScroll(uint scroll) {
|
2021-04-13 23:06:10 +03:00
|
|
|
assert(scroll + _drawSurface.h <= _fullFrame.h);
|
2021-03-19 18:37:20 +02:00
|
|
|
|
|
|
|
Common::Rect sourceBounds = _screenPosition;
|
2021-04-13 23:06:10 +03:00
|
|
|
sourceBounds.moveTo(0, scroll);
|
2021-03-19 18:37:20 +02:00
|
|
|
_drawSurface.create(_fullFrame, sourceBounds);
|
|
|
|
_needsRedraw = true;
|
|
|
|
|
|
|
|
if (scroll == getMaxScroll()) {
|
|
|
|
disableEdges(kDown);
|
|
|
|
enableEdges(kUp);
|
|
|
|
} else if (scroll == 0) {
|
|
|
|
disableEdges(kUp);
|
|
|
|
enableEdges(kDown);
|
|
|
|
} else {
|
|
|
|
enableEdges(kUp | kDown);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::scrollUp(uint delta) {
|
2021-04-17 00:37:48 +03:00
|
|
|
if (_drawSurface.getOffsetFromOwner().y != 0) {
|
|
|
|
setVerticalScroll(_drawSurface.getOffsetFromOwner().y < (int16)delta ? 0 : _drawSurface.getOffsetFromOwner().y - delta);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::scrollDown(uint delta) {
|
2021-04-17 00:37:48 +03:00
|
|
|
if (_drawSurface.getOffsetFromOwner().y != getMaxScroll()) {
|
|
|
|
setVerticalScroll(_drawSurface.getOffsetFromOwner().y + delta > getMaxScroll() ? getMaxScroll() : _drawSurface.getOffsetFromOwner().y + delta);
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::Rect Viewport::getBoundsByFormat(uint format) const {
|
2021-03-19 18:37:20 +02:00
|
|
|
if (format == 1) {
|
|
|
|
return _format1Bounds;
|
|
|
|
} else if (format == 2) {
|
|
|
|
return _format2Bounds;
|
|
|
|
} else {
|
|
|
|
return Common::Rect();
|
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a viewport-space rectangle to screen coordinates
|
|
|
|
Common::Rect Viewport::convertViewportToScreen(const Common::Rect &viewportRect) const {
|
2021-03-19 18:37:20 +02:00
|
|
|
Common::Rect ret = convertToScreen(viewportRect);
|
|
|
|
ret.translate(0, -getCurVerticalScroll());
|
|
|
|
ret.clip(_screenPosition);
|
|
|
|
return ret;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a screen-space coordinate to viewport coordinates
|
|
|
|
Common::Rect Viewport::convertScreenToViewport(const Common::Rect &viewportRect) const {
|
2021-03-19 18:37:20 +02:00
|
|
|
Common::Rect ret = convertToLocal(viewportRect);
|
|
|
|
ret.translate(0, getCurVerticalScroll());
|
|
|
|
return ret;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-02-19 18:46:33 +02:00
|
|
|
void Viewport::setEdgesSize(uint16 upSize, uint16 downSize, uint16 leftSize, uint16 rightSize) {
|
2021-03-19 18:37:20 +02:00
|
|
|
_upHotspot.setHeight(upSize);
|
|
|
|
_leftHotspot.setWidth(leftSize);
|
|
|
|
|
|
|
|
_downHotspot.top = _screenPosition.bottom;
|
|
|
|
_downHotspot.setHeight(downSize);
|
|
|
|
_downHotspot.translate(0, -downSize);
|
|
|
|
|
|
|
|
_rightHotspot.left = _screenPosition.right;
|
|
|
|
_rightHotspot.setWidth(rightSize);
|
|
|
|
_rightHotspot.translate(-rightSize, 0);
|
2021-02-19 18:46:33 +02:00
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-02-19 18:46:33 +02:00
|
|
|
void Viewport::disableEdges(byte edges) {
|
2021-03-19 18:37:20 +02:00
|
|
|
_edgesMask |= edges;
|
2021-02-19 18:46:33 +02:00
|
|
|
}
|
2021-02-19 02:17:41 +02:00
|
|
|
|
2021-02-19 18:46:33 +02:00
|
|
|
void Viewport::enableEdges(byte edges) {
|
2021-03-19 18:37:20 +02:00
|
|
|
_edgesMask &= ~edges;
|
2021-02-19 02:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace UI
|
2021-03-13 23:19:32 +02:00
|
|
|
} // End of namespace Nancy
|