Skip to content

Commit

Permalink
BrushEditor: size preview based on max dimension to avoid jumping
Browse files Browse the repository at this point in the history
- Removed red line
- Added size number to preview when it's larger than the preview bitmap
  • Loading branch information
dsizzle committed Nov 8, 2023
1 parent 1297272 commit abd98a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
9 changes: 2 additions & 7 deletions artpaint/paintwindow/StatusView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ CurrentBrushView::CurrentBrushView(BRect frame)

SetToolTip(B_TRANSLATE("Brush"));

frame.InsetBy(1.0, 1.0);
fBrushPreview = new BBitmap(BRect(0.0, 0.0, frame.Width(), frame.Height()), B_RGBA32);
fBrushPreview = new BBitmap(BRect(0.0, 0.0, frame.Width(), frame.Height()), B_RGBA32, true);
BitmapUtilities::ClearBitmap(fBrushPreview, 0xFFFFFFFF);

list_of_views.AddItem(this);
Expand All @@ -412,11 +411,7 @@ CurrentBrushView::~CurrentBrushView()

void CurrentBrushView::Draw(BRect)
{
DrawBitmap(fBrushPreview, BPoint(1.0, 1.0));

SetPenSize(1);
SetHighColor(0, 0, 0, 255);
StrokeRect(Bounds());
DrawBitmap(fBrushPreview, BPoint(0.0, 0.0));
}


Expand Down
77 changes: 49 additions & 28 deletions artpaint/tools/Brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,15 @@ Brush::delete_all_data()
float
Brush::PreviewBrush(BBitmap* preview_bitmap)
{
float preview_width = width_;
float preview_height = height_;
float bmap_width = (preview_bitmap->Bounds().Width() + 1);
float bmap_height = (preview_bitmap->Bounds().Height() + 1);

float bmap_width = preview_bitmap->Bounds().Width() + 1;
float bmap_height = preview_bitmap->Bounds().Height() + 1;
float preview_width = 0.9 * bmap_width;
float preview_height = 0.9 * bmap_height;

while ((preview_width > bmap_width) || (preview_height > bmap_height)) {
preview_width /= 2.0;
preview_height /= 2.0;
}
int32 scale = (int32)(width_ / preview_width);
float max_dim = max_c(width_, height_);

int32 top = (int32)((bmap_height - preview_height) / 2.0);
int32 left = (int32)((bmap_width - preview_width) / 2.0);
float scale = max_c(1.0, max_dim / preview_width);

uint32* bits = (uint32*)preview_bitmap->Bits();
int32 bpr = preview_bitmap->BytesPerRow() / 4;
Expand All @@ -413,11 +408,22 @@ Brush::PreviewBrush(BBitmap* preview_bitmap)
uint32 brush_bpr = brush_bmap->BytesPerRow() / 4;
brush_bmap->Unlock();

int32 draw_width = brush_bpr / scale;
int32 draw_height = brush_bmap->Bounds().Height() / scale;

int32 top = (int32)((bmap_height - draw_height) / 2.0);
int32 left = (int32)((bmap_width - draw_width) / 2.0);

// Here we draw the brush to the bitmap.
for (int32 y = 0; y < preview_height; ++y) {
if (ceil(y * scale) > brush_bmap->Bounds().Height())
break;
for (int32 x = 0; x < preview_width; ++x) {
if (ceil(x * scale) > brush_bpr)
break;

union color_conversion color;
color.word = *(brush_bits + (x * scale) + (y * scale * brush_bpr));
color.word = *(brush_bits + (int32)ceil(x * scale) + ((int32)ceil(y * scale) * brush_bpr));
color.bytes[0] = 255 - color.bytes[0];
color.bytes[1] = 255 - color.bytes[1];
color.bytes[2] = 255 - color.bytes[2];
Expand All @@ -428,22 +434,37 @@ Brush::PreviewBrush(BBitmap* preview_bitmap)
}
}

// Here we draw a line indicating the relative size of the brush
color.bytes[0] = 0x00;
color.bytes[1] = 0x00;
color.bytes[2] = 0xFF;
color.bytes[3] = 0xFF;
bits = (uint32*)preview_bitmap->Bits();
bits += bpr * (int32)(bmap_height - 5) + 1;
*(bits - bpr) = color.word;
*(bits + bpr) = color.word;
for (int32 i = 0; i < bmap_width * (preview_width / width_) - 3; i++)
*bits++ = color.word;
bits--;
*(bits - bpr) = color.word;
*(bits + bpr) = color.word;

return preview_width / width_;
// Add the brush size to the bottom of the preview
BFont font;
font.SetSize(bmap_width / 6);
BString brushSizeString;
brushSizeString.SetToFormat("%d", (int32)max_dim - 2);

int32 strWidth = font.StringWidth("000");
font_height strHeight;
font.GetHeight(&strHeight);
int32 strX = 0;
int32 strY = (int32)(bmap_height - 1);
BRect fontBGRect(strX - 1, strY - strHeight.ascent + 2,
strX + strWidth + 2, bmap_height);

preview_bitmap->Lock();
BView* numView = new BView(preview_bitmap->Bounds(), "", B_FOLLOW_NONE, B_WILL_DRAW);
preview_bitmap->AddChild(numView);
rgb_color highColor = numView->HighColor();
numView->StrokeRect(preview_bitmap->Bounds(), B_SOLID_HIGH);
numView->StrokeRect(fontBGRect.OffsetByCopy(1.0, -1.0), B_SOLID_HIGH);
numView->SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
numView->FillRect(fontBGRect, B_SOLID_HIGH);
numView->MovePenTo(BPoint(strX, strY));
numView->SetHighColor(highColor);
numView->SetFont(&font);
numView->DrawString(brushSizeString);

numView->Sync();
preview_bitmap->Unlock();

return preview_width / max_dim;
}


