2020-10-14 13:16:30 +00: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-12-21 11:31:32 +00:00
|
|
|
#include "twine/menu/interface.h"
|
2020-11-16 17:01:16 +00:00
|
|
|
#include "graphics/managed_surface.h"
|
2021-06-03 16:43:07 +00:00
|
|
|
#include "graphics/primitives.h"
|
2020-10-14 13:16:30 +00:00
|
|
|
#include "twine/twine.h"
|
|
|
|
|
|
|
|
namespace TwinE {
|
|
|
|
|
|
|
|
Interface::Interface(TwinEEngine *engine) : _engine(engine) {}
|
|
|
|
|
2020-10-14 12:20:38 +00:00
|
|
|
const int32 INSIDE = 0; // 0000
|
|
|
|
const int32 LEFT = 1; // 0001
|
|
|
|
const int32 RIGHT = 2; // 0010
|
2020-10-14 13:16:30 +00:00
|
|
|
const int32 TOP = 4; // 0100
|
|
|
|
const int32 BOTTOM = 8; // 1000
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2020-12-29 11:00:49 +00:00
|
|
|
int32 Interface::checkClipping(int32 x, int32 y) const {
|
2020-10-14 12:20:38 +00:00
|
|
|
int32 code = INSIDE;
|
2020-11-25 17:20:38 +00:00
|
|
|
if (x < textWindow.left) {
|
2020-10-14 13:16:30 +00:00
|
|
|
code |= LEFT;
|
2020-11-25 17:20:38 +00:00
|
|
|
} else if (x > textWindow.right) {
|
2020-10-14 13:16:30 +00:00
|
|
|
code |= RIGHT;
|
2020-11-05 19:02:19 +00:00
|
|
|
}
|
2020-11-25 17:20:38 +00:00
|
|
|
if (y < textWindow.top) {
|
2020-10-14 13:16:30 +00:00
|
|
|
code |= TOP;
|
2020-11-25 17:20:38 +00:00
|
|
|
} else if (y > textWindow.bottom) {
|
2020-10-14 13:16:30 +00:00
|
|
|
code |= BOTTOM;
|
2020-11-05 19:02:19 +00:00
|
|
|
}
|
2020-10-14 12:20:38 +00:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:16:30 +00:00
|
|
|
// TODO: check if Graphics::drawLine() works here
|
2021-03-15 21:11:15 +00:00
|
|
|
bool Interface::drawLine(int32 startWidth, int32 startHeight, int32 endWidth, int32 endHeight, uint8 lineColor) {
|
2020-10-14 12:20:38 +00:00
|
|
|
// draw line from left to right
|
|
|
|
if (startWidth > endWidth) {
|
2020-12-23 09:50:50 +00:00
|
|
|
SWAP(endWidth, startWidth);
|
|
|
|
SWAP(endHeight, startHeight);
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 13:16:30 +00:00
|
|
|
// Perform proper clipping (CohenSutherland algorithm)
|
2020-10-22 14:32:52 +00:00
|
|
|
int32 outcode0 = checkClipping(startWidth, startHeight);
|
|
|
|
int32 outcode1 = checkClipping(endWidth, endHeight);
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2020-11-16 18:35:33 +00:00
|
|
|
while ((outcode0 | outcode1) != INSIDE) {
|
|
|
|
if ((outcode0 & outcode1) != INSIDE && outcode0 != INSIDE) {
|
2021-03-15 21:11:15 +00:00
|
|
|
return false; // Reject lines which are behind one clipping plane
|
2020-11-05 19:02:19 +00:00
|
|
|
}
|
2020-10-14 12:20:38 +00:00
|
|
|
|
|
|
|
// At least one endpoint is outside the clip rectangle; pick it.
|
2020-12-23 09:37:51 +00:00
|
|
|
const int32 outcodeOut = outcode0 ? outcode0 : outcode1;
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2020-10-22 14:32:52 +00:00
|
|
|
int32 x = 0;
|
|
|
|
int32 y = 0;
|
2020-10-14 13:16:30 +00:00
|
|
|
if (outcodeOut & TOP) { // point is above the clip rectangle
|
2020-11-25 17:20:38 +00:00
|
|
|
x = startWidth + (int)((endWidth - startWidth) * (float)(textWindow.top - startHeight) / (float)(endHeight - startHeight));
|
|
|
|
y = textWindow.top;
|
2020-10-14 12:20:38 +00:00
|
|
|
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
|
2020-11-25 17:20:38 +00:00
|
|
|
x = startWidth + (int)((endWidth - startWidth) * (float)(textWindow.bottom - startHeight) / (float)(endHeight - startHeight));
|
|
|
|
y = textWindow.bottom;
|
2020-10-14 13:16:30 +00:00
|
|
|
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
|
2020-11-25 17:20:38 +00:00
|
|
|
y = startHeight + (int)((endHeight - startHeight) * (float)(textWindow.right - startWidth) / (float)(endWidth - startWidth));
|
|
|
|
x = textWindow.right;
|
2020-10-14 13:16:30 +00:00
|
|
|
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
|
2020-11-25 17:20:38 +00:00
|
|
|
y = startHeight + (int)((endHeight - startHeight) * (float)(textWindow.left - startWidth) / (float)(endWidth - startWidth));
|
|
|
|
x = textWindow.left;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clip the point
|
|
|
|
if (outcodeOut == outcode0) {
|
|
|
|
startWidth = x;
|
|
|
|
startHeight = y;
|
|
|
|
outcode0 = checkClipping(startWidth, startHeight);
|
|
|
|
} else {
|
|
|
|
endWidth = x;
|
|
|
|
endHeight = y;
|
|
|
|
outcode1 = checkClipping(endWidth, endHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:17:53 +00:00
|
|
|
int32 pitch = _engine->width();
|
2020-10-14 12:20:38 +00:00
|
|
|
endWidth -= startWidth;
|
|
|
|
endHeight -= startHeight;
|
|
|
|
if (endHeight < 0) {
|
2020-12-23 09:55:13 +00:00
|
|
|
pitch = -pitch;
|
2020-10-14 12:20:38 +00:00
|
|
|
endHeight = -endHeight;
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:35:33 +00:00
|
|
|
uint8 *out = (uint8*)_engine->frontVideoBuffer.getBasePtr(startWidth, startHeight);
|
2021-06-03 16:43:07 +00:00
|
|
|
_engine->frontVideoBuffer.addDirtyRect(Common::Rect(startWidth, startHeight, startWidth + endWidth, startHeight + endHeight));
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2020-10-14 13:16:30 +00:00
|
|
|
if (endWidth < endHeight) { // significant slope
|
2020-12-23 09:50:50 +00:00
|
|
|
SWAP(endWidth, endHeight);
|
2020-12-23 09:37:51 +00:00
|
|
|
const int16 var2 = endWidth << 1;
|
2020-10-14 12:20:38 +00:00
|
|
|
startHeight = endWidth;
|
|
|
|
endHeight <<= 1;
|
|
|
|
endWidth++;
|
|
|
|
do {
|
2020-12-23 09:50:50 +00:00
|
|
|
*out = lineColor;
|
2020-10-14 12:20:38 +00:00
|
|
|
startHeight -= endHeight;
|
|
|
|
if (startHeight > 0) {
|
2020-12-23 09:55:13 +00:00
|
|
|
out += pitch;
|
2020-10-14 12:20:38 +00:00
|
|
|
} else {
|
|
|
|
startHeight += var2;
|
2020-12-23 09:55:13 +00:00
|
|
|
out += pitch + 1;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
} while (--endWidth);
|
2020-10-14 13:16:30 +00:00
|
|
|
} else { // reduced slope
|
2020-12-23 09:37:51 +00:00
|
|
|
const int16 var2 = endWidth << 1;
|
2020-10-14 12:20:38 +00:00
|
|
|
startHeight = endWidth;
|
|
|
|
endHeight <<= 1;
|
|
|
|
endWidth++;
|
|
|
|
do {
|
2020-12-23 09:50:50 +00:00
|
|
|
*out++ = lineColor;
|
2020-10-14 12:20:38 +00:00
|
|
|
startHeight -= endHeight;
|
|
|
|
if (startHeight < 0) {
|
|
|
|
startHeight += var2;
|
2020-12-23 09:55:13 +00:00
|
|
|
out += pitch;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
} while (--endWidth);
|
|
|
|
}
|
2021-03-15 21:11:15 +00:00
|
|
|
return true;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 20:36:18 +00:00
|
|
|
void Interface::blitBox(const Common::Rect &rect, const Graphics::ManagedSurface &source, Graphics::ManagedSurface &dest) {
|
2021-01-06 06:44:03 +00:00
|
|
|
Common::Rect r(rect);
|
|
|
|
r.right += 1;
|
|
|
|
r.bottom += 1;
|
|
|
|
dest.blitFrom(source, r, Common::Point(rect.left, rect.top));
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:59:54 +00:00
|
|
|
void Interface::drawTransparentBox(const Common::Rect &rect, int32 colorAdj) {
|
2021-01-07 21:00:23 +00:00
|
|
|
Common::Rect r = rect;
|
|
|
|
r.clip(_engine->rect());
|
|
|
|
if (r.isEmpty()) {
|
2020-10-14 12:20:38 +00:00
|
|
|
return;
|
2020-11-05 19:02:19 +00:00
|
|
|
}
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2021-02-25 04:07:47 +00:00
|
|
|
uint8 *pos = (uint8*)_engine->frontVideoBuffer.getBasePtr(0, r.top);
|
2020-10-14 12:20:38 +00:00
|
|
|
|
2021-02-25 04:11:37 +00:00
|
|
|
for (int32 y = r.top; y <= r.bottom; ++y) {
|
|
|
|
for (int32 x = r.left; x <= r.right; ++x) {
|
2020-12-23 10:16:32 +00:00
|
|
|
const int8 color = (pos[x] & 0x0F) - colorAdj;
|
2020-12-23 10:15:28 +00:00
|
|
|
const int8 color2 = pos[x] & 0xF0;
|
2020-10-22 14:32:52 +00:00
|
|
|
if (color < 0) {
|
2020-12-23 10:16:32 +00:00
|
|
|
pos[x] = color2;
|
2020-10-22 14:32:52 +00:00
|
|
|
} else {
|
2020-12-23 10:16:32 +00:00
|
|
|
pos[x] = color + color2;
|
2020-10-22 14:32:52 +00:00
|
|
|
}
|
2020-12-23 10:15:28 +00:00
|
|
|
}
|
2020-12-29 10:53:16 +00:00
|
|
|
pos += _engine->frontVideoBuffer.pitch;
|
2020-12-23 10:13:26 +00:00
|
|
|
}
|
2021-06-03 16:43:07 +00:00
|
|
|
_engine->frontVideoBuffer.addDirtyRect(r);
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 13:35:12 +00:00
|
|
|
void Interface::drawFilledRect(const Common::Rect &rect, uint8 colorIndex) {
|
2021-02-25 04:11:37 +00:00
|
|
|
_engine->frontVideoBuffer.fillRect(Common::Rect(rect.left, rect.top, rect.right + 1, rect.bottom + 1), colorIndex);
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:59:54 +00:00
|
|
|
void Interface::setClip(const Common::Rect &rect) {
|
2021-01-07 20:17:53 +00:00
|
|
|
textWindow.left = MAX((int32)0, (int32)rect.left);
|
|
|
|
textWindow.top = MAX((int32)0, (int32)rect.top);
|
|
|
|
textWindow.right = MIN((int32)(_engine->width() - 1), (int32)rect.right);
|
|
|
|
textWindow.bottom = MIN((int32)(_engine->height() - 1), (int32)rect.bottom);
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:37:22 +00:00
|
|
|
void Interface::saveClip() {
|
2020-12-29 10:44:32 +00:00
|
|
|
textWindowSave = textWindow;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:37:22 +00:00
|
|
|
void Interface::loadClip() {
|
2020-12-29 10:44:32 +00:00
|
|
|
textWindow = textWindowSave;
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 13:16:30 +00:00
|
|
|
void Interface::resetClip() {
|
2021-01-07 20:37:27 +00:00
|
|
|
textWindow = _engine->rect();
|
2020-10-14 12:20:38 +00:00
|
|
|
}
|
2020-10-14 13:16:30 +00:00
|
|
|
|
|
|
|
} // namespace TwinE
|