aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/tattoo/widget_tooltip.cpp
blob: ee4b3cee2a855c47e6b674bd58b9d8981b4620c0 (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
/* 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 "sherlock/tattoo/widget_tooltip.h"
#include "sherlock/tattoo/tattoo_user_interface.h"
#include "sherlock/tattoo/tattoo.h"

namespace Sherlock {

namespace Tattoo {

WidgetTooltip::WidgetTooltip(SherlockEngine *vm) : WidgetBase(vm) {
}

void WidgetTooltip::execute() {
	Events &events = *_vm->_events;
	People &people = *_vm->_people;
	Scene &scene = *_vm->_scene;
	Screen &screen = *_vm->_screen;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;
	Common::Point mousePos = events.mousePos();
	bool reset = false;

	// See if thay are pointing at a different object and we need to regenerate the tooltip text
	if (ui._bgFound != ui._oldBgFound || (ui._bgFound != -1 && _surface.empty()) ||
			ui._arrowZone != ui._oldArrowZone || (ui._arrowZone != -1 && _surface.empty())) {
		// Keep track of the last place we drew the text
		_oldBounds = _bounds;

		// See if there is a new object to be displayed
		if ((ui._bgFound != -1 && (ui._bgFound != ui._oldBgFound || (ui._bgFound != -1 && _surface.empty()))) ||
				(ui._arrowZone != -1 && (ui._arrowZone != ui._oldArrowZone || (ui._arrowZone != -1 && _surface.empty())))) {
			Common::String str;
			if (ui._bgFound != -1) {
				// Clear the Arrow Zone fields so it won't think we're displaying an Arrow Zone cursor
				if (scene._currentScene != 90)  // RRR Take out the cludge for room 90
					ui._arrowZone = ui._oldArrowZone = -1;

				// Get the description string
				str = (ui._bgFound < 1000) ? scene._bgShapes[ui._bgFound]._description :
					people[ui._bgFound - 1000]._description;
			} else {
				// Get the exit zone description
				str = scene._exits[ui._arrowZone]._dest;
			}

			// Make sure that the description is present
			if (!str.empty() && !str.hasPrefix(" ")) {
				int width = screen.stringWidth(str) + 2;
				int height = screen.stringHeight(str) + 2;
				Common::String line1 = str, line2 = "";

				// See if we need to split it into two lines
				if (width > 150) {
					// Go forward word by word to find out where to split the line
					const char *s = str.c_str();
					const char *space = nullptr;
					int dif = 10000;

					for (;;) {
						// Find end of next word
						s = strchr(s + 1, ' ');

						if (s == nullptr) {
							// Reached end of string
							if (space != nullptr) {
								line1 = Common::String(str.c_str(), space);
								line2 = Common::String(space + 1);
								height = screen.stringHeight(line1) + screen.stringHeight(line2) + 4;
							}
							break;
						}

						// Found space separating words, so see what width the string up to now is
						Common::String tempLine1 = Common::String(str.c_str(), s);
						Common::String tempLine2 = Common::String(s + 1);
						int width1 = screen.stringWidth(tempLine1);
						int width2 = screen.stringWidth(tempLine2);

						// See if we've found a split point that results in a less overall width
						if (ABS(width1 - width2) < dif) {
							// Found a better split point
							dif = ABS(width1 - width2);
							space = s;
							line1 = tempLine1;
							line2 = tempLine2;
						}
					}
				} else {
					// No line split needed
					height = screen.stringHeight(str) + 2;
				}

				// Reallocate the text surface with the new size
				_surface.create(width, height, _vm->getPlatform());
				_surface.fill(TRANSPARENCY);

				if (line2.empty()) {
					// Only a single line
					_surface.writeString(str, Common::Point(0, 0), BLACK);
					_surface.writeString(str, Common::Point(1, 0), BLACK);
					_surface.writeString(str, Common::Point(2, 0), BLACK);
					_surface.writeString(str, Common::Point(0, 1), BLACK);
					_surface.writeString(str, Common::Point(2, 1), BLACK);
					_surface.writeString(str, Common::Point(0, 2), BLACK);
					_surface.writeString(str, Common::Point(1, 2), BLACK);
					_surface.writeString(str, Common::Point(2, 2), BLACK);
					_surface.writeString(str, Common::Point(1, 1), INFO_TOP);
				} else {
					// Two lines to display
					int xp, yp;
					xp = (width - screen.stringWidth(line1) - 2) / 2;
					_surface.writeString(line1, Common::Point(xp, 0), BLACK);
					_surface.writeString(line1, Common::Point(xp + 1, 0), BLACK);
					_surface.writeString(line1, Common::Point(xp + 2, 0), BLACK);
					_surface.writeString(line1, Common::Point(xp, 1), BLACK);
					_surface.writeString(line1, Common::Point(xp + 2, 1), BLACK);
					_surface.writeString(line1, Common::Point(xp, 2), BLACK);
					_surface.writeString(line1, Common::Point(xp + 1, 2), BLACK);
					_surface.writeString(line1, Common::Point(xp + 2, 2), BLACK);
					_surface.writeString(line1, Common::Point(xp + 1, 1), INFO_TOP);
					
					xp = (width - screen.stringWidth(line2) - 2) / 2;
					yp = screen.stringHeight(line1) + 2;
					_surface.writeString(line2, Common::Point(xp, yp), BLACK);
					_surface.writeString(line2, Common::Point(xp + 1, yp), BLACK);
					_surface.writeString(line2, Common::Point(xp + 2, yp), BLACK);
					_surface.writeString(line2, Common::Point(xp, yp + 1), BLACK);
					_surface.writeString(line2, Common::Point(xp + 2, yp + 1), BLACK);
					_surface.writeString(line2, Common::Point(xp, yp + 2), BLACK);
					_surface.writeString(line2, Common::Point(xp + 1, yp + 2), BLACK);
					_surface.writeString(line2, Common::Point(xp + 2, yp + 2), BLACK);
					_surface.writeString(line2, Common::Point(xp + 1, yp + 1), INFO_TOP);
				}

				// Set the initial display position for the tooltip text
				int tagX = CLIP(mousePos.x - width / 2, 0, SHERLOCK_SCREEN_WIDTH - width);
				int tagY = MAX(mousePos.y - height, 0);

				_bounds = Common::Rect(tagX, tagY, tagX + width, tagY + height);
			} else {
				reset = true;
			}
		} else if ((ui._bgFound == -1 && ui._oldBgFound != -1) || (ui._arrowZone == -1 && ui._oldArrowZone != -1)) {
			reset = true;
		}

		if (reset && !_surface.empty()) {
			_surface.free();
		}

		ui._oldBgFound = ui._bgFound;
	} else {
		// Keep track of the last place we drew the Text
		_oldBounds = _bounds;

		// Set the New position of the Text Tag
		int tagX = CLIP(mousePos.x - _bounds.width() / 2, 0, SHERLOCK_SCREEN_WIDTH - _bounds.width());
		int tagY = MAX(mousePos.y - _bounds.height(), 0);

		_bounds.moveTo(tagX, tagY);
	}

	ui._oldArrowZone = ui._arrowZone;
}

void WidgetTooltip::draw() {
	Screen &screen = *_vm->_screen;

	if (!_surface.empty())
		screen._backBuffer1.transBlitFrom(_surface, Common::Point(_bounds.left, _bounds.top));
}

void WidgetTooltip::erase() {
	Screen &screen = *_vm->_screen;
	TattooUserInterface &ui = *(TattooUserInterface *)_vm->_ui;

	if (_bounds.width() > 0) {
		screen.slamArea(_oldBounds.left - ui._currentScroll.x, _oldBounds.top, _oldBounds.width(), _oldBounds.height());

		// If there's no text actually being displayed, then reset bounds so we don't keep restoring the area
		if (_surface.empty()) {
			_bounds.left = _bounds.top = _bounds.right = _bounds.bottom = 0;
			_oldBounds.left = _oldBounds.top = _oldBounds.right = _oldBounds.bottom = 0;
		}
	}

	if (!_surface.empty())
		screen.slamArea(_bounds.left - ui._currentScroll.x, _bounds.top, _bounds.width(), _bounds.height());
}

void WidgetTooltip::erasePrevious() {
	Screen &screen = *_vm->_screen;
	if (_oldBounds.width() > 0)
		screen._backBuffer1.blitFrom(screen._backBuffer2, Common::Point(_oldBounds.left, _oldBounds.top),
		_oldBounds);
}

} // End of namespace Tattoo

} // End of namespace Sherlock