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 #include "xeen/dialogs/dialogs.h"
25 #include "xeen/events.h"
26 #include "xeen/resources.h"
27 #include "xeen/screen.h"
28 #include "xeen/xeen.h"
29 
30 namespace Xeen {
31 
saveButtons()32 void ButtonContainer::saveButtons() {
33 	_savedButtons.push(_buttons);
34 	clearButtons();
35 }
36 
37 /*
38  * Clears the current list of defined buttons
39  */
clearButtons()40 void ButtonContainer::clearButtons() {
41 	_buttons.clear();
42 }
43 
restoreButtons()44 void ButtonContainer::restoreButtons() {
45 	_buttons = _savedButtons.pop();
46 }
47 
addButton(const Common::Rect & bounds,int val,SpriteResource * sprites)48 void ButtonContainer::addButton(const Common::Rect &bounds, int val,
49 		SpriteResource *sprites) {
50 	_buttons.push_back(UIButton(bounds, val, _buttons.size() * 2, sprites, sprites != nullptr));
51 }
52 
addButton(const Common::Rect & bounds,int val,int frameNum,SpriteResource * sprites)53 void ButtonContainer::addButton(const Common::Rect &bounds, int val,
54 		int frameNum, SpriteResource *sprites) {
55 	_buttons.push_back(UIButton(bounds, val, frameNum, sprites, sprites != nullptr));
56 }
57 
addPartyButtons(XeenEngine * vm)58 void ButtonContainer::addPartyButtons(XeenEngine *vm) {
59 	for (uint idx = 0; idx < MAX_ACTIVE_PARTY; ++idx) {
60 		addButton(Common::Rect(Res.CHAR_FACES_X[idx], 150, Res.CHAR_FACES_X[idx] + 32, 182),
61 			Common::KEYCODE_F1 + idx);
62 	}
63 }
64 
checkEvents(XeenEngine * vm)65 bool ButtonContainer::checkEvents(XeenEngine *vm) {
66 	EventsManager &events = *vm->_events;
67 	Party &party = *vm->_party;
68 	Windows &windows = *_vm->_windows;
69 	PendingEvent event;
70 	_buttonValue = 0;
71 
72 	if (events.getEvent(event)) {
73 		if (event._leftButton) {
74 			Common::Point pt = events._mousePos;
75 
76 			// Check for party member glyphs being clicked
77 			Common::Rect r(0, 0, 32, 32);
78 			for (uint idx = 0; idx < party._activeParty.size(); ++idx) {
79 				r.moveTo(Res.CHAR_FACES_X[idx], 150);
80 				if (r.contains(pt)) {
81 					_buttonValue = Common::KEYCODE_F1 + idx;
82 					break;
83 				}
84 			}
85 
86 			// Check whether any button is selected
87 			for (uint i = 0; i < _buttons.size(); ++i) {
88 				if (_buttons[i]._bounds.contains(pt) && _buttons[i]._value) {
89 					events.debounceMouse();
90 
91 					_buttonValue = _buttons[i]._value;
92 					break;
93 				}
94 			}
95 
96 			if (!_buttonValue && _waitBounds.contains(pt)) {
97 				_buttonValue = Common::KEYCODE_SPACE;
98 				return true;
99 			}
100 
101 		} else if (event.isKeyboard()) {
102 			const Common::KeyCode &keycode = event._keyState.keycode;
103 
104 			if (keycode == Common::KEYCODE_KP8)
105 				_buttonValue = Common::KEYCODE_UP;
106 			else if (keycode == Common::KEYCODE_KP2)
107 				_buttonValue = Common::KEYCODE_DOWN;
108 			else if (keycode == Common::KEYCODE_KP_ENTER)
109 				_buttonValue = Common::KEYCODE_RETURN;
110 			else if (keycode != Common::KEYCODE_LCTRL && keycode != Common::KEYCODE_RCTRL
111 					&& keycode != Common::KEYCODE_LALT && keycode != Common::KEYCODE_RALT)
112 				_buttonValue = keycode;
113 
114 			if (_buttonValue)
115 				_buttonValue |= (event._keyState.flags & ~Common::KBD_STICKY) << 16;
116 		}
117 	}
118 
119 	if (_buttonValue) {
120 		// Check for a button matching the selected _buttonValue
121 		Window &win = windows[39];
122 		for (uint btnIndex = 0; btnIndex < _buttons.size(); ++btnIndex) {
123 			UIButton &btn = _buttons[btnIndex];
124 			if (btn._draw && btn._value == _buttonValue) {
125 				// Found the correct button
126 				// Draw button depressed
127 				btn._sprites->draw(0, btn._selectedFrame, Common::Point(btn._bounds.left, btn._bounds.top));
128 				win.setBounds(btn._bounds);
129 				win.update();
130 
131 				// Slight delay
132 				events.updateGameCounter();
133 				events.wait(2);
134 
135 				// Redraw button in it's original non-depressed form
136 				btn._sprites->draw(0, btn._frameNum, Common::Point(btn._bounds.left, btn._bounds.top));
137 				win.setBounds(btn._bounds);
138 				win.update();
139 				break;
140 			}
141 		}
142 
143 		return true;
144 	}
145 
146 	return false;
147 }
148 
drawButtons(XSurface * surface)149 void ButtonContainer::drawButtons(XSurface *surface) {
150 	for (uint btnIndex = 0; btnIndex < _buttons.size(); ++btnIndex) {
151 		UIButton &btn = _buttons[btnIndex];
152 		if (btn._draw) {
153 			assert(btn._sprites);
154 			btn._sprites->draw(*surface, btn._frameNum,
155 				Common::Point(btn._bounds.left, btn._bounds.top));
156 		}
157 	}
158 }
159 
doScroll(bool rollUp,bool fadeIn)160 bool ButtonContainer::doScroll(bool rollUp, bool fadeIn) {
161 	if (_vm->_files->_ccNum) {
162 		return Cutscenes::doScroll(rollUp, fadeIn);
163 	} else {
164 		saveButtons();
165 		clearButtons();
166 		bool result = Cutscenes::doScroll(rollUp, fadeIn);
167 		restoreButtons();
168 		return result;
169 	}
170 }
171 
loadStrings(const Common::String & name)172 void ButtonContainer::loadStrings(const Common::String &name) {
173 	File f(name);
174 	_textStrings.clear();
175 	while (f.pos() < f.size())
176 		_textStrings.push_back(f.readString());
177 	f.close();
178 }
179 
loadStrings(const Common::String & name,int ccMode)180 void ButtonContainer::loadStrings(const Common::String &name, int ccMode) {
181 	File f(name, ccMode);
182 	_textStrings.clear();
183 	while (f.pos() < f.size())
184 		_textStrings.push_back(f.readString());
185 	f.close();
186 }
187 
setWaitBounds()188 void ButtonContainer::setWaitBounds() {
189 	_waitBounds = Common::Rect(8, 8, 224, 140);
190 }
191 
192 /*------------------------------------------------------------------------*/
193 
showContents(SpriteResource & title1,bool waitFlag)194 void SettingsBaseDialog::showContents(SpriteResource &title1, bool waitFlag) {
195 	_vm->_events->pollEventsAndWait();
196 	checkEvents(_vm);
197 }
198 
199 } // End of namespace Xeen
200