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/star_control/star_control.h"
24 #include "titanic/core/dont_save_file_item.h"
25 #include "titanic/core/project_item.h"
26 #include "titanic/game_manager.h"
27 #include "titanic/pet_control/pet_control.h"
28 #include "titanic/star_control/motion_control.h"
29 #include "titanic/star_control/error_code.h"
30 #include "titanic/support/screen_manager.h"
31 
32 namespace Titanic {
33 
BEGIN_MESSAGE_MAP(CStarControl,CGameObject)34 BEGIN_MESSAGE_MAP(CStarControl, CGameObject)
35 	ON_MESSAGE(MouseMoveMsg)
36 	ON_MESSAGE(MouseButtonDownMsg)
37 	ON_MESSAGE(KeyCharMsg)
38 	ON_MESSAGE(FrameMsg)
39 	ON_MESSAGE(MovementMsg)
40 END_MESSAGE_MAP()
41 
42 CStarControl::CStarControl() : _enabled(false), _petControl(nullptr),
43 		_starRect(20, 10, 620, 350) {
44 	CCamera::init();
45 }
46 
~CStarControl()47 CStarControl::~CStarControl() {
48 	CCamera::deinit();
49 }
50 
save(SimpleFile * file,int indent)51 void CStarControl::save(SimpleFile *file, int indent) {
52 	file->writeNumberLine(0, indent);
53 	_starField.save(file, indent);
54 	_view.save(file, indent);
55 	CGameObject::save(file, indent);
56 }
57 
load(SimpleFile * file)58 void CStarControl::load(SimpleFile *file) {
59 	int val = file->readNumber();
60 
61 	if (!val) {
62 		_starField.load(file);
63 		if (!_starField.initDocument())
64 			error("Couldn't initialise the StarField document");
65 
66 		_view.load(file, 0);
67 		CScreenManager *screenManager = CScreenManager::setCurrent();
68 		if (!screenManager)
69 			error("There's no screen manager during loading");
70 
71 		_view.setup(screenManager, &_starField, this);
72 		_view.takeCurrentHomePhoto();
73 
74 		_enabled = true;
75 	}
76 
77 	CGameObject::load(file);
78 }
79 
draw(CScreenManager * screenManager)80 void CStarControl::draw(CScreenManager *screenManager) {
81 	if (_visible)
82 		_view.draw(screenManager);
83 }
84 
MouseButtonDownMsg(CMouseButtonDownMsg * msg)85 bool CStarControl::MouseButtonDownMsg(CMouseButtonDownMsg *msg) {
86 	if (_visible && _starRect.contains(msg->_mousePos)) {
87 		_view.MouseButtonDownMsg(0, Point(msg->_mousePos.x - 20,
88 			msg->_mousePos.y - 10));
89 		return true;
90 	} else {
91 		return false;
92 	}
93 }
94 
MouseMoveMsg(CMouseMoveMsg * msg)95 bool CStarControl::MouseMoveMsg(CMouseMoveMsg *msg) {
96 	if (_visible && _starRect.contains(msg->_mousePos)) {
97 		_view.MouseMoveMsg(0, Point(msg->_mousePos.x - 20,
98 			msg->_mousePos.y - 10));
99 		makeDirty();
100 		return true;
101 	} else {
102 		return false;
103 	}
104 }
105 
KeyCharMsg(CKeyCharMsg * msg)106 bool CStarControl::KeyCharMsg(CKeyCharMsg *msg) {
107 	if (_visible) {
108 		CErrorCode errorCode;
109 		_view.KeyCharMsg(msg->_key, &errorCode);
110 		return errorCode.get();
111 	}
112 
113 	return false;
114 }
115 
FrameMsg(CFrameMsg * msg)116 bool CStarControl::FrameMsg(CFrameMsg *msg) {
117 	if (_visible) {
118 		Point pt = getMousePos();
119 		if (_starRect.contains(pt))
120 			_view.MouseMoveMsg(0, pt);
121 
122 		newFrame();
123 		makeDirty();
124 		return true;
125 	} else {
126 		return false;
127 	}
128 }
129 
newFrame()130 void CStarControl::newFrame() {
131 	if (!_petControl)
132 		_petControl = getPetControl();
133 
134 	if (_petControl) {
135 		int matchIndex = _starField.getMatchedIndex();
136 		bool isClose = false;
137 
138 		if (_starField.getMode() == MODE_STARFIELD) {
139 			isClose = _starField.isCloseToMarker();
140 			if ((matchIndex + 2) != _starField.getMarkerCount())
141 				isClose = false;
142 		}
143 
144 		_petControl->starsSetButtons(matchIndex, isClose);
145 	}
146 }
147 
isStarFieldMode()148 bool CStarControl::isStarFieldMode() {
149 	if (!_petControl)
150 		_petControl = getPetControl();
151 
152 	if (_petControl) {
153 
154 		if (_starField.getMode() == MODE_STARFIELD)
155 			return true;
156 	}
157 	return false;
158 }
159 
doAction(StarControlAction action)160 void CStarControl::doAction(StarControlAction action) {
161 	if (!_enabled)
162 		return;
163 
164 	switch (action) {
165 	case STAR_SHOW: {
166 		CGameManager *gameManager = getGameManager();
167 		CViewItem *view = gameManager ? gameManager->getView() : nullptr;
168 		if (view) {
169 			detach();
170 			addUnder(view);
171 			_view.resetView();
172 			_view.triggerFade(true);
173 			_visible = true;
174 		}
175 		break;
176 	}
177 
178 	case STAR_HIDE: {
179 		CProjectItem *root = getRoot();
180 		CDontSaveFileItem *fileItem = root ? root->getDontSaveFileItem() : nullptr;
181 		if (fileItem) {
182 			detach();
183 			addUnder(fileItem);
184 			_visible = false;
185 		}
186 		break;
187 	}
188 
189 	case STAR_VIEW_EARTH:
190 		_view.viewEarth();
191 		break;
192 
193 	case STAR_VIEW_FROM_EARTH:
194 		_view.viewFromEarth();
195 		break;
196 
197 	case STAR_VIEW_BOUNDARIES:
198 		_view.viewBoundaries();
199 		break;
200 
201 	case STAR_VIEW_CONSTELLATIONS:
202 		_view.viewConstellations();
203 		break;
204 
205 	case STAR_VIEW_RANDOM_STAR:
206 		_view.viewRandomStar();
207 		break;
208 
209 	case STAR_FULL_SPEED:
210 		_view.fullSpeed();
211 		break;
212 
213 	case STAR_TOGGLE_STEREO_PAIR:
214 		_view.toggleSteroPair();
215 		break;
216 
217 	case STAR_TOGGLE_HOME_PHOTO:
218 		_view.toggleHomePhoto();
219 		break;
220 
221 	case STAR_TOGGLE_SOLAR_RENDERING:
222 		_view.toggleSolarRendering();
223 		break;
224 
225 	case STAR_TOGGLE_POS_FRAME:
226 		_view.TogglePosFrame();
227 		break;
228 
229 	case STAR_STEREO_PAIR_ON:
230 		_view.stereoPairOn();
231 		break;
232 
233 	case STAR_STEREO_PAIR_OFF:
234 		_view.stereoPairOff();
235 		break;
236 
237 	case STAR_SET_REFERENCE: {
238 		_view.takeCurrentHomePhoto();
239 		CPetControl *pet = getPetControl();
240 		if (pet)
241 			pet->starsSetReference();
242 		break;
243 	}
244 
245 	case STAR_FADE_IN:
246 		_view.triggerFade(true);
247 		break;
248 
249 	case STAR_FADE_OUT:
250 		_view.triggerFade(false);
251 		break;
252 
253 	case LOCK_STAR:
254 		_view.lockStar();
255 		break;
256 
257 	case UNLOCK_STAR:
258 		_view.unlockStar();
259 		break;
260 
261 	case STAR_CLEAR_MODIFIED:
262 		_view.starDestinationSet();
263 		break;
264 
265 	default:
266 		break;
267 	}
268 }
269 
isSolved() const270 bool CStarControl::isSolved() const {
271 	return _starField.isSolved();
272 }
273 
isSkipped() const274 bool CStarControl::isSkipped() const {
275 	return _starField.isSkipped();
276 }
277 
forceSolved()278 void CStarControl::forceSolved() {
279 	_starField.skipPuzzle();
280 }
281 
canSetStarDestination() const282 bool CStarControl::canSetStarDestination() const {
283 	return _view.canSetStarDestination();
284 }
285 
starDestinationSet()286 void CStarControl::starDestinationSet() {
287 	_view.starDestinationSet();
288 }
289 
MovementMsg(CMovementMsg * msg)290 bool CStarControl::MovementMsg(CMovementMsg *msg) {
291 	// The star control view has an unused turn right link hidden
292 	// under the star view. For cleanliness, explicitly consume any
293 	// movements in the star view so the link is never used
294 	return true;
295 }
296 
297 } // End of namespace Titanic
298