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/input_control.h"
26 #include "zvision/scripting/controls/save_control.h"
27 
28 #include "zvision/zvision.h"
29 #include "zvision/scripting/script_manager.h"
30 #include "zvision/text/string_manager.h"
31 
32 #include "zvision/file/save_manager.h"
33 #include "zvision/graphics/render_manager.h"
34 
35 #include "common/str.h"
36 #include "common/stream.h"
37 
38 namespace ZVision {
39 
SaveControl(ZVision * engine,uint32 key,Common::SeekableReadStream & stream)40 SaveControl::SaveControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream)
41 	: Control(engine, key, CONTROL_SAVE),
42 	  _saveControl(false) {
43 	// Loop until we find the closing brace
44 	Common::String line = stream.readLine();
45 	_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
46 	Common::String param;
47 	Common::String values;
48 	getParams(line, param, values);
49 
50 	while (!stream.eos() && !line.contains('}')) {
51 		if (param.matchString("savebox", true)) {
52 			int saveId;
53 			int inputId;
54 
55 			sscanf(values.c_str(), "%d %d", &saveId, &inputId);
56 			saveElement elmnt;
57 			elmnt.inputKey = inputId;
58 			elmnt.saveId = saveId;
59 			elmnt.exist = false;
60 			_inputs.push_back(elmnt);
61 		} else if (param.matchString("control_type", true)) {
62 			if (values.contains("save"))
63 				_saveControl = true;
64 			else
65 				_saveControl = false;
66 		}
67 
68 		line = stream.readLine();
69 		_engine->getScriptManager()->trimCommentsAndWhiteSpace(&line);
70 		getParams(line, param, values);
71 	}
72 
73 	for (saveElmntList::iterator iter = _inputs.begin(); iter != _inputs.end(); ++iter) {
74 		Control *ctrl = _engine->getScriptManager()->getControl(iter->inputKey);
75 		if (ctrl && ctrl->getType() == Control::CONTROL_INPUT) {
76 			InputControl *inp = (InputControl *)ctrl;
77 			inp->setReadOnly(!_saveControl);
78 			Common::SeekableReadStream *save = _engine->getSaveManager()->getSlotFile(iter->saveId);
79 			if (save) {
80 				SaveGameHeader header;
81 				if (_engine->getSaveManager()->readSaveGameHeader(save, header)) {
82 					inp->setText(header.saveName);
83 					iter->exist = true;
84 				}
85 				delete save;
86 			}
87 		}
88 	}
89 }
90 
process(uint32 deltaTimeInMillis)91 bool SaveControl::process(uint32 deltaTimeInMillis) {
92 	for (saveElmntList::iterator iter = _inputs.begin(); iter != _inputs.end(); ++iter) {
93 		Control *ctrl = _engine->getScriptManager()->getControl(iter->inputKey);
94 		if (ctrl && ctrl->getType() == Control::CONTROL_INPUT) {
95 			InputControl *inp = (InputControl *)ctrl;
96 			if (inp->enterPress()) {
97 				if (_saveControl) {
98 					if (inp->getText().size() > 0) {
99 						bool toSave = true;
100 						if (iter->exist)
101 							if (!_engine->getRenderManager()->askQuestion(_engine->getStringManager()->getTextLine(StringManager::ZVISION_STR_SAVEEXIST)))
102 								toSave = false;
103 
104 						if (toSave) {
105 							_engine->getSaveManager()->saveGame(iter->saveId, inp->getText(), true);
106 							_engine->getRenderManager()->delayedMessage(_engine->getStringManager()->getTextLine(StringManager::ZVISION_STR_SAVED), 2000);
107 							_engine->getScriptManager()->changeLocation(_engine->getScriptManager()->getLastMenuLocation());
108 						}
109 					} else {
110 						_engine->getRenderManager()->timedMessage(_engine->getStringManager()->getTextLine(StringManager::ZVISION_STR_SAVEEMPTY), 2000);
111 					}
112 				} else {
113 					_engine->getSaveManager()->loadGame(iter->saveId);
114 					return true;
115 				}
116 				break;
117 			}
118 		}
119 	}
120 	return false;
121 }
122 
123 } // End of namespace ZVision
124