[Scummvm-cvs-logs] CVS: scummvm/gui walkthrough.cpp,NONE,1.1 walkthrough.h,NONE,1.1 module.mk,1.13,1.14
Pawel Kolodziejski
aquadran at users.sourceforge.net
Fri Nov 19 11:51:11 CET 2004
Update of /cvsroot/scummvm/scummvm/gui
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16339/gui
Modified Files:
module.mk
Added Files:
walkthrough.cpp walkthrough.h
Log Message:
added walkthrough
--- NEW FILE: walkthrough.cpp ---
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2004 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: /cvsroot/scummvm/scummvm/gui/walkthrough.cpp,v 1.1 2004/11/19 19:50:00 aquadran Exp $
*/
#include "stdafx.h"
#include "common/file.h"
#include "graphics/font.h"
#include "gui/walkthrough.h"
#include "gui/ScrollBarWidget.h"
namespace GUI {
extern const Graphics::NewFont g_consolefont;
#define kConsoleCharWidth (g_consolefont.getMaxCharWidth())
#define kConsoleLineHeight (g_consolefont.getFontHeight() + 2)
WalkthroughDialog::WalkthroughDialog(float widthPercent, float heightPercent)
: Dialog(0, 0, 1, 1),
_initialized(false), _widthPercent(widthPercent), _heightPercent(heightPercent) {
}
WalkthroughDialog::~WalkthroughDialog() {
destroy();
}
static int getNextWordLength(byte *src, int maxLength) {
int l;
for (l = 0; l < maxLength; l++) {
byte tmp = src[l];
if ((tmp >= 0) && (tmp <= ' '))
break;
}
return l;
}
bool WalkthroughDialog::loadWalkthroughText(const char *gameName) {
char filename[MAX_PATH];
File file;
sprintf(filename, "%s.wkt", gameName);
file.open(filename);
if (!file.isOpen())
return false;
int bufferSize = file.size();
byte *buffer = (byte *)malloc(bufferSize);
file.read(buffer, bufferSize);
file.close();
int currentLinePos = 0;
byte *currentBuffer = buffer;
byte *lineBuffer = (byte *)malloc(_lineWidth + 1);
lineBuffer[0] = 0;
for (;;) {
if ((currentBuffer - buffer) >= bufferSize)
break;
int wordLength = getNextWordLength(currentBuffer, _lineWidth);
if (((currentLinePos + wordLength) < _lineWidth) &&
((*currentBuffer != 0x0a) && (*currentBuffer != 0x0d))) {
if ((*currentBuffer >= 0) && (*currentBuffer <= ' ')) {
lineBuffer[currentLinePos++] = ' ';
currentBuffer++;
} else {
memcpy(lineBuffer + currentLinePos, currentBuffer, wordLength);
currentLinePos += wordLength;
currentBuffer += wordLength;
if ((currentLinePos + 1) < _lineWidth) {
lineBuffer[currentLinePos] = ' ';
currentLinePos++;
}
}
} else {
if (*currentBuffer == 0x0d) {
currentBuffer++;
if ((*currentBuffer == 0x0a) && ((currentBuffer - buffer) < bufferSize)) {
currentBuffer++;
}
} else if (*currentBuffer == 0x0a) {
currentBuffer++;
}
lineBuffer[currentLinePos] = 0;
Entry line;
line.text = String((char *)lineBuffer);
_linesArray.push_back(line);
lineBuffer[0] = 0;
currentLinePos = 0;
}
}
free(buffer);
free(lineBuffer);
return true;
}
void WalkthroughDialog::create(const char *gameName) {
// Setup basic layout/dialog size
reflowLayout();
// Add scrollbar
_scrollBar = new ScrollBarWidget(this, _w - kScrollBarWidth - 1, 0, kScrollBarWidth, _h);
_scrollBar->setTarget(this);
_currentPos = 0;
_scrollLine = _linesPerPage - 1;
loadWalkthroughText(gameName);
_initialized = true;
}
void WalkthroughDialog::destroy() {
if (!_initialized)
return;
_linesArray.clear();
_initialized = false;
}
void WalkthroughDialog::reflowLayout() {
// Calculate the real width/height (rounded to char/line multiples)
_w = (uint16)(_widthPercent * g_system->getOverlayWidth());
_h = (uint16)((_heightPercent * g_system->getOverlayHeight() - 2) / kConsoleLineHeight);
_h = _h * kConsoleLineHeight + 2;
// Calculate depending values
_lineWidth = (_w - kScrollBarWidth - 2) / kConsoleCharWidth;
_linesPerPage = (_h - 2) / kConsoleLineHeight;
}
void WalkthroughDialog::open() {
Dialog::open();
}
void WalkthroughDialog::drawDialog() {
// Blend over the background
g_gui.fillRect(_x, _y, _w, _h, g_gui._bgcolor);
// Draw a border
g_gui.hLine(_x, _y + _h - 1, _x + _w - 1, g_gui._color);
// Draw text
int y = _y + 2;
for (int line = 0; (line < _linesPerPage) && ((_currentPos + line) < (int)_linesArray.size()); line++) {
const char *text = _linesArray[line + _currentPos].text.c_str();
int textLen = strlen(text);
int x = _x + 1;
for (int column = 0; (column < _lineWidth) && (column < textLen); column++) {
byte c = text[column];
g_gui.drawChar(c, x, y, g_gui._textcolor, &g_consolefont);
x += kConsoleCharWidth;
}
y += kConsoleLineHeight;
}
// Draw the scrollbar
_scrollBar->_numEntries = _linesArray.size();
_scrollBar->_currentPos = _currentPos;
_scrollBar->_entriesPerPage = _linesPerPage;
_scrollBar->recalc();
_scrollBar->draw();
// Finally blit it all to the screen
g_gui.addDirtyRect(_x, _y, _w, _h);
}
void WalkthroughDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
switch (cmd) {
case kSetPositionCmd:
_currentPos = _scrollBar->_currentPos;
drawDialog();
break;
default:
return;
}
}
void WalkthroughDialog::handleMouseWheel(int x, int y, int direction) {
_scrollBar->handleMouseWheel(x, y, direction);
}
void WalkthroughDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
if ((modifiers == OSystem::KBD_CTRL) && (keycode == 'w')) {
close();
return;
}
switch (keycode) {
case 256 + 17: // up arrow
_currentPos--;
break;
case 256 + 18: // down arrow
_currentPos++;
break;
case 256 + 22: // home
_currentPos = 0;
break;
case 256 + 23: // end
_currentPos = _linesArray.size() - _linesPerPage;
break;
case 256 + 24: // page up
_currentPos -= _linesPerPage;
break;
case 256 + 25: // page down
_currentPos += _linesPerPage;
break;
default:
return;
}
if (_currentPos < 0) {
_currentPos = 0;
}
if ((_currentPos + _linesPerPage) >= (int)_linesArray.size()) {
_currentPos = _linesArray.size() - _linesPerPage;
}
drawDialog();
}
} // End of namespace GUI
--- NEW FILE: walkthrough.h ---
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2004 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: /cvsroot/scummvm/scummvm/gui/walkthrough.h,v 1.1 2004/11/19 19:50:00 aquadran Exp $
*/
#ifndef WALKTHROUGH_DIALOG_H
#define WALKTHROUGH_DIALOG_H
#include "common/array.h"
#include "common/str.h"
#include "gui/dialog.h"
#include "gui/newgui.h"
#include <stdarg.h>
namespace GUI {
class ScrollBarWidget;
class WalkthroughDialog : public Dialog {
private:
bool _initialized;
int _lineWidth;
int _linesPerPage;
int _currentPos;
int _scrollLine;
int _firstLineInBuffer;
typedef Common::String String;
struct Entry {
String text;
};
typedef Common::Array<Entry> EntryList;
EntryList _linesArray;
ScrollBarWidget *_scrollBar;
float _widthPercent, _heightPercent;
void reflowLayout();
bool loadWalkthroughText(const char *gameName);
public:
WalkthroughDialog(float widthPercent, float heightPercent);
~WalkthroughDialog();
void create(const char *gameName);
void destroy();
void open();
void drawDialog();
void handleMouseWheel(int x, int y, int direction);
void handleKeyDown(uint16 ascii, int keycode, int modifiers);
void handleCommand(CommandSender *sender, uint32 cmd, uint32 data);
};
} // End of namespace GUI
#endif
Index: module.mk
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/module.mk,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- module.mk 15 Aug 2004 14:39:35 -0000 1.13
+++ module.mk 19 Nov 2004 19:50:00 -0000 1.14
@@ -16,6 +16,7 @@
gui/PopUpWidget.o \
gui/ScrollBarWidget.o \
gui/TabWidget.o \
+ gui/walkthrough.o \
gui/widget.o
MODULE_DIRS += \
More information about the Scummvm-git-logs
mailing list