serenity/Widgets/Painter.cpp

181 lines
4.8 KiB
C++
Raw Normal View History

2018-10-10 16:49:36 +02:00
#include "Painter.h"
#include "FrameBufferSDL.h"
#include "Widget.h"
2018-10-11 23:45:57 +02:00
#include "Font.h"
#include "Window.h"
2018-10-10 16:49:36 +02:00
#include <AK/Assertions.h>
#include <SDL.h>
2018-10-10 16:49:36 +02:00
Painter::Painter(Widget& widget)
: m_widget(widget)
2018-10-11 23:45:57 +02:00
, m_font(Font::defaultFont())
2018-10-10 16:49:36 +02:00
{
if (auto* window = widget.window()) {
2018-10-12 10:06:50 +02:00
m_translation = window->position();
2018-10-12 14:15:14 +02:00
m_translation.moveBy(widget.relativePosition());
} else {
m_translation.setX(widget.x());
m_translation.setY(widget.y());
}
2018-10-10 16:49:36 +02:00
}
Painter::~Painter()
{
int rc = SDL_UpdateWindowSurface(FrameBufferSDL::the().window());
ASSERT(rc == 0);
}
static dword* scanline(int y)
2018-10-10 16:49:36 +02:00
{
auto& surface = *FrameBufferSDL::the().surface();
return (dword*)(((byte*)surface.pixels) + (y * surface.pitch));
}
2018-10-10 16:49:36 +02:00
void Painter::fillRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
2018-10-10 16:49:36 +02:00
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
for (int x = r.left(); x < r.right(); ++x) {
bits[x] = color.value();
}
}
2018-10-10 16:49:36 +02:00
}
2018-10-11 23:14:51 +02:00
void Painter::drawRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
2018-10-11 23:14:51 +02:00
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
if (y == r.top() || y == (r.bottom() - 1)) {
for (int x = r.left(); x < r.right(); ++x) {
bits[x] = color.value();
}
} else {
bits[r.left()] = color.value();
bits[r.right() - 1] = color.value();
}
}
}
void Painter::xorRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
if (y == r.top() || y == (r.bottom() - 1)) {
for (int x = r.left(); x < r.right(); ++x) {
bits[x] ^= color.value();
}
} else {
bits[r.left()] ^= color.value();
bits[r.right() - 1] ^= color.value();
}
}
}
2018-10-12 14:15:14 +02:00
void Painter::drawBitmap(const Point& p, const CBitmap& bitmap, Color color)
{
2018-10-12 14:15:14 +02:00
Point point = p;
point.moveBy(m_translation);
for (unsigned row = 0; row < bitmap.height(); ++row) {
int y = point.y() + row;
int x = point.x();
dword* bits = scanline(y);
for (unsigned j = 0; j < bitmap.width(); ++j) {
char fc = bitmap.bits()[row * bitmap.width() + j];
if (fc == '#')
bits[x + j] = color.value();
}
}
}
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, Color color)
{
2018-10-11 01:48:09 +02:00
Point point;
if (alignment == TextAlignment::TopLeft) {
point = rect.location();
} else if (alignment == TextAlignment::Center) {
2018-10-11 23:45:57 +02:00
int textWidth = text.length() * m_font.glyphWidth();
2018-10-11 01:48:09 +02:00
point = rect.center();
point.moveBy(-(textWidth / 2), -(m_font.glyphHeight() / 2));
2018-10-11 01:48:09 +02:00
} else {
ASSERT_NOT_REACHED();
}
for (unsigned i = 0; i < text.length(); ++i) {
byte ch = text[i];
if (ch == ' ')
continue;
auto* bitmap = m_font.glyphBitmap(ch);
if (!bitmap) {
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
ASSERT_NOT_REACHED();
}
int x = point.x() + i * m_font.glyphWidth();
int y = point.y();
drawBitmap({ x, y }, *bitmap, color);
}
}
void Painter::drawPixel(const Point& p, Color color)
{
auto point = p;
point.moveBy(m_translation);
scanline(point.y())[point.x()] = color.value();
}
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()) {
if (point1.y() > point2.y())
std::swap(point1, point2);
for (int y = point1.y(); y <= point2.y(); ++y)
scanline(y)[point1.x()] = color.value();
return;
}
if (point1.x() > point2.x())
std::swap(point1, point2);
// Special case: horizontal line.
if (point1.y() == point2.y()) {
if (point1.y() > point2.y())
std::swap(point1, point2);
auto* pixels = scanline(point1.y());
for (int x = point1.x(); x <= point2.x(); ++x)
pixels[x] = color.value();
return;
}
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) {
scanline(y)[x] = color.value();
error += deltaError;
if (error >= 0.5) {
y = (double)y + yStep;
error -= 1.0;
}
}
}