diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-28 17:32:38 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-28 17:32:38 +0100 |
commit | c7ab643883b5b2fbadd799e84ed9e79fdeafb9e6 (patch) | |
tree | eec552685be7e999ab9471d7d835a6d9c9101ea8 /SharedGraphics | |
parent | d12b6b867760fc9faa366d765aade072eaa4a8dc (diff) | |
download | serenity-c7ab643883b5b2fbadd799e84ed9e79fdeafb9e6.zip |
Move LibGUI/GStyle to SharedGraphics/StylePainter.
I want to paint some buttons in WindowServer where we don't have LibGUI.
Diffstat (limited to 'SharedGraphics')
-rw-r--r-- | SharedGraphics/StylePainter.cpp | 106 | ||||
-rw-r--r-- | SharedGraphics/StylePainter.h | 17 |
2 files changed, 123 insertions, 0 deletions
diff --git a/SharedGraphics/StylePainter.cpp b/SharedGraphics/StylePainter.cpp new file mode 100644 index 0000000000..9d37a9de75 --- /dev/null +++ b/SharedGraphics/StylePainter.cpp @@ -0,0 +1,106 @@ +#include <SharedGraphics/StylePainter.h> +#include <LibGUI/GPainter.h> + +static StylePainter* s_the; + +StylePainter& StylePainter::the() +{ + if (!s_the) + s_the = new StylePainter; + return *s_the; +} + +StylePainter::StylePainter() +{ +} + +static void paint_button_new(Painter& painter, const Rect& rect, bool pressed) +{ + Color button_color = Color::from_rgb(0xc0c0c0); + Color highlight_color1 = Color::from_rgb(0xffffff); + Color highlight_color2 = Color::from_rgb(0xdfdfdf); + Color shadow_color1 = Color::from_rgb(0x808080); + Color shadow_color2 = Color::from_rgb(0x404040); + + PainterStateSaver saver(painter); + painter.translate(rect.location()); + + if (pressed) { + // Base + painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color); + + painter.draw_rect(rect, shadow_color2); + + // Sunken shadow + painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color1); + painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color1); + } else { + // Base + painter.fill_rect({ 2, 2, rect.width() - 4, rect.height() - 4 }, button_color); + + // Outer highlight + painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color2); + painter.draw_line({ 0, 1 }, { 0, rect.height() - 2 }, highlight_color2); + + // Inner highlight + painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, highlight_color1); + painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, highlight_color1); + + // Outer shadow + painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2); + painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2); + + // Inner shadow + painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1); + painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1); + } +} + +void StylePainter::paint_button(Painter& painter, const Rect& rect, ButtonStyle button_style, bool pressed, bool hovered) +{ + if (button_style == ButtonStyle::Normal) + return paint_button_new(painter, rect, pressed); + + Color button_color = Color::LightGray; + Color highlight_color = Color::White; + Color shadow_color = Color(96, 96, 96); + + if (button_style == ButtonStyle::OldNormal) + painter.draw_rect(rect, Color::Black); + + PainterStateSaver saver(painter); + painter.translate(rect.location()); + + if (pressed) { + // Base + painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color); + + // Sunken shadow + painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color); + painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color); + + // Bottom highlight + painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color); + painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color); + } else if (button_style == ButtonStyle::OldNormal || (button_style == ButtonStyle::CoolBar && hovered)) { + // Base + painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color); + + // White highlight + painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color); + painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color); + + // Gray shadow + painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color); + painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color); + } +} + +void StylePainter::paint_surface(Painter& painter, const Rect& rect) +{ + painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, Color::LightGray); + painter.draw_line(rect.top_left(), rect.top_right(), Color::White); + painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::MidGray); + painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), Color::White); + painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), Color::MidGray); +} diff --git a/SharedGraphics/StylePainter.h b/SharedGraphics/StylePainter.h new file mode 100644 index 0000000000..7e6ad5f955 --- /dev/null +++ b/SharedGraphics/StylePainter.h @@ -0,0 +1,17 @@ +#pragma once + +class Painter; +class Rect; + +enum class ButtonStyle { Normal, CoolBar, OldNormal }; + +class StylePainter { +public: + static StylePainter& the(); + + void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false); + void paint_surface(Painter&, const Rect&); + +private: + StylePainter(); +}; |