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


Generated on Sat Mar 16 2019 05:01:33 for ResidualVM by doxygen 1.7.1
curved edge   curved edge