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

stark/ui/cursor.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/cursor.h"
00024 
00025 #include "engines/stark/gfx/driver.h"
00026 #include "engines/stark/gfx/texture.h"
00027 
00028 #include "engines/stark/services/gameinterface.h"
00029 #include "engines/stark/services/global.h"
00030 #include "engines/stark/services/services.h"
00031 #include "engines/stark/services/staticprovider.h"
00032 
00033 #include "engines/stark/resources/item.h"
00034 
00035 #include "engines/stark/visual/image.h"
00036 #include "engines/stark/visual/text.h"
00037 
00038 namespace Stark {
00039 
00040 const float Cursor::_fadeValueMax = 0.3f;
00041 
00042 Cursor::Cursor(Gfx::Driver *gfx) :
00043         _gfx(gfx),
00044         _cursorImage(nullptr),
00045         _mouseText(nullptr),
00046         _currentCursorType(kImage),
00047         _fading(false),
00048         _fadeLevelIncreasing(true),
00049         _fadeLevel(0),
00050         _hintDisplayDelay(150) {
00051     setCursorType(kDefault);
00052 }
00053 
00054 Cursor::~Cursor() {
00055     delete _mouseText;
00056 }
00057 
00058 void Cursor::setCursorType(CursorType type) {
00059     assert(type != kImage);
00060     if (type == _currentCursorType) {
00061         return;
00062     }
00063     _currentCursorType = type;
00064     _cursorImage = nullptr;
00065 }
00066 
00067 void Cursor::setCursorImage(VisualImageXMG *image) {
00068     _currentCursorType = kImage;
00069     _cursorImage = image;
00070 }
00071 
00072 void Cursor::setMousePosition(const Common::Point &pos) {
00073     _mousePos = pos;
00074     _hintDisplayDelay = 150;
00075 }
00076 
00077 void Cursor::setFading(bool fading) {
00078     _fading = fading;
00079 }
00080 
00081 void Cursor::onScreenChanged() {
00082     if (_mouseText) {
00083         _mouseText->resetTexture();
00084     }
00085 }
00086 
00087 void Cursor::updateFadeLevel() {
00088     if (_fading) {
00089         if (_fadeLevelIncreasing) {
00090             _fadeLevel += 0.001f * StarkGlobal->getMillisecondsPerGameloop();
00091         } else {
00092             _fadeLevel -= 0.001f * StarkGlobal->getMillisecondsPerGameloop();
00093         }
00094         if (ABS(_fadeLevel) >= _fadeValueMax) {
00095             _fadeLevelIncreasing = !_fadeLevelIncreasing;
00096             _fadeLevel = CLIP(_fadeLevel, -_fadeValueMax, _fadeValueMax);
00097         }
00098     } else {
00099         _fadeLevel = 0;
00100     }
00101 }
00102 
00103 void Cursor::updateHintDelay() {
00104     if (_hintDisplayDelay >= 0) {
00105         _hintDisplayDelay -= StarkGlobal->getMillisecondsPerGameloop();
00106 
00107         if (_hintDisplayDelay <= 0) {
00108             _hintDisplayDelay = -1;
00109         }
00110     }
00111 }
00112 
00113 void Cursor::render() {
00114     updateFadeLevel();
00115     updateHintDelay();
00116 
00117     if (!_gfx->isPosInScreenBounds(_mousePos)) {
00118         setCursorType(Cursor::kPassive);
00119     }
00120 
00121     if (_mouseText && _gfx->gameViewport().contains(_mousePos) && _hintDisplayDelay <= 0) {
00122         _gfx->setScreenViewport(false);
00123 
00124         // TODO: Should probably query the image for the width of the cursor
00125         // TODO: Add delay to the mouse hints like in the game
00126         const int16 cursorDistance = 32;
00127         Common::Rect mouseRect = _mouseText->getRect();
00128         Common::Point pos = _gfx->convertCoordinateCurrentToOriginal(_mousePos);
00129         pos.x = CLIP<int16>(pos.x, 48, Gfx::Driver::kOriginalWidth - 48);
00130         pos.y = CLIP<int16>(pos.y, Gfx::Driver::kTopBorderHeight, Gfx::Driver::kOriginalHeight - Gfx::Driver::kBottomBorderHeight - cursorDistance - mouseRect.height());
00131         pos.x -= mouseRect.width() / 2;
00132         pos.y += cursorDistance;
00133 
00134         _mouseText->render(pos);
00135     }
00136 
00137     if (_currentCursorType != kImage) {
00138         _cursorImage = StarkStaticProvider->getCursorImage(_currentCursorType);
00139     }
00140 
00141     if (_cursorImage) {
00142         _gfx->setScreenViewport(true); // Unscaled viewport so that cursor is drawn on native pixel space, thus no 'skipping', perform scaling below instead
00143 
00144         _cursorImage->setFadeLevel(_fadeLevel);
00145         _cursorImage->render(_mousePos, true, false); // Draws image (scaled)
00146     }
00147 }
00148 
00149 Common::Point Cursor::getMousePosition(bool unscaled) const {
00150     if (unscaled) {
00151         return _mousePos;
00152     } else {
00153         // Most of the engine expects 640x480 coordinates
00154         return _gfx->convertCoordinateCurrentToOriginal(_mousePos);
00155     }
00156 }
00157 
00158 void Cursor::setMouseHint(const Common::String &hint) {
00159     if (hint != _currentHint) {
00160         delete _mouseText;
00161         if (!hint.empty()) {
00162             _mouseText = new VisualText(_gfx);
00163             _mouseText->setText(hint);
00164             _mouseText->setColor(Color(0xFF, 0xFF, 0xFF));
00165             _mouseText->setBackgroundColor(Color(0x00, 0x00, 0x00, 0x80));
00166             _mouseText->setFont(FontProvider::kSmallFont);
00167             _mouseText->setTargetWidth(96);
00168         } else {
00169             _mouseText = nullptr;
00170         }
00171         _currentHint = hint;
00172         _hintDisplayDelay = 150;
00173     }
00174 }
00175 
00176 Common::Rect Cursor::getHotRectangle() const {
00177     if (!_cursorImage) {
00178         return Common::Rect();
00179     } else {
00180         Common::Point hotSpot = _cursorImage->getHotspot();
00181 
00182         Common::Rect hotRectangle;
00183         hotRectangle.setWidth(_cursorImage->getWidth());
00184         hotRectangle.setHeight(_cursorImage->getHeight());
00185         hotRectangle.translate(-hotSpot.x, -hotSpot.y);
00186 
00187         return hotRectangle;
00188     }
00189 }
00190 
00191 } // End of namespace Stark


Generated on Sat Feb 23 2019 05:01:00 for ResidualVM by doxygen 1.7.1
curved edge   curved edge