Update Painter class to the new coding style.

This commit is contained in:
Andreas Kling 2019-01-12 17:02:54 +01:00
parent 959ef2e681
commit bac9aa90dd
Notes: sideshowbarker 2024-07-19 16:04:01 +09:00
11 changed files with 96 additions and 97 deletions

View file

@ -26,45 +26,45 @@ void Button::paintEvent(PaintEvent&)
Painter painter(*this);
painter.drawPixel({ 0, 0 }, backgroundColor());
painter.drawPixel({ width() - 1, 0 }, backgroundColor());
painter.drawPixel({ 0, height() - 1 }, backgroundColor());
painter.drawPixel({ width() - 1, height() - 1 }, backgroundColor());
painter.set_pixel({ 0, 0 }, backgroundColor());
painter.set_pixel({ width() - 1, 0 }, backgroundColor());
painter.set_pixel({ 0, height() - 1 }, backgroundColor());
painter.set_pixel({ width() - 1, height() - 1 }, backgroundColor());
painter.drawLine({ 1, 0 }, { width() - 2, 0 }, Color::Black);
painter.drawLine({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
painter.drawLine({ 0, 1 }, { 0, height() - 2 }, Color::Black);
painter.drawLine({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
painter.draw_line({ 1, 0 }, { width() - 2, 0 }, Color::Black);
painter.draw_line({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
painter.draw_line({ 0, 1 }, { 0, height() - 2 }, Color::Black);
painter.draw_line({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
if (m_beingPressed) {
// Base
painter.fillRect({ 1, 1, width() - 1, height() - 1 }, buttonColor);
painter.fill_rect({ 1, 1, width() - 1, height() - 1 }, buttonColor);
// Sunken shadow
painter.drawLine({ 1, 1 }, { width() - 2, 1 }, shadowColor);
painter.drawLine({ 1, 2 }, {1, height() - 2 }, shadowColor);
painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadowColor);
painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadowColor);
} else {
// Base
painter.fillRect({ 3, 3, width() - 5, height() - 5 }, buttonColor);
painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, buttonColor);
// White highlight
painter.drawLine({ 1, 1 }, { width() - 2, 1 }, highlightColor);
painter.drawLine({ 1, 2 }, { width() - 3, 2 }, highlightColor);
painter.drawLine({ 1, 3 }, { 1, height() - 2 }, highlightColor);
painter.drawLine({ 2, 3 }, { 2, height() - 3 }, highlightColor);
painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlightColor);
painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlightColor);
painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlightColor);
painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlightColor);
// Gray shadow
painter.drawLine({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadowColor);
painter.drawLine({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadowColor);
painter.drawLine({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadowColor);
painter.drawLine({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadowColor);
painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadowColor);
painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadowColor);
painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadowColor);
painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadowColor);
}
if (!caption().is_empty()) {
auto textRect = rect();
if (m_beingPressed)
textRect.moveBy(1, 1);
painter.drawText(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
painter.draw_text(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
}
}

View file

@ -85,11 +85,11 @@ void CheckBox::paintEvent(PaintEvent&)
bitmapPosition.setX(2);
bitmapPosition.setY(height() / 2 - bitmap->height() / 2 - 1);
painter.fillRect(rect(), backgroundColor());
painter.drawBitmap(bitmapPosition, *bitmap, foregroundColor());
painter.fill_rect(rect(), backgroundColor());
painter.draw_bitmap(bitmapPosition, *bitmap, foregroundColor());
if (!caption().is_empty()) {
painter.drawText(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
painter.draw_text(textRect, caption(), Painter::TextAlignment::TopLeft, foregroundColor());
}
}

View file

@ -22,8 +22,8 @@ void ClockWidget::paintEvent(PaintEvent&)
sprintf(timeBuf, "%02u:%02u:%02u ", tm.tm_hour, tm.tm_min, tm.tm_sec);
Painter painter(*this);
painter.fillRect(rect(), Color::MidGray);
painter.drawText(rect(), timeBuf, Painter::TextAlignment::Center, Color::Black);
painter.fill_rect(rect(), Color::MidGray);
painter.draw_text(rect(), timeBuf, Painter::TextAlignment::Center, Color::Black);
}
void ClockWidget::timerEvent(TimerEvent&)

View file

@ -21,9 +21,9 @@ void Label::setText(String&& text)
void Label::paintEvent(PaintEvent&)
{
Painter painter(*this);
painter.fillRect({ 0, 0, width(), height() }, backgroundColor());
painter.fill_rect({ 0, 0, width(), height() }, backgroundColor());
if (!text().is_empty())
painter.drawText({ 4, 4, width(), height() }, text(), Painter::TextAlignment::TopLeft, foregroundColor());
painter.draw_text({ 4, 4, width(), height() }, text(), Painter::TextAlignment::TopLeft, foregroundColor());
}
void Label::mouseMoveEvent(MouseEvent& event)

View file

@ -22,11 +22,11 @@ void ListBox::paintEvent(PaintEvent&)
Painter painter(*this);
// FIXME: Reduce overdraw.
painter.fillRect(rect(), Color::White);
painter.drawRect(rect(), Color::Black);
painter.fill_rect(rect(), Color::White);
painter.draw_rect(rect(), Color::Black);
if (isFocused())
painter.drawFocusRect(rect());
painter.draw_focus_rect(rect());
for (unsigned i = m_scrollOffset; i < m_items.size(); ++i) {
Rect itemRect(2, 2 + (i * itemHeight()), width() - 4, itemHeight());
@ -35,12 +35,12 @@ void ListBox::paintEvent(PaintEvent&)
Color itemTextColor = foregroundColor();
if (m_selectedIndex == i) {
if (isFocused())
painter.fillRect(itemRect, Color(0, 32, 128));
painter.fill_rect(itemRect, Color(0, 32, 128));
else
painter.fillRect(itemRect, Color(96, 96, 96));
painter.fill_rect(itemRect, Color(96, 96, 96));
itemTextColor = Color::White;
}
painter.drawText(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor);
painter.draw_text(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor);
}
}

