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     if (selectedInventoryItem != -1 && !defaultAction) {
00136         VisualImageXMG *cursorImage = StarkGameInterface->getCursorImage(selectedInventoryItem);
00137         _cursor->setCursorImage(cursorImage);
00138         _cursor->setFading(singlePossibleAction == selectedInventoryItem);
00139     } else if (_objectUnderCursor) {
00140         switch (singlePossibleAction) {
00141             case -1:
00142                 _cursor->setCursorType(Cursor::kActive);
00143                 break;
00144             case Resources::PATTable::kActionLook:
00145                 _cursor->setCursorType(Cursor::kEye);
00146                 break;
00147             case Resources::PATTable::kActionTalk:
00148                 _cursor->setCursorType(Cursor::kMouth);
00149                 break;
00150             case Resources::PATTable::kActionUse:
00151                 _cursor->setCursorType(Cursor::kHand);
00152                 break;
00153             default:
00154                 VisualImageXMG *cursorImage = StarkGameInterface->getCursorImage(singlePossibleAction);
00155                 _cursor->setCursorImage(cursorImage);
00156                 break;
00157         }
00158     } else {
00159         // Not an object
00160         _cursor->setCursorType(Cursor::kDefault);
00161     }
00162 
00163     Common::String mouseHint;
00164     if (_objectUnderCursor) {
00165         mouseHint = StarkGameInterface->getItemTitleAt(_objectUnderCursor, _objectRelativePosition);
00166     }
00167     _cursor->setMouseHint(mouseHint);
00168 }
00169 
00170 void GameWindow::onClick(const Common::Point &pos) {
00171     if (!StarkGlobal->getCurrent()) {
00172         return; // No level is loaded yet, interaction is impossible
00173     }
00174 
00175     if (!StarkUserInterface->isInteractive()) {
00176         StarkUserInterface->markInteractionDenied();
00177         return;
00178     }
00179 
00180     _actionMenu->close();
00181 
00182     int16 selectedInventoryItem = _inventory->getSelectedInventoryItem();
00183     int16 singlePossibleAction = -1;
00184     bool defaultAction;
00185 
00186     checkObjectAtPos(pos, selectedInventoryItem, singlePossibleAction, defaultAction);
00187 
00188     if (_objectUnderCursor) {
00189         if (singlePossibleAction != -1) {
00190             StarkGameInterface->itemDoActionAt(_objectUnderCursor, singlePossibleAction, _objectRelativePosition);
00191         } else if (selectedInventoryItem == -1) {
00192             _actionMenu->open(_objectUnderCursor, _objectRelativePosition);
00193         }
00194     } else {
00195         // The walk code expects unscaled absolute mouse coordinates
00196         StarkGameInterface->walkTo(_cursor->getMousePosition(true));
00197     }
00198 }
00199 
00200 void GameWindow::onRightClick(const Common::Point &pos) {
00201     if (!StarkUserInterface->isInteractive()) {
00202         return;
00203     }
00204 
00205     int16 selectedInventoryItem = _inventory->getSelectedInventoryItem();
00206 
00207     if (selectedInventoryItem == -1) {
00208         _inventory->open();
00209     } else {
00210         _inventory->setSelectedInventoryItem(-1);
00211     }
00212 }
00213 
00214 void GameWindow::onDoubleClick(const Common::Point &pos) {
00215     if (!StarkUserInterface->isInteractive()) {
00216         StarkUserInterface->markInteractionDenied();
00217         return;
00218     }
00219 
00220     if (StarkGameInterface->isAprilWalking()) {
00221         StarkGameInterface->setAprilRunning();
00222     }
00223 }
00224 
00225 void GameWindow::checkObjectAtPos(const Common::Point &pos, int16 selectedInventoryItem, int16 &singlePossibleAction, bool &isDefaultAction) {
00226     _objectUnderCursor = nullptr;
00227     singlePossibleAction = -1;
00228     isDefaultAction = false;
00229 
00230     Math::Ray ray = StarkScene->makeRayFromMouse(_cursor->getMousePosition(true));
00231 
00232     Common::Rect cursorRect;
00233     if (selectedInventoryItem != -1) {
00234         cursorRect = _cursor->getHotRectangle();
00235         cursorRect.translate(pos.x, pos.y);
00236     }
00237 
00238     // Render entries are sorted from the farthest to the camera to the nearest
00239     // Loop in reverse order
00240     for (int i = _renderEntries.size() - 1; i >= 0; i--) {
00241         if (_renderEntries[i]->containsPoint(pos, _objectRelativePosition, cursorRect)
00242             || _renderEntries[i]->intersectRay(ray)) {
00243             _objectUnderCursor = _renderEntries[i]->getOwner();
00244             break;
00245         }
00246     }
00247 
00248     if (!_objectUnderCursor || !StarkGameInterface->itemHasActionAt(_objectUnderCursor, _objectRelativePosition, -1)) {
00249         // Only consider items with runnable scripts
00250         _objectUnderCursor = nullptr;
00251         return;
00252     }
00253 
00254     int32 defaultAction = StarkGameInterface->itemGetDefaultActionAt(_objectUnderCursor, _objectRelativePosition);
00255     if (defaultAction != -1) {
00256         // Use the default action if there is one
00257         singlePossibleAction = defaultAction;
00258         isDefaultAction = true;
00259     } else if (selectedInventoryItem != -1) {
00260         // Use the selected inventory item if there is one
00261         if (StarkGameInterface->itemHasActionAt(_objectUnderCursor, _objectRelativePosition, selectedInventoryItem)) {
00262             singlePossibleAction = selectedInventoryItem;
00263         }
00264     } else {
00265         // Otherwise, use stock actions
00266         Resources::ActionArray actionsPossible = StarkGameInterface->listStockActionsPossibleForObjectAt(
00267                 _objectUnderCursor, _objectRelativePosition);
00268 
00269         if (actionsPossible.size() == 1) {
00270             singlePossibleAction = actionsPossible[0];
00271         }
00272     }
00273 }
00274 
00275 void GameWindow::reset() {
00276     _renderEntries.clear();
00277     _objectUnderCursor = nullptr;
00278     _objectRelativePosition.x = 0;
00279     _objectRelativePosition.y = 0;
00280 }
00281 
00282 void GameWindow::onScreenChanged() {
00283     // May be called when resources have not been loaded
00284     if (!StarkGlobal->getCurrent()) {
00285         return;
00286     }
00287 
00288     Resources::Location *location = StarkGlobal->getCurrent()->getLocation();
00289     Common::Array<Resources::ImageText *> images = location->listChildrenRecursive<Resources::ImageText>(Resources::Image::kImageText);
00290 
00291     for (uint i = 0; i < images.size(); i++) {
00292         images[i]->resetVisual();
00293     }
00294 }
00295 
00296 } // End of namespace Stark


Generated on Sat Dec 7 2019 05:00:40 for ResidualVM by doxygen 1.7.1
curved edge   curved edge