(libinterface) Add 'square' scaling option to BitmapView

When toggled, this option disallows the scaled-to view rect being
uneven.
This commit is contained in:
Jaidyn Ann 2021-07-27 20:48:03 -05:00
parent 7df377d996
commit 758bbb072a
3 changed files with 39 additions and 1 deletions

View File

@ -286,6 +286,7 @@ ConversationView::_InitInterface()
fIcon->SetExplicitMinSize(BSize(50, 50)); fIcon->SetExplicitMinSize(BSize(50, 50));
fIcon->SetExplicitPreferredSize(BSize(50, 50)); fIcon->SetExplicitPreferredSize(BSize(50, 50));
fIcon->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE)); fIcon->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE));
fIcon->SetSquare(true);
fProtocolView = new BitmapView("protocolView"); fProtocolView = new BitmapView("protocolView");

View File

@ -22,6 +22,7 @@ BitmapView::BitmapView(const char* name, uint32 flags)
: :
BView(name, flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), BView(name, flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fBitmap(NULL), fBitmap(NULL),
fIsSquare(false),
fWidth(kMinWidth), fWidth(kMinWidth),
fHeight(kMinHeight) fHeight(kMinHeight)
{ {
@ -97,6 +98,13 @@ BitmapView::SetBitmap(const BBitmap* bitmap)
} }
void
BitmapView::SetSquare(bool isSquare)
{
fIsSquare = isSquare;
}
BSize BSize
BitmapView::MinSize() BitmapView::MinSize()
{ {
@ -131,5 +139,30 @@ BitmapView::Draw(BRect frame)
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY); SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
DrawBitmap(fBitmap, fBitmap->Bounds(), DrawBitmap(fBitmap, fBitmap->Bounds(),
Bounds(), B_FILTER_BITMAP_BILINEAR); _ViewBounds(), B_FILTER_BITMAP_BILINEAR);
}
BRect
BitmapView::_ViewBounds()
{
BRect bounds = Bounds();
if (fIsSquare == false || bounds.Height() == bounds.Width())
return bounds;
BPoint lt = bounds.LeftTop();
BPoint rb = bounds.RightBottom();
float diff = 0.0;
if (bounds.Height() > bounds.Width()) {
diff = bounds.Height() - bounds.Width();
lt -= BPoint(0.0, diff / 2);
rb += BPoint(0.0, diff / 2);
}
else if (bounds.Width() > bounds.Height()) {
diff = bounds.Height() - bounds.Width();
lt -= BPoint(diff / 2, 0.0);
rb += BPoint(diff / 2, 0.0);
}
return BRect(lt, rb);
} }

View File

@ -23,6 +23,7 @@ public:
status_t SetBitmap(const char* filename); status_t SetBitmap(const char* filename);
status_t SetBitmap(const BBitmap* bitmap); status_t SetBitmap(const BBitmap* bitmap);
void SetSquare(bool isSquare);
virtual BSize MinSize(); virtual BSize MinSize();
virtual BSize MaxSize(); virtual BSize MaxSize();
@ -31,7 +32,10 @@ public:
virtual void Draw(BRect frame); virtual void Draw(BRect frame);
private: private:
BRect _ViewBounds();
BBitmap* fBitmap; BBitmap* fBitmap;
bool fIsSquare;
float fWidth; float fWidth;
float fHeight; float fHeight;
}; };