window-projector.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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::fromTheme("obs",
  31. QIcon(":/res/images/obs.png")));
  32. UpdateProjectorTitle(projectorTitle);
  33. windowedProjectors.push_back(this);
  34. resize(480, 270);
  35. } else {
  36. setWindowFlags(Qt::FramelessWindowHint |
  37. Qt::X11BypassWindowManagerHint);
  38. QScreen *screen = QGuiApplication::screens()[savedMonitor];
  39. setGeometry(screen->geometry());
  40. QAction *action = new QAction(this);
  41. action->setShortcut(Qt::Key_Escape);
  42. addAction(action);
  43. connect(action, SIGNAL(triggered()), this,
  44. SLOT(EscapeTriggered()));
  45. }
  46. SetAlwaysOnTop(this, config_get_bool(GetGlobalConfig(),
  47. "BasicWindow", "ProjectorAlwaysOnTop"));
  48. setAttribute(Qt::WA_DeleteOnClose, true);
  49. //disable application quit when last window closed
  50. setAttribute(Qt::WA_QuitOnClose, false);
  51. installEventFilter(CreateShortcutFilter());
  52. auto addDrawCallback = [this] ()
  53. {
  54. bool isMultiview = type == ProjectorType::Multiview;
  55. obs_display_add_draw_callback(GetDisplay(),
  56. isMultiview ? OBSRenderMultiview : OBSRender,
  57. this);
  58. };
  59. connect(this, &OBSQTDisplay::DisplayCreated, addDrawCallback);
  60. bool hideCursor = config_get_bool(GetGlobalConfig(),
  61. "BasicWindow", "HideProjectorCursor");
  62. if (hideCursor && !isWindow && type != ProjectorType::Multiview) {
  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. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  495. window->ppiCY, backgroundColor);
  496. // Scale and Draw the program
  497. gs_matrix_push();
  498. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  499. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  500. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  501. window->ppiCY);
  502. obs_render_main_texture();
  503. endRegion();
  504. gs_matrix_pop();
  505. /* ----------- */
  506. // Draw the Label
  507. if (drawLabel) {
  508. gs_matrix_push();
  509. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  510. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  511. drawBox(obs_source_get_width(programLabel),
  512. obs_source_get_height(programLabel) +
  513. int(window->pvwprgCX * 0.015f), labelColor);
  514. obs_source_video_render(programLabel);
  515. gs_matrix_pop();
  516. }
  517. // Region for future usage with aditional info.
  518. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  519. // Just paint the background for now
  520. paintAreaWithColor(window->thickness, window->thickness,
  521. window->siCX, window->siCY * 2 +
  522. window->thicknessx2, backgroundColor);
  523. paintAreaWithColor(window->thickness + 2.5 * (
  524. window->thicknessx2 + window->ppiCX),
  525. window->thickness, window->siCX,
  526. window->siCY * 2 + window->thicknessx2,
  527. backgroundColor);
  528. }
  529. endRegion();
  530. }
  531. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  532. {
  533. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  534. if (!window->ready)
  535. return;
  536. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  537. OBSSource source = window->source;
  538. uint32_t targetCX;
  539. uint32_t targetCY;
  540. int x, y;
  541. int newCX, newCY;
  542. float scale;
  543. if (source) {
  544. targetCX = std::max(obs_source_get_width(source), 1u);
  545. targetCY = std::max(obs_source_get_height(source), 1u);
  546. } else {
  547. struct obs_video_info ovi;
  548. obs_get_video_info(&ovi);
  549. targetCX = ovi.base_width;
  550. targetCY = ovi.base_height;
  551. }
  552. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  553. newCX = int(scale * float(targetCX));
  554. newCY = int(scale * float(targetCY));
  555. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  556. float(targetCY));
  557. if (window->type == ProjectorType::Preview &&
  558. main->IsPreviewProgramMode()) {
  559. OBSSource curSource = main->GetCurrentSceneSource();
  560. if (source != curSource) {
  561. obs_source_dec_showing(source);
  562. obs_source_inc_showing(curSource);
  563. source = curSource;
  564. window->source = source;
  565. }
  566. }
  567. if (source)
  568. obs_source_video_render(source);
  569. else
  570. obs_render_main_texture();
  571. endRegion();
  572. }
  573. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  574. {
  575. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  576. window->deleteLater();
  577. UNUSED_PARAMETER(params);
  578. }
  579. static int getSourceByPosition(int x, int y, float ratio)
  580. {
  581. int pos = -1;
  582. QWidget *rec = QApplication::activeWindow();
  583. if (!rec)
  584. return pos;
  585. int cx = rec->width();
  586. int cy = rec->height();
  587. int minX = 0;
  588. int minY = 0;
  589. int maxX = cx;
  590. int maxY = cy;
  591. switch (multiviewLayout) {
  592. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  593. if (float(cx) / float(cy) > ratio) {
  594. int validX = cy * ratio;
  595. minX = (cx / 2) - (validX / 2);
  596. maxX = (cx / 2) + (validX / 2);
  597. minY = cy / 3;
  598. } else {
  599. int validY = cx / ratio;
  600. maxY = (cy / 2) + (validY / 2);
  601. minY = (cy / 2) - (validY / 6);
  602. }
  603. if (x < minX || x > maxX || y < minY || y > maxY)
  604. break;
  605. pos = (x - minX) / ((maxX - minX) / 6);
  606. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  607. break;
  608. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  609. if (float(cx) / float(cy) > ratio) {
  610. int validX = cy * ratio;
  611. maxX = (cx / 2) + (validX / 2);
  612. } else {
  613. int validY = cx / ratio;
  614. minY = (cy / 2) - (validY / 2);
  615. maxY = (cy / 2) + (validY / 2);
  616. }
  617. minX = cx / 2;
  618. if (x < minX || x > maxX || y < minY || y > maxY)
  619. break;
  620. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  621. if (x > minX + ((maxX - minX) / 2))
  622. pos++;
  623. break;
  624. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  625. if (float(cx) / float(cy) > ratio) {
  626. int validX = cy * ratio;
  627. minX = (cx / 2) - (validX / 2);
  628. } else {
  629. int validY = cx / ratio;
  630. minY = (cy / 2) - (validY / 2);
  631. maxY = (cy / 2) + (validY / 2);
  632. }
  633. maxX = (cx / 2);
  634. if (x < minX || x > maxX || y < minY || y > maxY)
  635. break;
  636. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  637. if (x > minX + ((maxX - minX) / 2))
  638. pos++;
  639. break;
  640. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  641. if (float(cx) / float(cy) > ratio) {
  642. int validX = cy * ratio;
  643. minX = (cx / 2) - (validX / 2);
  644. maxX = (cx / 2) + (validX / 2);
  645. } else {
  646. int validY = cx / ratio;
  647. minY = (cy / 2) - (validY / 2);
  648. }
  649. maxY = (cy / 2);
  650. if (x < minX || x > maxX || y < minY || y > maxY)
  651. break;
  652. pos = (x - minX) / ((maxX - minX) / 4);
  653. if (y > minY + ((maxY - minY) / 2))
  654. pos += 4;
  655. break;
  656. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  657. if (float(cx) / float(cy) > ratio) {
  658. int validX = cy * ratio;
  659. minX = (cx / 2) - (validX / 2);
  660. maxX = (cx / 2) + (validX / 2);
  661. } else {
  662. int validY = cx / ratio;
  663. maxY = (cy / 2) + (validY / 2);
  664. }
  665. minY = (cy / 2);
  666. if (x < minX || x > maxX || y < minY || y > maxY)
  667. break;
  668. pos = (x - minX) / ((maxX - minX) / 4);
  669. if (y > minY + ((maxY - minY) / 2))
  670. pos += 4;
  671. }
  672. return pos;
  673. }
  674. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  675. {
  676. OBSQTDisplay::mouseDoubleClickEvent(event);
  677. if (!mouseSwitching)
  678. return;
  679. if (!transitionOnDoubleClick)
  680. return;
  681. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  682. if (!main->IsPreviewProgramMode())
  683. return;
  684. if (event->button() == Qt::LeftButton) {
  685. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  686. if (pos < 0 || pos >= (int)numSrcs)
  687. return;
  688. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  689. if (!src)
  690. return;
  691. if (main->GetProgramSource() != src)
  692. main->TransitionToScene(src);
  693. }
  694. }
  695. void OBSProjector::mousePressEvent(QMouseEvent *event)
  696. {
  697. OBSQTDisplay::mousePressEvent(event);
  698. if (event->button() == Qt::RightButton) {
  699. QMenu popup(this);
  700. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  701. popup.exec(QCursor::pos());
  702. }
  703. if (!mouseSwitching)
  704. return;
  705. if (event->button() == Qt::LeftButton) {
  706. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  707. if (pos < 0 || pos >= (int)numSrcs)
  708. return;
  709. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  710. if (!src)
  711. return;
  712. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  713. if (main->GetCurrentSceneSource() != src)
  714. main->SetCurrentScene(src, false);
  715. }
  716. }
  717. void OBSProjector::EscapeTriggered()
  718. {
  719. deleteLater();
  720. }
  721. void OBSProjector::UpdateMultiview()
  722. {
  723. multiviewScenes.clear();
  724. multiviewLabels.clear();
  725. struct obs_video_info ovi;
  726. obs_get_video_info(&ovi);
  727. uint32_t w = ovi.base_width;
  728. uint32_t h = ovi.base_height;
  729. fw = float(w);
  730. fh = float(h);
  731. ratio = fw / fh;
  732. struct obs_frontend_source_list scenes = {};
  733. obs_frontend_get_scenes(&scenes);
  734. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Preview"),
  735. h / 2));
  736. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Program"),
  737. h / 2));
  738. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  739. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  740. drawLabel = config_get_bool(GetGlobalConfig(),
  741. "BasicWindow", "MultiviewDrawNames");
  742. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  743. "MultiviewDrawAreas");
  744. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  745. "MultiviewMouseSwitch");
  746. transitionOnDoubleClick = config_get_bool(GetGlobalConfig(),
  747. "BasicWindow", "TransitionOnDoubleClick");
  748. switch(multiviewLayout) {
  749. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  750. pvwprgCX = fw / 3;
  751. pvwprgCY = fh / 3;
  752. maxSrcs = 24;
  753. break;
  754. default:
  755. pvwprgCX = fw / 2;
  756. pvwprgCY = fh / 2;
  757. maxSrcs = 8;
  758. }
  759. ppiCX = pvwprgCX - thicknessx2;
  760. ppiCY = pvwprgCY - thicknessx2;
  761. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  762. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  763. scenesCX = pvwprgCX / 2;
  764. scenesCY = pvwprgCY / 2;
  765. siCX = scenesCX - thicknessx2;
  766. siCY = scenesCY - thicknessx2;
  767. siScaleX = (scenesCX - thicknessx2) / fw;
  768. siScaleY = (scenesCY - thicknessx2) / fh;
  769. numSrcs = 0;
  770. size_t i = 0;
  771. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  772. obs_source_t *src = scenes.sources.array[i++];
  773. OBSData data = obs_source_get_private_settings(src);
  774. obs_data_release(data);
  775. obs_data_set_default_bool(data, "show_in_multiview", true);
  776. if (!obs_data_get_bool(data, "show_in_multiview"))
  777. continue;
  778. // We have a displayable source.
  779. numSrcs++;
  780. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  781. obs_source_inc_showing(src);
  782. std::string name = std::to_string(numSrcs) + " - " +
  783. obs_source_get_name(src);
  784. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  785. }
  786. obs_frontend_source_list_free(&scenes);
  787. }
  788. void OBSProjector::UpdateProjectorTitle(QString name)
  789. {
  790. projectorTitle = name;
  791. QString title = nullptr;
  792. switch (type) {
  793. case ProjectorType::Scene:
  794. title = QTStr("SceneWindow") + " - " + name;
  795. break;
  796. case ProjectorType::Source:
  797. title = QTStr("SourceWindow") + " - " + name;
  798. break;
  799. default:
  800. title = name;
  801. break;
  802. }
  803. setWindowTitle(title);
  804. }
  805. OBSSource OBSProjector::GetSource()
  806. {
  807. return source;
  808. }
  809. ProjectorType OBSProjector::GetProjectorType()
  810. {
  811. return type;
  812. }
  813. int OBSProjector::GetMonitor()
  814. {
  815. return savedMonitor;
  816. }
  817. void OBSProjector::UpdateMultiviewProjectors()
  818. {
  819. obs_enter_graphics();
  820. updatingMultiview = true;
  821. obs_leave_graphics();
  822. for (auto &projector : multiviewProjectors)
  823. projector->UpdateMultiview();
  824. obs_enter_graphics();
  825. updatingMultiview = false;
  826. obs_leave_graphics();
  827. }
  828. void OBSProjector::RenameProjector(QString oldName, QString newName)
  829. {
  830. for (auto &projector : windowedProjectors)
  831. if (projector->projectorTitle == oldName)
  832. projector->UpdateProjectorTitle(newName);
  833. }