yolobs-studio/UI/qt-display.cpp

124 lines
2.7 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include "qt-display.hpp"
#include "qt-wrappers.hpp"
#include "display-helpers.hpp"
#include <QWindow>
#include <QScreen>
#include <QResizeEvent>
#include <QShowEvent>
2019-09-22 21:19:10 +00:00
static inline long long color_to_int(const QColor &color)
2019-07-27 12:47:10 +00:00
{
2019-09-22 21:19:10 +00:00
auto shift = [&](unsigned val, int shift) {
2019-07-27 12:47:10 +00:00
return ((val & 0xff) << shift);
};
2019-09-22 21:19:10 +00:00
return shift(color.red(), 0) | shift(color.green(), 8) |
shift(color.blue(), 16) | shift(color.alpha(), 24);
2019-07-27 12:47:10 +00:00
}
static inline QColor rgba_to_color(uint32_t rgba)
{
2019-09-22 21:19:10 +00:00
return QColor::fromRgb(rgba & 0xFF, (rgba >> 8) & 0xFF,
(rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
2019-07-27 12:47:10 +00:00
}
2016-02-23 23:16:51 +00:00
OBSQTDisplay::OBSQTDisplay(QWidget *parent, Qt::WindowFlags flags)
: QWidget(parent, flags)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow);
2019-09-22 21:19:10 +00:00
auto windowVisible = [this](bool visible) {
2016-02-23 23:16:51 +00:00
if (!visible)
return;
if (!display) {
CreateDisplay();
} else {
QSize size = GetPixelSize(this);
2019-09-22 21:19:10 +00:00
obs_display_resize(display, size.width(),
size.height());
2016-02-23 23:16:51 +00:00
}
};
2019-09-22 21:19:10 +00:00
auto sizeChanged = [this](QScreen *) {
2016-02-23 23:16:51 +00:00
CreateDisplay();
QSize size = GetPixelSize(this);
obs_display_resize(display, size.width(), size.height());
};
connect(windowHandle(), &QWindow::visibleChanged, windowVisible);
connect(windowHandle(), &QWindow::screenChanged, sizeChanged);
}
2019-07-27 12:47:10 +00:00
QColor OBSQTDisplay::GetDisplayBackgroundColor() const
{
return rgba_to_color(backgroundColor);
}
void OBSQTDisplay::SetDisplayBackgroundColor(const QColor &color)
{
uint32_t newBackgroundColor = (uint32_t)color_to_int(color);
if (newBackgroundColor != backgroundColor) {
backgroundColor = newBackgroundColor;
UpdateDisplayBackgroundColor();
}
}
void OBSQTDisplay::UpdateDisplayBackgroundColor()
{
obs_display_set_background_color(display, backgroundColor);
}
2016-02-23 23:16:51 +00:00
void OBSQTDisplay::CreateDisplay()
{
if (display || !windowHandle()->isExposed())
return;
QSize size = GetPixelSize(this);
2019-09-22 21:19:10 +00:00
gs_init_data info = {};
info.cx = size.width();
info.cy = size.height();
info.format = GS_RGBA;
info.zsformat = GS_ZS_NONE;
2016-02-23 23:16:51 +00:00
QTToGSWindow(winId(), info.window);
2019-07-27 12:47:10 +00:00
display = obs_display_create(&info, backgroundColor);
2016-02-23 23:16:51 +00:00
emit DisplayCreated(this);
}
void OBSQTDisplay::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
CreateDisplay();
if (isVisible() && display) {
QSize size = GetPixelSize(this);
obs_display_resize(display, size.width(), size.height());
}
emit DisplayResized();
}
void OBSQTDisplay::paintEvent(QPaintEvent *event)
{
CreateDisplay();
QWidget::paintEvent(event);
}
QPaintEngine *OBSQTDisplay::paintEngine() const
{
return nullptr;
}