Use custom image view widget in Haiku frontend
Some checks failed
Build / get-source-code (push) Has been skipped
Build / build-deb (push) Has been skipped
Build / build-appimage (push) Has been skipped
Build / build-android (push) Has been skipped
Build / build-windows (push) Has been skipped
Build / build-gentoo (push) Failing after 1m6s
Build / download-system-deps (push) Failing after 0s

This commit is contained in:
Zachary Hall 2024-12-22 15:21:32 -08:00
parent b0bfa7945f
commit 735a5eb255
6 changed files with 122 additions and 11 deletions

View file

@ -1,4 +1,4 @@
set(BACKEND_HAIKU_SRC_BASE main.cpp main_window.cpp prefs.cpp slider.cpp slider.h main.h main_window.h aboutwindow.h aboutwindow.cpp prefs.h)
set(BACKEND_HAIKU_SRC_BASE main.cpp main_window.cpp prefs.cpp slider.cpp slider.h main.h main_window.h aboutwindow.h aboutwindow.cpp prefs.h image_view.cpp image_view.h)
set(BACKEND_HAIKU_SRC )
foreach(SRC IN ITEMS ${BACKEND_HAIKU_SRC_BASE})
set(BACKEND_HAIKU_SRC ${BACKEND_HAIKU_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/${SRC})

View file

@ -0,0 +1,87 @@
#include "image_view.h"
#include <stdio.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();
}

View file

@ -0,0 +1,19 @@
#pragma once
#include <View.h>
#include <Bitmap.h>
#include <Alignment.h>
#include <Rect.h>
class LImageView : public BView {
BBitmap *bitmap;
BAlignment align;
public:
void GetPreferredSize(float *w, float *h) override;
void SetBitmap(BBitmap *bitmap);
BBitmap *Bitmap();
void SetAlignment(BAlignment align);
BAlignment Alignment();
void Draw(BRect updateRect) override;
LImageView(const char *name, BBitmap *bitmap, BAlignment align);
};

View file

@ -97,7 +97,7 @@ HaikuLooperWindow::HaikuLooperWindow(Playback *playback) : BWindow(BRect(100, 10
menu_bar->AddItem(file_menu);
menu_bar->AddItem(help_menu);
layout->AddView(menu_bar);
spacer = new BView("spacer", B_SUPPORTS_LAYOUT|B_FRAME_EVENTS);
spacer = new LImageView("spacer", NULL, BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM));
spacer->SetExplicitPreferredSize(BSize(0, 0));
spacer->SetExplicitMinSize(BSize(0, 0));
layout->AddView(spacer);
@ -184,9 +184,9 @@ void HaikuLooperWindow::UpdateCat(BBitmap *cat) {
}
}
BRect dst(sw - w, sh - h, w, h);
spacer->SetViewOverlay(cat, src, dst, NULL, B_FOLLOW_RIGHT|B_FOLLOW_BOTTOM);
spacer->SetBitmap(cat);
} else {
spacer->ClearViewOverlay();
spacer->SetBitmap(NULL);
}
UnlockLooper();
}

View file

@ -14,6 +14,7 @@
#include "aboutwindow.h"
#include "slider.h"
#include "prefs.h"
#include "image_view.h"
#include <atomic>
#include <util.hpp>
#define CMD_UPDATE_LABEL_SETTING 0x1000
@ -59,7 +60,7 @@ class HaikuLooperWindow : public BWindow {
BButton *restart_btn;
BButton *stop_btn;
BButton *pause_resume_btn;
BView *spacer;
LImageView *spacer;
void UpdateCat(BBitmap *cat);
bool volume_clicked = false;
bool speed_clicked = false;

View file

@ -9,6 +9,7 @@
#include "main_window.h"
#include <cats.hpp>
#include <TranslationUtils.h>
#include "image_view.h"
using namespace Looper::Options;
#define CMD_UPDATE_LABEL_SETTING 0x1000
#define CMD_FRONTEND 0x1001
@ -101,7 +102,8 @@ HaikuPrefsWindow::HaikuPrefsWindow(BLooper *next_handler) : BWindow(BRect(100, 1
BMessage *msg = new BMessage(CMD_SET_SETTING);
msg->AddString("pref_path", "ui.cat");
msg->AddString("pref_value", kv.first.c_str());
BRadioButton *cat_radio = new BRadioButton(fmt::format("prefs:cat:{}", kv.first).c_str(), msg);
BGroupView *cat_row = new BGroupView(B_HORIZONTAL);
BRadioButton *cat_radio = new BRadioButton(kv.first.c_str(), msg);
cat_btns[kv.first] = cat_radio;
BBitmap *cat_bmp = kv.second;
BRect src = cat_bmp->Bounds();
@ -119,14 +121,16 @@ HaikuPrefsWindow::HaikuPrefsWindow(BLooper *next_handler) : BWindow(BRect(100, 1
}
}
BRect dst(rw - w, (rh - h) / 2, rw, rh);
LockLooper();
cat_radio->SetViewOverlay(cat_bmp, src, dst, NULL, B_FOLLOW_RIGHT|B_FOLLOW_TOP_BOTTOM);
UnlockLooper();
cat_layout->AddView(cat_radio);
LImageView *img_view = new LImageView("prefs:cat:preview", cat_bmp, BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM));
img_view->SetExplicitMinSize(BSize(16, 16));
img_view->SetExplicitPreferredSize(BSize(16, 16));
cat_row->AddChild(cat_radio);
cat_row->AddChild(img_view);
cat_layout->AddView(cat_row);
}
root_layout->AddView(box, 3.0);
cat_box->AddChild(cat_group);
root_layout->AddView(cat_enable);
root_layout->AddView(cat_enable, 1.0);
root_layout->AddView(cat_box, 3.0);
BGroupView *btn_view = new BGroupView(B_HORIZONTAL);
BGroupLayout *btn_box = btn_view->GroupLayout();