FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
obs-studio-ftl/UI/window-basic-interaction.cpp at develop · mixer/obs-studio-ftl · GitHub
This repository was archived by the owner on Jul 21, 2020. It is now read-only.
mixer
/
obs-studio-ftl
Public archive
forked from
obsproject/obs-studio
Notifications
You must be signed in to change notification settings
Fork
12
Star
95
Code
Issues
6
Pull requests
0
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
obs-studio-ftl
/
UI
/
window-basic-interaction.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
369 lines (293 loc) · 9.82 KB
Breadcrumbs
obs-studio-ftl
/
UI
/
window-basic-interaction.cpp
Copy path
File metadata and controls
369 lines (293 loc) · 9.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
*****************************************************************************
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_)
: 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())
{
int
cx = (
int
)
config_get_int
(
App
()->
GlobalConfig
(),
"
InteractionWindow
"
,
"
cx
"
);
int
cy = (
int
)
config_get_int
(
App
()->
GlobalConfig
(),
"
InteractionWindow
"
,
"
cy
"
);
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)));
auto
addDrawCallback = [
this
] ()
{
obs_display_add_draw_callback
(ui->
preview
->
GetDisplay
(),
OBSBasicInteraction::DrawPreview,
this
);
};
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
()
{
return
new
OBSEventFilter
(
[
this
](QObject *obj, QEvent *event)
{
UNUSED_PARAMETER
(obj);
switch
(event->
type
()) {
case
QEvent::MouseButtonPress:
case
QEvent::MouseButtonRelease:
case
QEvent::MouseButtonDblClick:
return
this
->
HandleMouseClickEvent
(
static_cast
<QMouseEvent *>(event));
case
QEvent::MouseMove:
case
QEvent::Enter:
case
QEvent::Leave:
return
this
->
HandleMouseMoveEvent
(
static_cast
<QMouseEvent *>(event));
case
QEvent::Wheel:
return
this
->
HandleMouseWheelEvent
(
static_cast
<QWheelEvent *>(event));
case
QEvent::FocusIn:
case
QEvent::FocusOut:
return
this
->
HandleFocusEvent
(
static_cast
<QFocusEvent *>(event));
case
QEvent::KeyPress:
case
QEvent::KeyRelease:
return
this
->
HandleKeyEvent
(
static_cast
<QKeyEvent *>(event));
default
:
return
false
;
}
});
}
void
OBSBasicInteraction::SourceRemoved
(
void
*data,
calldata_t
*params)
{
QMetaObject::invokeMethod
(
static_cast
<OBSBasicInteraction*>(data),
"
close
"
);
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));
QMetaObject::invokeMethod
(
static_cast
<OBSBasicProperties*>(data),
"
setWindowTitle
"
,
Q_ARG
(QString, title));
}
void
OBSBasicInteraction::DrawPreview
(
void
*data,
uint32_t
cx,
uint32_t
cy)
{
OBSBasicInteraction *window =
static_cast
<OBSBasicInteraction*>(data);
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
);
int
x, y;
int
newCX, newCY;
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
();
gs_ortho
(
0
.
0f
,
float
(sourceCX),
0
.
0f
,
float
(sourceCY),
-
100
.
0f
,
100
.
0f
);
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
"
,
width
());
config_set_int
(
App
()->
GlobalConfig
(),
"
InteractionWindow
"
,
"
cy
"
,
height
());
obs_display_remove_draw_callback
(ui->
preview
->
GetDisplay
(),
OBSBasicInteraction::DrawPreview,
this
);
}
static
int
TranslateQtKeyboardEventModifiers
(QInputEvent *event,
bool
mouseEvent) {
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;
}
static
int
TranslateQtMouseEventModifiers
(
QMouseEvent *event)
{
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;
}
bool
OBSBasicInteraction::GetSourceRelativeXY
(
int
mouseX,
int
mouseY,
int
&relX,
int
&relY)
{
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
);
int
x, y;
float
scale;
GetScaleAndCenterPos
(sourceCX, sourceCY, size.
width
(), size.
height
(),
x, y, scale);
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
;
}
bool
OBSBasicInteraction::HandleMouseClickEvent
(
QMouseEvent *event)
{
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
:
blog
(
LOG_WARNING
,
"
unknown button type %d
"
,
event->
button
());
return
false
;
}
//
Why doesn't this work?
//
if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
//
clickCount = 2;
bool
insideSource =
GetSourceRelativeXY
(event->
x
(), event->
y
(),
mouseEvent.
x
, mouseEvent.
y
);
if
(mouseUp || insideSource)
obs_source_send_mouse_click
(source, &mouseEvent, button,
mouseUp, clickCount);
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
(),
mouseEvent.
x
, mouseEvent.
y
);
}
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
,
mouseEvent.
y
))
obs_source_send_mouse_wheel
(source, &mouseEvent, xDelta,
yDelta);
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
();
}
Back
|
FazBrowse Home
|
New Git URL