window-projector.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #include <QAction>
  2. #include <QGuiApplication>
  3. #include <QMouseEvent>
  4. #include <QMenu>
  5. #include <QScreen>
  6. #include "obs-app.hpp"
  7. #include "window-basic-main.hpp"
  8. #include "display-helpers.hpp"
  9. #include "qt-wrappers.hpp"
  10. #include "platform.hpp"
  11. static QList<OBSProjector *> windowedProjectors;
  12. static QList<OBSProjector *> multiviewProjectors;
  13. static bool updatingMultiview = false, drawLabel, drawSafeArea, mouseSwitching,
  14. transitionOnDoubleClick;
  15. static MultiviewLayout multiviewLayout;
  16. static size_t maxSrcs, numSrcs;
  17. OBSProjector::OBSProjector(QWidget *widget, obs_source_t *source_, int monitor,
  18. QString title, ProjectorType type_)
  19. : OBSQTDisplay (widget,
  20. Qt::Window),
  21. source (source_),
  22. removedSignal (obs_source_get_signal_handler(source),
  23. "remove", OBSSourceRemoved, this)
  24. {
  25. projectorTitle = std::move(title);
  26. savedMonitor = monitor;
  27. isWindow = savedMonitor < 0;
  28. type = type_;
  29. if (isWindow) {
  30. setWindowIcon(QIcon(":/res/images/obs.png"));
  31. UpdateProjectorTitle(projectorTitle);
  32. windowedProjectors.push_back(this);
  33. resize(480, 270);
  34. } else {
  35. setWindowFlags(Qt::FramelessWindowHint |
  36. Qt::X11BypassWindowManagerHint);
  37. QScreen *screen = QGuiApplication::screens()[savedMonitor];
  38. setGeometry(screen->geometry());
  39. QAction *action = new QAction(this);
  40. action->setShortcut(Qt::Key_Escape);
  41. addAction(action);
  42. connect(action, SIGNAL(triggered()), this,
  43. SLOT(EscapeTriggered()));
  44. }
  45. SetAlwaysOnTop(this, config_get_bool(GetGlobalConfig(),
  46. "BasicWindow", "ProjectorAlwaysOnTop"));
  47. setAttribute(Qt::WA_DeleteOnClose, true);
  48. //disable application quit when last window closed
  49. setAttribute(Qt::WA_QuitOnClose, false);
  50. installEventFilter(CreateShortcutFilter());
  51. auto addDrawCallback = [this] ()
  52. {
  53. bool isMultiview = type == ProjectorType::Multiview;
  54. obs_display_add_draw_callback(GetDisplay(),
  55. isMultiview ? OBSRenderMultiview : OBSRender,
  56. this);
  57. obs_display_set_background_color(GetDisplay(), 0x000000);
  58. };
  59. connect(this, &OBSQTDisplay::DisplayCreated, addDrawCallback);
  60. bool hideCursor = config_get_bool(GetGlobalConfig(),
  61. "BasicWindow", "HideProjectorCursor");
  62. if (hideCursor && !isWindow) {
  63. QPixmap empty(16, 16);
  64. empty.fill(Qt::transparent);
  65. setCursor(QCursor(empty));
  66. }
  67. if (type == ProjectorType::Multiview) {
  68. obs_enter_graphics();
  69. // All essential action should be placed inside this area
  70. gs_render_start(true);
  71. gs_vertex2f(actionSafePercentage, actionSafePercentage);
  72. gs_vertex2f(actionSafePercentage, 1 - actionSafePercentage);
  73. gs_vertex2f(1 - actionSafePercentage, 1 - actionSafePercentage);
  74. gs_vertex2f(1 - actionSafePercentage, actionSafePercentage);
  75. gs_vertex2f(actionSafePercentage, actionSafePercentage);
  76. actionSafeMargin = gs_render_save();
  77. // All graphics should be placed inside this area
  78. gs_render_start(true);
  79. gs_vertex2f(graphicsSafePercentage, graphicsSafePercentage);
  80. gs_vertex2f(graphicsSafePercentage, 1 - graphicsSafePercentage);
  81. gs_vertex2f(1 - graphicsSafePercentage,
  82. 1 - graphicsSafePercentage);
  83. gs_vertex2f(1 - graphicsSafePercentage, graphicsSafePercentage);
  84. gs_vertex2f(graphicsSafePercentage, graphicsSafePercentage);
  85. graphicsSafeMargin = gs_render_save();
  86. // 4:3 safe area for widescreen
  87. gs_render_start(true);
  88. gs_vertex2f(fourByThreeSafePercentage, graphicsSafePercentage);
  89. gs_vertex2f(1 - fourByThreeSafePercentage,
  90. graphicsSafePercentage);
  91. gs_vertex2f(1 - fourByThreeSafePercentage, 1 -
  92. graphicsSafePercentage);
  93. gs_vertex2f(fourByThreeSafePercentage,
  94. 1 - graphicsSafePercentage);
  95. gs_vertex2f(fourByThreeSafePercentage, graphicsSafePercentage);
  96. fourByThreeSafeMargin = gs_render_save();
  97. gs_render_start(true);
  98. gs_vertex2f(0.0f, 0.5f);
  99. gs_vertex2f(lineLength, 0.5f);
  100. leftLine = gs_render_save();
  101. gs_render_start(true);
  102. gs_vertex2f(0.5f, 0.0f);
  103. gs_vertex2f(0.5f, lineLength);
  104. topLine = gs_render_save();
  105. gs_render_start(true);
  106. gs_vertex2f(1.0f, 0.5f);
  107. gs_vertex2f(1 - lineLength, 0.5f);
  108. rightLine = gs_render_save();
  109. obs_leave_graphics();
  110. solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  111. color = gs_effect_get_param_by_name(solid, "color");
  112. UpdateMultiview();
  113. multiviewProjectors.push_back(this);
  114. }
  115. App()->IncrementSleepInhibition();
  116. if (source)
  117. obs_source_inc_showing(source);
  118. ready = true;
  119. show();
  120. // We need it here to allow keyboard input in X11 to listen to Escape
  121. if (!isWindow)
  122. activateWindow();
  123. }
  124. OBSProjector::~OBSProjector()
  125. {
  126. bool isMultiview = type == ProjectorType::Multiview;
  127. obs_display_remove_draw_callback(GetDisplay(),
  128. isMultiview ? OBSRenderMultiview : OBSRender, this);
  129. if (source)
  130. obs_source_dec_showing(source);
  131. if (isMultiview) {
  132. for (OBSWeakSource &weakSrc : multiviewScenes) {
  133. OBSSource src = OBSGetStrongRef(weakSrc);
  134. if (src)
  135. obs_source_dec_showing(src);
  136. }
  137. obs_enter_graphics();
  138. gs_vertexbuffer_destroy(actionSafeMargin);
  139. gs_vertexbuffer_destroy(graphicsSafeMargin);
  140. gs_vertexbuffer_destroy(fourByThreeSafeMargin);
  141. gs_vertexbuffer_destroy(leftLine);
  142. gs_vertexbuffer_destroy(topLine);
  143. gs_vertexbuffer_destroy(rightLine);
  144. obs_leave_graphics();
  145. }
  146. if (type == ProjectorType::Multiview)
  147. multiviewProjectors.removeAll(this);
  148. if (isWindow)
  149. windowedProjectors.removeAll(this);
  150. App()->DecrementSleepInhibition();
  151. }
  152. static OBSSource CreateLabel(const char *name, size_t h)
  153. {
  154. obs_data_t *settings = obs_data_create();
  155. obs_data_t *font = obs_data_create();
  156. std::string text;
  157. text += " ";
  158. text += name;
  159. text += " ";
  160. #if defined(_WIN32)
  161. obs_data_set_string(font, "face", "Arial");
  162. #elif defined(__APPLE__)
  163. obs_data_set_string(font, "face", "Helvetica");
  164. #else
  165. obs_data_set_string(font, "face", "Monospace");
  166. #endif
  167. obs_data_set_int(font, "flags", 1); // Bold text
  168. obs_data_set_int(font, "size", int(h / 9.81));
  169. obs_data_set_obj(settings, "font", font);
  170. obs_data_set_string(settings, "text", text.c_str());
  171. obs_data_set_bool(settings, "outline", false);
  172. #ifdef _WIN32
  173. const char *text_source_id = "text_gdiplus";
  174. #else
  175. const char *text_source_id = "text_ft2_source";
  176. #endif
  177. OBSSource txtSource = obs_source_create_private(text_source_id, name,
  178. settings);
  179. obs_source_release(txtSource);
  180. obs_data_release(font);
  181. obs_data_release(settings);
  182. return txtSource;
  183. }
  184. static inline void renderVB(gs_effect_t *effect, gs_vertbuffer_t *vb,
  185. int cx, int cy)
  186. {
  187. if (!vb)
  188. return;
  189. matrix4 transform;
  190. matrix4_identity(&transform);
  191. transform.x.x = cx;
  192. transform.y.y = cy;
  193. gs_load_vertexbuffer(vb);
  194. gs_matrix_push();
  195. gs_matrix_mul(&transform);
  196. while (gs_effect_loop(effect, "Solid"))
  197. gs_draw(GS_LINESTRIP, 0, 0);
  198. gs_matrix_pop();
  199. }
  200. static inline uint32_t labelOffset(obs_source_t *label, uint32_t cx)
  201. {
  202. uint32_t w = obs_source_get_width(label);
  203. int n; // Number of scenes per row
  204. switch (multiviewLayout) {
  205. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  206. n = 6;
  207. break;
  208. default:
  209. n = 4;
  210. break;
  211. }
  212. w = uint32_t(w * ((1.0f) / n));
  213. return (cx / 2) - w;
  214. }
  215. static inline void startRegion(int vX, int vY, int vCX, int vCY, float oL,
  216. float oR, float oT, float oB)
  217. {
  218. gs_projection_push();
  219. gs_viewport_push();
  220. gs_set_viewport(vX, vY, vCX, vCY);
  221. gs_ortho(oL, oR, oT, oB, -100.0f, 100.0f);
  222. }
  223. static inline void endRegion()
  224. {
  225. gs_viewport_pop();
  226. gs_projection_pop();
  227. }
  228. void OBSProjector::OBSRenderMultiview(void *data, uint32_t cx, uint32_t cy)
  229. {
  230. OBSProjector *window = (OBSProjector *)data;
  231. if (updatingMultiview || !window->ready)
  232. return;
  233. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  234. uint32_t targetCX, targetCY;
  235. int x, y;
  236. float scale;
  237. targetCX = (uint32_t)window->fw;
  238. targetCY = (uint32_t)window->fh;
  239. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  240. OBSSource previewSrc = main->GetCurrentSceneSource();
  241. OBSSource programSrc = main->GetProgramSource();
  242. bool studioMode = main->IsPreviewProgramMode();
  243. auto renderVB = [&](gs_vertbuffer_t *vb, int cx, int cy,
  244. uint32_t colorVal)
  245. {
  246. if (!vb)
  247. return;
  248. matrix4 transform;
  249. matrix4_identity(&transform);
  250. transform.x.x = cx;
  251. transform.y.y = cy;
  252. gs_load_vertexbuffer(vb);
  253. gs_matrix_push();
  254. gs_matrix_mul(&transform);
  255. gs_effect_set_color(window->color, colorVal);
  256. while (gs_effect_loop(window->solid, "Solid"))
  257. gs_draw(GS_LINESTRIP, 0, 0);
  258. gs_matrix_pop();
  259. };
  260. auto drawBox = [&](float cx, float cy, uint32_t colorVal)
  261. {
  262. gs_effect_set_color(window->color, colorVal);
  263. while (gs_effect_loop(window->solid, "Solid"))
  264. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  265. };
  266. auto setRegion = [&](float bx, float by, float cx,
  267. float cy)
  268. {
  269. float vX = int(x + bx * scale);
  270. float vY = int(y + by * scale);
  271. float vCX = int(cx * scale);
  272. float vCY = int(cy * scale);
  273. float oL = bx;
  274. float oT = by;
  275. float oR = (bx + cx);
  276. float oB = (by + cy);
  277. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  278. };
  279. auto calcBaseSource = [&](size_t i)
  280. {
  281. switch (multiviewLayout) {
  282. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  283. window->sourceX = (i % 6) * window->scenesCX;
  284. window->sourceY = window->pvwprgCY +
  285. (i / 6) * window->scenesCY;
  286. break;
  287. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  288. window->sourceX = window->pvwprgCX;
  289. window->sourceY = (i / 2 ) * window->scenesCY;
  290. if (i % 2 != 0)
  291. window->sourceX += window->scenesCX;
  292. break;
  293. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  294. window->sourceX = 0;
  295. window->sourceY = (i / 2 ) * window->scenesCY;
  296. if (i % 2 != 0)
  297. window->sourceX = window->scenesCX;
  298. break;
  299. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  300. if (i < 4) {
  301. window->sourceX = (float(i) * window->scenesCX);
  302. window->sourceY = 0;
  303. } else {
  304. window->sourceX = (float(i - 4) *
  305. window->scenesCX);
  306. window->sourceY = window->scenesCY;
  307. }
  308. break;
  309. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  310. if (i < 4) {
  311. window->sourceX = (float(i) * window->scenesCX);
  312. window->sourceY = window->pvwprgCY;
  313. } else {
  314. window->sourceX = (float(i - 4) *
  315. window->scenesCX);
  316. window->sourceY = window->pvwprgCY +
  317. window->scenesCY;
  318. }
  319. }
  320. window->siX = window->sourceX + window->thickness;
  321. window->siY = window->sourceY + window->thickness;
  322. };
  323. auto calcPreviewProgram = [&](bool program)
  324. {
  325. switch (multiviewLayout) {
  326. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  327. window->sourceX = window->thickness +
  328. window->pvwprgCX / 2;
  329. window->sourceY = window->thickness;
  330. window->labelX = window->offset + window->pvwprgCX / 2;
  331. window->labelY = window->pvwprgCY * 0.85f;
  332. if (program) {
  333. window->sourceX += window->pvwprgCX;
  334. window->labelX += window->pvwprgCX;
  335. }
  336. break;
  337. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  338. window->sourceX = window->thickness;
  339. window->sourceY = window->pvwprgCY + window->thickness;
  340. window->labelX = window->offset;
  341. window->labelY = window->pvwprgCY * 1.85f;
  342. if (program) {
  343. window->sourceY = window->thickness;
  344. window->labelY = window->pvwprgCY * 0.85f;
  345. }
  346. break;
  347. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  348. window->sourceX = window->pvwprgCX + window->thickness;
  349. window->sourceY = window->pvwprgCY + window->thickness;
  350. window->labelX = window->pvwprgCX + window->offset;
  351. window->labelY = window->pvwprgCY * 1.85f;
  352. if (program) {
  353. window->sourceY = window->thickness;
  354. window->labelY = window->pvwprgCY * 0.85f;
  355. }
  356. break;
  357. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  358. window->sourceX = window->thickness;
  359. window->sourceY = window->pvwprgCY + window->thickness;
  360. window->labelX = window->offset;
  361. window->labelY = window->pvwprgCY * 1.85f;
  362. if (program) {
  363. window->sourceX += window->pvwprgCX;
  364. window->labelX += window->pvwprgCX;
  365. }
  366. break;
  367. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  368. window->sourceX = window->thickness;
  369. window->sourceY = window->thickness;
  370. window->labelX = window->offset;
  371. window->labelY = window->pvwprgCY * 0.85f;
  372. if (program) {
  373. window->sourceX += window->pvwprgCX;
  374. window->labelX += window->pvwprgCX;
  375. }
  376. }
  377. };
  378. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  379. uint32_t color)
  380. {
  381. gs_matrix_push();
  382. gs_matrix_translate3f(tx, ty, 0.0f);
  383. drawBox(cx, cy, color);
  384. gs_matrix_pop();
  385. };
  386. // Define the whole usable region for the multiview
  387. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, window->fw,
  388. 0.0f, window->fh);
  389. // Change the background color to highlight all sources
  390. drawBox(window->fw, window->fh, outerColor);
  391. /* ----------------------------- */
  392. /* draw sources */
  393. for (size_t i = 0; i < maxSrcs; i++) {
  394. // Handle all the offsets
  395. calcBaseSource(i);
  396. if (i >= numSrcs) {
  397. // Just paint the background and continue
  398. paintAreaWithColor(window->sourceX, window->sourceY,
  399. window->scenesCX, window->scenesCY,
  400. outerColor);
  401. paintAreaWithColor(window->siX, window->siY,
  402. window->siCX, window->siCY,
  403. backgroundColor);
  404. continue;
  405. }
  406. OBSSource src = OBSGetStrongRef(window->multiviewScenes[i]);
  407. // We have a source. Now chose the proper highlight color
  408. uint32_t colorVal = outerColor;
  409. if (src == programSrc)
  410. colorVal = programColor;
  411. else if (src == previewSrc)
  412. colorVal = studioMode ? previewColor : programColor;
  413. // Paint the background
  414. paintAreaWithColor(window->sourceX, window->sourceY,
  415. window->scenesCX, window->scenesCY, colorVal);
  416. paintAreaWithColor(window->siX, window->siY, window->siCX,
  417. window->siCY, backgroundColor);
  418. /* ----------- */
  419. // Render the source
  420. gs_matrix_push();
  421. gs_matrix_translate3f(window->siX, window->siY, 0.0f);
  422. gs_matrix_scale3f(window->siScaleX, window->siScaleY, 1.0f);
  423. setRegion(window->siX, window->siY, window->siCX, window->siCY);
  424. obs_source_video_render(src);
  425. endRegion();
  426. gs_matrix_pop();
  427. /* ----------- */
  428. // Render the label
  429. if (!drawLabel)
  430. continue;
  431. obs_source *label = window->multiviewLabels[i + 2];
  432. if (!label)
  433. continue;
  434. window->offset = labelOffset(label, window->scenesCX);
  435. gs_matrix_push();
  436. gs_matrix_translate3f(window->sourceX + window->offset,
  437. (window->scenesCY * 0.85f) + window->sourceY,
  438. 0.0f);
  439. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  440. drawBox(obs_source_get_width(label),
  441. obs_source_get_height(label) +
  442. int(window->sourceY * 0.015f), labelColor);
  443. obs_source_video_render(label);
  444. gs_matrix_pop();
  445. }
  446. /* ----------------------------- */
  447. /* draw preview */
  448. obs_source_t *previewLabel = window->multiviewLabels[0];
  449. window->offset = labelOffset(previewLabel, window->pvwprgCX);
  450. calcPreviewProgram(false);
  451. // Paint the background
  452. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  453. window->ppiCY, backgroundColor);
  454. // Scale and Draw the preview
  455. gs_matrix_push();
  456. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  457. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  458. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  459. window->ppiCY);
  460. if (studioMode)
  461. obs_source_video_render(previewSrc);
  462. else
  463. obs_render_main_texture();
  464. if (drawSafeArea) {
  465. renderVB(window->actionSafeMargin, targetCX, targetCY,
  466. outerColor);
  467. renderVB(window->graphicsSafeMargin, targetCX, targetCY,
  468. outerColor);
  469. renderVB(window->fourByThreeSafeMargin, targetCX, targetCY,
  470. outerColor);
  471. renderVB(window->leftLine, targetCX, targetCY, outerColor);
  472. renderVB(window->topLine, targetCX, targetCY, outerColor);
  473. renderVB(window->rightLine, targetCX, targetCY, outerColor);
  474. }
  475. endRegion();
  476. gs_matrix_pop();
  477. /* ----------- */
  478. // Draw the Label
  479. if (drawLabel) {
  480. gs_matrix_push();
  481. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  482. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  483. drawBox(obs_source_get_width(previewLabel),
  484. obs_source_get_height(previewLabel) +
  485. int(window->pvwprgCX * 0.015f), labelColor);
  486. obs_source_video_render(previewLabel);
  487. gs_matrix_pop();
  488. }
  489. /* ----------------------------- */
  490. /* draw program */
  491. obs_source_t *programLabel = window->multiviewLabels[1];
  492. window->offset = labelOffset(programLabel, window->pvwprgCX);
  493. calcPreviewProgram(true);
  494. // Scale and Draw the program
  495. gs_matrix_push();
  496. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  497. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  498. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  499. window->ppiCY);
  500. obs_render_main_texture();
  501. endRegion();
  502. gs_matrix_pop();
  503. /* ----------- */
  504. // Draw the Label
  505. if (drawLabel) {
  506. gs_matrix_push();
  507. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  508. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  509. drawBox(obs_source_get_width(programLabel),
  510. obs_source_get_height(programLabel) +
  511. int(window->pvwprgCX * 0.015f), labelColor);
  512. obs_source_video_render(programLabel);
  513. gs_matrix_pop();
  514. }
  515. // Region for future usage with aditional info.
  516. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  517. // Just paint the background for now
  518. paintAreaWithColor(window->thickness, window->thickness,
  519. window->siCX, window->siCY * 2 +
  520. window->thicknessx2, backgroundColor);
  521. paintAreaWithColor(window->thickness + 2.5 * (
  522. window->thicknessx2 + window->ppiCX),
  523. window->thickness, window->siCX,
  524. window->siCY * 2 + window->thicknessx2,
  525. backgroundColor);
  526. }
  527. endRegion();
  528. }
  529. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  530. {
  531. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  532. if (!window->ready)
  533. return;
  534. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  535. OBSSource source = window->source;
  536. uint32_t targetCX;
  537. uint32_t targetCY;
  538. int x, y;
  539. int newCX, newCY;
  540. float scale;
  541. if (source) {
  542. targetCX = std::max(obs_source_get_width(source), 1u);
  543. targetCY = std::max(obs_source_get_height(source), 1u);
  544. } else {
  545. struct obs_video_info ovi;
  546. obs_get_video_info(&ovi);
  547. targetCX = ovi.base_width;
  548. targetCY = ovi.base_height;
  549. }
  550. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  551. newCX = int(scale * float(targetCX));
  552. newCY = int(scale * float(targetCY));
  553. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  554. float(targetCY));
  555. if (window->type == ProjectorType::Preview &&
  556. main->IsPreviewProgramMode()) {
  557. OBSSource curSource = main->GetCurrentSceneSource();
  558. if (source != curSource) {
  559. obs_source_dec_showing(source);
  560. obs_source_inc_showing(curSource);
  561. source = curSource;
  562. window->source = source;
  563. }
  564. }
  565. if (source)
  566. obs_source_video_render(source);
  567. else
  568. obs_render_main_texture();
  569. endRegion();
  570. }
  571. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  572. {
  573. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  574. window->deleteLater();
  575. UNUSED_PARAMETER(params);
  576. }
  577. static int getSourceByPosition(int x, int y, float ratio)
  578. {
  579. int pos = -1;
  580. QWidget *rec = QApplication::activeWindow();
  581. if (!rec)
  582. return pos;
  583. int cx = rec->width();
  584. int cy = rec->height();
  585. int minX = 0;
  586. int minY = 0;
  587. int maxX = cx;
  588. int maxY = cy;
  589. switch (multiviewLayout) {
  590. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  591. if (float(cx) / float(cy) > ratio) {
  592. int validX = cy * ratio;
  593. minX = (cx / 2) - (validX / 2);
  594. maxX = (cx / 2) + (validX / 2);
  595. minY = cy / 3;
  596. } else {
  597. int validY = cx / ratio;
  598. maxY = (cy / 2) + (validY / 2);
  599. minY = (cy / 2) - (validY / 6);
  600. }
  601. if (x < minX || x > maxX || y < minY || y > maxY)
  602. break;
  603. pos = (x - minX) / ((maxX - minX) / 6);
  604. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  605. break;
  606. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  607. if (float(cx) / float(cy) > ratio) {
  608. int validX = cy * ratio;
  609. maxX = (cx / 2) + (validX / 2);
  610. } else {
  611. int validY = cx / ratio;
  612. minY = (cy / 2) - (validY / 2);
  613. maxY = (cy / 2) + (validY / 2);
  614. }
  615. minX = cx / 2;
  616. if (x < minX || x > maxX || y < minY || y > maxY)
  617. break;
  618. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  619. if (x > minX + ((maxX - minX) / 2))
  620. pos++;
  621. break;
  622. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  623. if (float(cx) / float(cy) > ratio) {
  624. int validX = cy * ratio;
  625. minX = (cx / 2) - (validX / 2);
  626. } else {
  627. int validY = cx / ratio;
  628. minY = (cy / 2) - (validY / 2);
  629. maxY = (cy / 2) + (validY / 2);
  630. }
  631. maxX = (cx / 2);
  632. if (x < minX || x > maxX || y < minY || y > maxY)
  633. break;
  634. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  635. if (x > minX + ((maxX - minX) / 2))
  636. pos++;
  637. break;
  638. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  639. if (float(cx) / float(cy) > ratio) {
  640. int validX = cy * ratio;
  641. minX = (cx / 2) - (validX / 2);
  642. maxX = (cx / 2) + (validX / 2);
  643. } else {
  644. int validY = cx / ratio;
  645. minY = (cy / 2) - (validY / 2);
  646. }
  647. maxY = (cy / 2);
  648. if (x < minX || x > maxX || y < minY || y > maxY)
  649. break;
  650. pos = (x - minX) / ((maxX - minX) / 4);
  651. if (y > minY + ((maxY - minY) / 2))
  652. pos += 4;
  653. break;
  654. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  655. if (float(cx) / float(cy) > ratio) {
  656. int validX = cy * ratio;
  657. minX = (cx / 2) - (validX / 2);
  658. maxX = (cx / 2) + (validX / 2);
  659. } else {
  660. int validY = cx / ratio;
  661. maxY = (cy / 2) + (validY / 2);
  662. }
  663. minY = (cy / 2);
  664. if (x < minX || x > maxX || y < minY || y > maxY)
  665. break;
  666. pos = (x - minX) / ((maxX - minX) / 4);
  667. if (y > minY + ((maxY - minY) / 2))
  668. pos += 4;
  669. }
  670. return pos;
  671. }
  672. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  673. {
  674. OBSQTDisplay::mouseDoubleClickEvent(event);
  675. if (!mouseSwitching)
  676. return;
  677. if (!transitionOnDoubleClick)
  678. return;
  679. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  680. if (!main->IsPreviewProgramMode())
  681. return;
  682. if (event->button() == Qt::LeftButton) {
  683. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  684. if (pos < 0 || pos >= (int)numSrcs)
  685. return;
  686. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  687. if (!src)
  688. return;
  689. if (main->GetProgramSource() != src)
  690. main->TransitionToScene(src);
  691. }
  692. }
  693. void OBSProjector::mousePressEvent(QMouseEvent *event)
  694. {
  695. OBSQTDisplay::mousePressEvent(event);
  696. if (event->button() == Qt::RightButton) {
  697. QMenu popup(this);
  698. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  699. popup.exec(QCursor::pos());
  700. }
  701. if (!mouseSwitching)
  702. return;
  703. if (event->button() == Qt::LeftButton) {
  704. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  705. if (pos < 0 || pos >= (int)numSrcs)
  706. return;
  707. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  708. if (!src)
  709. return;
  710. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  711. if (main->GetCurrentSceneSource() != src)
  712. main->SetCurrentScene(src, false);
  713. }
  714. }
  715. void OBSProjector::EscapeTriggered()
  716. {
  717. deleteLater();
  718. }
  719. void OBSProjector::UpdateMultiview()
  720. {
  721. multiviewScenes.clear();
  722. multiviewLabels.clear();
  723. struct obs_video_info ovi;
  724. obs_get_video_info(&ovi);
  725. uint32_t w = ovi.base_width;
  726. uint32_t h = ovi.base_height;
  727. fw = float(w);
  728. fh = float(h);
  729. ratio = fw / fh;
  730. struct obs_frontend_source_list scenes = {};
  731. obs_frontend_get_scenes(&scenes);
  732. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Preview"),
  733. h / 2));
  734. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Program"),
  735. h / 2));
  736. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  737. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  738. drawLabel = config_get_bool(GetGlobalConfig(),
  739. "BasicWindow", "MultiviewDrawNames");
  740. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  741. "MultiviewDrawAreas");
  742. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  743. "MultiviewMouseSwitch");
  744. transitionOnDoubleClick = config_get_bool(GetGlobalConfig(),
  745. "BasicWindow", "TransitionOnDoubleClick");
  746. switch(multiviewLayout) {
  747. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  748. pvwprgCX = fw / 3;
  749. pvwprgCY = fh / 3;
  750. maxSrcs = 24;
  751. break;
  752. default:
  753. pvwprgCX = fw / 2;
  754. pvwprgCY = fh / 2;
  755. maxSrcs = 8;
  756. }
  757. ppiCX = pvwprgCX - thicknessx2;
  758. ppiCY = pvwprgCY - thicknessx2;
  759. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  760. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  761. scenesCX = pvwprgCX / 2;
  762. scenesCY = pvwprgCY / 2;
  763. siCX = scenesCX - thicknessx2;
  764. siCY = scenesCY - thicknessx2;
  765. siScaleX = (scenesCX - thicknessx2) / fw;
  766. siScaleY = (scenesCY - thicknessx2) / fh;
  767. numSrcs = 0;
  768. size_t i = 0;
  769. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  770. obs_source_t *src = scenes.sources.array[i++];
  771. OBSData data = obs_source_get_private_settings(src);
  772. obs_data_release(data);
  773. obs_data_set_default_bool(data, "show_in_multiview", true);
  774. if (!obs_data_get_bool(data, "show_in_multiview"))
  775. continue;
  776. // We have a displayable source.
  777. numSrcs++;
  778. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  779. obs_source_inc_showing(src);
  780. std::string name = std::to_string(numSrcs) + " - " +
  781. obs_source_get_name(src);
  782. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  783. }
  784. obs_frontend_source_list_free(&scenes);
  785. }
  786. void OBSProjector::UpdateProjectorTitle(QString name)
  787. {
  788. projectorTitle = name;
  789. QString title = nullptr;
  790. switch (type) {
  791. case ProjectorType::Scene:
  792. title = QTStr("SceneWindow") + " - " + name;
  793. break;
  794. case ProjectorType::Source:
  795. title = QTStr("SourceWindow") + " - " + name;
  796. break;
  797. default:
  798. title = name;
  799. break;
  800. }
  801. setWindowTitle(title);
  802. }
  803. OBSSource OBSProjector::GetSource()
  804. {
  805. return source;
  806. }
  807. ProjectorType OBSProjector::GetProjectorType()
  808. {
  809. return type;
  810. }
  811. int OBSProjector::GetMonitor()
  812. {
  813. return savedMonitor;
  814. }
  815. void OBSProjector::UpdateMultiviewProjectors()
  816. {
  817. obs_enter_graphics();
  818. updatingMultiview = true;
  819. obs_leave_graphics();
  820. for (auto &projector : multiviewProjectors)
  821. projector->UpdateMultiview();
  822. obs_enter_graphics();
  823. updatingMultiview = false;
  824. obs_leave_graphics();
  825. }
  826. void OBSProjector::RenameProjector(QString oldName, QString newName)
  827. {
  828. for (auto &projector : windowedProjectors)
  829. if (projector->projectorTitle == oldName)
  830. projector->UpdateProjectorTitle(newName);
  831. }