aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/star_control/star_crosshairs.cpp
blob: 0393da5dcec7d0b8c31b98afebb66bddfa24d7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* 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.
 *
 */

#include "titanic/star_control/star_crosshairs.h"
#include "titanic/star_control/star_markers.h"
#include "titanic/star_control/star_camera.h"
#include "titanic/star_control/star_field.h"
#include "titanic/star_control/star_ref.h"
#include "titanic/support/simple_file.h"

namespace Titanic {

CStarCrosshairs::CStarCrosshairs() : _matchIndex(-1), _entryIndex(-1) {
}

void CStarCrosshairs::selectStar(int index, CVideoSurface *surface,
		CStarField *starField, CStarMarkers *markers) {
	if (_entryIndex >= 0) {
		// There are existing selected stars already
		if (_entryIndex == _matchIndex) {
			// All the stars selected so far have been matched. Only allow
			// a selection addition if not all three stars have been found
			if (!isSolved()) {
				// Don't allow the most recent match or the one before
				// it to be re-selected (while they are locked/matched)
				if (_positions[index] != _entries[_entryIndex]) {
					if (_entryIndex == 1) {
						// 2 stars are matched
						if (_positions[index] == _entries[_entryIndex - 1])
							return;
					}

					surface->lock();

					// Draw crosshairs around the selected star
					CSurfaceArea surfaceArea(surface);
					drawStar(index, &surfaceArea);
					surface->unlock();

					// Copy the star into the list of selected ones
					++_entryIndex;
					CStarPosition &newP = _entries[_entryIndex];
					newP = _positions[index];

					// Set up a marker in the main starfield for that same star
					const CBaseStarEntry *starP = starField->getDataPtr(newP._index1);
					markers->addStar(starP);
				}
			}
		} else if (_entryIndex == _matchIndex + 1) {
			// There is a most recently selected star that has not yet been matched.
			// So we allow the user to reselect it to remove the selection, or shift
			// the selection to some other star
			if (_positions[index] == _entries[_entryIndex]) {
				// Player has selected the most recent star
				// Remove the crosshairs for the previously selected star
				surface->lock();
				CSurfaceArea surfaceArea(surface);
				eraseCurrent(&surfaceArea);
				surface->unlock();

				// Decrement number of selections
				--_entryIndex;

				// Call the markers addStar method, which will remove the existing marker
				const CBaseStarEntry *starP = starField->getDataPtr(_positions[index]._index1);
				markers->addStar(starP);
			} else {
				// Player has selected some other star other than the most recent
				// Remove/Add it if it is not one of the other star(s) already matched

				// Check that it is not a previously star and don't remove it if it is
				for (int i = 0; i < _entryIndex; ++i) {
					if (_positions[index] == _entries[i])
						return;
				}

				// Erase the prior selection and draw the new one
				surface->lock();
				CSurfaceArea surfaceArea(surface);
				eraseCurrent(&surfaceArea);
				drawStar(index, &surfaceArea);
				surface->unlock();

				// Remove the old selection from the starfield markers
				const CBaseStarEntry *starP;
				starP = starField->getDataPtr(_entries[_entryIndex]._index1);
				markers->addStar(starP);

				// Add the new selection to the markers list
				starP = starField->getDataPtr(_positions[index]._index1);
				markers->addStar(starP);

				// Copy the newly selected star's details into our selections list
				CStarPosition &newP = _entries[_entryIndex];
				newP = _positions[index];
			}
		}
	} else {
		// Very first star being selected
		// Draw crosshairs around the selected star
		surface->lock();
		CSurfaceArea surfaceArea(surface);
		drawStar(index, &surfaceArea);
		surface->unlock();

		// Copy the star into the list of selected ones
		++_entryIndex;
		const CStarPosition &srcPos = _positions[index];
		CStarPosition &destPos = _entries[_entryIndex];
		destPos = srcPos;

		// Set up a marker in the main starfield for that same star
		const CBaseStarEntry *starP = starField->getDataPtr(destPos._index1);
		markers->addStar(starP);
	}
}

bool CStarCrosshairs::fn1(CStarField *starField, CSurfaceArea *surfaceArea, CStarCamera *camera) {
	int count = starField->baseFn2(surfaceArea, camera);

	if (count > 0) {
		allocate(count);
		CStarRefArray starRef(starField, &_positions);
		starRef.process(surfaceArea, camera);
		return true;
	} else {
		clear();
		return false;
	}
}

void CStarCrosshairs::fn2(CVideoSurface *surface, CStarField *starField, CStarMarkers *markers) {
	if (_matchIndex <= -1) {
		if (_entryIndex > -1) {
			drawEntry(_entryIndex, surface, starField, markers);
			--_entryIndex;
		}
	} else {
		--_matchIndex;
		if (_entryIndex - _matchIndex > 1) {
			drawEntry(_entryIndex, surface, starField, markers);
			--_entryIndex;
		}
	}
}

void CStarCrosshairs::incMatches() {
	if (_matchIndex < 3)
		++_matchIndex;
}

FPoint CStarCrosshairs::getPosition() const {
	return (_entryIndex >= 0 && _entryIndex <= 2) ? 
		FPoint(_entries[_entryIndex]) : FPoint();
}

void CStarCrosshairs::draw(CSurfaceArea *surfaceArea) {
	if (!_positions.empty()) {
		uint savedPixel = surfaceArea->_pixel;
		surfaceArea->_pixel = 0xff;
		surfaceArea->setColorFromPixel();
		SurfaceAreaMode savedMode = surfaceArea->setMode(SA_SOLID);

		for (int idx = 0; idx <= _entryIndex; ++idx) {
			const CStarPosition &src = _entries[idx];
			double xp = src.x, yp = src.y;

			surfaceArea->drawLine(FRect(xp - 8.0, yp, xp - 4.0, yp));
			surfaceArea->drawLine(FRect(xp + 4.0, yp, xp + 8.0, yp));
			surfaceArea->drawLine(FRect(xp, yp - 8.0, xp, yp - 4.0));
			surfaceArea->drawLine(FRect(xp, yp + 4.0, xp, yp + 8.0));
		}

		surfaceArea->_pixel = savedPixel;
		surfaceArea->setColorFromPixel();
		surfaceArea->setMode(savedMode);
	}
}

void CStarCrosshairs::allocate(int count) {
	if (!_positions.empty()) {
		if ((int)_positions.size() == count)
			return;

		clear();
	}

	_positions.resize(count);
}

void CStarCrosshairs::clear() {
	_positions.clear();
	_matchIndex = _entryIndex = -1;
}

int CStarCrosshairs::indexOf(const Common::Point &pt) const {
	Common::Rect r(pt.x - 2, pt.y - 2, pt.x + 2, pt.y + 2);

	for (int idx = 0; idx < (int)_positions.size(); ++idx) {
		if (r.contains(_positions[idx]))
			return idx;
	}

	return -1;
}

void CStarCrosshairs::drawStar(int index, CSurfaceArea *surfaceArea) {
	if (index >= 0 && index < (int)_positions.size()) {
		const CStarPosition &pt = _positions[index];
		drawAt(pt, surfaceArea);
	}
}

void CStarCrosshairs::drawEntry(int index, CVideoSurface *surface, CStarField *starField, CStarMarkers *markers) {
	surface->lock();
	CSurfaceArea surfaceArea(surface);
	drawAt(_entries[index], &surfaceArea);
	surface->unlock();

	const CBaseStarEntry *starP = starField->getDataPtr(_entries[index]._index1);
	markers->addStar(starP);
}

void CStarCrosshairs::eraseCurrent(CSurfaceArea *surfaceArea) {
	assert(_entryIndex >= 0);
	const CStarPosition &pt = _entries[_entryIndex];
	drawAt(pt, surfaceArea);
}

void CStarCrosshairs::drawAt(const FPoint &pt, CSurfaceArea *surfaceArea) {
	uint savedPixel = surfaceArea->_pixel;
	surfaceArea->_pixel = 255;
	surfaceArea->setColorFromPixel();
	SurfaceAreaMode savedMode = surfaceArea->setMode(SA_XOR);


	surfaceArea->drawLine(FRect(pt._x - 8.0, pt._y, pt._x - 4.0, pt._y));
	surfaceArea->drawLine(FRect(pt._x + 4.0, pt._y, pt._x + 8.0, pt._y));
	surfaceArea->drawLine(FRect(pt._x, pt._y - 8.0, pt._x, pt._y - 4.0));
	surfaceArea->drawLine(FRect(pt._x, pt._y + 4.0, pt._x, pt._y + 8.0));

	surfaceArea->_pixel = savedPixel;
	surfaceArea->setColorFromPixel();
	surfaceArea->setMode(savedMode);
}

} // End of namespace Titanic