diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 14:58:16 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 14:58:16 +0200 |
commit | 5412dac05d9f75cc07ae8bd5f01462ed04626ece (patch) | |
tree | f588668687c9fe2ecf3e71899fb74f6a4606cf0e /Widgets | |
parent | 16576112b0004a912f73f216488ebfda50ed1efe (diff) | |
download | serenity-5412dac05d9f75cc07ae8bd5f01462ed04626ece.zip |
Paint Button in a very Windows 3-ish style.
Diffstat (limited to 'Widgets')
-rw-r--r-- | Widgets/Button.cpp | 30 | ||||
-rw-r--r-- | Widgets/Painter.cpp | 53 | ||||
-rw-r--r-- | Widgets/Painter.h | 2 |
3 files changed, 84 insertions, 1 deletions
diff --git a/Widgets/Button.cpp b/Widgets/Button.cpp index b5648252f0..c7ceaacf7d 100644 --- a/Widgets/Button.cpp +++ b/Widgets/Button.cpp @@ -21,8 +21,36 @@ void Button::setCaption(String&& caption) void Button::onPaint(PaintEvent&) { + Color buttonColor(192, 192, 192); + Color shadowColor(96, 96, 96); + Painter painter(*this); - painter.fillRect(rect(), backgroundColor()); + painter.fillRect(rect(), Color(255, 0, 255)); + + 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.drawLine({ 1, 0 }, { width() - 2, 0 }, Color(0, 0, 0)); + painter.drawLine({ 1, height() - 1 }, { width() - 2, height() - 1}, Color(0, 0, 0)); + painter.drawLine({ 0, 1 }, { 0, height() - 2 }, Color(0, 0, 0)); + painter.drawLine({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color(0, 0, 0)); + + // White highlight + painter.drawLine({ 1, 1 }, { width() - 2, 1 }, Color(255, 255, 255)); + painter.drawLine({ 1, 2 }, { width() - 3, 2 }, Color(255, 255, 255)); + painter.drawLine({ 1, 3 }, { 1, height() - 2 }, Color(255, 255, 255)); + painter.drawLine({ 2, 3 }, { 2, height() - 3 }, Color(255, 255, 255)); + + // 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.fillRect({ 3, 3, width() - 6, height() - 6 }, buttonColor); + if (!caption().isEmpty()) { painter.drawText(rect(), caption(), Painter::TextAlignment::Center, Color(0, 0, 0)); } diff --git a/Widgets/Painter.cpp b/Widgets/Painter.cpp index c95bb5cdaa..63b81bdf25 100644 --- a/Widgets/Painter.cpp +++ b/Widgets/Painter.cpp @@ -125,3 +125,56 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align } } +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; + } + } +} diff --git a/Widgets/Painter.h b/Widgets/Painter.h index 057f78c5a1..bc53725a7b 100644 --- a/Widgets/Painter.h +++ b/Widgets/Painter.h @@ -19,6 +19,8 @@ public: void drawRect(const Rect&, Color); void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color()); void drawBitmap(const Point&, const CBitmap&, Color = Color()); + void drawPixel(const Point&, Color); + void drawLine(const Point& p1, const Point& p2, Color); void xorRect(const Rect&, Color); |