Skip to content

Commit

Permalink
PEGASUS: Add the ItemDragger classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Hoops committed Sep 28, 2011
1 parent 817f775 commit af78726
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 0 deletions.
188 changes: 188 additions & 0 deletions engines/pegasus/items/itemdragger.cpp
@@ -0,0 +1,188 @@
/* 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 "pegasus/elements.h"
#include "pegasus/hotspot.h"
#include "pegasus/pegasus.h"
#include "pegasus/items/itemdragger.h"

namespace Pegasus {

SpriteDragger::SpriteDragger() {
_draggingSprite = 0;
_limitRect = Common::Rect(-30000, -30000, 30000, 30000);
_slopRect = Common::Rect(-30000, -30000, 30000, 30000);
_dragOffset.x = 0;
_dragOffset.y = 0;
_lastHotspot = 0;
}

void SpriteDragger::setDragSprite(Sprite *newSprite) {
if (!isTracking())
_draggingSprite = newSprite;
}

void SpriteDragger::setDragConstraints(const Common::Rect &limitRect, const Common::Rect &slopRect) {
if (!isTracking()) {
_rawLimitRect = limitRect;
_slopRect = slopRect;
}
}

void SpriteDragger::getDragConstraints(Common::Rect &limitRect, Common::Rect &slopRect) const {
limitRect = _rawLimitRect;
slopRect = _slopRect;
}

void SpriteDragger::startTracking(const Input &input) {
if (_draggingSprite) {
Tracker::startTracking(input);

if (isTracking()) {
input.getInputLocation(_startPoint);
_lastRawPoint = _startRawPoint = _startPoint;

Common::Rect r;
_draggingSprite->getBounds(r);
_dragOffset.x = _startPoint.x - r.left;
_dragOffset.y = _startPoint.y - r.top;

_limitRect = _rawLimitRect;
_limitRect.left += _dragOffset.x;
_limitRect.top += _dragOffset.y;
_limitRect.right -= r.width() - _dragOffset.x;
_limitRect.bottom -= r.height() - _dragOffset.y;
pinPointInRect(_limitRect, _startPoint);

_lastPoint = _startPoint;
if (_startPoint != _startRawPoint) {
Common::Point pt = _startPoint - _dragOffset;
_draggingSprite->moveElementTo(pt.x, pt.y);
}

_lastHotspot = g_allHotspots.findHotspot(_lastRawPoint);
if (_lastHotspot)
enterHotspot(_lastHotspot);
}
}
}

void SpriteDragger::continueTracking(const Input &input) {
if (_draggingSprite) {
Common::Point rawPoint;
input.getInputLocation(rawPoint);

if (!_slopRect.contains(rawPoint))
rawPoint = _startRawPoint;

if (rawPoint != _lastRawPoint) {
Common::Point newPoint = rawPoint;
pinPointInRect(_limitRect, newPoint);
newPoint -= _dragOffset;

if (newPoint != _lastPoint) {
_draggingSprite->moveElementTo(newPoint.x, newPoint.y);
_lastPoint = newPoint;
}

Hotspot *newHotspot = g_allHotspots.findHotspot(rawPoint);
if (newHotspot != _lastHotspot) {
if (_lastHotspot)
exitHotspot(_lastHotspot);
if (newHotspot)
enterHotspot(newHotspot);
_lastHotspot = newHotspot;
}

_lastRawPoint = rawPoint;
}
}
}

void SpriteDragger::pinPointInRect(const Common::Rect &r, Common::Point &pt) {
pt.x = CLIP<int>(pt.x, r.left, r.right - 1);
pt.y = CLIP<int>(pt.y, r.top, r.bottom - 1);
}

ItemDragger::ItemDragger(PegasusEngine *owner) : _inventoryDropSpot(kInventoryDropSpotID), _biochipDropSpot(kBiochipDropSpotID),
_inventoryHighlight(kInventoryDropHighlightID), _biochipHighlight(kBiochipDropHighlightID) {
_owner = owner;

_inventoryHighlight.setBounds(Common::Rect(76, 334, 172, 430));
_biochipHighlight.setBounds(Common::Rect(364, 334, 460, 430));

Common::Rect r(kInventoryDropLeft, kInventoryDropTop, kInventoryDropRight, kInventoryDropBottom);
_inventoryDropSpot.setArea(r);
_inventoryDropSpot.setHotspotFlags(kDropItemSpotFlag);
g_allHotspots.push_back(&_inventoryDropSpot);

r = Common::Rect(kBiochipDropLeft, kBiochipDropTop, kBiochipDropRight, kBiochipDropBottom);
_biochipDropSpot.setArea(r);
_biochipDropSpot.setHotspotFlags(kDropBiochipSpotFlag);
g_allHotspots.push_back(&_biochipDropSpot);
}

void ItemDragger::startTracking(const Input &input) {
_inventoryHighlight.setDisplayOrder(kInventoryHiliteOrder);
_inventoryHighlight.startDisplaying();

_biochipHighlight.setDisplayOrder(kBiochipHiliteOrder);
_biochipHighlight.startDisplaying();

SpriteDragger::startTracking(input);
}

void ItemDragger::stopTracking(const Input &input) {
SpriteDragger::stopTracking(input);
_inventoryHighlight.hide();
_biochipHighlight.hide();
_inventoryHighlight.stopDisplaying();
_biochipHighlight.stopDisplaying();
_owner->dragTerminated(input);
}

bool ItemDragger::stopTrackingInput(const Input &input) {
return !JMPPPInput::isDraggingInput(input);
}

void ItemDragger::enterHotspot(Hotspot *spot) {
if (spot->getObjectID() == kInventoryDropSpotID)
_inventoryHighlight.show();
else if (spot->getObjectID() == kBiochipDropSpotID)
_biochipHighlight.show();
else if ((spot->getHotspotFlags() & kDropItemSpotFlag) != 0)
_draggingSprite->setCurrentFrameIndex(1);
}

void ItemDragger::exitHotspot(Hotspot *spot) {
if (spot->getObjectID() == kInventoryDropSpotID)
_inventoryHighlight.hide();
else if (spot->getObjectID() == kBiochipDropSpotID)
_biochipHighlight.hide();
else if ((spot->getHotspotFlags() & kDropItemSpotFlag) != 0)
_draggingSprite->setCurrentFrameIndex(0);
}

} // End of namespace Pegasus
94 changes: 94 additions & 0 deletions engines/pegasus/items/itemdragger.h
@@ -0,0 +1,94 @@
/* 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.
*
*/

