2017-04-02 01:34:15 +03:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
#include "common/timer.h"
|
|
|
|
#include "common/system.h"
|
2017-04-02 01:59:32 +03:00
|
|
|
|
2017-04-02 01:34:15 +03:00
|
|
|
#include "graphics/macgui/macwindowmanager.h"
|
2017-04-02 01:59:32 +03:00
|
|
|
#include "graphics/macgui/macfontmanager.h"
|
2017-04-02 01:34:15 +03:00
|
|
|
#include "graphics/macgui/mactextwindow.h"
|
|
|
|
|
|
|
|
namespace Graphics {
|
|
|
|
|
2017-07-31 19:51:29 +02:00
|
|
|
enum {
|
|
|
|
kConWOverlap = 20,
|
|
|
|
kConHOverlap = 20,
|
|
|
|
kConWPadding = 3,
|
|
|
|
kConHPadding = 4,
|
2017-08-02 23:14:07 +02:00
|
|
|
kConOverscan = 3,
|
|
|
|
kConScrollStep = 12
|
2017-07-31 19:51:29 +02:00
|
|
|
};
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
static void cursorTimerHandler(void *refCon);
|
|
|
|
|
2017-07-30 08:56:38 +02:00
|
|
|
MacTextWindow::MacTextWindow(MacWindowManager *wm, const MacFont *font, int fgcolor, int bgcolor, int maxWidth, TextAlign textAlignment) :
|
2017-07-30 09:18:42 +02:00
|
|
|
MacWindow(wm->getLastId(), true, true, true, wm) {
|
2017-04-03 04:44:53 +03:00
|
|
|
|
|
|
|
_font = font;
|
|
|
|
_mactext = new MacText("", _wm, font, fgcolor, bgcolor, maxWidth, textAlignment);
|
2017-07-30 08:56:38 +02:00
|
|
|
|
|
|
|
_fontRef = wm->_fontMan->getFont(*font);
|
|
|
|
|
2017-08-02 17:27:47 +02:00
|
|
|
_inputTextHeight = 0;
|
2017-07-30 08:56:38 +02:00
|
|
|
_maxWidth = maxWidth;
|
2017-07-30 22:46:58 +02:00
|
|
|
|
2017-08-02 17:27:47 +02:00
|
|
|
_inputIsDirty = true;
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
_scrollPos = 0;
|
|
|
|
|
|
|
|
_cursorX = 0;
|
|
|
|
_cursorY = 0;
|
|
|
|
_cursorState = false;
|
|
|
|
_cursorOff = false;
|
|
|
|
|
2017-07-31 19:21:16 +02:00
|
|
|
_cursorDirty = true;
|
|
|
|
|
2017-07-31 09:20:31 +02:00
|
|
|
_cursorRect = new Common::Rect(0, 0, 1, kCursorHeight);
|
|
|
|
|
|
|
|
_cursorSurface = new ManagedSurface(1, kCursorHeight);
|
|
|
|
_cursorSurface->fillRect(*_cursorRect, kColorBlack);
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
g_system->getTimerManager()->installTimerProc(&cursorTimerHandler, 200000, this, "textWindowCursor");
|
2017-04-03 04:44:53 +03:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:08:46 +02:00
|
|
|
void MacTextWindow::resize(int w, int h) {
|
2017-08-02 21:19:37 +02:00
|
|
|
undrawInput();
|
|
|
|
|
2017-08-02 21:08:46 +02:00
|
|
|
MacWindow::resize(w, h);
|
2017-08-02 22:57:46 +02:00
|
|
|
|
|
|
|
_maxWidth = _innerDims.width();
|
|
|
|
_mactext->setMaxWidth(_maxWidth);
|
2017-08-02 21:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:46:25 +02:00
|
|
|
void MacTextWindow::appendText(Common::String str, const MacFont *macFont, bool skipAdd) {
|
|
|
|
_mactext->appendText(str, macFont->getId(), macFont->getSize(), macFont->getSlant(), skipAdd);
|
2017-07-31 19:21:16 +02:00
|
|
|
|
2017-08-02 10:17:28 +02:00
|
|
|
_contentIsDirty = true;
|
|
|
|
|
2017-08-02 22:58:28 +02:00
|
|
|
_scrollPos = MAX(0, _mactext->getTextHeight() - _innerDims.height());
|
|
|
|
|
2017-07-31 19:21:16 +02:00
|
|
|
updateCursorPos();
|
2017-04-09 00:05:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacTextWindow::clearText() {
|
|
|
|
_mactext->clearText();
|
2017-07-31 19:21:16 +02:00
|
|
|
|
2017-08-02 10:17:28 +02:00
|
|
|
_contentIsDirty = true;
|
|
|
|
|
2017-07-31 19:21:16 +02:00
|
|
|
updateCursorPos();
|
2017-04-09 00:05:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacTextWindow::setSelection(int selStartX, int selStartY, int selEndX, int selEndY) {
|
|
|
|
_selectedText.startX = selStartX;
|
|
|
|
_selectedText.startY = selStartY;
|
|
|
|
_selectedText.endX = selEndX;
|
|
|
|
_selectedText.endY = selEndY;
|
|
|
|
}
|
|
|
|
|
2017-04-02 01:34:15 +03:00
|
|
|
MacTextWindow::~MacTextWindow() {
|
2017-07-31 09:20:31 +02:00
|
|
|
delete _cursorSurface;
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
g_system->getTimerManager()->removeTimerProc(&cursorTimerHandler);
|
2017-04-02 01:34:15 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 09:57:11 +02:00
|
|
|
void MacTextWindow::setTextWindowFont(const MacFont *font) {
|
|
|
|
_font = font;
|
|
|
|
|
|
|
|
_fontRef = _wm->_fontMan->getFont(*font);
|
2017-08-02 10:15:47 +02:00
|
|
|
|
|
|
|
_mactext->setDefaultFormatting(font->getId(), font->getSlant(), font->getSize(), 0, 0, 0);
|
2017-07-30 09:57:11 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 00:05:56 +03:00
|
|
|
const MacFont *MacTextWindow::getTextWindowFont() {
|
2017-04-03 04:44:53 +03:00
|
|
|
return _font;
|
2017-04-02 01:59:32 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
bool MacTextWindow::draw(ManagedSurface *g, bool forceRedraw) {
|
2017-08-02 17:27:47 +02:00
|
|
|
if (!_borderIsDirty && !_contentIsDirty && !_cursorDirty && !_inputIsDirty && !forceRedraw)
|
2017-07-30 22:46:58 +02:00
|
|
|
return false;
|
|
|
|
|
2017-08-01 00:30:27 +02:00
|
|
|
if (_borderIsDirty || forceRedraw) {
|
2017-07-30 22:46:58 +02:00
|
|
|
drawBorder();
|
|
|
|
|
2017-08-01 00:30:27 +02:00
|
|
|
_composeSurface.clear(kColorWhite);
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:27:47 +02:00
|
|
|
if (_inputIsDirty || forceRedraw) {
|
|
|
|
drawInput();
|
|
|
|
_inputIsDirty = false;
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
_contentIsDirty = false;
|
|
|
|
|
|
|
|
// Compose
|
2017-08-02 22:58:28 +02:00
|
|
|
_mactext->draw(&_composeSurface, 0, _scrollPos, _surface.w - 2, _scrollPos + _surface.h - 2, kConWOverlap - 2, kConWOverlap - 2);
|
2017-07-31 09:20:31 +02:00
|
|
|
|
|
|
|
if (_cursorState)
|
2017-07-31 19:51:29 +02:00
|
|
|
_composeSurface.blitFrom(*_cursorSurface, *_cursorRect, Common::Point(_cursorX + kConWOverlap, _cursorY + kConHOverlap));
|
2017-07-31 09:20:31 +02:00
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
_composeSurface.transBlitFrom(_borderSurface, kColorGreen);
|
|
|
|
|
|
|
|
g->transBlitFrom(_composeSurface, _composeSurface.getBounds(), Common::Point(_dims.left - 2, _dims.top - 2), kColorGreen2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-30 08:56:38 +02:00
|
|
|
bool MacTextWindow::processEvent(Common::Event &event) {
|
2017-08-02 13:27:53 +01:00
|
|
|
/*WindowClick click =*/ isInBorder(event.mouse.x, event.mouse.y);
|
2017-07-30 08:56:38 +02:00
|
|
|
|
|
|
|
if (event.type == Common::EVENT_KEYDOWN) {
|
2017-07-31 20:41:24 +02:00
|
|
|
_wm->setActive(getId());
|
|
|
|
|
2017-07-30 08:56:38 +02:00
|
|
|
switch (event.kbd.keycode) {
|
|
|
|
case Common::KEYCODE_BACKSPACE:
|
|
|
|
if (!_inputText.empty()) {
|
|
|
|
_inputText.deleteLastChar();
|
2017-08-02 17:27:47 +02:00
|
|
|
_inputIsDirty = true;
|
2017-07-30 08:56:38 +02:00
|
|
|
}
|
2017-08-02 23:00:48 +02:00
|
|
|
return true;
|
2017-07-30 08:56:38 +02:00
|
|
|
|
|
|
|
case Common::KEYCODE_RETURN:
|
2017-08-02 22:38:55 +02:00
|
|
|
undrawInput();
|
2017-07-30 08:56:38 +02:00
|
|
|
return false; // Pass it to the higher level for processing
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (event.kbd.ascii == '~')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (event.kbd.ascii >= 0x20 && event.kbd.ascii <= 0x7f) {
|
|
|
|
_inputText += (char)event.kbd.ascii;
|
2017-08-02 17:27:47 +02:00
|
|
|
_inputIsDirty = true;
|
2017-07-31 20:15:04 +02:00
|
|
|
|
|
|
|
return true;
|
2017-07-30 08:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 23:14:07 +02:00
|
|
|
if (click == kBorderScrollUp || click == kBorderScrollDown) {
|
|
|
|
if (event.type == Common::EVENT_LBUTTONDOWN) {
|
|
|
|
int consoleHeight = _innerDims.height();
|
|
|
|
int textFullSize = _mactext->getTextHeight();
|
|
|
|
float scrollPos = (float)_scrollPos / textFullSize;
|
|
|
|
float scrollSize = (float)consoleHeight / textFullSize;
|
|
|
|
|
|
|
|
setScroll(scrollPos, scrollSize);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if (event.type == Common::EVENT_LBUTTONUP) {
|
|
|
|
int oldScrollPos = _scrollPos;
|
|
|
|
|
|
|
|
switch (click) {
|
|
|
|
case kBorderScrollUp:
|
|
|
|
_scrollPos = MAX<int>(0, _scrollPos - kConScrollStep);
|
|
|
|
undrawCursor();
|
|
|
|
_cursorY -= (_scrollPos - oldScrollPos);
|
|
|
|
_contentIsDirty = true;
|
|
|
|
_borderIsDirty = true;
|
|
|
|
break;
|
|
|
|
case kBorderScrollDown:
|
|
|
|
_scrollPos = MIN<int>(_mactext->getTextHeight() - kConScrollStep, _scrollPos + kConScrollStep);
|
|
|
|
undrawCursor();
|
|
|
|
_cursorY -= (_scrollPos - oldScrollPos);
|
|
|
|
_contentIsDirty = true;
|
|
|
|
_borderIsDirty = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:52:51 +02:00
|
|
|
return MacWindow::processEvent(event);
|
2017-07-30 08:56:38 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:19:37 +02:00
|
|
|
void MacTextWindow::undrawInput() {
|
|
|
|
for (uint i = 0; i < _inputTextHeight; i++)
|
2017-07-30 08:56:38 +02:00
|
|
|
_mactext->removeLastLine();
|
|
|
|
|
2017-08-02 21:46:25 +02:00
|
|
|
if (_inputTextHeight)
|
|
|
|
appendText("\n", _font, true);
|
|
|
|
|
2017-08-02 21:19:37 +02:00
|
|
|
_inputTextHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacTextWindow::drawInput() {
|
|
|
|
undrawInput();
|
|
|
|
|
2017-07-30 08:56:38 +02:00
|
|
|
Common::Array<Common::String> text;
|
|
|
|
|
|
|
|
// Now recalc new text height
|
|
|
|
_fontRef->wordWrapText(_inputText, _maxWidth, text);
|
2017-07-30 09:05:28 +02:00
|
|
|
_inputTextHeight = MAX(1u, text.size()); // We always have line to clean
|
2017-07-30 08:56:38 +02:00
|
|
|
|
|
|
|
// And add new input line to the text
|
2017-08-02 21:46:25 +02:00
|
|
|
appendText(_inputText, _font, true);
|
2017-07-30 22:46:58 +02:00
|
|
|
|
2017-07-31 20:08:11 +02:00
|
|
|
_cursorX = _inputText.empty() ? 0 : _fontRef->getStringWidth(text[_inputTextHeight - 1]);
|
2017-07-30 22:46:58 +02:00
|
|
|
|
2017-07-31 19:21:16 +02:00
|
|
|
updateCursorPos();
|
2017-07-31 20:50:36 +02:00
|
|
|
|
|
|
|
_contentIsDirty = true;
|
2017-07-30 08:56:38 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:01:39 +02:00
|
|
|
void MacTextWindow::clearInput() {
|
|
|
|
undrawCursor();
|
|
|
|
|
|
|
|
_cursorX = 0;
|
|
|
|
_inputText.clear();
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
//////////////////
|
|
|
|
// Cursor stuff
|
|
|
|
static void cursorTimerHandler(void *refCon) {
|
|
|
|
MacTextWindow *w = (MacTextWindow *)refCon;
|
|
|
|
|
|
|
|
if (!w->_cursorOff)
|
|
|
|
w->_cursorState = !w->_cursorState;
|
|
|
|
|
|
|
|
w->_cursorDirty = true;
|
|
|
|
}
|
|
|
|
|
2017-07-31 19:21:16 +02:00
|
|
|
void MacTextWindow::updateCursorPos() {
|
|
|
|
if (_scrollPos)
|
|
|
|
_cursorY = _mactext->getTextHeight() - kCursorHeight * 2;
|
|
|
|
else
|
2017-08-01 00:30:27 +02:00
|
|
|
_cursorY = _mactext->getTextHeight() - kCursorHeight - 2;
|
2017-07-31 20:50:36 +02:00
|
|
|
|
|
|
|
_cursorDirty = true;
|
2017-07-31 19:21:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 22:46:58 +02:00
|
|
|
void MacTextWindow::undrawCursor() {
|
|
|
|
_cursorState = false;
|
2017-07-31 09:20:31 +02:00
|
|
|
_cursorDirty = true;
|
2017-07-30 22:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-02 01:34:15 +03:00
|
|
|
} // End of namespace Graphics
|