scummvm/engines/pegasus/elements.cpp
2011-09-22 15:28:01 -04:00

481 lines
12 KiB
C++

/* 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.
*
* Additional copyright for this file:
* Copyright (C) 1995-1997 Presto Studios, Inc.
*
* 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.
*
*/
#include "common/macresman.h"
#include "common/stream.h"
#include "pegasus/elements.h"
#include "pegasus/graphics.h"
#include "pegasus/surface.h"
namespace Pegasus {
DisplayElement::DisplayElement(const tDisplayElementID id) : IDObject(id) {
_elementIsDisplaying = false;
_elementIsVisible = false;
_elementOrder = 0;
_triggeredElement = this;
_nextElement = 0;
}
DisplayElement::~DisplayElement() {
if (isDisplaying())
((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
}
void DisplayElement::setDisplayOrder(const tDisplayOrder order) {
if (_elementOrder != order) {
_elementOrder = order;
if (isDisplaying()) {
((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
((PegasusEngine *)g_engine)->_gfx->addDisplayElement(this);
triggerRedraw();
}
}
}
void DisplayElement::startDisplaying() {
if (!isDisplaying()) {
((PegasusEngine *)g_engine)->_gfx->addDisplayElement(this);
triggerRedraw();
}
}
void DisplayElement::stopDisplaying() {
if (isDisplaying()) {
triggerRedraw();
((PegasusEngine *)g_engine)->_gfx->removeDisplayElement(this);
}
}
void DisplayElement::setBounds(const tCoordType left, const tCoordType top, const tCoordType right, const tCoordType bottom) {
setBounds(Common::Rect(left, top, right, bottom));
}
void DisplayElement::getBounds(Common::Rect &r) const {
r = _bounds;
}
void DisplayElement::sizeElement(const tCoordType h, const tCoordType v) {
Common::Rect newBounds = _bounds;
newBounds.right = _bounds.left + h;
newBounds.bottom = _bounds.top + v;
setBounds(newBounds);
}
void DisplayElement::moveElementTo(const tCoordType h, const tCoordType v) {
Common::Rect newBounds = _bounds;
newBounds.moveTo(h, v);
setBounds(newBounds);
}
void DisplayElement::moveElement(const tCoordType dh, const tCoordType dv) {
Common::Rect newBounds = _bounds;
newBounds.translate(dh, dv);
setBounds(newBounds);
}
void DisplayElement::getLocation(tCoordType &h, tCoordType &v) const {
h = _bounds.left;
v = _bounds.top;
}
void DisplayElement::centerElementAt(const tCoordType h, const tCoordType v) {
Common::Rect newBounds = _bounds;
newBounds.moveTo(h - (_bounds.width() / 2), v - (_bounds.height() / 2));
setBounds(newBounds);
}
void DisplayElement::getCenter(tCoordType &h, tCoordType &v) const {
h = (_bounds.left + _bounds.right) / 2;
v = (_bounds.top + _bounds.bottom) / 2;
}
void DisplayElement::setBounds(const Common::Rect &r) {
if (r != _bounds) {
triggerRedraw();
_bounds = r;
triggerRedraw();
}
}
void DisplayElement::hide() {
if (_elementIsVisible) {
triggerRedraw();
_elementIsVisible = false;
}
}
void DisplayElement::show() {
if (!_elementIsVisible) {
_elementIsVisible = true;
triggerRedraw();
}
}
// Only invalidates this element's bounding rectangle if all these conditions are true:
// -- The triggered element is this element.
// -- The element is displaying on the display list.
// -- The element is visible.
// -- The element is part of the active layer OR is one of the reserved items.
void DisplayElement::triggerRedraw() {
GraphicsManager *gfx = ((PegasusEngine *)g_engine)->_gfx;
if (_triggeredElement == this) {
if (validToDraw(gfx->getBackOfActiveLayer(), gfx->getFrontOfActiveLayer()))
gfx->invalRect(_bounds);
} else {
_triggeredElement->triggerRedraw();
}
}
void DisplayElement::setTriggeredElement(DisplayElement *element) {
if (element)
_triggeredElement = element;
else
_triggeredElement = this;
}
bool DisplayElement::validToDraw(tDisplayOrder backLayer, tDisplayOrder frontLayer) {
return isDisplaying() && _elementIsVisible &&
(getObjectID() <= kHighestReservedElementID ||
(getDisplayOrder() >= backLayer &&
getDisplayOrder() <= frontLayer));
}
DropHighlight::DropHighlight(const tDisplayElementID id) : DisplayElement(id) {
_highlightColor = g_system->getScreenFormat().RGBToColor(0x48, 0x80, 0xD8);
_thickness = 2;
_cornerDiameter = 0;
}
void DropHighlight::draw(const Common::Rect &) {
Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea();
// Since this is only used in two different ways, I'm only
// going to implement it in those two ways. Deal with it.
Common::Rect rect = _bounds;
rect.grow(-_thickness);
screen->frameRect(rect, _highlightColor);
rect.grow(1);
screen->frameRect(rect, _highlightColor);
if (_cornerDiameter == 8 && _thickness == 4) {
rect.grow(1);
screen->frameRect(rect, _highlightColor);
screen->hLine(rect.left + 1, rect.top - 1, rect.right - 2, _highlightColor);
screen->hLine(rect.left + 1, rect.bottom, rect.right - 2, _highlightColor);
screen->vLine(rect.left - 1, rect.top + 1, rect.bottom - 2, _highlightColor);
screen->vLine(rect.right, rect.top + 1, rect.bottom - 2, _highlightColor);
}
}
IdlerAnimation::IdlerAnimation(const tDisplayElementID id) : Animation(id) {
_lastTime = 0xffffffff;
}
void IdlerAnimation::startDisplaying() {
if (!isDisplaying()) {
Animation::startDisplaying();
startIdling();
}
}
void IdlerAnimation::stopDisplaying() {
if (isDisplaying()) {
Animation::stopDisplaying();
stopIdling();
}
}
void IdlerAnimation::useIdleTime() {
uint32 currentTime = getTime();
if (currentTime != _lastTime) {
_lastTime = currentTime;
timeChanged(_lastTime);
}
}
void IdlerAnimation::timeChanged(const TimeValue) {
triggerRedraw();
}
FrameSequence::FrameSequence(const tDisplayElementID id) : IdlerAnimation(id) {
_duration = 0;
_currentFrameNum = 0;
_resFork = new Common::MacResManager();
}
FrameSequence::~FrameSequence() {
delete _resFork;
}
void FrameSequence::useFileName(const Common::String &fileName) {
_resFork->open(fileName);
}
void FrameSequence::openFrameSequence() {
if (!_resFork->hasResFork())
return;
Common::SeekableReadStream *res = _resFork->getResource(MKTAG('P', 'F', 'r', 'm'), 0x80);
if (!res)
return;
uint32 scale = res->readUint32BE();
_bounds.top = res->readUint16BE();
_bounds.left = res->readUint16BE();
_bounds.bottom = res->readUint16BE();
_bounds.right = res->readUint16BE();
_numFrames = res->readUint16BE();
_duration = 0;
_frameTimes.clear();
for (uint32 i = 0; i < _numFrames; i++) {
TimeValue time = res->readUint32BE();
_duration += time;
_frameTimes.push_back(_duration);
}
setScale(scale);
setSegment(0, _duration);
setTime(0);
_currentFrameNum = 0;
newFrame(_currentFrameNum);
triggerRedraw();
delete res;
}
void FrameSequence::closeFrameSequence() {
stop();
_resFork->close();
_duration = 0;
_numFrames = 0;
_frameTimes.clear();
}
void FrameSequence::timeChanged(const TimeValue time) {
int16 frameNum = 0;
for (int16 i = _numFrames - 1; i >= 0; i--) {
if (_frameTimes[i] < time) {
frameNum = i;
break;
}
}
if (frameNum != _currentFrameNum) {
_currentFrameNum = frameNum;
newFrame(_currentFrameNum);
triggerRedraw();
}
}
void FrameSequence::setFrameNum(const int16 frameNum) {
int16 f = CLIP<int>(frameNum, 0, _numFrames);
if (_currentFrameNum != f) {
_currentFrameNum = f;
setTime(_frameTimes[f]);
newFrame(f);
triggerRedraw();
}
}
bool FrameSequence::isSequenceOpen() const {
return _numFrames != 0;
}
Sprite::Sprite(const tDisplayElementID id) : DisplayElement(id) {
_numFrames = 0;
_currentFrameNum = 0xffffffff;
_currentFrame = 0;
}
Sprite::~Sprite() {
discardFrames();
}
void Sprite::discardFrames() {
if (!_frameArray.empty()) {
for (uint32 i = 0; i < _numFrames; i++) {
SpriteFrame *frame = _frameArray[i].frame;
frame->_referenceCount--;
if (frame->_referenceCount == 0)
delete frame;
}
_frameArray.clear();
_numFrames = 0;
_currentFrame = 0;
_currentFrameNum = 0xffffffff;
setBounds(0, 0, 0, 0);
}
}
void Sprite::addPICTResourceFrame(const tResIDType pictID, bool transparent, const tCoordType left, const tCoordType top) {
SpriteFrame *frame = new SpriteFrame();
frame->initFromPICTResource(((PegasusEngine *)g_engine)->_resFork, pictID, transparent);
addFrame(frame, left, top);
}
uint32 Sprite::addFrame(SpriteFrame *frame, const tCoordType left, const tCoordType top) {
SpriteFrameRec frameRecord;
frameRecord.frame = frame;
frameRecord.frameLeft = left;
frameRecord.frameTop = top;
_frameArray.push_back(frameRecord);
_numFrames++;
frame->_referenceCount++;
Common::Rect frameBounds;
frame->getSurfaceBounds(frameBounds);
// 9/3/96
// BB Should this be + left or - left?
frameBounds.moveTo(_bounds.left + left, _bounds.top + top);
frameBounds.extend(_bounds);
if (_bounds != frameBounds)
setBounds(frameBounds);
return _numFrames - 1;
}
void Sprite::removeFrame(const uint32 frameNum) {
_frameArray[frameNum].frame->_referenceCount--;
if (_frameArray[frameNum].frame->_referenceCount == 0)
delete _frameArray[frameNum].frame;
// Calculate the new bounds
Common::Rect frameBounds;
for (uint32 i = 0; i < _numFrames; i++) {
if (i == frameNum)
continue;
Common::Rect r;
_frameArray[i].frame->getSurfaceBounds(r);
r.translate(_frameArray[i].frameLeft, _frameArray[i].frameTop);
frameBounds.extend(r);
}
_frameArray.remove_at(frameNum);
frameBounds.moveTo(_bounds.left, _bounds.top);
setBounds(frameBounds);
if (_currentFrameNum == frameNum)
triggerRedraw();
else if (_currentFrameNum != 0xffffffff && _currentFrameNum > frameNum)
--_currentFrameNum;
}
void Sprite::setCurrentFrameIndex(const int32 frameNum) {
if (frameNum < 0) {
if (_currentFrameNum != 0xffffffff) {
_currentFrameNum = 0xffffffff;
_currentFrame = 0;
triggerRedraw();
}
} else if (_numFrames > 0) {
uint32 f = frameNum % _numFrames;
if (f != _currentFrameNum) {
_currentFrameNum = f;
_currentFrame = &_frameArray[f];
triggerRedraw();
}
}
}
SpriteFrame *Sprite::getFrame(const int32 index) {
if (index < 0 || (uint32)index >= _numFrames)
return 0;
return _frameArray[index].frame;
}
void Sprite::draw(const Common::Rect &r) {
if (_currentFrame) {
Common::Rect frameBounds;
_currentFrame->frame->getSurfaceBounds(frameBounds);
frameBounds.translate(_bounds.left + _currentFrame->frameLeft, _bounds.top + _currentFrame->frameTop);
Common::Rect r1 = frameBounds.findIntersectingRect(r);
Common::Rect r2 = frameBounds;
r2.translate(frameBounds.left - _bounds.left - _currentFrame->frameLeft, frameBounds.top - _bounds.top - _currentFrame->frameTop);
_currentFrame->frame->drawImage(r2, r1);
}
}
SpriteSequence::SpriteSequence(const tDisplayElementID id, const tDisplayElementID spriteID) :
FrameSequence(id), _sprite(spriteID), _transparent(false) {
}
void SpriteSequence::openFrameSequence() {
if (!isSequenceOpen()) {
FrameSequence::openFrameSequence();
if (isSequenceOpen()) {
uint32 numFrames = getNumFrames();
for (uint32 i = 0; i < numFrames; ++i) {
SpriteFrame *frame = new SpriteFrame();
frame->initFromPICTResource(_resFork, i + 0x80, _transparent);
_sprite.addFrame(frame, 0, 0);
}
_sprite.setBounds(_bounds);
}
}
}
void SpriteSequence::closeFrameSequence() {
if (isSequenceOpen()) {
FrameSequence::closeFrameSequence();
_sprite.discardFrames();
}
}
void SpriteSequence::setBounds(const Common::Rect &bounds) {
FrameSequence::setBounds(bounds);
_sprite.setBounds(_bounds);
}
void SpriteSequence::draw(const Common::Rect &r) {
_sprite.draw(r);
}
void SpriteSequence::newFrame(const uint16 frame) {
_sprite.setCurrentFrameIndex(frame);
}
} // End of namespace Pegasus