View file

@ -10,7 +10,7 @@ Painter::Painter(GraphicsBitmap& bitmap)
{
m_font = &Font::defaultFont();
m_target = &bitmap;
m_clipRect = { { 0, 0 }, bitmap.size() };
m_clip_rect = { { 0, 0 }, bitmap.size() };
}
Painter::Painter(Widget& widget)
@ -20,59 +20,59 @@ Painter::Painter(Widget& widget)
ASSERT(m_target);
m_window = widget.window();
m_translation.moveBy(widget.relativePosition());
m_clipRect.setWidth(AbstractScreen::the().width());
m_clipRect.setHeight(AbstractScreen::the().height());
m_clip_rect.setWidth(AbstractScreen::the().width());
m_clip_rect.setHeight(AbstractScreen::the().height());
}
Painter::~Painter()
{
}
void Painter::fillRect(const Rect& rect, Color color)
void Painter::fill_rect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = max(r.top(), m_clipRect.top()); y <= min(r.bottom(), m_clipRect.bottom()); ++y) {
for (int y = max(r.top(), m_clip_rect.top()); y <= min(r.bottom(), m_clip_rect.bottom()); ++y) {
auto* bits = m_target->scanline(y);
for (int x = max(r.left(), m_clipRect.left()); x <= min(r.right(), m_clipRect.right()); ++x) {
for (int x = max(r.left(), m_clip_rect.left()); x <= min(r.right(), m_clip_rect.right()); ++x) {
bits[x] = color.value();
}
}
}
void Painter::drawRect(const Rect& rect, Color color)
void Painter::draw_rect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = max(r.top(), m_clipRect.top()); y <= min(r.bottom(), m_clipRect.bottom()); ++y) {
for (int y = max(r.top(), m_clip_rect.top()); y <= min(r.bottom(), m_clip_rect.bottom()); ++y) {
auto* bits = m_target->scanline(y);
if (y == r.top() || y == r.bottom()) {
for (int x = max(r.left(), m_clipRect.left()); x <= min(r.right(), m_clipRect.right()); ++x) {
for (int x = max(r.left(), m_clip_rect.left()); x <= min(r.right(), m_clip_rect.right()); ++x) {
bits[x] = color.value();
}
} else {
if (r.left() >= m_clipRect.left() && r.left() < m_clipRect.right())
if (r.left() >= m_clip_rect.left() && r.left() < m_clip_rect.right())
bits[r.left()] = color.value();
if (r.right() >= m_clipRect.left() && r.right() < m_clipRect.right())
if (r.right() >= m_clip_rect.left() && r.right() < m_clip_rect.right())
bits[r.right()] = color.value();
}
}
}
void Painter::drawBitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
{
Point point = p;
point.moveBy(m_translation);
for (unsigned row = 0; row < bitmap.height(); ++row) {
int y = point.y() + row;
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
if (y < m_clip_rect.top() || y >= m_clip_rect.bottom())
break;
auto* bits = m_target->scanline(y);
for (unsigned j = 0; j < bitmap.width(); ++j) {
int x = point.x() + j;
if (x < m_clipRect.left() || x >= m_clipRect.right())
if (x < m_clip_rect.left() || x >= m_clip_rect.right())
break;
char fc = bitmap.bits()[row * bitmap.width() + j];
if (fc == '#')
@ -81,7 +81,7 @@ void Painter::drawBitmap(const Point& p, const CharacterBitmap& bitmap, Color co
}
}
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, Color color)
void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
{
Point point;
@ -108,20 +108,20 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
}
int x = point.x() + i * font().glyphWidth();
int y = point.y();
drawBitmap({ x, y }, *bitmap, color);
draw_bitmap({ x, y }, *bitmap, color);
}
}
void Painter::drawPixel(const Point& p, Color color)
void Painter::set_pixel(const Point& p, Color color)
{
auto point = p;
point.moveBy(m_translation);
if (!m_clipRect.contains(point))
if (!m_clip_rect.contains(point))
return;
m_target->scanline(point.y())[point.x()] = color.value();
}
void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
ALWAYS_INLINE void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
{
if (m_draw_op == DrawOp::Copy)
pixel = color.value();
@ -129,7 +129,7 @@ void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
pixel ^= color.value();
}
void Painter::drawLine(const Point& p1, const Point& p2, Color color)
void Painter::draw_line(const Point& p1, const Point& p2, Color color)
{
auto point1 = p1;
point1.moveBy(m_translation);
@ -140,12 +140,12 @@ void Painter::drawLine(const Point& p1, const Point& p2, Color color)
// Special case: vertical line.
if (point1.x() == point2.x()) {
const int x = point1.x();
if (x < m_clipRect.left() || x >= m_clipRect.right())
if (x < m_clip_rect.left() || x >= m_clip_rect.right())
return;
if (point1.y() > point2.y())
swap(point1, point2);
int min_y = max(point1.y(), m_clipRect.top());
int max_y = min(point2.y(), m_clipRect.bottom());
int min_y = max(point1.y(), m_clip_rect.top());
int max_y = min(point2.y(), m_clip_rect.bottom());
for (int y = min_y; y <= max_y; ++y)
set_pixel_with_draw_op(m_target->scanline(y)[x], color);
return;
@ -157,12 +157,12 @@ void Painter::drawLine(const Point& p1, const Point& p2, Color color)
// Special case: horizontal line.
if (point1.y() == point2.y()) {
const int y = point1.y();
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
if (y < m_clip_rect.top() || y >= m_clip_rect.bottom())
return;
if (point1.x() > point2.x())
swap(point1, point2);
int min_x = max(point1.x(), m_clipRect.left());
int max_x = min(point2.x(), m_clipRect.right());
int min_x = max(point1.x(), m_clip_rect.left());
int max_x = min(point2.x(), m_clip_rect.right());
auto* pixels = m_target->scanline(point1.y());
for (int x = min_x; x <= max_x; ++x)
set_pixel_with_draw_op(pixels[x], color);
@ -175,14 +175,14 @@ void Painter::drawLine(const Point& p1, const Point& p2, Color color)
#if 0
const double dx = point2.x() - point1.x();
const double dy = point2.y() - point1.y();
const double deltaError = fabs(dy / dx);
const double delta_error = 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) {
m_target->scanline(y)[x] = color.value();
error += deltaError;
error += delta_error;
if (error >= 0.5) {
y = (double)y + yStep;
error -= 1.0;
@ -191,19 +191,19 @@ void Painter::drawLine(const Point& p1, const Point& p2, Color color)
#endif
}
void Painter::drawFocusRect(const Rect& rect)
void Painter::draw_focus_rect(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));
Rect focus_rect = rect;
focus_rect.moveBy(1, 1);
focus_rect.setWidth(focus_rect.width() - 2);
focus_rect.setHeight(focus_rect.height() - 2);
draw_rect(focus_rect, Color(96, 96, 192));
}
void Painter::blit(const Point& position, const GraphicsBitmap& source)
{
Rect dst_rect(position, source.size());
dst_rect.intersect(m_clipRect);
dst_rect.intersect(m_clip_rect);
for (int y = 0; y < dst_rect.height(); ++y) {
auto* dst_scanline = m_target->scanline(position.y() + y);

View file

@ -14,21 +14,20 @@ class Window;
class Painter {
public:
enum class TextAlignment { TopLeft, CenterLeft, Center };
explicit Painter(Widget&);
explicit Painter(GraphicsBitmap&);
~Painter();
void fillRect(const Rect&, Color);
void drawRect(const Rect&, Color);
void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
void drawBitmap(const Point&, const CharacterBitmap&, Color = Color());
void drawPixel(const Point&, Color);
void drawLine(const Point& p1, const Point& p2, Color);
void drawFocusRect(const Rect&);
void fill_rect(const Rect&, Color);
void draw_rect(const Rect&, Color);
void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
void set_pixel(const Point&, Color);
void draw_line(const Point&, const Point&, Color);
void draw_focus_rect(const Rect&);
void blit(const Point&, const GraphicsBitmap&);
enum class TextAlignment { TopLeft, CenterLeft, Center };
void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
const Font& font() const { return *m_font; }
enum class DrawOp { Copy, Xor };
@ -40,7 +39,7 @@ private:
const Font* m_font;
Point m_translation;
Rect m_clipRect;
Rect m_clip_rect;
RetainPtr<GraphicsBitmap> m_target;
Window* m_window { nullptr };
DrawOp m_draw_op { DrawOp::Copy };

View file

@ -67,7 +67,7 @@ CharacterWithAttributes& TerminalWidget::at(unsigned row, unsigned column)
void TerminalWidget::paintEvent(PaintEvent&)
{
Painter painter(*this);
painter.fillRect(rect(), Color::Black);
painter.fill_rect(rect(), Color::Black);
char buf[2] = { 0, 0 };
for (unsigned row = 0; row < m_rows; ++row) {
@ -75,12 +75,12 @@ void TerminalWidget::paintEvent(PaintEvent&)
for (unsigned column = 0; column < m_columns; ++column) {
int x = column * font().glyphWidth();
buf[0] = at(row, column).character;
painter.drawText({ x + 2, y + 2, width(), font().glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0));
painter.draw_text({ x + 2, y + 2, width(), font().glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0));
}
}
if (m_belling)
painter.drawRect(rect(), Color::Red);
painter.draw_rect(rect(), Color::Red);
}
void TerminalWidget::onReceive(const ByteBuffer& buffer)

View file

@ -26,11 +26,11 @@ void TextBox::paintEvent(PaintEvent&)
Painter painter(*this);
// FIXME: Reduce overdraw.
painter.fillRect(rect(), backgroundColor());
painter.drawRect(rect(), foregroundColor());
painter.fill_rect(rect(), backgroundColor());
painter.draw_rect(rect(), foregroundColor());
if (isFocused())
painter.drawFocusRect(rect());
painter.draw_focus_rect(rect());
Rect innerRect = rect();
innerRect.shrink(6, 6);
@ -51,13 +51,13 @@ void TextBox::paintEvent(PaintEvent&)
printf("TextBox: glyph missing: %02x\n", ch);
ASSERT_NOT_REACHED();
}
painter.drawBitmap({x, y}, *bitmap, Color::Black);
painter.draw_bitmap({x, y}, *bitmap, Color::Black);
}
if (isFocused() && m_cursorBlinkState) {
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyphWidth(), innerRect.y(), 1, innerRect.height());
painter.fillRect(cursorRect, foregroundColor());
painter.fill_rect(cursorRect, foregroundColor());
}
}

