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  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "titanic/pet_control/pet_slider.h"
24 #include "titanic/pet_control/pet_control.h"
25 
26 namespace Titanic {
27 
CPetSlider()28 CPetSlider::CPetSlider() {
29 	_orientation = 0;
30 	_thumbWidth = 0;
31 	_thumbHeight = 0;
32 	_sliderOffset = 0;
33 	_thumbFocused = false;
34 }
35 
clearDirtyArea()36 Rect CPetSlider::clearDirtyArea() {
37 	Rect result = _dirtyArea;
38 	_dirtyArea.clear();
39 	return result;
40 }
41 
checkThumb(const Point & pt)42 bool CPetSlider::checkThumb(const Point &pt) {
43 	_thumbFocused = thumbContains(pt);
44 	if (_thumbFocused)
45 		return true;
46 	else
47 		return containsPt(pt);
48 }
49 
resetThumbFocus()50 bool CPetSlider::resetThumbFocus() {
51 	bool result = _thumbFocused;
52 	_thumbFocused = false;
53 	return result;
54 }
55 
MouseDragMoveMsg(const Point & pt)56 bool CPetSlider::MouseDragMoveMsg(const Point &pt) {
57 	int newOffset = calcSliderOffset(pt);
58 	setOffsetPixels(newOffset);
59 	return true;
60 }
61 
MouseButtonUpMsg(const Point & pt)62 bool CPetSlider::MouseButtonUpMsg(const Point &pt) {
63 	if (thumbContains(pt))
64 		return true;
65 	if (!containsPt(pt))
66 		return false;
67 
68 	int newOffset = calcSliderOffset(pt);
69 	setOffsetPixels(newOffset);
70 	return true;
71 }
72 
contains(const Point & pt) const73 bool CPetSlider::contains(const Point &pt) const {
74 	return thumbContains(pt) || containsPt(pt);
75 }
76 
getOffsetPixels() const77 double CPetSlider::getOffsetPixels() const {
78 	int maxVal = 0, minVal = 0;
79 	if (_orientation & ORIENTATION_HORIZONTAL) {
80 		maxVal = _slidingRect.right;
81 		minVal = _slidingRect.left;
82 	}
83 
84 	if (_orientation & ORIENTATION_VERTICAL) {
85 		maxVal = _slidingRect.bottom;
86 		minVal = _slidingRect.top;
87 	}
88 
89 	if (minVal == maxVal)
90 		return 0.0;
91 
92 	return (double)_sliderOffset / (maxVal - minVal);
93 }
94 
setSliderOffset(double offset)95 void CPetSlider::setSliderOffset(double offset) {
96 	if (_orientation & ORIENTATION_HORIZONTAL)
97 		_sliderOffset = (int)(offset * (_slidingRect.right - _slidingRect.left));
98 
99 	if (_orientation & ORIENTATION_VERTICAL)
100 		_sliderOffset = (int)(offset * (_slidingRect.bottom - _slidingRect.top));
101 }
102 
setOffsetPixels(int offset)103 void CPetSlider::setOffsetPixels(int offset) {
104 	// Add the slider's old position to the dirty area
105 	Rect tempRect = getThumbRect();
106 	_dirtyArea.combine(tempRect);
107 
108 	// Set the new offset
109 	_sliderOffset = offset;
110 
111 	// Add the thumb's new location to the dirty area
112 	tempRect = getThumbRect();
113 	_dirtyArea.combine(tempRect);
114 }
115 
getBackgroundDrawPos()116 Point CPetSlider::getBackgroundDrawPos() {
117 	return Point(_bounds.left, _bounds.top);
118 }
119 
getThumbDrawPos()120 Point CPetSlider::getThumbDrawPos() {
121 	Point thumbPos = getThumbCentroidPos();
122 	thumbPos -= Point(_thumbWidth / 2, _thumbHeight / 2);
123 	return thumbPos;
124 }
125 
getThumbCentroidPos() const126 Point CPetSlider::getThumbCentroidPos() const {
127 	Point pt;
128 
129 	if (_orientation & ORIENTATION_HORIZONTAL) {
130 		pt = Point(_slidingRect.left + _sliderOffset,
131 			_slidingRect.top + _slidingRect.height() / 2);
132 	}
133 
134 	if (_orientation & ORIENTATION_VERTICAL) {
135 		pt = Point(_slidingRect.left + _slidingRect.width() / 2,
136 			_slidingRect.top + _sliderOffset);
137 	}
138 
139 	return pt;
140 }
141 
thumbContains(const Point & pt) const142 bool CPetSlider::thumbContains(const Point &pt) const {
143 	return getThumbRect().contains(pt);
144 }
145 
getThumbRect() const146 Rect CPetSlider::getThumbRect() const {
147 	Rect thumbRect(0, 0, _thumbWidth, _thumbHeight);
148 	Point centroid = getThumbCentroidPos();
149 	thumbRect.moveTo(centroid.x - _thumbWidth / 2, centroid.y - _thumbHeight / 2);
150 
151 	return thumbRect;
152 }
153 
calcSliderOffset(const Point & pt) const154 int CPetSlider::calcSliderOffset(const Point &pt) const {
155 	int result = 0;
156 
157 	if (_orientation & ORIENTATION_HORIZONTAL) {
158 		result = CLIP(pt.x, _slidingRect.left, _slidingRect.right) - _slidingRect.left;
159 	}
160 
161 	if (_orientation & ORIENTATION_VERTICAL) {
162 		result = CLIP(pt.y, _slidingRect.top, _slidingRect.bottom) - _slidingRect.top;
163 	}
164 
165 	return result;
166 }
167 
setOrientation(SliderOrientation orientation)168 void CPetSlider::setOrientation(SliderOrientation orientation) {
169 	_orientation |= orientation;
170 }
171 
stepPosition(int direction)172 void CPetSlider::stepPosition(int direction) {
173 	double val = getOffsetPixels();
174 
175 	if (direction == -1) {
176 		val = MAX<double>(val - 0.1, 0.0);
177 	} else if (direction == 1) {
178 		val = MIN<double>(val + 0.1, 1.0);
179 	}
180 
181 	setSliderOffset(val);
182 }
183 
184 /*------------------------------------------------------------------------*/
185 
setupBackground(const CString & name,CPetControl * petControl)186 void CPetSoundSlider::setupBackground(const CString &name, CPetControl *petControl) {
187 	if (petControl) {
188 		_background = petControl->getHiddenObject(name);
189 	}
190 }
191 
setupThumb(const CString & name,CPetControl * petControl)192 void CPetSoundSlider::setupThumb(const CString &name, CPetControl *petControl) {
193 	if (petControl) {
194 		_thumb = petControl->getHiddenObject(name);
195 	}
196 }
197 
setupBackground2(const CString & name,CPetControl * petControl)198 void CPetSoundSlider::setupBackground2(const CString &name, CPetControl *petControl) {
199 	if (petControl) {
200 		CString numStr = "3";
201 		int mode = petControl->getPassengerClass();
202 		if (mode <= 3) {
203 			numStr = CString(mode);
204 		} else if (mode == 4) {
205 			mode = petControl->getPriorClass();
206 			if (mode == 1) {
207 				numStr = CString(mode);
208 			}
209 		}
210 
211 		CString fullName = numStr + name;
212 		setupBackground(fullName, petControl);
213 	}
214 }
215 
setupThumb2(const CString & name,CPetControl * petControl)216 void CPetSoundSlider::setupThumb2(const CString &name, CPetControl *petControl) {
217 	if (petControl) {
218 		CString numStr = "3";
219 		int mode = petControl->getPassengerClass();
220 		if (mode <= 3) {
221 			numStr = CString(mode);
222 		} else if (mode == 4) {
223 			mode = petControl->getPriorClass();
224 			if (mode == 1) {
225 				numStr = CString(mode);
226 			}
227 		}
228 
229 		CString fullName = numStr + name;
230 		setupThumb(fullName, petControl);
231 	}
232 }
233 
draw(CScreenManager * screenManager)234 void CPetSoundSlider::draw(CScreenManager *screenManager) {
235 	if (_background) {
236 		Point pt = getBackgroundDrawPos();
237 		_background->draw(screenManager, pt);
238 	}
239 
240 	if (_thumb) {
241 		Point pt = getThumbDrawPos();
242 		_thumb->draw(screenManager, pt);
243 	}
244 }
245 
246 } // End of namespace Titanic
247