ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

locationscreen.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/ui/menu/locationscreen.h"
00024 
00025 #include "engines/stark/gfx/driver.h"
00026 
00027 #include "engines/stark/services/services.h"
00028 #include "engines/stark/services/staticprovider.h"
00029 
00030 #include "engines/stark/resources/item.h"
00031 #include "engines/stark/resources/location.h"
00032 #include "engines/stark/resources/sound.h"
00033 
00034 #include "engines/stark/ui/cursor.h"
00035 
00036 #include "engines/stark/visual/text.h"
00037 
00038 #include "audio/mixer.h"
00039 #include "common/system.h"
00040 
00041 namespace Stark {
00042 
00043 StaticLocationScreen::StaticLocationScreen(Gfx::Driver *gfx, Cursor *cursor,
00044                                            const char *locationName, Screen::Name screenName) :
00045         SingleWindowScreen(screenName, gfx, cursor),
00046         _locationName(locationName),
00047         _location(nullptr),
00048         _hoveredWidgetIndex(-1) {
00049     _position = Common::Rect(Gfx::Driver::kOriginalWidth, Gfx::Driver::kOriginalHeight);
00050     _visible = true;
00051 }
00052 
00053 StaticLocationScreen::~StaticLocationScreen() {
00054     freeWidgets();
00055 }
00056 
00057 void StaticLocationScreen::open() {
00058     _location = StarkStaticProvider->loadLocation(_locationName);
00059 }
00060 
00061 void StaticLocationScreen::close() {
00062     freeWidgets();
00063 
00064     StarkStaticProvider->unloadLocation(_location);
00065     _location = nullptr;
00066 }
00067 
00068 void StaticLocationScreen::freeWidgets() {
00069     for (uint i = 0; i < _widgets.size(); i++) {
00070         delete _widgets[i];
00071     }
00072 
00073     _widgets.clear();
00074     _hoveredWidgetIndex = -1;
00075 }
00076 
00077 void StaticLocationScreen::onGameLoop() {
00078     for (uint i = 0; i < _widgets.size(); i++) {
00079         StaticLocationWidget *widget = _widgets[i];
00080         if (widget->isVisible()) {
00081             widget->onGameLoop();
00082         }
00083     }
00084 }
00085 
00086 void StaticLocationScreen::onMouseMove(const Common::Point &pos) {
00087     int newHoveredWidget = -1;
00088 
00089     for (uint i = 0; i < _widgets.size(); i++) {
00090         StaticLocationWidget *widget = _widgets[i];
00091         widget->onMouseMove(pos);
00092 
00093         if (widget->isVisible() && widget->isMouseInside(pos)) {
00094             newHoveredWidget = i;
00095         }
00096     }
00097 
00098     // The first widget is always the background. It is ignored below.
00099 
00100     if (newHoveredWidget != _hoveredWidgetIndex) {
00101         if (_hoveredWidgetIndex > 0 && uint(_hoveredWidgetIndex) < _widgets.size()) {
00102             _widgets[_hoveredWidgetIndex]->onMouseLeave();
00103         }
00104 
00105         if (newHoveredWidget > 0) {
00106             _widgets[newHoveredWidget]->onMouseEnter();
00107         }
00108 
00109         _hoveredWidgetIndex = newHoveredWidget;
00110     }
00111 
00112     _cursor->setCursorType(_hoveredWidgetIndex > 0 ? Cursor::kActive : Cursor::kDefault);
00113 }
00114 
00115 void StaticLocationScreen::onClick(const Common::Point &pos) {
00116     for (uint i = 1; i < _widgets.size(); i++) {
00117         StaticLocationWidget *widget = _widgets[i];
00118         if (widget->isVisible() && widget->isMouseInside(pos)) {
00119             widget->onClick();
00120             break;
00121         }
00122     }
00123 }
00124 
00125 void StaticLocationScreen::onRender() {
00126     for (uint i = 0; i < _widgets.size(); i++) {
00127         if (_widgets[i]->isVisible()) {
00128             _widgets[i]->render();
00129         }
00130     }
00131 }
00132 
00133 void StaticLocationScreen::onScreenChanged() {
00134     for (uint i = 0; i < _widgets.size(); i++) {
00135         _widgets[i]->onScreenChanged();
00136     }
00137 }
00138 
00139 void StaticLocationScreen::waitForSoundsToComplete() {
00140     while (g_system->getMixer()->hasActiveChannelOfType(Audio::Mixer::kSFXSoundType)) {
00141         StarkGfx->clearScreen();
00142         g_system->delayMillis(10);
00143         StarkGfx->flipBuffer();
00144     }
00145 }
00146 
00147 StaticLocationWidget::StaticLocationWidget(const char *renderEntryName, WidgetOnClickCallback *onClickCallback,
00148                                            WidgetOnMouseMoveCallback *onMouseMoveCallback):
00149         _onClick(onClickCallback),
00150         _onMouseMove(onMouseMoveCallback),
00151         _renderEntry(nullptr),
00152         _item(nullptr),
00153         _soundMouseEnter(nullptr),
00154         _soundMouseClick(nullptr),
00155         _visible(true) {
00156     if (renderEntryName) {
00157         Resources::Location *location = StarkStaticProvider->getLocation();
00158         _renderEntry = location->getRenderEntryByName(renderEntryName);
00159 
00160         if (_renderEntry == nullptr) {
00161             debug("Widget disabled: unable to find render entry with name '%s' in location '%s'",
00162                     renderEntryName, location->getName().c_str());
00163             setVisible(false);
00164         } else {
00165             _item = _renderEntry->getOwner();
00166         }
00167     }
00168 }
00169 
00170 void StaticLocationWidget::render() {
00171     if (_renderEntry) {
00172         _renderEntry->render();
00173     }
00174 }
00175 
00176 bool StaticLocationWidget::isVisible() const {
00177     return _visible;
00178 }
00179 
00180 void StaticLocationWidget::setVisible(bool visible) {
00181     _visible = visible;
00182 }
00183 
00184 bool StaticLocationWidget::isMouseInside(const Common::Point &mousePos) const {
00185     if (!_renderEntry) return false;
00186 
00187     Common::Point relativePosition;
00188     return _renderEntry->containsPoint(mousePos, relativePosition, Common::Rect());
00189 }
00190 
00191 void StaticLocationWidget::onClick() {
00192     onMouseLeave();
00193 
00194     if (_soundMouseClick) {
00195         _soundMouseClick->play();
00196         _soundMouseClick->setStopOnDestroy(false);
00197     }
00198 
00199     if (_onClick) {
00200         (*_onClick)();
00201     }
00202 }
00203 
00204 void StaticLocationWidget::onGameLoop() {
00205     if (_item) {
00206         _item->onGameLoop();
00207     }
00208 }
00209 
00210 void StaticLocationWidget::onMouseEnter() {
00211     if (_soundMouseEnter && !_soundMouseEnter->isPlaying()) {
00212         _soundMouseEnter->play();
00213     }
00214 }
00215 
00216 void StaticLocationWidget::onMouseLeave() {
00217     if (_soundMouseEnter) {
00218         _soundMouseEnter->stop();
00219     }
00220 }
00221 
00222 void StaticLocationWidget::setupSounds(int16 enterSound, int16 clickSound) {
00223     if (enterSound != -1) {
00224         _soundMouseEnter = StarkStaticProvider->getLocationSound(enterSound);
00225     }
00226 
00227     if (clickSound != -1) {
00228         _soundMouseClick = StarkStaticProvider->getLocationSound(clickSound);
00229     }
00230 }
00231 
00232 void StaticLocationWidget::setTextColor(const Color &textColor) {
00233     if (!_renderEntry) return;
00234 
00235     VisualText *text = _renderEntry->getText();
00236     assert(text);
00237 
00238     text->setColor(textColor);
00239 }
00240 
00241 void StaticLocationWidget::onScreenChanged() {
00242     if (!_renderEntry) return;
00243 
00244     VisualText *text = _renderEntry->getText();
00245     if (text) {
00246         text->resetTexture();
00247     }
00248 }
00249 
00250 void StaticLocationWidget::onMouseMove(const Common::Point &mousePos) {
00251     if (_onMouseMove) {
00252         (*_onMouseMove)(*this, mousePos);
00253     }
00254 }
00255 
00256 Common::Point StaticLocationWidget::getPosition() const {
00257     if (_renderEntry) {
00258         return _renderEntry->getPosition();
00259     }
00260 
00261     return Common::Point(0, 0);
00262 }
00263 
00264 StaticLocationWidget::~StaticLocationWidget() {
00265     delete _onClick;
00266     delete _onMouseMove;
00267 }
00268 
00269 } // End of namespace Stark


Generated on Sat Jul 20 2019 05:00:59 for ResidualVM by doxygen 1.7.1
curved edge   curved edge