1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

PixelPaint: Add selection functionality to the LayerListWidget

Now we can get rid of the old table view and with it the LayerModel.
This commit is contained in:
Andreas Kling 2020-05-26 09:51:28 +02:00
parent e4b11a23b7
commit 18d68c8c94
10 changed files with 77 additions and 185 deletions

View file

@ -26,8 +26,8 @@
#include "LayerListWidget.h"
#include "Image.h"
#include "ImageEditor.h"
#include "Layer.h"
#include <LibGUI/Model.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Palette.h>
@ -61,7 +61,7 @@ void LayerListWidget::rebuild_gadgets()
m_gadgets.clear();
if (m_image) {
for (size_t layer_index = 0; layer_index < m_image->layer_count(); ++layer_index) {
m_gadgets.append({ layer_index, {}, {}, false, {} });
m_gadgets.append({ layer_index, {}, {}, false, false, {} });
}
}
relayout_gadgets();
@ -92,7 +92,12 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event)
if (gadget.is_moving) {
adjusted_rect.move_by(0, gadget.movement_delta.y());
}
if (gadget.is_moving) {
painter.fill_rect(adjusted_rect, palette().threed_shadow1());
} else if (gadget.is_selected) {
painter.fill_rect(adjusted_rect, palette().selection());
}
painter.draw_rect(adjusted_rect, Color::Black);
@ -104,7 +109,7 @@ void LayerListWidget::paint_event(GUI::PaintEvent& event)
Gfx::Rect text_rect { thumbnail_rect.right() + 10, adjusted_rect.y(), adjusted_rect.width(), adjusted_rect.height() };
text_rect.intersect(adjusted_rect);
painter.draw_text(text_rect, layer.name(), Gfx::TextAlignment::CenterLeft);
painter.draw_text(text_rect, layer.name(), Gfx::TextAlignment::CenterLeft, gadget.is_selected ? palette().selection_text() : palette().button_text());
};
for (auto& gadget : m_gadgets) {
@ -132,11 +137,16 @@ void LayerListWidget::mousedown_event(GUI::MouseEvent& event)
if (event.button() != GUI::MouseButton::Left)
return;
auto gadget_index = gadget_at(event.position());
if (!gadget_index.has_value())
if (!gadget_index.has_value()) {
if (on_layer_select)
on_layer_select(nullptr);
return;
}
m_moving_gadget_index = gadget_index;
m_moving_event_origin = event.position();
auto& gadget = m_gadgets[m_moving_gadget_index.value()];
if (on_layer_select)
on_layer_select(&m_image->layer(gadget_index.value()));
gadget.is_moving = true;
gadget.movement_delta = {};
update();
@ -180,7 +190,7 @@ void LayerListWidget::image_did_add_layer(size_t layer_index)
m_gadgets[m_moving_gadget_index.value()].is_moving = false;
m_moving_gadget_index = {};
}
Gadget gadget { layer_index, {}, {}, false, {} };
Gadget gadget { layer_index, {}, {}, false, false, {} };
m_gadgets.insert(layer_index, move(gadget));
relayout_gadgets();
}
@ -217,6 +227,28 @@ size_t LayerListWidget::hole_index_during_move() const
return center_y_of_moving_gadget / vertical_step;
}
void LayerListWidget::select_bottom_layer()
{
if (!m_image || !m_image->layer_count())
return;
set_selected_layer(&m_image->layer(0));
}
void LayerListWidget::select_top_layer()
{
if (!m_image || !m_image->layer_count())
return;
set_selected_layer(&m_image->layer(m_image->layer_count() - 1));
}
void LayerListWidget::move_selection(int delta)
{
if (!m_image || !m_image->layer_count())
return;
int new_layer_index = min(max(0, (int)m_image->layer_count() + delta), (int)m_image->layer_count() - 1);
set_selected_layer(&m_image->layer(new_layer_index));
}
void LayerListWidget::relayout_gadgets()
{
int y = 0;
@ -239,4 +271,23 @@ void LayerListWidget::relayout_gadgets()
update();
}
void LayerListWidget::set_selected_layer(Layer* layer)
{
if (!m_image)
return;
if (!layer) {
for (auto& gadget : m_gadgets)
gadget.is_selected = false;
} else {
auto layer_index = m_image->index_of(*layer);
for (auto& gadget : m_gadgets) {
if (gadget.layer_index == layer_index)
gadget.is_selected = true;
else
gadget.is_selected = false;
}
}
update();
}
}