1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1995-1997 Presto Studios, Inc.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  */
25 
26 #include "pegasus/elements.h"
27 #include "pegasus/hotspot.h"
28 #include "pegasus/pegasus.h"
29 #include "pegasus/items/itemdragger.h"
30 
31 namespace Pegasus {
32 
SpriteDragger()33 SpriteDragger::SpriteDragger() {
34 	_draggingSprite = 0;
35 	_limitRect = Common::Rect(-30000, -30000, 30000, 30000);
36 	_slopRect = Common::Rect(-30000, -30000, 30000, 30000);
37 	_dragOffset.x = 0;
38 	_dragOffset.y = 0;
39 	_lastHotspot = 0;
40 }
41 
setDragSprite(Sprite * newSprite)42 void SpriteDragger::setDragSprite(Sprite *newSprite) {
43 	if (!isTracking())
44 		_draggingSprite = newSprite;
45 }
46 
setDragConstraints(const Common::Rect & limitRect,const Common::Rect & slopRect)47 void SpriteDragger::setDragConstraints(const Common::Rect &limitRect, const Common::Rect &slopRect) {
48 	if (!isTracking()) {
49 		_rawLimitRect = limitRect;
50 		_slopRect = slopRect;
51 	}
52 }
53 
getDragConstraints(Common::Rect & limitRect,Common::Rect & slopRect) const54 void SpriteDragger::getDragConstraints(Common::Rect &limitRect, Common::Rect &slopRect) const {
55 	limitRect = _rawLimitRect;
56 	slopRect = _slopRect;
57 }
58 
startTracking(const Input & input)59 void SpriteDragger::startTracking(const Input &input) {
60 	if (_draggingSprite) {
61 		Tracker::startTracking(input);
62 
63 		if (isTracking()) {
64 			input.getInputLocation(_startPoint);
65 			_lastRawPoint = _startRawPoint = _startPoint;
66 
67 			Common::Rect r;
68 			_draggingSprite->getBounds(r);
69 			_dragOffset.x = _startPoint.x - r.left;
70 			_dragOffset.y = _startPoint.y - r.top;
71 
72 			_limitRect = _rawLimitRect;
73 			_limitRect.left += _dragOffset.x;
74 			_limitRect.top += _dragOffset.y;
75 			_limitRect.right -= r.width() - _dragOffset.x;
76 			_limitRect.bottom -= r.height() - _dragOffset.y;
77 			pinPointInRect(_limitRect, _startPoint);
78 
79 			_lastPoint = _startPoint;
80 			if (_startPoint != _startRawPoint) {
81 				Common::Point pt = _startPoint - _dragOffset;
82 				_draggingSprite->moveElementTo(pt.x, pt.y);
83 			}
84 
85 			_lastHotspot = g_allHotspots.findHotspot(_lastRawPoint);
86 			if (_lastHotspot)
87 				enterHotspot(_lastHotspot);
88 		}
89 	}
90 }
91 
continueTracking(const Input & input)92 void SpriteDragger::continueTracking(const Input &input) {
93 	if (_draggingSprite) {
94 		Common::Point rawPoint;
95 		input.getInputLocation(rawPoint);
96 
97 		if (!_slopRect.contains(rawPoint))
98 			rawPoint = _startRawPoint;
99 
100 		if (rawPoint != _lastRawPoint) {
101 			Common::Point newPoint = rawPoint;
102 			pinPointInRect(_limitRect, newPoint);
103 			newPoint -= _dragOffset;
104 
105 			if (newPoint != _lastPoint) {
106 				_draggingSprite->moveElementTo(newPoint.x, newPoint.y);
107 				_lastPoint = newPoint;
108 			}
109 
110 			Hotspot *newHotspot = g_allHotspots.findHotspot(rawPoint);
111 			if (newHotspot != _lastHotspot) {
112 				if (_lastHotspot)
113 					exitHotspot(_lastHotspot);
114 				if (newHotspot)
115 					enterHotspot(newHotspot);
116 				_lastHotspot = newHotspot;
117 			}
118 
119 			_lastRawPoint = rawPoint;
120 		}
121 	}
122 }
123 
pinPointInRect(const Common::Rect & r,Common::Point & pt)124 void SpriteDragger::pinPointInRect(const Common::Rect &r, Common::Point &pt) {
125 	pt.x = CLIP<int>(pt.x, r.left, r.right - 1);
126 	pt.y = CLIP<int>(pt.y, r.top, r.bottom - 1);
127 }
128 
ItemDragger(PegasusEngine * owner)129 ItemDragger::ItemDragger(PegasusEngine *owner) : _inventoryDropSpot(kInventoryDropSpotID), _biochipDropSpot(kBiochipDropSpotID),
130 		_inventoryHighlight(kInventoryDropHighlightID), _biochipHighlight(kBiochipDropHighlightID) {
131 	_owner = owner;
132 
133 	Common::Rect r(kInventoryDropLeft, kInventoryDropTop, kInventoryDropRight, kInventoryDropBottom);
134 	_inventoryDropSpot.setArea(r);
135 	_inventoryDropSpot.setHotspotFlags(kDropItemSpotFlag);
136 	g_allHotspots.push_back(&_inventoryDropSpot);
137 
138 	r = Common::Rect(kBiochipDropLeft, kBiochipDropTop, kBiochipDropRight, kBiochipDropBottom);
139 	_biochipDropSpot.setArea(r);
140 	_biochipDropSpot.setHotspotFlags(kDropBiochipSpotFlag);
141 	g_allHotspots.push_back(&_biochipDropSpot);
142 }
143 
startTracking(const Input & input)144 void ItemDragger::startTracking(const Input &input) {
145 	_inventoryHighlight.setDisplayOrder(kInventoryHiliteOrder);
146 	_inventoryHighlight.startDisplaying();
147 
148 	_biochipHighlight.setDisplayOrder(kBiochipHiliteOrder);
149 	_biochipHighlight.startDisplaying();
150 
151 	SpriteDragger::startTracking(input);
152 }
153 
stopTracking(const Input & input)154 void ItemDragger::stopTracking(const Input &input) {
155 	SpriteDragger::stopTracking(input);
156 	_inventoryHighlight.hide();
157 	_biochipHighlight.hide();
158 	_inventoryHighlight.stopDisplaying();
159 	_biochipHighlight.stopDisplaying();
160 	_owner->dragTerminated(input);
161 }
162 
stopTrackingInput(const Input & input)163 bool ItemDragger::stopTrackingInput(const Input &input) {
164 	return !JMPPPInput::isDraggingInput(input);
165 }
166 
enterHotspot(Hotspot * spot)167 void ItemDragger::enterHotspot(Hotspot *spot) {
168 	if (spot->getObjectID() == kInventoryDropSpotID)
169 		_inventoryHighlight.show();
170 	else if (spot->getObjectID() == kBiochipDropSpotID)
171 		_biochipHighlight.show();
172 	else if ((spot->getHotspotFlags() & kDropItemSpotFlag) != 0)
173 		_draggingSprite->setCurrentFrameIndex(1);
174 }
175 
exitHotspot(Hotspot * spot)176 void ItemDragger::exitHotspot(Hotspot *spot) {
177 	if (spot->getObjectID() == kInventoryDropSpotID)
178 		_inventoryHighlight.hide();
179 	else if (spot->getObjectID() == kBiochipDropSpotID)
180 		_biochipHighlight.hide();
181 	else if ((spot->getHotspotFlags() & kDropItemSpotFlag) != 0)
182 		_draggingSprite->setCurrentFrameIndex(0);
183 }
184 
setHighlightBounds()185 void ItemDragger::setHighlightBounds() {
186 	uint32 color = g_system->getScreenFormat().RGBToColor(0x48, 0x80, 0xD8);
187 	_inventoryHighlight.setBounds(Common::Rect(76, 334, 172, 430));
188 	_inventoryHighlight.setHighlightColor(color);
189 	_biochipHighlight.setBounds(Common::Rect(364, 334, 460, 430));
190 	_biochipHighlight.setHighlightColor(color);
191 }
192 
193 } // End of namespace Pegasus
194