PaintBrush: Add a PaletteWidget to allow color selection.

Also use different colors for left/right mouse button. :^)
This commit is contained in:
Andreas Kling 2019-06-10 19:54:09 +02:00
parent 642c82fbff
commit f86b1bdca1
Notes: sideshowbarker 2024-07-19 13:39:36 +09:00
6 changed files with 82 additions and 9 deletions

View file

@ -2,6 +2,7 @@ include ../../Makefile.common
OBJS = \ OBJS = \
PaintableWidget.o \ PaintableWidget.o \
PaletteWidget.o \
main.o main.o
APP = PaintBrush APP = PaintBrush

View file

@ -22,20 +22,29 @@ void PaintableWidget::paint_event(GPaintEvent& event)
painter.blit({ 0, 0 }, *m_bitmap, m_bitmap->rect()); 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) void PaintableWidget::mousedown_event(GMouseEvent& event)
{ {
if (event.button() != GMouseButton::Left) if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
return; return;
GPainter painter(*m_bitmap); GPainter painter(*m_bitmap);
painter.set_pixel(event.position(), Color::Black); painter.set_pixel(event.position(), color_for(event));
update({ event.position(), { 1, 1 } }); update({ event.position(), { 1, 1 } });
m_last_drawing_event_position = event.position(); m_last_drawing_event_position = event.position();
} }
void PaintableWidget::mouseup_event(GMouseEvent& event) 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 }; m_last_drawing_event_position = { -1, -1 };
} }
@ -44,14 +53,14 @@ void PaintableWidget::mousemove_event(GMouseEvent& event)
if (!rect().contains(event.position())) if (!rect().contains(event.position()))
return; return;
if (event.buttons() & GMouseButton::Left) { if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
GPainter painter(*m_bitmap); GPainter painter(*m_bitmap);
if (m_last_drawing_event_position != Point(-1, -1)) { 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(); update();
} else { } else {
painter.set_pixel(event.position(), Color::Black); painter.set_pixel(event.position(), color_for(event));
update({ event.position(), { 1, 1 } }); update({ event.position(), { 1, 1 } });
} }

View file

@ -9,12 +9,23 @@ public:
virtual const char* class_name() const override { return "PaintableWidget"; } 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: private:
virtual void paint_event(GPaintEvent&) override; virtual void paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override; virtual void mousedown_event(GMouseEvent&) override;
virtual void mouseup_event(GMouseEvent&) override; virtual void mouseup_event(GMouseEvent&) override;
virtual void mousemove_event(GMouseEvent&) override; virtual void mousemove_event(GMouseEvent&) override;
Color color_for(const GMouseEvent&);
Point m_last_drawing_event_position { -1, -1 }; Point m_last_drawing_event_position { -1, -1 };
RetainPtr<GraphicsBitmap> m_bitmap; RetainPtr<GraphicsBitmap> m_bitmap;
Color m_primary_color { Color::Black };
Color m_secondary_color { Color::White };
}; };

View file

@ -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()
{
}

View file

@ -0,0 +1,13 @@
#pragma once
#include <LibGUI/GFrame.h>
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"; }
};

View file

@ -1,5 +1,7 @@
#include "PaintableWidget.h" #include "PaintableWidget.h"
#include "PaletteWidget.h"
#include <LibGUI/GApplication.h> #include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GWindow.h> #include <LibGUI/GWindow.h>
int main(int argc, char** argv) int main(int argc, char** argv)
@ -8,10 +10,16 @@ int main(int argc, char** argv)
auto* window = new GWindow; auto* window = new GWindow;
window->set_title("PaintBrush"); window->set_title("PaintBrush");
window->set_rect(100, 100, 600, 400); window->set_rect(100, 100, 600, 432);
auto* paintable_widget = new PaintableWidget(nullptr); auto* main_widget = new GWidget(nullptr);
window->set_main_widget(paintable_widget); window->set_main_widget(main_widget);
main_widget->set_layout(make<GBoxLayout>(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(); window->show();
return app.exec(); return app.exec();