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

dialogmenu.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/dialogmenu.h"
00024 
00025 #include "engines/stark/services/services.h"
00026 #include "engines/stark/services/userinterface.h"
00027 #include "engines/stark/services/diary.h"
00028 #include "engines/stark/services/gamechapter.h"
00029 #include "engines/stark/services/staticprovider.h"
00030 #include "engines/stark/services/global.h"
00031 
00032 #include "engines/stark/gfx/renderentry.h"
00033 
00034 #include "engines/stark/resources/location.h"
00035 #include "engines/stark/resources/item.h"
00036 
00037 namespace Stark {
00038 
00039 const Color ChapterTitleText::_color = Color(0x68, 0x05, 0x04);
00040 const Color DialogTitleWidget::_textColorHovered = Color(0x1E, 0x1E, 0x96);
00041 const Color DialogTitleWidget::_textColorDefault = Color(0x00, 0x00, 0x00);
00042 const Color DialogLineText::_textColorApril = Color(0x68, 0x05, 0x04);
00043 const Color DialogLineText::_textColorNormal = Color(0x1E, 0x1E, 0x96);
00044 
00045 DialogScreen::DialogScreen(Gfx::Driver *gfx, Cursor *cursor) :
00046         StaticLocationScreen(gfx, cursor, "DiaryLog", Screen::kScreenDialog),
00047         _startTitleIndex(0), _nextTitleIndex(0),
00048         _startLineIndex(0), _nextLineIndex(0),
00049         _curMaxChapter(0), _curLogIndex(0),
00050         _indexFrame(nullptr),
00051         _logFrame(nullptr),
00052         _chapterTitleTexts(),
00053         _prevTitleIndexStack(),
00054         _prevLineIndexStack() {
00055 }
00056 
00057 DialogScreen::~DialogScreen() {
00058     freeResources();
00059 }
00060 
00061 void DialogScreen::open() {
00062     StaticLocationScreen::open();
00063 
00064     _widgets.push_back(new StaticLocationWidget(
00065             "BGImage",
00066             nullptr,
00067             nullptr));
00068             
00069     _widgets.push_back(new StaticLocationWidget(
00070             "Return",
00071             CLICK_HANDLER(DialogScreen, backHandler),
00072             nullptr));
00073 
00074     _widgets.push_back(new StaticLocationWidget(
00075             "Back",
00076             CLICK_HANDLER(DialogScreen, backHandler),
00077             nullptr));
00078             
00079     _widgets.push_back(new StaticLocationWidget(
00080             "IndexBack",
00081             CLICK_HANDLER(DialogScreen, indexBackHandler),
00082             nullptr));
00083             
00084     _widgets.push_back(new StaticLocationWidget(
00085             "IndexNext",
00086             CLICK_HANDLER(DialogScreen, indexNextHandler),
00087             nullptr));
00088             
00089     _widgets.push_back(new StaticLocationWidget(
00090             "LogBack",
00091             CLICK_HANDLER(DialogScreen, logBackHandler),
00092             nullptr));
00093     _widgets.back()->setVisible(false);
00094             
00095     _widgets.push_back(new StaticLocationWidget(
00096             "Index",
00097             CLICK_HANDLER(DialogScreen, backIndexHandler),
00098             nullptr));
00099     _widgets.back()->setVisible(false);
00100             
00101     _widgets.push_back(new StaticLocationWidget(
00102             "LogNext",
00103             CLICK_HANDLER(DialogScreen, logNextHandler),
00104             nullptr));
00105     _widgets.back()->setVisible(false);
00106     
00107     for (uint i = 1; i < _widgets.size(); ++i) {
00108         _widgets[i]->setupSounds(0, 1);
00109     }
00110 
00111     Resources::Location *location = StarkStaticProvider->getLocation();
00112     _indexFrame = location->getRenderEntryByName("IndexFrame");
00113     _logFrame = location->getRenderEntryByName("LogFrame");
00114 
00115     _nextTitleIndex = 0;
00116     loadIndex();
00117 }
00118 
00119 void DialogScreen::close() {
00120     freeResources();
00121     StaticLocationScreen::close();
00122 }
00123 
00124 void DialogScreen::freeResources() {
00125     freeChapterTitleTexts();
00126     freeDialogLineTexts();
00127     _prevTitleIndexStack.clear();
00128     _prevLineIndexStack.clear();
00129 }
00130 
00131 void DialogScreen::onScreenChanged() {
00132     StaticLocationScreen::onScreenChanged();
00133     for (uint i = 0; i < _chapterTitleTexts.size(); ++i) {
00134         _chapterTitleTexts[i]->onScreenChanged();
00135     }
00136     for (uint i = 0; i < _dialogLineTexts.size(); ++i) {
00137         _dialogLineTexts[i]->onScreenChanged();
00138     }
00139 }
00140 
00141 void DialogScreen::onDialogClick(uint logIndex) {
00142     freeLogTitleWidgets();
00143     freeChapterTitleTexts();
00144 
00145     _widgets[kWidgetIndexBack]->setVisible(false);
00146     _widgets[kWidgetIndexNext]->setVisible(false);
00147     _widgets[kWidgetIndex]->setVisible(true);
00148 
00149     _nextLineIndex = 0;
00150     _curLogIndex = logIndex;
00151     loadDialog();
00152 }
00153 
00154 void DialogScreen::onRender() {
00155     StaticLocationScreen::onRender();
00156     for (uint i = 0; i < _chapterTitleTexts.size(); ++i) {
00157         _chapterTitleTexts[i]->render();
00158     }
00159     for (uint i = 0; i < _dialogLineTexts.size(); ++i) {
00160         _dialogLineTexts[i]->render();
00161     }
00162 }
00163 
00164 void DialogScreen::loadIndex() {
00165     static const int space = 4;
00166 
00167     freeLogTitleWidgets();
00168     freeChapterTitleTexts();
00169 
00170     _startTitleIndex = _nextTitleIndex;
00171 
00172     Common::Point pos = _indexFrame->getPosition();
00173     int bottom = _indexFrame->getText()->getTargetHeight() + pos.y;
00174     int height;
00175     ChapterTitleText *chapterTitleText;
00176     DialogTitleWidget *dialogTitleWidget;
00177 
00178     _curMaxChapter = 99;
00179     while (_nextTitleIndex < StarkDiary->countDialog()) {
00180         height = 0;
00181         chapterTitleText = nullptr;
00182 
00183         dialogTitleWidget = new DialogTitleWidget(this, _gfx, _nextTitleIndex);
00184         height += dialogTitleWidget->getHeight();
00185 
00186         if (dialogTitleWidget->getChapter() != _curMaxChapter) {
00187             _curMaxChapter = dialogTitleWidget->getChapter();
00188             chapterTitleText = new ChapterTitleText(_gfx, _curMaxChapter);
00189             height += chapterTitleText->getHeight() + 2 * space;
00190         }
00191 
00192         if (pos.y + height > bottom) {
00193             delete dialogTitleWidget;
00194             delete chapterTitleText;
00195             break;
00196         }
00197 
00198         if (chapterTitleText) {
00199             pos.y += space;
00200             chapterTitleText->setPosition(pos);
00201             pos.y += chapterTitleText->getHeight() + space;
00202         }
00203         dialogTitleWidget->setPosition(pos);
00204         pos.y += dialogTitleWidget->getHeight() + space;
00205 
00206         _widgets.push_back(dialogTitleWidget);
00207         if (chapterTitleText) {
00208             _chapterTitleTexts.push_back(chapterTitleText);
00209         }
00210 
00211         ++_nextTitleIndex;
00212 
00213         if (pos.y > bottom) {
00214             break;
00215         }
00216     }
00217 
00218     _widgets[kWidgetIndexBack]->setVisible(_startTitleIndex > 0);
00219     _widgets[kWidgetIndexNext]->setVisible(_nextTitleIndex < StarkDiary->countDialog());
00220 }
00221 
00222 void DialogScreen::loadDialog() {
00223     static const int space = 16;
00224 
00225     freeDialogLineTexts();
00226 
00227     _startLineIndex = _nextLineIndex;
00228 
00229     Common::Point pos = _logFrame->getPosition();
00230     int boxWidth = _logFrame->getText()->getTargetWidth();
00231     int bottom = _logFrame->getText()->getTargetHeight() + pos.y;
00232     int height;
00233     DialogLineText *dialogLineText;
00234 
00235     Diary::ConversationLog dialog = StarkDiary->getDialog(_curLogIndex);
00236 
00237     while (_nextLineIndex < dialog.lines.size()) {
00238         height = 0;
00239 
00240         dialogLineText = new DialogLineText(_gfx, _curLogIndex, _nextLineIndex, boxWidth);
00241         height = dialogLineText->getHeight();
00242 
00243         if (pos.y + height + space > bottom) {
00244             delete dialogLineText;
00245             break;
00246         }
00247 
00248         dialogLineText->setPosition(pos);
00249         _dialogLineTexts.push_back(dialogLineText);
00250 
00251         pos.y += height + space;
00252 
00253         ++_nextLineIndex;
00254     }
00255 
00256     _widgets[kWidgetLogBack]->setVisible(_startLineIndex > 0);
00257     _widgets[kWidgetLogNext]->setVisible(_nextLineIndex < dialog.lines.size());
00258 }
00259 
00260 void DialogScreen::backHandler() {
00261     StarkUserInterface->backPrevScreen();
00262 }
00263 
00264 void DialogScreen::indexBackHandler() {
00265     _nextTitleIndex = _prevTitleIndexStack.back();
00266     _prevTitleIndexStack.pop_back();
00267     loadIndex();
00268 }
00269 
00270 void DialogScreen::indexNextHandler() {
00271     _prevTitleIndexStack.push_back(_startTitleIndex);
00272     loadIndex();
00273 }
00274 
00275 void DialogScreen::logBackHandler() {
00276     _nextLineIndex = _prevLineIndexStack.back();
00277     _prevLineIndexStack.pop_back();
00278     loadDialog();
00279 }
00280 
00281 void DialogScreen::backIndexHandler() {
00282     freeDialogLineTexts();
00283     _prevLineIndexStack.clear();
00284 
00285     _widgets[kWidgetLogBack]->setVisible(false);
00286     _widgets[kWidgetLogNext]->setVisible(false);
00287     _widgets[kWidgetIndex]->setVisible(false);
00288 
00289     _nextTitleIndex = _startTitleIndex;
00290     loadIndex();
00291 }
00292 
00293 void DialogScreen::logNextHandler() {
00294     _prevLineIndexStack.push_back(_startLineIndex);
00295     loadDialog();
00296 }
00297 
00298 void DialogScreen::freeLogTitleWidgets() {
00299     uint size = _widgets.size();
00300     for (uint i = 0; i < size - _dialogTitleWidgetOffset; ++i) {
00301         delete _widgets.back();
00302         _widgets.pop_back();
00303     }
00304 }
00305 
00306 void DialogScreen::freeChapterTitleTexts() {
00307     for (uint i = 0; i < _chapterTitleTexts.size(); ++i) {
00308         delete _chapterTitleTexts[i];
00309     }
00310     _chapterTitleTexts.clear();
00311 }
00312 
00313 void DialogScreen::freeDialogLineTexts() {
00314     for (uint i = 0; i < _dialogLineTexts.size(); ++i) {
00315         delete _dialogLineTexts[i];
00316     }
00317     _dialogLineTexts.clear();
00318 }
00319 
00320 ChapterTitleText::ChapterTitleText(Gfx::Driver *gfx, uint chapter) :
00321         _pos(), _text(gfx) {
00322     Common::String text = Common::String::format(
00323             "%s: %s",
00324             StarkGameChapter->getChapterTitle(chapter).c_str(),
00325             StarkGameChapter->getChapterSubtitle(chapter).c_str());
00326     text.toUppercase();
00327 
00328     _text.setText(text);
00329     _text.setColor(_color);
00330     _text.setFont(FontProvider::kCustomFont, 5);
00331 }
00332 
00333 DialogLineText::DialogLineText(Gfx::Driver *gfx, uint logIndex, uint lineIndex, uint boxWidth) :
00334         _namePos(), _linePos(), _boxWidth(boxWidth),
00335         _nameText(gfx), _lineText(gfx) {
00336     Diary::ConversationLogLine logLine = StarkDiary->getDialog(logIndex).lines[lineIndex];
00337 
00338     Common::String name = StarkGlobal->getCharacterName(logLine.characterId);
00339     name.toUppercase();
00340 
00341     Color color = logLine.characterId == StarkGlobal->getApril()->getCharacterIndex() ? _textColorApril : _textColorNormal;
00342 
00343     _nameText.setText(name);
00344     _nameText.setColor(color);
00345     _nameText.setFont(FontProvider::kCustomFont, 5);
00346 
00347     _lineText.setTargetWidth(_boxWidth);
00348     _lineText.setText(logLine.line);
00349     _lineText.setColor(color);
00350     _lineText.setFont(FontProvider::kCustomFont, 3);
00351 
00352     Common::Rect rect = _nameText.getRect();
00353     _nameWidth = rect.right - rect.left;
00354     _nameHeight = rect.bottom - rect.top;
00355 
00356     rect = _lineText.getRect();
00357     _lineHeight = rect.bottom - rect.top;
00358 }
00359 
00360 void DialogLineText::setPosition(const Common::Point &pos) {
00361     static const uint space = 4;
00362 
00363     _namePos.x = pos.x + (_boxWidth - _nameWidth) / 2;
00364     _namePos.y = pos.y;
00365 
00366     _linePos.x = pos.x;
00367     _linePos.y = pos.y + _nameHeight + space;
00368 }
00369 
00370 DialogTitleWidget::DialogTitleWidget(DialogScreen *screen, Gfx::Driver *gfx, uint logIndex) :
00371         StaticLocationWidget(nullptr, nullptr, nullptr),
00372         _logIndex(logIndex), _pos(), _text(gfx),
00373         _screen(screen) {
00374     const Diary::ConversationLog &dialog = StarkDiary->getDialog(_logIndex);
00375 
00376     _chapter = dialog.chapter;
00377 
00378     _text.setText(dialog.title);
00379     _text.setColor(_textColorDefault);
00380     _text.setFont(FontProvider::kCustomFont, 3);
00381 
00382     Common::Rect rect = _text.getRect();
00383     _width = rect.right - rect.left;
00384     _height = rect.bottom - rect.top;
00385 }
00386 
00387 bool DialogTitleWidget::isMouseInside(const Common::Point &mousePos) const {
00388     return mousePos.x >= _pos.x && mousePos.x <= _pos.x + _width &&
00389            mousePos.y >= _pos.y && mousePos.y <= _pos.y + _height;
00390 }
00391 
00392 void DialogTitleWidget::onClick() {
00393     _screen->onDialogClick(_logIndex);
00394 }
00395 
00396 void DialogTitleWidget::onScreenChanged() {
00397     _text.resetTexture();
00398 }
00399 
00400 } // End of namespace Stark


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