diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 15:52:41 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-12 15:52:41 +0200 |
commit | 0dcdbe3e03b491ff2ed2dd7ec8f3193f047c608c (patch) | |
tree | 469d76f753cc537c52cde8692d9e2266ef880bb0 | |
parent | 5412dac05d9f75cc07ae8bd5f01462ed04626ece (diff) | |
download | serenity-0dcdbe3e03b491ff2ed2dd7ec8f3193f047c608c.zip |
Make buttons pressable.
-rw-r--r-- | Widgets/Button.cpp | 51 | ||||
-rw-r--r-- | Widgets/Button.h | 2 |
2 files changed, 40 insertions, 13 deletions
diff --git a/Widgets/Button.cpp b/Widgets/Button.cpp index c7ceaacf7d..6862c93fe4 100644 --- a/Widgets/Button.cpp +++ b/Widgets/Button.cpp @@ -22,6 +22,7 @@ void Button::setCaption(String&& caption) void Button::onPaint(PaintEvent&) { Color buttonColor(192, 192, 192); + Color highlightColor(255, 255, 255); Color shadowColor(96, 96, 96); Painter painter(*this); @@ -37,22 +38,35 @@ void Button::onPaint(PaintEvent&) 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)); + if (m_beingPressed) { + // Base + painter.fillRect({ 1, 1, width() - 2, height() - 2 }, buttonColor); - // 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); + // Sunken shadow + painter.drawLine({ 1, 1 }, { width() - 2, 1 }, shadowColor); + painter.drawLine({ 1, 2 }, {1, height() - 2 }, shadowColor); + } else { + // Base + painter.fillRect({ 3, 3, width() - 6, height() - 6 }, buttonColor); - painter.fillRect({ 3, 3, width() - 6, height() - 6 }, 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); + + // 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); + } if (!caption().isEmpty()) { - painter.drawText(rect(), caption(), Painter::TextAlignment::Center, Color(0, 0, 0)); + auto textRect = rect(); + if (m_beingPressed) + textRect.moveBy(1, 1); + painter.drawText(textRect, caption(), Painter::TextAlignment::Center, Color(0, 0, 0)); } } @@ -60,8 +74,19 @@ void Button::onMouseDown(MouseEvent& event) { printf("Button::onMouseDown: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); - setBackgroundColor(Color(0xff, 0xc0, 0xc0)); + m_beingPressed = true; + update(); Widget::onMouseDown(event); } +void Button::onMouseUp(MouseEvent& event) +{ + printf("Button::onMouseUp: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); + + m_beingPressed = false; + + update(); + Widget::onMouseUp(event); +} + diff --git a/Widgets/Button.h b/Widgets/Button.h index 9bd51334bf..040b363744 100644 --- a/Widgets/Button.h +++ b/Widgets/Button.h @@ -14,9 +14,11 @@ public: private: virtual void onPaint(PaintEvent&) override; virtual void onMouseDown(MouseEvent&) override; + virtual void onMouseUp(MouseEvent&) override; virtual const char* className() const override { return "Button"; } String m_caption; + bool m_beingPressed { false }; }; |