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 "common/scummsys.h"
24 
25 #include "zvision/scripting/controls/hotmov_control.h"
26 
27 #include "zvision/zvision.h"
28 #include "zvision/scripting/script_manager.h"
29 #include "zvision/graphics/render_manager.h"
30 #include "zvision/graphics/cursors/cursor_manager.h"
31 
32 #include "common/stream.h"
33 #include "common/file.h"
34 #include "common/system.h"
35 #include "graphics/surface.h"
36 #include "video/video_decoder.h"
37 
38 namespace ZVision {
39 
HotMovControl(ZVision * engine,uint32 key,Common::SeekableReadStream & stream)40 HotMovControl::HotMovControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream)
41 	: Control(engine, key, CONTROL_HOTMOV) {
42 	_animation = NULL;
43 	_cycle = 0;
44 	_frames.clear();
45 	_cyclesCount = 0;
46 	_framesCount = 0;
47 
48 	_engine->getScriptManager()->setStateValue(_key, 0);
49 
50 	// Loop until we find the closing brace
51 	Common::String line = stream.readLine();
52 	_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
53 	Common::String param;
54 	Common::String values;
55 	getParams(line, param, values);
56 
57 	while (!stream.eos() && !line.contains('}')) {
58 		if (param.matchString("hs_frame_list", true)) {
59 			readHsFile(values);
60 		} else if (param.matchString("rectangle", true)) {
61 			int x;
62 			int y;
63 			int width;
64 			int height;
65 
66 			sscanf(values.c_str(), "%d %d %d %d", &x, &y, &width, &height);
67 
68 			_rectangle = Common::Rect(x, y, width, height);
69 		} else if (param.matchString("num_frames", true)) {
70 			_framesCount = atoi(values.c_str());
71 		} else if (param.matchString("num_cycles", true)) {
72 			_cyclesCount = atoi(values.c_str());
73 		} else if (param.matchString("animation", true)) {
74 			char filename[64];
75 			sscanf(values.c_str(), "%s", filename);
76 			values = Common::String(filename);
77 			_animation = _engine->loadAnimation(values);
78 			_animation->start();
79 		} else if (param.matchString("venus_id", true)) {
80 			_venusId = atoi(values.c_str());
81 		}
82 
83 		line = stream.readLine();
84 		_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
85 		getParams(line, param, values);
86 	}
87 }
88 
~HotMovControl()89 HotMovControl::~HotMovControl() {
90 	if (_animation)
91 		delete _animation;
92 
93 	_frames.clear();
94 }
95 
process(uint32 deltaTimeInMillis)96 bool HotMovControl::process(uint32 deltaTimeInMillis) {
97 	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
98 		return false;
99 
100 	if (_cycle < _cyclesCount) {
101 		if (_animation && _animation->endOfVideo()) {
102 			_cycle++;
103 
104 			if (_cycle == _cyclesCount) {
105 				_engine->getScriptManager()->setStateValue(_key, 2);
106 				return false;
107 			}
108 
109 			_animation->rewind();
110 		}
111 
112 		if (_animation && _animation->needsUpdate()) {
113 			const Graphics::Surface *frameData = _animation->decodeNextFrame();
114 			if (frameData)
115 				_engine->getRenderManager()->blitSurfaceToBkgScaled(*frameData, _rectangle);
116 		}
117 	}
118 
119 	return false;
120 }
121 
onMouseMove(const Common::Point & screenSpacePos,const Common::Point & backgroundImageSpacePos)122 bool HotMovControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
123 	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
124 		return false;
125 
126 	if (!_animation)
127 		return false;
128 
129 	if (_cycle < _cyclesCount) {
130 		if (_frames[_animation->getCurFrame()].contains(backgroundImageSpacePos)) {
131 			_engine->getCursorManager()->changeCursor(CursorIndex_Active);
132 			return true;
133 		}
134 	}
135 
136 	return false;
137 }
138 
onMouseUp(const Common::Point & screenSpacePos,const Common::Point & backgroundImageSpacePos)139 bool HotMovControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
140 	if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
141 		return false;
142 
143 	if (!_animation)
144 		return false;
145 
146 	if (_cycle < _cyclesCount) {
147 		if (_frames[_animation->getCurFrame()].contains(backgroundImageSpacePos)) {
148 			setVenus();
149 			_engine->getScriptManager()->setStateValue(_key, 1);
150 			return true;
151 		}
152 	}
153 
154 	return false;
155 }
156 
readHsFile(const Common::String & fileName)157 void HotMovControl::readHsFile(const Common::String &fileName) {
158 	if (_framesCount == 0)
159 		return;
160 
161 	Common::File file;
162 	if (!_engine->getSearchManager()->openFile(file, fileName)) {
163 		warning("HS file %s could could be opened", fileName.c_str());
164 		return;
165 	}
166 
167 	Common::String line;
168 
169 	_frames.resize(_framesCount);
170 
171 	while (!file.eos()) {
172 		line = file.readLine();
173 
174 		int frame;
175 		int x;
176 		int y;
177 		int width;
178 		int height;
179 
180 		sscanf(line.c_str(), "%d:%d %d %d %d~", &frame, &x, &y, &width, &height);
181 
182 		if (frame >= 0 && frame < _framesCount)
183 			_frames[frame] = Common::Rect(x, y, width, height);
184 	}
185 	file.close();
186 }
187 
188 } // End of namespace ZVision
189