Expand Down
9 changes: 2 additions & 7 deletions artpaint/tools/BrushEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,8 @@ BrushView::BrushView(BRect frame, Brush* brush)

SetToolTip(B_TRANSLATE("Hold SHIFT to snap to 45° angles"));

frame.InsetBy(1.0, 1.0);
fBrushPreview
= new BBitmap(BRect(0.0, 0.0, frame.Width() - 1.0, frame.Height() - 1.0), B_RGBA32);
= new BBitmap(BRect(0.0, 0.0, frame.Width(), frame.Height()), B_RGBA32, true);
fBrush->PreviewBrush(fBrushPreview);
}

Expand All @@ -389,11 +388,7 @@ BrushView::~BrushView()

void BrushView::Draw(BRect)
{
DrawBitmap(fBrushPreview, BPoint(1.0, 1.0));

SetPenSize(1);
SetHighColor(0, 0, 0, 255);
StrokeRect(Bounds());
DrawBitmap(fBrushPreview, BPoint(0.0, 0.0));

if (fDrawControls) {
float r1 = Bounds().Width() / 2;
Expand Down
11 changes: 6 additions & 5 deletions artpaint/windows/BrushStoreWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,18 @@ BrushStoreView::Draw(BRect area)
SetPenSize(1.0);
for (int32 i = 0; i < brush_images->CountItems(); i++) {
if ((area & get_bitmap_frame(i)).IsValid() == TRUE) {
DrawBitmapAsync((BBitmap*)brush_images->ItemAt(i), get_bitmap_frame(i));
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
BRect outer_frame = get_bitmap_frame(i);
outer_frame.InsetBy(-1, -1);
StrokeRect(outer_frame);
DrawBitmap((BBitmap*)brush_images->ItemAt(i), get_bitmap_frame(i));
StrokeRect(outer_frame.InsetByCopy(-1, -1));
StrokeRect(outer_frame.InsetByCopy(-2, -2));
}
}
if (IsFocus() && (brush_data->CountItems() > 0)) {
SetHighColor(ui_color(B_NAVIGATION_BASE_COLOR));
BRect outer_frame = get_bitmap_frame(selected_brush_index);
SetPenSize(1.5);
StrokeRect(outer_frame);
StrokeRect(outer_frame.InsetByCopy(-1, -1));
}
Sync();
}
Expand Down Expand Up @@ -483,7 +484,7 @@ BrushStoreView::AddBrush(Brush* brush)
bool added = false;

BBitmap* a_bitmap
= new BBitmap(BRect(0, 0, BRUSH_PREVIEW_WIDTH - 1, BRUSH_PREVIEW_HEIGHT - 1), B_RGB_32_BIT);
= new BBitmap(BRect(0, 0, BRUSH_PREVIEW_WIDTH, BRUSH_PREVIEW_HEIGHT), B_RGB_32_BIT, true);

brush_info new_brush_info = brush->GetInfo();

Expand Down

0 comments on commit abd98a1

Please sign in to comment.