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

gamewindow.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/world/gamewindow.h"
00024 
00025 #include "engines/stark/scene.h"
00026 
00027 #include "engines/stark/gfx/driver.h"
00028 
00029 #include "engines/stark/resources/anim.h"
00030 #include "engines/stark/resources/knowledgeset.h"
00031 #include "engines/stark/resources/image.h"
00032 #include "engines/stark/resources/item.h"
00033 #include "engines/stark/resources/location.h"
00034 #include "engines/stark/resources/layer.h"
00035 
00036 #include "engines/stark/services/global.h"
00037 #include "engines/stark/services/services.h"
00038 #include "engines/stark/services/staticprovider.h"
00039 #include "engines/stark/services/gameinterface.h"
00040 #include "engines/stark/services/userinterface.h"
00041 
00042 #include "engines/stark/ui/cursor.h"
00043 #include "engines/stark/ui/world/actionmenu.h"
00044 #include "engines/stark/ui/world/inventorywindow.h"
00045 
00046 #include "engines/stark/visual/text.h"
00047 #include "engines/stark/visual/image.h"
00048 
00049 namespace Stark {
00050 
00051 GameWindow::GameWindow(Gfx::Driver *gfx, Cursor *cursor, ActionMenu *actionMenu, InventoryWindow *inventory) :
00052         Window(gfx, cursor),
00053     _actionMenu(actionMenu),
00054     _inventory(inventory),
00055     _objectUnderCursor(nullptr),
00056     _displayExit(false) {
00057     _position = Common::Rect(Gfx::Driver::kGameViewportWidth, Gfx::Driver::kGameViewportHeight);
00058     _position.translate(0, Gfx::Driver::kTopBorderHeight);
00059     _visible = true;
00060 
00061     _fadeRenderer = _gfx->createFadeRenderer();
00062 
00063     _exitArrow = StarkStaticProvider->getUIElement(StaticProvider::kExitArrow);
00064     _exitArrowLeft = StarkStaticProvider->getUIElement(StaticProvider::kExitArrowLeft);
00065     _exitArrowRight = StarkStaticProvider->getUIElement(StaticProvider::kExitArrowRight);
00066 
00067     _exitLeftBoundary = 5;
00068     _exitRightBoundary = Gfx::Driver::kGameViewportWidth - _exitArrowRight->getWidth() - 5;
00069 }
00070 
00071 GameWindow::~GameWindow() {
00072     delete _fadeRenderer;
00073 }
00074 
00075 void GameWindow::onRender() {
00076     // List the items to render
00077     Resources::Location *location = StarkGlobal->getCurrent()->getLocation();
00078     _renderEntries = location->listRenderEntries();
00079     Gfx::LightEntryArray lightEntries = location->listLightEntries();
00080 
00081     // Render all the scene items
00082     Gfx::RenderEntryArray::iterator element = _renderEntries.begin();
00083     while (element != _renderEntries.end()) {
00084         // Draw the current element
00085         (*element)->render(lightEntries);
00086 
00087         // Go for the next one
00088         element++;
00089     }
00090 
00091     if (_displayExit) {
00092         Common::Array<Common::Point> exitPositions = StarkGameInterface->listExitPositions();
00093 
00094         for (uint i = 0; i < exitPositions.size(); ++i) {
00095             Common::Point pos = exitPositions[i];
00096             VisualImageXMG *exitImage = nullptr;
00097 
00098             if (pos.x < _exitLeftBoundary) {
00099                 pos.x = _exitLeftBoundary;
00100                 exitImage = _exitArrowLeft;
00101             } else if (pos.x > _exitRightBoundary) {
00102                 pos.x = _exitRightBoundary;
00103                 exitImage = _exitArrowRight;
00104             } else {
00105                 exitImage = _exitArrow;
00106             }
00107 
00108             exitImage->render(pos, false);
00109         }
00110     }
00111 
00112     float fadeLevel = StarkScene->getFadeLevel();
00113     if ((1.0f - fadeLevel) > 0.00001f) {
00114         _fadeRenderer->render(fadeLevel);
00115     }
00116 }
00117 
00118 void GameWindow::onMouseMove(const Common::Point &pos) {
00119     _renderEntries = StarkGlobal->getCurrent()->getLocation()->listRenderEntries();
00120     _cursor->setFading(false);
00121 
00122     if (!StarkUserInterface->isInteractive()) {
00123         _objectUnderCursor = nullptr;
00124         _cursor->setCursorType(Cursor::kPassive);
00125         _cursor->setMouseHint("");
00126         return;
00127     }
00128 
00129     int16 selectedInventoryItem = _inventory->getSelectedInventoryItem();
00130     int16 singlePossibleAction = -1;
00131     bool defaultAction = false;
00132 
00133     checkObjectAtPos(pos, selectedInventoryItem, singlePossibleAction, defaultAction);
00134 
00135     Common::String mouseHint;
00136 
00137     if (selectedInventoryItem != -1 && !defaultAction) {
00138         VisualImageXMG *cursorImage = StarkGameInterface->getCursorImage(selectedInventoryItem);
00139         _cursor->setCursorImage(cursorImage);
00140         _cursor->setFading(singlePossibleAction == selectedInventoryItem);
00141     } else if (_objectUnderCursor) {
00142         switch (singlePossibleAction) {
00143             case -1:
00144                 _cursor->setCursorType(Cursor::kActive);
00145                 break;
00146             case Resources::PATTable::kActionLook:
00147                 _cursor->setCursorType(Cursor::kEye);
00148                 break;
00149             case Resources::PATTable::kActionTalk:
00150                 _cursor->setCursorType(Cursor::kMouth);
00151                 break;
00152             case Resources::PATTable::kActionUse:
00153                 _cursor->setCursorType(Cursor::kHand);
00154                 break;
00155             default:
00156                 VisualImageXMG *cursorImage = StarkGameInterface->getCursorImage(singlePossibleAction);
00157                 _cursor->setCursorImage(cursorImage);
00158                 break;
00159         }
00160 
00161         mouseHint = StarkGameInterface->getItemTitleAt(_objectUnderCursor, _objectRelativePosition);
00162     } else {
00163         // Not an object
00164         _cursor->setCursorType(Cursor::kDefault);
00165     }
00166     _cursor->setMouseHint(mouseHint);
00167 }
00168 
00169 void GameWindow::onClick(const Common::Point &pos) {
00170     if (!StarkGlobal->getCurrent()) {
00171         return; // No level is loaded yet, interaction is impossible
00172     }
00173 
00174     if (!StarkUserInterface->isInteractive()) {
00175         StarkUserInterface->markInteractionDenied();
00176         return;
00177     }
00178 
00179     _actionMenu->close();
00180 
00181     int16 selectedInventoryItem = _inventory->getSelectedInventoryItem();
00182     int16 singlePossibleAction = -1;
00183     bool defaultAction;
00184 
00185     checkObjectAtPos(pos, selectedInventoryItem, singlePossibleAction, defaultAction);
00186 
00187     if (_objectUnderCursor) {
00188         if (singlePossibleAction != -1) {
00189             StarkGameInterface->itemDoActionAt(_objectUnderCursor, singlePossibleAction, _objectRelativePosition);
00190         } else if (selectedInventoryItem == -1) {
00191             _actionMenu->open(_objectUnderCursor, _objectRelativePosition);
00192         }
00193     } else {
00194         // The walk code expects unscaled absolute mouse coordinates
00195         StarkGameInterface->walkTo(_cursor->getMousePosition(true));
00196     }
00197 }
00198 
00199 void GameWindow::onRightClick(const Common::Point &pos) {
00200     if (!StarkUserInterface->isInteractive()) {
00201         return;
00202     }
00203 
00204     int16 selectedInventoryItem = _inventory->getSelectedInventoryItem();
00205 
00206     if (selectedInventoryItem == -1) {
00207         _inventory->open();
00208     } else {
00209         _inventory->setSelectedInventoryItem(-1);
00210     }
00211 }
00212 
00213 void GameWindow::onDoubleClick(const Common::Point &pos) {
00214     if (!StarkUserInterface->isInteractive()) {
00215         StarkUserInterface->markInteractionDenied();
00216         return;
00217     }
00218 
00219     if (StarkGameInterface->isAprilWalking()) {
00220         StarkGameInterface->setAprilRunning();
00221     }
00222 }
00223 
00224 void GameWindow::checkObjectAtPos(const Common::Point &pos, int16 selectedInventoryItem, int16 &singlePossibleAction, bool &isDefaultAction) {
00225     _objectUnderCursor = nullptr;
00226     singlePossibleAction = -1;
00227     isDefaultAction = false;
00228 
00229     Math::Ray ray = StarkScene->makeRayFromMouse(_cursor->getMousePosition(true));
00230 
00231     Common::Rect cursorRect;
00232     if (selectedInventoryItem != -1) {
00233         cursorRect = _cursor->getHotRectangle();
00234         cursorRect.translate(pos.x, pos.y);
00235     }
00236 
00237     // Render entries are sorted from the farthest to the camera to the nearest
00238     // Loop in reverse order
00239     for (int i = _renderEntries.size() - 1; i >= 0; i--) {
00240         if (_renderEntries[i]->containsPoint(pos, _objectRelativePosition, cursorRect)
00241             || _renderEntries[i]->intersectRay(ray)) {
00242             _objectUnderCursor = _renderEntries[i]->getOwner();
00243             break;
00244         }
00245     }
00246 
00247     if (!_objectUnderCursor || !StarkGameInterface->itemHasActionAt(_objectUnderCursor, _objectRelativePosition, -1)) {
00248         // Only consider items with runnable scripts
00249         _objectUnderCursor = nullptr;
00250         return;
00251     }
00252 
00253     int32 defaultAction = StarkGameInterface->itemGetDefaultActionAt(_objectUnderCursor, _objectRelativePosition);
00254     if (defaultAction != -1) {
00255         // Use the default action if there is one
00256         singlePossibleAction = defaultAction;
00257         isDefaultAction = true;
00258     } else if (selectedInventoryItem != -1) {
00259         // Use the selected inventory item if there is one
00260         if (StarkGameInterface->itemHasActionAt(_objectUnderCursor, _objectRelativePosition, selectedInventoryItem)) {
00261             singlePossibleAction = selectedInventoryItem;
00262         }
00263     } else {
00264         // Otherwise, use stock actions
00265         Resources::ActionArray actionsPossible = StarkGameInterface->listStockActionsPossibleForObjectAt(
00266                 _objectUnderCursor, _objectRelativePosition);
00267 
00268         if (actionsPossible.size() == 1) {
00269             singlePossibleAction = actionsPossible[0];
00270         }
00271     }
00272 }
00273 
00274 void GameWindow::reset() {
00275     _renderEntries.clear();
00276     _objectUnderCursor = nullptr;
00277     _objectRelativePosition.x = 0;
00278     _objectRelativePosition.y = 0;
00279 }
00280 
00281 void GameWindow::onScreenChanged() {
00282     // May be called when resources have not been loaded
00283     if (!StarkGlobal->getCurrent()) {
00284         return;
00285     }
00286 
00287     Resources::Location *location = StarkGlobal->getCurrent()->getLocation();
00288     Common::Array<Resources::ImageText *> images = location->listChildrenRecursive<Resources::ImageText>(Resources::Image::kImageText);
00289 
00290     for (uint i = 0; i < images.size(); i++) {
00291         images[i]->resetVisual();
00292     }
00293 }
00294 
00295 } // End of namespace Stark


Generated on Sat Jul 13 2019 05:01:14 for ResidualVM by doxygen 1.7.1
curved edge   curved edge