From d0d00f6beb9a2fab19455f653837af1b629d00da Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 1 Jan 2017 05:24:31 -0500 Subject: TITANIC: Rename CPetText to CTextControl The class is used in several areas outside of the PET, so it makes better sense to not have it in the pet_control/ folder and named so --- engines/titanic/gfx/text_control.cpp | 491 +++++++++++++++++++++++++++++++++++ engines/titanic/gfx/text_control.h | 285 ++++++++++++++++++++ 2 files changed, 776 insertions(+) create mode 100644 engines/titanic/gfx/text_control.cpp create mode 100644 engines/titanic/gfx/text_control.h (limited to 'engines/titanic/gfx') diff --git a/engines/titanic/gfx/text_control.cpp b/engines/titanic/gfx/text_control.cpp new file mode 100644 index 0000000000..f731dbb340 --- /dev/null +++ b/engines/titanic/gfx/text_control.cpp @@ -0,0 +1,491 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software(0), you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation(0), 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(0), 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(0), if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "titanic/gfx/text_control.h" +#include "titanic/titanic.h" + +namespace Titanic { + +CTextControl::CTextControl(uint count) : + _stringsMerged(false), _maxCharsPerLine(-1), _lineCount(0), + _displayEndCharIndex(-1), _unused1(0), _unused2(0), _unused3(0), + _backR(0xff), _backG(0xff), _backB(0xff), + _textR(0), _textG(0), _textB(200), + _fontNumber(0), _npcFlag(0), _npcId(0), _hasBorder(true), + _scrollTop(0), _textCursor(nullptr) { + setupArrays(count); +} + +void CTextControl::setupArrays(int count) { + freeArrays(); + if (count < 10 || count > 60) + count = 10; + _array.resize(count); +} + +void CTextControl::freeArrays() { + _array.clear(); +} + +void CTextControl::setup() { + for (int idx = 0; idx < (int)_array.size(); ++idx) { + _array[idx]._line.clear(); + setLineColor(idx, _textR, _textG, _textB); + _array[idx]._string3.clear(); + } + + _lineCount = 0; + _stringsMerged = false; +} + +void CTextControl::setLineColor(uint lineNum, uint col) { + setLineColor(lineNum, col & 0xff, (col >> 8) & 0xff, (col >> 16) & 0xff); +} + +void CTextControl::setLineColor(uint lineNum, byte r, byte g, byte b) { + _array[lineNum]._rgb = getColorText(r, g, b); + _stringsMerged = false; +} + +CString CTextControl::getColorText(byte r, byte g, byte b) { + char buffer[6]; + if (!r) + r = 1; + if (!g) + g = 1; + if (!b) + b = 1; + + buffer[0] = TEXTCMD_SET_COLOR; + buffer[1] = r; + buffer[2] = g; + buffer[3] = b; + buffer[4] = TEXTCMD_SET_COLOR; + buffer[5] = '\0'; + + return CString(buffer); +} + +void CTextControl::load(SimpleFile *file, int param) { + if (!param) { + uint numLines = file->readNumber(); + int charsPerLine = file->readNumber(); + uint count = file->readNumber(); + _bounds = file->readRect(); + _unused1 = file->readNumber(); + _unused2 = file->readNumber(); + _unused3 = file->readNumber(); + _backR = file->readNumber(); + _backG = file->readNumber(); + _backB = file->readNumber(); + _textR = file->readNumber(); + _textG = file->readNumber(); + _textB = file->readNumber(); + _hasBorder = file->readNumber() != 0; + _scrollTop = file->readNumber(); + + setMaxCharsPerLine(charsPerLine); + resize(numLines); + _lineCount = (count == 0) ? 0 : count - 1; + + assert(_array.size() >= count); + for (uint idx = 0; idx < count; ++idx) { + _array[idx]._line = file->readString(); + _array[idx]._rgb = file->readString(); + _array[idx]._string3 = file->readString(); + } + } +} + +void CTextControl::save(SimpleFile *file, int indent) { + int numLines = _lineCount + 1; + + file->writeNumberLine(_array.size(), indent); + file->writeNumberLine(_maxCharsPerLine, indent); + file->writeNumberLine(numLines, indent); + + file->writeRect(_bounds, indent); + file->writeNumberLine(_unused1, indent); + file->writeNumberLine(_unused2, indent); + file->writeNumberLine(_unused3, indent); + file->writeNumberLine(_backR, indent); + file->writeNumberLine(_backG, indent); + file->writeNumberLine(_backB, indent); + file->writeNumberLine(_textR, indent); + file->writeNumberLine(_textG, indent); + file->writeNumberLine(_textB, indent); + file->writeNumberLine(_hasBorder, indent); + file->writeNumberLine(_scrollTop, indent); + + for (int idx = 0; idx < numLines; ++idx) { + file->writeQuotedLine(_array[idx]._line, indent); + file->writeQuotedLine(_array[idx]._rgb, indent); + file->writeQuotedLine(_array[idx]._string3, indent); + } +} + +void CTextControl::draw(CScreenManager *screenManager) { + Rect tempRect = _bounds; + + if (_hasBorder) { + // Create border effect + // Top edge + tempRect.bottom = tempRect.top + 1; + screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB); + + // Bottom edge + tempRect.top = _bounds.bottom - 1; + tempRect.bottom = _bounds.bottom; + screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB); + + // Left edge + tempRect = _bounds; + tempRect.right = tempRect.left + 1; + screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB); + + // Right edge + tempRect = _bounds; + tempRect.left = tempRect.right - 1; + screenManager->fillRect(SURFACE_BACKBUFFER, &tempRect, _backR, _backG, _backB); + } + + getTextHeight(screenManager); + + tempRect = _bounds; + tempRect.grow(-2); + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + + _displayEndCharIndex = screenManager->writeString(SURFACE_BACKBUFFER, tempRect, _scrollTop, _lines, _textCursor); + + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::mergeStrings() { + if (!_stringsMerged) { + _lines.clear(); + + for (int idx = 0; idx <= _lineCount; ++idx) { + CString line = _array[idx]._rgb + _array[idx]._string3 + + _array[idx]._line + "\n"; + _lines += line; + } + + _stringsMerged = true; + } +} + +void CTextControl::resize(uint count) { + if (!count || _array.size() == count) + return; + _array.clear(); + _array.resize(count); +} + +CString CTextControl::getText() const { + CString result = ""; + for (int idx = 0; idx <= _lineCount; ++idx) + result += _array[idx]._line; + + return result; +} + +void CTextControl::setText(const CString &str) { + setup(); + appendText(str); +} + +void CTextControl::setText(StringId stringId) { + setText(g_vm->_strings[stringId]); +} + +void CTextControl::appendText(const CString &str) { + int lineSize = _array[_lineCount]._line.size(); + int strSize = str.size(); + + if (_maxCharsPerLine == -1) { + // No limit on horizontal characters, so append string to current line + _array[_lineCount]._line += str; + } else if ((lineSize + strSize) <= _maxCharsPerLine) { + // New string fits into line, so add it on + _array[_lineCount]._line += str; + } else { + // Only add part of the str up to the maximum allowed limit for line + _array[_lineCount]._line += str.left(_maxCharsPerLine - lineSize); + } + + updateStr3(_lineCount); + _stringsMerged = false; +} + +void CTextControl::setColor(uint col) { + _textR = col & 0xff; + _textG = (col >> 8) & 0xff; + _textB = (col >> 16) & 0xff; +} + +void CTextControl::setColor(byte r, byte g, byte b) { + _textR = r; + _textG = g; + _textB = b; +} + +void CTextControl::remapColors(uint count, uint *srcColors, uint *destColors) { + for (int lineNum = 0; lineNum <= _lineCount; ++lineNum) { + if (_array[lineNum]._rgb.empty()) + continue; + + // Get the rgb values + uint r = _array[lineNum]._rgb[1]; + uint g = _array[lineNum]._rgb[2]; + uint b = _array[lineNum]._rgb[3]; + uint color = r | (g << 8) | (b << 16); + + for (uint index = 0; index < count; ++index) { + if (color == srcColors[index]) { + // Found a match, so replace the color + setLineColor(lineNum, destColors[lineNum]); + break; + } + } + } + + _stringsMerged = false; +} + +void CTextControl::setMaxCharsPerLine(int maxChars) { + if (maxChars >= -1 && maxChars < 257) + _maxCharsPerLine = maxChars; +} + +void CTextControl::updateStr3(int lineNum) { + if (_npcFlag > 0 && _npcId > 0) { + char line[5]; + line[0] = line[3] = TEXTCMD_NPC; + line[1] = _npcFlag; + line[2] = _npcId; + line[4] = '\0'; + _array[lineNum]._string3 = CString(line); + + _stringsMerged = false; + _npcFlag = _npcId = 0; + } +} + +int CTextControl::getTextWidth(CScreenManager *screenManager) { + mergeStrings(); + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + int textWidth = screenManager->stringWidth(_lines); + screenManager->setFontNumber(oldFontNumber); + + return textWidth; +} + +int CTextControl::getTextHeight(CScreenManager *screenManager) { + mergeStrings(); + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + int textHeight = screenManager->getTextBounds(_lines, _bounds.width() - 4); + screenManager->setFontNumber(oldFontNumber); + + return textHeight; +} + +void CTextControl::deleteLastChar() { + if (!_array[_lineCount]._line.empty()) { + _array[_lineCount]._line.deleteLastChar(); + _stringsMerged = false; + } +} + +void CTextControl::setNPC(int npcFlag, int npcId) { + _npcFlag = npcFlag; + _npcId = npcId; +} + +void CTextControl::scrollUp(CScreenManager *screenManager) { + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + _scrollTop -= screenManager->getFontHeight(); + constrainScrollUp(screenManager); + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::scrollDown(CScreenManager *screenManager) { + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + _scrollTop += screenManager->getFontHeight(); + constrainScrollDown(screenManager); + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::scrollUpPage(CScreenManager *screenManager) { + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + _scrollTop -= getPageHeight(screenManager); + constrainScrollUp(screenManager); + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::scrollDownPage(CScreenManager *screenManager) { + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + _scrollTop += getPageHeight(screenManager); + constrainScrollDown(screenManager); + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::scrollToTop(CScreenManager *screenManager) { + _scrollTop = 0; +} + +void CTextControl::scrollToBottom(CScreenManager *screenManager) { + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + _scrollTop = getTextHeight(screenManager); + constrainScrollDown(screenManager); + screenManager->setFontNumber(oldFontNumber); +} + +void CTextControl::constrainScrollUp(CScreenManager *screenManager) { + if (_scrollTop < 0) + _scrollTop = 0; +} + +void CTextControl::constrainScrollDown(CScreenManager *screenManager) { + // Figure out the maximum scroll amount allowed + int maxScroll = getTextHeight(screenManager) - _bounds.height() - 4; + if (maxScroll < 0) + maxScroll = 0; + + if (_scrollTop > maxScroll) + _scrollTop = maxScroll; +} + +int CTextControl::getPageHeight(CScreenManager *screenManager) { + int textHeight = _bounds.height(); + int oldFontNumber = screenManager->setFontNumber(_fontNumber); + int fontHeight = screenManager->getFontHeight(); + screenManager->setFontNumber(oldFontNumber); + + if (fontHeight) { + int lines = textHeight / fontHeight; + if (lines > 1) + --lines; + return lines * fontHeight; + } else { + return 0; + } +} + +void CTextControl::addLine(const CString &str) { + addLine(str, _textR, _textG, _textB); +} + +void CTextControl::addLine(const CString &str, uint color) { + addLine(str, color & 0xff, (color >> 8) & 0xff, + (color >> 16) & 0xff); +} + +void CTextControl::addLine(const CString &str, byte r, byte g, byte b) { + if (_lineCount == ((int)_array.size() - 1)) { + // Lines array is full + if (_array.size() > 1) { + // Delete the oldest line, and add a new entry at the end + _array.remove_at(0); + _array.resize(_array.size() + 1); + } + + --_lineCount; + } + + setLineColor(_lineCount, r, g, b); + appendText(str); + ++_lineCount; +} + +bool CTextControl::handleKey(char c) { + switch (c) { + case (char)Common::KEYCODE_BACKSPACE: + deleteLastChar(); + break; + + case (char)Common::KEYCODE_RETURN: + return true; + + default: + if ((byte)c >= 32 && (byte)c <= 127) + appendText(CString(c, 1)); + break; + } + + return false; +} + +void CTextControl::showCursor(int mode) { + CScreenManager *screenManager = CScreenManager::setCurrent(); + _textCursor = screenManager->_textCursor; + if (_textCursor) { + _textCursor->setPos(Point(0, 0)); + _textCursor->setSize(Point(2, 10)); + _textCursor->setColor(0, 0, 0); + _textCursor->setBlinkRate(300); + _textCursor->setMode(mode); + _textCursor->setBounds(_bounds); + _textCursor->show(); + } +} + +void CTextControl::hideCursor() { + if (_textCursor) { + _textCursor->setMode(-1); + _textCursor->hide(); + _textCursor = nullptr; + } +} + +int CTextControl::getNPCNum(uint ident, uint startIndex) { + if (!_stringsMerged) { + mergeStrings(); + if (!_stringsMerged) + return -1; + } + + uint size = _lines.size(); + if (startIndex < 5 || startIndex >= size) + return -1; + + // Loop backwards from the starting index to find an NPC ident sequence + for (const char *strP = _lines.c_str() + startIndex; + strP >= (_lines.c_str() + 5); --strP) { + if (*strP == 26) { + byte id = *(strP - 2); + if (id == ident) + return *(strP - 1); + strP -= 3; + } else if (*strP == 27) { + strP -= 4; + } + } + + return -1; +} + +void CTextControl::setFontNumber(int fontNumber) { + if (fontNumber >= 0 && fontNumber <= 2) + _fontNumber = fontNumber; +} + +} // End of namespace Titanic diff --git a/engines/titanic/gfx/text_control.h b/engines/titanic/gfx/text_control.h new file mode 100644 index 0000000000..d4ef19a7cf --- /dev/null +++ b/engines/titanic/gfx/text_control.h @@ -0,0 +1,285 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef TITANIC_TEXT_CONTROL_H +#define TITANIC_TEXT_CONTROL_H + +#include "common/keyboard.h" +#include "titanic/support/simple_file.h" +#include "titanic/support/screen_manager.h" +#include "titanic/support/text_cursor.h" + +namespace Titanic { + +class CTextControl { + struct ArrayEntry { + CString _line; + CString _rgb; + CString _string3; + }; +private: + Common::Array _array; + CString _lines; + bool _stringsMerged; + Rect _bounds; + int _maxCharsPerLine; + int _lineCount; + int _displayEndCharIndex; + int _unused1; + int _unused2; + int _unused3; + int _backR; + int _backG; + int _backB; + int _textR; + int _textG; + int _textB; + int _fontNumber; + int _npcFlag; + int _npcId; + bool _hasBorder; + int _scrollTop; + CTextCursor *_textCursor; +private: + void setupArrays(int count); + + void freeArrays(); + + /** + * Merges the strings in the strings array + */ + void mergeStrings(); + + /** + * Append text to the current text line + */ + void appendText(const CString &str); + + void updateStr3(int lineNum); + + /** + * Ensures the Y scrolling for the text is in the valid range + */ + void constrainScrollUp(CScreenManager *screenManager); + + /** + * Ensures the Y scrolling for the text is in the valid range + */ + void constrainScrollDown(CScreenManager *screenManager); + + /** + * Get the page height for paging up and down + */ + int getPageHeight(CScreenManager *screenManager); +public: + CTextControl(uint count = 10); + + /** + * Set up the control + */ + void setup(); + + /** + * Load the data for the control + */ + void load(SimpleFile *file, int param); + + /** + * Save the data for the control + */ + void save(SimpleFile *file, int indent); + + /** + * Set the bounds for the control + */ + void setBounds(const Rect &bounds) { _bounds = bounds; } + + /** + * Sets the flag for whether to draw a frame border around the control + */ + void setHasBorder(bool val) { _hasBorder = val; } + + /** + * Draw the control + */ + void draw(CScreenManager *screenManager); + + void resize(uint count); + + /** + * Returns the text from all the lines as a single string + */ + CString getText() const; + + /** + * Set the text + */ + void setText(const CString &str); + + /** + * Set the text + */ + void setText(StringId stringId); + + /** + * Set text color + */ + void setColor(uint col); + + /** + * Set text color + */ + void setColor(byte r, byte g, byte b); + + /** + * Set the color for a line + */ + void setLineColor(uint lineNum, byte r, byte g, byte b); + + /** + * Gets the text string representing a color encoding + */ + static CString getColorText(byte r, byte g, byte b); + + /** + * Set the color for a line + */ + void setLineColor(uint lineNum, uint col); + + /** + * Sets the maximum number of characters per line + */ + void setMaxCharsPerLine(int maxChars); + + /** + * Delete the last character from the last line + */ + void deleteLastChar(); + + /** + * Sets the current NPC text is being added for + */ + void setNPC(int npcFlag, int npcId); + + /** + * Returns the character index into _lines of the last + * character to be displayed on-screen + */ + int displayEndIndex() const { return _displayEndCharIndex; } + + /** + * Scroll the text up + */ + void scrollUp(CScreenManager *screenManager); + + /** + * Scroll the text down + */ + void scrollDown(CScreenManager *screenManager); + + /** + * Scroll the text up one page + */ + void scrollUpPage(CScreenManager *screenManager); + + /** + * Scroll the text down one page + */ + void scrollDownPage(CScreenManager *screenManager); + + /** + * Scroll to the top of the text + */ + void scrollToTop(CScreenManager *screenManager); + + /** + * Scroll to the bottom of the text + */ + void scrollToBottom(CScreenManager *screenManager); + + /** + * Add a line to the text + */ + void addLine(const CString &str); + + /** + * Add a line to the text + */ + void addLine(const CString &str, uint color); + + /** + * Add a line to the text + */ + void addLine(const CString &str, byte r, byte g, byte b); + + /** + * Handles character processing to add or remove characters to + * the current text line + * @returns True if the Enter key was pressed + */ + bool handleKey(char c); + + /** + * Attaches the current system cursor to the text control, + * and give it suitable defaults + */ + void showCursor(int mode); + + /** + * Removes the cursor attached to the text + */ + void hideCursor(); + + /** + * Get an NPC Number embedded within on-screen text. + * Used by the PET log to encode which NPC spoke + * @param ident Npc Type. Always passed as 1 + * @param startIndex Starting index to scan backwards + * through the log text to find an NPC ident sequence + */ + int getNPCNum(uint ident, uint startIndex); + + /** + * Replaces any occurances of line colors that appear in the + * first list with the entry at the same index in the dest list + */ + void remapColors(uint count, uint *srcColors, uint *destColors); + + /** + * Set the font number to use + */ + void setFontNumber(int fontNumber); + + /** + * Get the width of the text + */ + int getTextWidth(CScreenManager *screenManager); + + /** + * Get the required height to draw the text + */ + int getTextHeight(CScreenManager *screenManager); +}; + +} // End of namespace Titanic + +#endif /* TITANIC_TEXT_CONTROL_H */ -- cgit v1.2.3