Zachary Hall
791c5103ea
Some checks failed
Build / download-system-deps (push) Successful in 42s
Build / build-gentoo (push) Failing after 1m5s
Build / get-source-code (push) Successful in 3m50s
Build / build-deb (push) Successful in 5m27s
Build / build-appimage (push) Successful in 1m9s
Build / build-android (push) Failing after 4s
Build / build-windows (push) Failing after 3m45s
96 lines
2 KiB
C++
96 lines
2 KiB
C++
#include "image_view.h"
|
|
#include <stdio.h>
|
|
|
|
|
|
LImageView::LImageView(const char *name, BBitmap *bitmap, BAlignment align) : BView(name, B_SUPPORTS_LAYOUT|B_FRAME_EVENTS|B_WILL_DRAW)
|
|
{
|
|
SetBitmap(bitmap);
|
|
SetAlignment(align);
|
|
}
|
|
void LImageView::SetBitmap(BBitmap *bitmap) {
|
|
this->bitmap = bitmap;
|
|
Invalidate();
|
|
}
|
|
void LImageView::SetAlignment(BAlignment align) {
|
|
this->align = align;
|
|
Invalidate();
|
|
}
|
|
BBitmap *LImageView::Bitmap() {
|
|
return bitmap;
|
|
}
|
|
BAlignment LImageView::Alignment() {
|
|
return align;
|
|
}
|
|
|
|
void LImageView::FrameResized(float newW, float newH) {
|
|
Invalidate();
|
|
}
|
|
void LImageView::Draw(BRect updateRect) {
|
|
AdoptSystemColors();
|
|
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
|
|
SetDrawingMode(B_OP_ALPHA);
|
|
FillRect(img_bounds, B_SOLID_LOW);
|
|
if (bitmap == NULL) return; // Handle null image.
|
|
int x = 0, y = 0;
|
|
int mw, mh;
|
|
int bw, bh;
|
|
bw = bitmap->Bounds().Width();
|
|
bh = bitmap->Bounds().Height();
|
|
int iw = bw, ih = bh;
|
|
mw = Bounds().Width();
|
|
mh = Bounds().Height();
|
|
float wr = ((float)mw) / ((float)bw);
|
|
float hr = ((float)mh) / ((float)bh);
|
|
if (wr < hr) {
|
|
iw *= wr;
|
|
ih *= wr;
|
|
} else {
|
|
iw *= hr;
|
|
ih *= hr;
|
|
}
|
|
|
|
|
|
switch (align.Horizontal()) {
|
|
case B_ALIGN_LEFT: {
|
|
x = 0;
|
|
} break;
|
|
case B_ALIGN_HORIZONTAL_CENTER: {
|
|
x = (mw - iw) / 2;
|
|
} break;
|
|
case B_ALIGN_RIGHT: {
|
|
x = mw - iw;
|
|
} break;
|
|
}
|
|
switch (align.Vertical()) {
|
|
case B_ALIGN_TOP: {
|
|
y = 0;
|
|
} break;
|
|
case B_ALIGN_VERTICAL_CENTER: {
|
|
y = (mh - ih) / 2;
|
|
} break;
|
|
case B_ALIGN_BOTTOM: {
|
|
y = mh - ih;
|
|
} break;
|
|
}
|
|
BRect bitmap_rect(0, 0, bw, bh);
|
|
BRect draw_rect(x, y, iw + x, ih + y);
|
|
int left = updateRect.left;
|
|
int right = updateRect.right;
|
|
int top = updateRect.top;
|
|
int bottom = updateRect.bottom;
|
|
int x2 = x + bw;
|
|
int y2 = y + bh;
|
|
FillRect(draw_rect, B_SOLID_LOW);
|
|
DrawBitmap(bitmap, bitmap_rect, draw_rect);
|
|
img_bounds = draw_rect;
|
|
}
|
|
|
|
void LImageView::GetPreferredSize(float *w, float *h) {
|
|
if (!bitmap) {
|
|
*w = 0;
|
|
*h = 0;
|
|
return;
|
|
}
|
|
*w = bitmap->Bounds().Width();
|
|
*h = bitmap->Bounds().Height();
|
|
}
|