View file

@ -73,7 +73,7 @@ void Widget::paintEvent(PaintEvent& event)
//printf("Widget::paintEvent :)\n");
if (fillWithBackgroundColor()) {
Painter painter(*this);
painter.fillRect(rect(), backgroundColor());
painter.fill_rect(rect(), backgroundColor());
}
for (auto* ch : children()) {
auto* child = (Widget*)ch;

View file

@ -106,14 +106,14 @@ void WindowManager::paintWindowFrame(Window& window)
auto titleColor = &window == activeWindow() ? m_activeWindowTitleColor : m_inactiveWindowTitleColor;
auto borderColor = &window == activeWindow() ? m_activeWindowBorderColor : m_inactiveWindowBorderColor;
p.drawRect(borderRect, Color::MidGray);
p.drawRect(outerRect, borderColor);
p.draw_rect(borderRect, Color::MidGray);
p.draw_rect(outerRect, borderColor);
p.fillRect(titleBarRect, borderColor);
p.fill_rect(titleBarRect, borderColor);
p.drawRect(inner_border_rect, borderColor);
p.draw_rect(inner_border_rect, borderColor);
p.drawText(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, titleColor);
p.draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, titleColor);
}
void WindowManager::addWindow(Window& window)
@ -241,7 +241,7 @@ void WindowManager::compose()
if (any_window_contains_rect(r))
continue;
//dbgprintf("Repaint root %d,%d %dx%d\n", r.x(), r.y(), r.width(), r.height());
painter.fillRect(r, Color(0, 72, 96));
painter.fill_rect(r, Color(0, 72, 96));
}
}
for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
@ -265,8 +265,8 @@ void WindowManager::redraw_cursor()
flush(m_last_cursor_rect);
flush(cursor_rect);
auto draw_cross = [&painter] (const Point& p) {
painter.drawLine({ p.x() - 10, p.y() }, { p.x() + 10, p.y() }, Color::Red);
painter.drawLine({ p.x(), p.y() - 10 }, { p.x(), p.y() + 10 }, Color::Red);
painter.draw_line({ p.x() - 10, p.y() }, { p.x() + 10, p.y() }, Color::Red);
painter.draw_line({ p.x(), p.y() - 10 }, { p.x(), p.y() + 10 }, Color::Red);
};
draw_cross(cursor_location);
m_last_cursor_rect = cursor_rect;