#ifndef PEGASUS_ITEMS_ITEMDRAGGER_H
#define PEGASUS_ITEMS_ITEMDRAGGER_H

#include "pegasus/input.h"

namespace Pegasus {

// TODO: Merge SpriteDragger and ItemDragger

class Hotspot;
class Sprite;

class SpriteDragger : public Tracker {
public:
SpriteDragger();
virtual ~SpriteDragger() {}

void setDragSprite(Sprite *);
Sprite *getDragSprite() { return _draggingSprite; }

void setDragConstraints(const Common::Rect &, const Common::Rect &);
void getDragConstraints(Common::Rect &, Common::Rect &) const;

void startTracking(const Input &);
void continueTracking(const Input&);

Hotspot *getLastHotspot() const { return _lastHotspot; }

protected:
virtual void enterHotspot(Hotspot *) {}
virtual void exitHotspot(Hotspot *) {}

Sprite *_draggingSprite;
Common::Point _startPoint, _lastPoint, _dragOffset;
Common::Point _startRawPoint, _lastRawPoint;
Common::Rect _rawLimitRect;
Common::Rect _limitRect;
Common::Rect _slopRect;
Hotspot *_lastHotspot;

// This is a replica of QuickDraw's PinPointInRect function
void pinPointInRect(const Common::Rect &, Common::Point &);
};

class PegasusEngine;

class ItemDragger : public SpriteDragger {
public:
ItemDragger(PegasusEngine *);
virtual ~ItemDragger() {}

void startTracking(const Input &);
void stopTracking(const Input &);
bool stopTrackingInput(const Input &);

protected:
virtual void enterHotspot(Hotspot *);
virtual void exitHotspot(Hotspot *);

PegasusEngine *_owner;
DropHighlight _inventoryHighlight;
Hotspot _inventoryDropSpot;
DropHighlight _biochipHighlight;
Hotspot _biochipDropSpot;
};

} // End of namespace Pegasus

#endif
1 change: 1 addition & 0 deletions engines/pegasus/module.mk
Expand Up @@ -29,6 +29,7 @@ MODULE_OBJS = \
items/inventory.o \
items/inventorypicture.o \
items/item.o \
items/itemdragger.o \
items/itemlist.o \
items/biochips/aichip.o \
items/biochips/biochipitem.o \
Expand Down
1 change: 1 addition & 0 deletions engines/pegasus/pegasus.h
Expand Up @@ -150,6 +150,7 @@ friend class InputHandler;
void dragItem(const Input &, Item *, tDragType);
tDragType getDragType() const { return (tDragType)0; } // TODO
Item *getDraggingItem() const { return 0; } // TODO
void dragTerminated(const Input &) {} // TODO

// Save/Load
void makeContinuePoint();
Expand Down

0 comments on commit af78726

Please sign in to comment.