2018-10-10 14:49:36 +00:00
|
|
|
#include "Painter.h"
|
2019-01-10 04:21:19 +00:00
|
|
|
#include "FrameBuffer.h"
|
2018-10-10 14:49:36 +00:00
|
|
|
#include "Widget.h"
|
2018-10-11 21:45:57 +00:00
|
|
|
#include "Font.h"
|
2018-10-12 00:41:27 +00:00
|
|
|
#include "Window.h"
|
2018-10-10 14:49:36 +00:00
|
|
|
#include <AK/Assertions.h>
|
2018-12-21 01:18:16 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-11 12:44:27 +00:00
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
Painter::Painter(Widget& widget)
|
|
|
|
: m_widget(widget)
|
2018-10-14 11:06:05 +00:00
|
|
|
, m_font(&widget.font())
|
2018-10-10 14:49:36 +00:00
|
|
|
{
|
2019-01-09 01:06:04 +00:00
|
|
|
m_target = widget.backing();
|
|
|
|
ASSERT(m_target);
|
|
|
|
m_window = widget.window();
|
|
|
|
m_translation.moveBy(widget.relativePosition());
|
2018-10-12 20:50:28 +00:00
|
|
|
m_clipRect.setWidth(AbstractScreen::the().width());
|
|
|
|
m_clipRect.setHeight(AbstractScreen::the().height());
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Painter::~Painter()
|
|
|
|
{
|
2018-10-10 18:06:58 +00:00
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2018-10-10 18:06:58 +00:00
|
|
|
void Painter::fillRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
2018-10-12 00:41:27 +00:00
|
|
|
r.moveBy(m_translation);
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
|
2019-01-10 04:36:32 +00:00
|
|
|
auto* bits = m_target->scanline(y);
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
|
2018-10-10 18:06:58 +00:00
|
|
|
bits[x] = color.value();
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 14:49:36 +00:00
|
|
|
}
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2018-10-11 21:14:51 +00:00
|
|
|
void Painter::drawRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
2018-10-12 00:41:27 +00:00
|
|
|
r.moveBy(m_translation);
|
2018-10-11 21:14:51 +00:00
|
|
|
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
|
2019-01-10 04:36:32 +00:00
|
|
|
auto* bits = m_target->scanline(y);
|
2018-10-11 21:14:51 +00:00
|
|
|
if (y == r.top() || y == (r.bottom() - 1)) {
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
|
2018-10-11 21:14:51 +00:00
|
|
|
bits[x] = color.value();
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-12 20:50:28 +00:00
|
|
|
if (r.left() >= m_clipRect.left() && r.left() < m_clipRect.right())
|
|
|
|
bits[r.left()] = color.value();
|
|
|
|
if (r.right() >= m_clipRect.left() && r.right() < m_clipRect.right())
|
|
|
|
bits[r.right() - 1] = color.value();
|
2018-10-11 21:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 00:24:05 +00:00
|
|
|
void Painter::xorRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
2018-10-12 00:41:27 +00:00
|
|
|
r.moveBy(m_translation);
|
2018-10-12 00:24:05 +00:00
|
|
|
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
|
2019-01-10 04:36:32 +00:00
|
|
|
auto* bits = m_target->scanline(y);
|
2018-10-12 00:24:05 +00:00
|
|
|
if (y == r.top() || y == (r.bottom() - 1)) {
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
|
2018-10-12 00:24:05 +00:00
|
|
|
bits[x] ^= color.value();
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-12 20:50:28 +00:00
|
|
|
if (r.left() >= m_clipRect.left() && r.left() < m_clipRect.right())
|
|
|
|
bits[r.left()] ^= color.value();
|
|
|
|
if (r.right() >= m_clipRect.left() && r.right() < m_clipRect.right())
|
|
|
|
bits[r.right() - 1] ^= color.value();
|
2018-10-12 00:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 04:41:49 +00:00
|
|
|
void Painter::drawBitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
|
2018-10-12 10:29:58 +00:00
|
|
|
{
|
2018-10-12 12:15:14 +00:00
|
|
|
Point point = p;
|
|
|
|
point.moveBy(m_translation);
|
2018-10-12 10:49:45 +00:00
|
|
|
for (unsigned row = 0; row < bitmap.height(); ++row) {
|
2018-10-12 10:29:58 +00:00
|
|
|
int y = point.y() + row;
|
2018-10-12 20:50:28 +00:00
|
|
|
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
|
|
|
|
break;
|
2019-01-10 04:36:32 +00:00
|
|
|
auto* bits = m_target->scanline(y);
|
2018-10-12 10:49:45 +00:00
|
|
|
for (unsigned j = 0; j < bitmap.width(); ++j) {
|
2018-10-12 20:50:28 +00:00
|
|
|
int x = point.x() + j;
|
|
|
|
if (x < m_clipRect.left() || x >= m_clipRect.right())
|
|
|
|
break;
|
2018-10-12 10:49:45 +00:00
|
|
|
char fc = bitmap.bits()[row * bitmap.width() + j];
|
2018-10-12 10:29:58 +00:00
|
|
|
if (fc == '#')
|
2018-10-12 20:50:28 +00:00
|
|
|
bits[x] = color.value();
|
2018-10-12 10:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, Color color)
|
2018-10-10 18:06:58 +00:00
|
|
|
{
|
2018-10-10 23:48:09 +00:00
|
|
|
Point point;
|
|
|
|
|
|
|
|
if (alignment == TextAlignment::TopLeft) {
|
|
|
|
point = rect.location();
|
2019-01-10 04:11:07 +00:00
|
|
|
} else if (alignment == TextAlignment::CenterLeft) {
|
|
|
|
int textWidth = text.length() * font().glyphWidth();
|
|
|
|
point = { rect.x(), rect.center().y() - (font().glyphHeight() / 2) };
|
2018-10-10 23:48:09 +00:00
|
|
|
} else if (alignment == TextAlignment::Center) {
|
2018-10-14 11:06:05 +00:00
|
|
|
int textWidth = text.length() * font().glyphWidth();
|
2018-10-10 23:48:09 +00:00
|
|
|
point = rect.center();
|
2018-10-14 11:06:05 +00:00
|
|
|
point.moveBy(-(textWidth / 2), -(font().glyphHeight() / 2));
|
2018-10-10 23:48:09 +00:00
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-10 18:06:58 +00:00
|
|
|
|
2018-10-12 10:29:58 +00:00
|
|
|
for (unsigned i = 0; i < text.length(); ++i) {
|
|
|
|
byte ch = text[i];
|
|
|
|
if (ch == ' ')
|
|
|
|
continue;
|
2018-10-14 11:06:05 +00:00
|
|
|
auto* bitmap = font().glyphBitmap(ch);
|
2018-10-12 10:49:45 +00:00
|
|
|
if (!bitmap) {
|
2018-10-12 10:29:58 +00:00
|
|
|
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 18:06:58 +00:00
|
|
|
}
|
2018-10-14 11:06:05 +00:00
|
|
|
int x = point.x() + i * font().glyphWidth();
|
2018-10-12 10:29:58 +00:00
|
|
|
int y = point.y();
|
2018-10-12 10:49:45 +00:00
|
|
|
drawBitmap({ x, y }, *bitmap, color);
|
2018-10-10 18:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 12:58:16 +00:00
|
|
|
void Painter::drawPixel(const Point& p, Color color)
|
|
|
|
{
|
|
|
|
auto point = p;
|
|
|
|
point.moveBy(m_translation);
|
2018-10-12 20:50:28 +00:00
|
|
|
if (!m_clipRect.contains(point))
|
|
|
|
return;
|
2019-01-09 01:06:04 +00:00
|
|
|
m_target->scanline(point.y())[point.x()] = color.value();
|
2018-10-12 12:58:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 02:52:09 +00:00
|
|
|
void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
|
|
|
|
{
|
|
|
|
if (m_draw_op == DrawOp::Copy)
|
|
|
|
pixel = color.value();
|
|
|
|
else if (m_draw_op == DrawOp::Xor)
|
|
|
|
pixel ^= color.value();
|
|
|
|
}
|
|
|
|
|
2018-10-12 12:58:16 +00:00
|
|
|
void Painter::drawLine(const Point& p1, const Point& p2, Color color)
|
|
|
|
{
|
|
|
|
auto point1 = p1;
|
|
|
|
point1.moveBy(m_translation);
|
|
|
|
|
|
|
|
auto point2 = p2;
|
|
|
|
point2.moveBy(m_translation);
|
|
|
|
|
|
|
|
// Special case: vertical line.
|
|
|
|
if (point1.x() == point2.x()) {
|
2018-10-12 20:50:28 +00:00
|
|
|
const int x = point1.x();
|
|
|
|
if (x < m_clipRect.left() || x >= m_clipRect.right())
|
|
|
|
return;
|
2018-10-12 12:58:16 +00:00
|
|
|
if (point1.y() > point2.y())
|
2019-01-10 21:52:14 +00:00
|
|
|
swap(point1, point2);
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int y = max(point1.y(), m_clipRect.top()); y <= min(point2.y(), m_clipRect.bottom()); ++y)
|
2019-01-11 02:52:09 +00:00
|
|
|
set_pixel_with_draw_op(m_target->scanline(y)[x], color);
|
2018-10-12 12:58:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (point1.x() > point2.x())
|
2019-01-10 21:52:14 +00:00
|
|
|
swap(point1, point2);
|
2018-10-12 12:58:16 +00:00
|
|
|
|
|
|
|
// Special case: horizontal line.
|
|
|
|
if (point1.y() == point2.y()) {
|
2018-10-12 20:50:28 +00:00
|
|
|
const int y = point1.y();
|
|
|
|
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
|
|
|
|
return;
|
|
|
|
if (point1.x() > point2.x())
|
2019-01-10 21:52:14 +00:00
|
|
|
swap(point1, point2);
|
2019-01-09 01:06:04 +00:00
|
|
|
auto* pixels = m_target->scanline(point1.y());
|
2018-10-12 20:50:28 +00:00
|
|
|
for (int x = max(point1.x(), m_clipRect.left()); x <= min(point2.x(), m_clipRect.right()); ++x)
|
2019-01-11 02:52:09 +00:00
|
|
|
set_pixel_with_draw_op(pixels[x], color);
|
2018-10-12 12:58:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-12 20:50:28 +00:00
|
|
|
// FIXME: Implement clipping below.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
2019-01-10 21:52:14 +00:00
|
|
|
#if 0
|
2018-10-12 12:58:16 +00:00
|
|
|
const double dx = point2.x() - point1.x();
|
|
|
|
const double dy = point2.y() - point1.y();
|
|
|
|
const double deltaError = fabs(dy / dx);
|
|
|
|
double error = 0;
|
|
|
|
const double yStep = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
|
|
|
|
|
|
|
|
int y = point1.y();
|
|
|
|
for (int x = point1.x(); x <= point2.x(); ++x) {
|
2019-01-09 01:06:04 +00:00
|
|
|
m_target->scanline(y)[x] = color.value();
|
2018-10-12 12:58:16 +00:00
|
|
|
error += deltaError;
|
|
|
|
if (error >= 0.5) {
|
|
|
|
y = (double)y + yStep;
|
|
|
|
error -= 1.0;
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 21:52:14 +00:00
|
|
|
#endif
|
2018-10-12 12:58:16 +00:00
|
|
|
}
|
2018-10-13 18:59:25 +00:00
|
|
|
|
|
|
|
void Painter::drawFocusRect(const Rect& rect)
|
|
|
|
{
|
|
|
|
Rect focusRect = rect;
|
|
|
|
focusRect.moveBy(1, 1);
|
|
|
|
focusRect.setWidth(focusRect.width() - 2);
|
|
|
|
focusRect.setHeight(focusRect.height() - 2);
|
|
|
|
drawRect(focusRect, Color(96, 96, 192));
|
|
|
|
}
|