summaryrefslogtreecommitdiff
path: root/Applications/PaintBrush
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-10 20:07:26 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-10 20:07:26 +0200
commit1f756378e819a7f881cabfe699101d92328f090f (patch)
tree4a0616bb62f1d03add655aef628957b8422546fc /Applications/PaintBrush
parentf86b1bdca1b9b5e0ce0246016837c61ba116f6be (diff)
downloadserenity-1f756378e819a7f881cabfe699101d92328f090f.zip
PaintBrush: Make it possible to switch colors using the PaletteWidget.
Diffstat (limited to 'Applications/PaintBrush')
-rw-r--r--Applications/PaintBrush/PaletteWidget.cpp80
-rw-r--r--Applications/PaintBrush/PaletteWidget.h8
2 files changed, 79 insertions, 9 deletions
diff --git a/Applications/PaintBrush/PaletteWidget.cpp b/Applications/PaintBrush/PaletteWidget.cpp
index 9c83a4cd3d..171f3b34a1 100644
--- a/Applications/PaintBrush/PaletteWidget.cpp
+++ b/Applications/PaintBrush/PaletteWidget.cpp
@@ -1,8 +1,36 @@
#include "PaletteWidget.h"
#include "PaintableWidget.h"
+#include <LibGUI/GBoxLayout.h>
+
+class ColorWidget : public GWidget {
+public:
+ explicit ColorWidget(Color color, PaletteWidget& palette_widget, GWidget* parent)
+ : GWidget(parent)
+ , m_palette_widget(palette_widget)
+ , m_color(color)
+ {
+ }
+
+ virtual ~ColorWidget() override
+ {
+ }
+
+ virtual void mousedown_event(GMouseEvent& event) override
+ {
+ if (event.button() == GMouseButton::Left)
+ m_palette_widget.set_primary_color(m_color);
+ else if (event.button() == GMouseButton::Right)
+ m_palette_widget.set_secondary_color(m_color);
+ }
+
+private:
+ PaletteWidget& m_palette_widget;
+ Color m_color;
+};
PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget, GWidget* parent)
: GFrame(parent)
+ , m_paintable_widget(paintable_widget)
{
set_frame_shape(FrameShape::Panel);
set_frame_shadow(FrameShadow::Raised);
@@ -13,19 +41,53 @@ PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget, GWidget* parent)
set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
set_preferred_size({ 0, 32 });
- auto* secondary_color_widget = new GWidget(this);
- secondary_color_widget->set_relative_rect({ 2, 2, 60, 28 });
- secondary_color_widget->set_fill_with_background_color(true);
- secondary_color_widget->set_background_color(paintable_widget.secondary_color());
+ m_secondary_color_widget = new GWidget(this);
+ m_secondary_color_widget->set_relative_rect({ 2, 2, 60, 28 });
+ m_secondary_color_widget->set_fill_with_background_color(true);
+ set_secondary_color(paintable_widget.secondary_color());
- auto* primary_color_widget = new GWidget(this);
+ m_primary_color_widget = new GWidget(this);
Rect rect { 0, 0, 38, 14 };
- rect.center_within(secondary_color_widget->relative_rect());
- primary_color_widget->set_relative_rect(rect);
- primary_color_widget->set_fill_with_background_color(true);
- primary_color_widget->set_background_color(paintable_widget.primary_color());
+ rect.center_within(m_secondary_color_widget->relative_rect());
+ m_primary_color_widget->set_relative_rect(rect);
+ m_primary_color_widget->set_fill_with_background_color(true);
+ set_primary_color(paintable_widget.primary_color());
+
+ auto* color_container = new GWidget(this);
+ color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 28);
+ color_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
+ color_container->layout()->set_spacing(0);
+
+ auto add_color_widget = [&] (Color color) {
+ auto* color_widget = new ColorWidget(color, *this, color_container);
+ color_widget->set_fill_with_background_color(true);
+ color_widget->set_background_color(color);
+ };
+
+ add_color_widget(Color::Black);
+ add_color_widget(Color::White);
+ add_color_widget(Color::Red);
+ add_color_widget(Color::Green);
+ add_color_widget(Color::Blue);
+ add_color_widget(Color::Cyan);
+ add_color_widget(Color::Magenta);
+ add_color_widget(Color::Yellow);
}
PaletteWidget::~PaletteWidget()
{
}
+
+void PaletteWidget::set_primary_color(Color color)
+{
+ m_paintable_widget.set_primary_color(color);
+ m_primary_color_widget->set_background_color(color);
+ m_primary_color_widget->update();
+}
+
+void PaletteWidget::set_secondary_color(Color color)
+{
+ m_paintable_widget.set_secondary_color(color);
+ m_secondary_color_widget->set_background_color(color);
+ m_secondary_color_widget->update();
+}
diff --git a/Applications/PaintBrush/PaletteWidget.h b/Applications/PaintBrush/PaletteWidget.h
index 3a7d7758ff..e4e867a334 100644
--- a/Applications/PaintBrush/PaletteWidget.h
+++ b/Applications/PaintBrush/PaletteWidget.h
@@ -10,4 +10,12 @@ public:
virtual ~PaletteWidget() override;
virtual const char* class_name() const override { return "PaletteWidget"; }
+
+ void set_primary_color(Color);
+ void set_secondary_color(Color);
+
+private:
+ PaintableWidget& m_paintable_widget;
+ GWidget* m_primary_color_widget { nullptr };
+ GWidget* m_secondary_color_widget { nullptr };
};