diff --git a/Applications/PaintBrush/Makefile b/Applications/PaintBrush/Makefile index 0a10066fb68..7321b2dea3c 100644 --- a/Applications/PaintBrush/Makefile +++ b/Applications/PaintBrush/Makefile @@ -2,6 +2,7 @@ include ../../Makefile.common OBJS = \ PaintableWidget.o \ + PaletteWidget.o \ main.o APP = PaintBrush diff --git a/Applications/PaintBrush/PaintableWidget.cpp b/Applications/PaintBrush/PaintableWidget.cpp index 767aac222d4..e33e2e72dc2 100644 --- a/Applications/PaintBrush/PaintableWidget.cpp +++ b/Applications/PaintBrush/PaintableWidget.cpp @@ -22,20 +22,29 @@ void PaintableWidget::paint_event(GPaintEvent& event) painter.blit({ 0, 0 }, *m_bitmap, m_bitmap->rect()); } +Color PaintableWidget::color_for(const GMouseEvent& event) +{ + if (event.buttons() & GMouseButton::Left) + return m_primary_color; + if (event.buttons() & GMouseButton::Right) + return m_secondary_color; + ASSERT_NOT_REACHED(); +} + void PaintableWidget::mousedown_event(GMouseEvent& event) { - if (event.button() != GMouseButton::Left) + if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right) return; GPainter painter(*m_bitmap); - painter.set_pixel(event.position(), Color::Black); + painter.set_pixel(event.position(), color_for(event)); update({ event.position(), { 1, 1 } }); m_last_drawing_event_position = event.position(); } void PaintableWidget::mouseup_event(GMouseEvent& event) { - if (event.button() == GMouseButton::Left) + if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right) m_last_drawing_event_position = { -1, -1 }; } @@ -44,14 +53,14 @@ void PaintableWidget::mousemove_event(GMouseEvent& event) if (!rect().contains(event.position())) return; - if (event.buttons() & GMouseButton::Left) { + if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) { GPainter painter(*m_bitmap); if (m_last_drawing_event_position != Point(-1, -1)) { - painter.draw_line(m_last_drawing_event_position, event.position(), Color::Black); + painter.draw_line(m_last_drawing_event_position, event.position(), color_for(event)); update(); } else { - painter.set_pixel(event.position(), Color::Black); + painter.set_pixel(event.position(), color_for(event)); update({ event.position(), { 1, 1 } }); } diff --git a/Applications/PaintBrush/PaintableWidget.h b/Applications/PaintBrush/PaintableWidget.h index 3714492c6d6..c572500f2b7 100644 --- a/Applications/PaintBrush/PaintableWidget.h +++ b/Applications/PaintBrush/PaintableWidget.h @@ -9,12 +9,23 @@ public: virtual const char* class_name() const override { return "PaintableWidget"; } + Color primary_color() const { return m_primary_color; } + Color secondary_color() const { return m_secondary_color; } + + void set_primary_color(Color color) { m_primary_color = color; } + void set_secondary_color(Color color) { m_secondary_color = color; } + private: virtual void paint_event(GPaintEvent&) override; virtual void mousedown_event(GMouseEvent&) override; virtual void mouseup_event(GMouseEvent&) override; virtual void mousemove_event(GMouseEvent&) override; + Color color_for(const GMouseEvent&); + Point m_last_drawing_event_position { -1, -1 }; RetainPtr m_bitmap; + + Color m_primary_color { Color::Black }; + Color m_secondary_color { Color::White }; }; diff --git a/Applications/PaintBrush/PaletteWidget.cpp b/Applications/PaintBrush/PaletteWidget.cpp new file mode 100644 index 00000000000..9c83a4cd3dd --- /dev/null +++ b/Applications/PaintBrush/PaletteWidget.cpp @@ -0,0 +1,31 @@ +#include "PaletteWidget.h" +#include "PaintableWidget.h" + +PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget, GWidget* parent) + : GFrame(parent) +{ + set_frame_shape(FrameShape::Panel); + set_frame_shadow(FrameShadow::Raised); + set_frame_thickness(1); + set_fill_with_background_color(true); + set_background_color(Color::LightGray); + + 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()); + + auto* 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()); +} + +PaletteWidget::~PaletteWidget() +{ +} diff --git a/Applications/PaintBrush/PaletteWidget.h b/Applications/PaintBrush/PaletteWidget.h new file mode 100644 index 00000000000..3a7d7758ff2 --- /dev/null +++ b/Applications/PaintBrush/PaletteWidget.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class PaintableWidget; + +class PaletteWidget final : public GFrame { +public: + explicit PaletteWidget(PaintableWidget&, GWidget* parent); + virtual ~PaletteWidget() override; + + virtual const char* class_name() const override { return "PaletteWidget"; } +}; diff --git a/Applications/PaintBrush/main.cpp b/Applications/PaintBrush/main.cpp index b2057f75fd9..8fc044ea827 100644 --- a/Applications/PaintBrush/main.cpp +++ b/Applications/PaintBrush/main.cpp @@ -1,5 +1,7 @@ #include "PaintableWidget.h" +#include "PaletteWidget.h" #include +#include #include int main(int argc, char** argv) @@ -8,10 +10,16 @@ int main(int argc, char** argv) auto* window = new GWindow; window->set_title("PaintBrush"); - window->set_rect(100, 100, 600, 400); + window->set_rect(100, 100, 600, 432); - auto* paintable_widget = new PaintableWidget(nullptr); - window->set_main_widget(paintable_widget); + auto* main_widget = new GWidget(nullptr); + window->set_main_widget(main_widget); + main_widget->set_layout(make(Orientation::Vertical)); + main_widget->layout()->set_spacing(0); + + + auto* paintable_widget = new PaintableWidget(main_widget); + auto* palette_widget = new PaletteWidget(*paintable_widget, main_widget); window->show(); return app.exec();