yolobs-studio/UI/window-basic-interaction.cpp

371 lines
9.9 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "obs-app.hpp"
#include "window-basic-interaction.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
#include "display-helpers.hpp"
#include <QKeyEvent>
#include <QCloseEvent>
#include <QScreen>
#include <QWindow>
using namespace std;
OBSBasicInteraction::OBSBasicInteraction(QWidget *parent, OBSSource source_)
2019-09-22 21:19:10 +00:00
: QDialog(parent),
main(qobject_cast<OBSBasic *>(parent)),
ui(new Ui::OBSBasicInteraction),
source(source_),
removedSignal(obs_source_get_signal_handler(source), "remove",
OBSBasicInteraction::SourceRemoved, this),
renamedSignal(obs_source_get_signal_handler(source), "rename",
OBSBasicInteraction::SourceRenamed, this),
eventFilter(BuildEventFilter())
2016-02-23 23:16:51 +00:00
{
int cx = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
2019-09-22 21:19:10 +00:00
"cx");
2016-02-23 23:16:51 +00:00
int cy = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
2019-09-22 21:19:10 +00:00
"cy");
2016-02-23 23:16:51 +00:00
ui->setupUi(this);
ui->preview->setMouseTracking(true);
ui->preview->setFocusPolicy(Qt::StrongFocus);
ui->preview->installEventFilter(eventFilter.get());
if (cx > 400 && cy > 400)
resize(cx, cy);
OBSData settings = obs_source_get_settings(source);
obs_data_release(settings);
const char *name = obs_source_get_name(source);
setWindowTitle(QTStr("Basic.InteractionWindow").arg(QT_UTF8(name)));
2019-12-10 19:31:54 +00:00
Qt::WindowFlags flags = windowFlags();
Qt::WindowFlags helpFlag = Qt::WindowContextHelpButtonHint;
setWindowFlags(flags & (~helpFlag));
2019-09-22 21:19:10 +00:00
auto addDrawCallback = [this]() {
2016-02-23 23:16:51 +00:00
obs_display_add_draw_callback(ui->preview->GetDisplay(),
2019-09-22 21:19:10 +00:00
OBSBasicInteraction::DrawPreview,
this);
2016-02-23 23:16:51 +00:00
};
connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDrawCallback);
}
OBSBasicInteraction::~OBSBasicInteraction()
{
// since QT fakes a mouse movement while destructing a widget
// remove our event filter
ui->preview->removeEventFilter(eventFilter.get());
}
OBSEventFilter *OBSBasicInteraction::BuildEventFilter()
{
2019-09-22 21:19:10 +00:00
return new OBSEventFilter([this](QObject *obj, QEvent *event) {
2016-02-23 23:16:51 +00:00
UNUSED_PARAMETER(obj);
2019-09-22 21:19:10 +00:00
switch (event->type()) {
2016-02-23 23:16:51 +00:00
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
return this->HandleMouseClickEvent(
2019-09-22 21:19:10 +00:00
static_cast<QMouseEvent *>(event));
2016-02-23 23:16:51 +00:00
case QEvent::MouseMove:
case QEvent::Enter:
case QEvent::Leave:
return this->HandleMouseMoveEvent(
2019-09-22 21:19:10 +00:00
static_cast<QMouseEvent *>(event));
2016-02-23 23:16:51 +00:00
case QEvent::Wheel:
return this->HandleMouseWheelEvent(
2019-09-22 21:19:10 +00:00
static_cast<QWheelEvent *>(event));
2016-02-23 23:16:51 +00:00
case QEvent::FocusIn:
case QEvent::FocusOut:
return this->HandleFocusEvent(
2019-09-22 21:19:10 +00:00
static_cast<QFocusEvent *>(event));
2016-02-23 23:16:51 +00:00
case QEvent::KeyPress:
case QEvent::KeyRelease:
return this->HandleKeyEvent(
2019-09-22 21:19:10 +00:00
static_cast<QKeyEvent *>(event));
2016-02-23 23:16:51 +00:00
default:
return false;
}
});
}
void OBSBasicInteraction::SourceRemoved(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(static_cast<OBSBasicInteraction *>(data),
"close");
2016-02-23 23:16:51 +00:00
UNUSED_PARAMETER(params);
}
void OBSBasicInteraction::SourceRenamed(void *data, calldata_t *params)
{
const char *name = calldata_string(params, "new_name");
QString title = QTStr("Basic.InteractionWindow").arg(QT_UTF8(name));
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
"setWindowTitle", Q_ARG(QString, title));
2016-02-23 23:16:51 +00:00
}
void OBSBasicInteraction::DrawPreview(void *data, uint32_t cx, uint32_t cy)
{
2019-09-22 21:19:10 +00:00
OBSBasicInteraction *window = static_cast<OBSBasicInteraction *>(data);
2016-02-23 23:16:51 +00:00
if (!window->source)
return;
uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
2019-09-22 21:19:10 +00:00
int x, y;
int newCX, newCY;
2016-02-23 23:16:51 +00:00
float scale;
GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
newCX = int(scale * float(sourceCX));
newCY = int(scale * float(sourceCY));
gs_viewport_push();
gs_projection_push();
2019-09-22 21:19:10 +00:00
gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
2016-02-23 23:16:51 +00:00
gs_set_viewport(x, y, newCX, newCY);
obs_source_video_render(window->source);
gs_projection_pop();
gs_viewport_pop();
}
void OBSBasicInteraction::closeEvent(QCloseEvent *event)
{
QDialog::closeEvent(event);
if (!event->isAccepted())
return;
config_set_int(App()->GlobalConfig(), "InteractionWindow", "cx",
2019-09-22 21:19:10 +00:00
width());
2016-02-23 23:16:51 +00:00
config_set_int(App()->GlobalConfig(), "InteractionWindow", "cy",
2019-09-22 21:19:10 +00:00
height());
2016-05-24 19:53:01 +00:00
obs_display_remove_draw_callback(ui->preview->GetDisplay(),
2019-09-22 21:19:10 +00:00
OBSBasicInteraction::DrawPreview,
this);
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
static int TranslateQtKeyboardEventModifiers(QInputEvent *event,
bool mouseEvent)
{
2016-02-23 23:16:51 +00:00
int obsModifiers = INTERACT_NONE;
if (event->modifiers().testFlag(Qt::ShiftModifier))
obsModifiers |= INTERACT_SHIFT_KEY;
if (event->modifiers().testFlag(Qt::AltModifier))
obsModifiers |= INTERACT_ALT_KEY;
#ifdef __APPLE__
// Mac: Meta = Control, Control = Command
if (event->modifiers().testFlag(Qt::ControlModifier))
obsModifiers |= INTERACT_COMMAND_KEY;
if (event->modifiers().testFlag(Qt::MetaModifier))
obsModifiers |= INTERACT_CONTROL_KEY;
#else
// Handle windows key? Can a browser even trap that key?
if (event->modifiers().testFlag(Qt::ControlModifier))
obsModifiers |= INTERACT_CONTROL_KEY;
#endif
if (!mouseEvent) {
if (event->modifiers().testFlag(Qt::KeypadModifier))
obsModifiers |= INTERACT_IS_KEY_PAD;
}
return obsModifiers;
}
2019-09-22 21:19:10 +00:00
static int TranslateQtMouseEventModifiers(QMouseEvent *event)
2016-02-23 23:16:51 +00:00
{
int modifiers = TranslateQtKeyboardEventModifiers(event, true);
if (event->buttons().testFlag(Qt::LeftButton))
modifiers |= INTERACT_MOUSE_LEFT;
if (event->buttons().testFlag(Qt::MiddleButton))
modifiers |= INTERACT_MOUSE_MIDDLE;
if (event->buttons().testFlag(Qt::RightButton))
modifiers |= INTERACT_MOUSE_RIGHT;
return modifiers;
}
2019-09-22 21:19:10 +00:00
bool OBSBasicInteraction::GetSourceRelativeXY(int mouseX, int mouseY, int &relX,
int &relY)
2016-02-23 23:16:51 +00:00
{
QSize size = GetPixelSize(ui->preview);
uint32_t sourceCX = max(obs_source_get_width(source), 1u);
uint32_t sourceCY = max(obs_source_get_height(source), 1u);
2019-09-22 21:19:10 +00:00
int x, y;
2016-02-23 23:16:51 +00:00
float scale;
2019-09-22 21:19:10 +00:00
GetScaleAndCenterPos(sourceCX, sourceCY, size.width(), size.height(), x,
y, scale);
2016-02-23 23:16:51 +00:00
if (x > 0) {
relX = int(float(mouseX - x) / scale);
relY = int(float(mouseY / scale));
} else {
relX = int(float(mouseX / scale));
relY = int(float(mouseY - y) / scale);
}
// Confirm mouse is inside the source
if (relX < 0 || relX > int(sourceCX))
return false;
if (relY < 0 || relY > int(sourceCY))
return false;
return true;
}
2019-09-22 21:19:10 +00:00
bool OBSBasicInteraction::HandleMouseClickEvent(QMouseEvent *event)
2016-02-23 23:16:51 +00:00
{
bool mouseUp = event->type() == QEvent::MouseButtonRelease;
int clickCount = 1;
if (event->type() == QEvent::MouseButtonDblClick)
clickCount = 2;
struct obs_mouse_event mouseEvent = {};
mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
int32_t button = 0;
switch (event->button()) {
case Qt::LeftButton:
button = MOUSE_LEFT;
break;
case Qt::MiddleButton:
button = MOUSE_MIDDLE;
break;
case Qt::RightButton:
button = MOUSE_RIGHT;
break;
default:
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING, "unknown button type %d", event->button());
2016-02-23 23:16:51 +00:00
return false;
}
// Why doesn't this work?
//if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
// clickCount = 2;
bool insideSource = GetSourceRelativeXY(event->x(), event->y(),
2019-09-22 21:19:10 +00:00
mouseEvent.x, mouseEvent.y);
2016-02-23 23:16:51 +00:00
if (mouseUp || insideSource)
obs_source_send_mouse_click(source, &mouseEvent, button,
2019-09-22 21:19:10 +00:00
mouseUp, clickCount);
2016-02-23 23:16:51 +00:00
return true;
}
bool OBSBasicInteraction::HandleMouseMoveEvent(QMouseEvent *event)
{
struct obs_mouse_event mouseEvent = {};
bool mouseLeave = event->type() == QEvent::Leave;
if (!mouseLeave) {
mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
mouseLeave = !GetSourceRelativeXY(event->x(), event->y(),
2019-09-22 21:19:10 +00:00
mouseEvent.x, mouseEvent.y);
2016-02-23 23:16:51 +00:00
}
obs_source_send_mouse_move(source, &mouseEvent, mouseLeave);
return true;
}
bool OBSBasicInteraction::HandleMouseWheelEvent(QWheelEvent *event)
{
struct obs_mouse_event mouseEvent = {};
mouseEvent.modifiers = TranslateQtKeyboardEventModifiers(event, true);
int xDelta = 0;
int yDelta = 0;
if (!event->pixelDelta().isNull()) {
if (event->orientation() == Qt::Horizontal)
xDelta = event->pixelDelta().x();
else
yDelta = event->pixelDelta().y();
} else {
if (event->orientation() == Qt::Horizontal)
xDelta = event->delta();
else
yDelta = event->delta();
}
if (GetSourceRelativeXY(event->x(), event->y(), mouseEvent.x,
2019-09-22 21:19:10 +00:00
mouseEvent.y))
2016-02-23 23:16:51 +00:00
obs_source_send_mouse_wheel(source, &mouseEvent, xDelta,
2019-09-22 21:19:10 +00:00
yDelta);
2016-02-23 23:16:51 +00:00
return true;
}
bool OBSBasicInteraction::HandleFocusEvent(QFocusEvent *event)
{
bool focus = event->type() == QEvent::FocusIn;
obs_source_send_focus(source, focus);
return true;
}
bool OBSBasicInteraction::HandleKeyEvent(QKeyEvent *event)
{
struct obs_key_event keyEvent;
QByteArray text = event->text().toUtf8();
keyEvent.modifiers = TranslateQtKeyboardEventModifiers(event, false);
keyEvent.text = text.data();
keyEvent.native_modifiers = event->nativeModifiers();
keyEvent.native_scancode = event->nativeScanCode();
keyEvent.native_vkey = event->nativeVirtualKey();
bool keyUp = event->type() == QEvent::KeyRelease;
obs_source_send_key_click(source, &keyEvent, keyUp);
return true;
}
void OBSBasicInteraction::Init()
{
show();
}