looper/backends/ui/haiku/image_view.cpp

87 lines
1.9 KiB
C++
Raw Normal View History

#include "image_view.h"
LImageView::LImageView(const char *name, BBitmap *bitmap, BAlignment align) : BView(name, B_SUPPORTS_LAYOUT|B_FRAME_EVENTS)
{
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::Draw(BRect updateRect) {
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();
mw = Bounds().Width();
mh = Bounds().Height();
float wr = ((float)mw) / ((float)bw);
float hr = ((float)mh) / ((float)bh);
if (wr > hr) {
bw *= wr;
bh *= wr;
} else {
bw *= hr;
bh *= hr;
}
switch (align.Horizontal()) {
case B_ALIGN_LEFT: {
x = 0;
} break;
case B_ALIGN_HORIZONTAL_CENTER: {
x = (mw - bw) / 2;
} break;
case B_ALIGN_RIGHT: {
x = mw - bw;
} break;
}
switch (align.Vertical()) {
case B_ALIGN_TOP: {
y = 0;
} break;
case B_ALIGN_VERTICAL_CENTER: {
y = (mh - bh) / 2;
} break;
case B_ALIGN_BOTTOM: {
y = mh - bh;
} break;
}
BRect bitmap_rect(0, 0, bw, bh);
BRect draw_rect(x, y, bw + x, bh + 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;
printf("Bitmap rect: (%f, %f) -> (%f, %f)\nView rect: (%f, %f) -> (%f, %f)\n", bitmap_rect.left, bitmap_rect.top, bitmap_rect.right, bitmap_rect.bottom, draw_rect.left, draw_rect.top, draw_rect.right, draw_rect.bottom);
DrawBitmap(bitmap, bitmap_rect, draw_rect);
}
void LImageView::GetPreferredSize(float *w, float *h) {
if (!bitmap) {
*w = 0;
*h = 0;
return;
}
*w = bitmap->Bounds().Width();
*h = bitmap->Bounds().Height();
}