window-projector.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 uint32_t labelOffset(obs_source_t *label, uint32_t cx)
  185. {
  186. uint32_t w = obs_source_get_width(label);
  187. int n; // Number of scenes per row
  188. switch (multiviewLayout) {
  189. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  190. n = 6;
  191. break;
  192. default:
  193. n = 4;
  194. break;
  195. }
  196. w = uint32_t(w * ((1.0f) / n));
  197. return (cx / 2) - w;
  198. }
  199. static inline void startRegion(int vX, int vY, int vCX, int vCY, float oL,
  200. float oR, float oT, float oB)
  201. {
  202. gs_projection_push();
  203. gs_viewport_push();
  204. gs_set_viewport(vX, vY, vCX, vCY);
  205. gs_ortho(oL, oR, oT, oB, -100.0f, 100.0f);
  206. }
  207. static inline void endRegion()
  208. {
  209. gs_viewport_pop();
  210. gs_projection_pop();
  211. }
  212. void OBSProjector::OBSRenderMultiview(void *data, uint32_t cx, uint32_t cy)
  213. {
  214. OBSProjector *window = (OBSProjector *)data;
  215. if (updatingMultiview || !window->ready)
  216. return;
  217. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  218. uint32_t targetCX, targetCY;
  219. int x, y;
  220. float scale;
  221. targetCX = (uint32_t)window->fw;
  222. targetCY = (uint32_t)window->fh;
  223. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  224. OBSSource previewSrc = main->GetCurrentSceneSource();
  225. OBSSource programSrc = main->GetProgramSource();
  226. bool studioMode = main->IsPreviewProgramMode();
  227. auto renderVB = [&](gs_vertbuffer_t *vb, int cx, int cy,
  228. uint32_t colorVal)
  229. {
  230. if (!vb)
  231. return;
  232. matrix4 transform;
  233. matrix4_identity(&transform);
  234. transform.x.x = cx;
  235. transform.y.y = cy;
  236. gs_load_vertexbuffer(vb);
  237. gs_matrix_push();
  238. gs_matrix_mul(&transform);
  239. gs_effect_set_color(window->color, colorVal);
  240. while (gs_effect_loop(window->solid, "Solid"))
  241. gs_draw(GS_LINESTRIP, 0, 0);
  242. gs_matrix_pop();
  243. };
  244. auto drawBox = [&](float cx, float cy, uint32_t colorVal)
  245. {
  246. gs_effect_set_color(window->color, colorVal);
  247. while (gs_effect_loop(window->solid, "Solid"))
  248. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  249. };
  250. auto setRegion = [&](float bx, float by, float cx,
  251. float cy)
  252. {
  253. float vX = int(x + bx * scale);
  254. float vY = int(y + by * scale);
  255. float vCX = int(cx * scale);
  256. float vCY = int(cy * scale);
  257. float oL = bx;
  258. float oT = by;
  259. float oR = (bx + cx);
  260. float oB = (by + cy);
  261. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  262. };
  263. auto calcBaseSource = [&](size_t i)
  264. {
  265. switch (multiviewLayout) {
  266. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  267. window->sourceX = (i % 6) * window->scenesCX;
  268. window->sourceY = window->pvwprgCY +
  269. (i / 6) * window->scenesCY;
  270. break;
  271. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  272. window->sourceX = window->pvwprgCX;
  273. window->sourceY = (i / 2 ) * window->scenesCY;
  274. if (i % 2 != 0)
  275. window->sourceX += window->scenesCX;
  276. break;
  277. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  278. window->sourceX = 0;
  279. window->sourceY = (i / 2 ) * window->scenesCY;
  280. if (i % 2 != 0)
  281. window->sourceX = window->scenesCX;
  282. break;
  283. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  284. if (i < 4) {
  285. window->sourceX = (float(i) * window->scenesCX);
  286. window->sourceY = 0;
  287. } else {
  288. window->sourceX = (float(i - 4) *
  289. window->scenesCX);
  290. window->sourceY = window->scenesCY;
  291. }
  292. break;
  293. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  294. if (i < 4) {
  295. window->sourceX = (float(i) * window->scenesCX);
  296. window->sourceY = window->pvwprgCY;
  297. } else {
  298. window->sourceX = (float(i - 4) *
  299. window->scenesCX);
  300. window->sourceY = window->pvwprgCY +
  301. window->scenesCY;
  302. }
  303. }
  304. window->siX = window->sourceX + window->thickness;
  305. window->siY = window->sourceY + window->thickness;
  306. };
  307. auto calcPreviewProgram = [&](bool program)
  308. {
  309. switch (multiviewLayout) {
  310. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  311. window->sourceX = window->thickness +
  312. window->pvwprgCX / 2;
  313. window->sourceY = window->thickness;
  314. window->labelX = window->offset + window->pvwprgCX / 2;
  315. window->labelY = window->pvwprgCY * 0.85f;
  316. if (program) {
  317. window->sourceX += window->pvwprgCX;
  318. window->labelX += window->pvwprgCX;
  319. }
  320. break;
  321. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  322. window->sourceX = window->thickness;
  323. window->sourceY = window->pvwprgCY + window->thickness;
  324. window->labelX = window->offset;
  325. window->labelY = window->pvwprgCY * 1.85f;
  326. if (program) {
  327. window->sourceY = window->thickness;
  328. window->labelY = window->pvwprgCY * 0.85f;
  329. }
  330. break;
  331. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  332. window->sourceX = window->pvwprgCX + window->thickness;
  333. window->sourceY = window->pvwprgCY + window->thickness;
  334. window->labelX = window->pvwprgCX + window->offset;
  335. window->labelY = window->pvwprgCY * 1.85f;
  336. if (program) {
  337. window->sourceY = window->thickness;
  338. window->labelY = window->pvwprgCY * 0.85f;
  339. }
  340. break;
  341. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  342. window->sourceX = window->thickness;
  343. window->sourceY = window->pvwprgCY + window->thickness;
  344. window->labelX = window->offset;
  345. window->labelY = window->pvwprgCY * 1.85f;
  346. if (program) {
  347. window->sourceX += window->pvwprgCX;
  348. window->labelX += window->pvwprgCX;
  349. }
  350. break;
  351. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  352. window->sourceX = window->thickness;
  353. window->sourceY = window->thickness;
  354. window->labelX = window->offset;
  355. window->labelY = window->pvwprgCY * 0.85f;
  356. if (program) {
  357. window->sourceX += window->pvwprgCX;
  358. window->labelX += window->pvwprgCX;
  359. }
  360. }
  361. };
  362. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  363. uint32_t color)
  364. {
  365. gs_matrix_push();
  366. gs_matrix_translate3f(tx, ty, 0.0f);
  367. drawBox(cx, cy, color);
  368. gs_matrix_pop();
  369. };
  370. // Define the whole usable region for the multiview
  371. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, window->fw,
  372. 0.0f, window->fh);
  373. // Change the background color to highlight all sources
  374. drawBox(window->fw, window->fh, outerColor);
  375. /* ----------------------------- */
  376. /* draw sources */
  377. for (size_t i = 0; i < maxSrcs; i++) {
  378. // Handle all the offsets
  379. calcBaseSource(i);
  380. if (i >= numSrcs) {
  381. // Just paint the background and continue
  382. paintAreaWithColor(window->sourceX, window->sourceY,
  383. window->scenesCX, window->scenesCY,
  384. outerColor);
  385. paintAreaWithColor(window->siX, window->siY,
  386. window->siCX, window->siCY,
  387. backgroundColor);
  388. continue;
  389. }
  390. OBSSource src = OBSGetStrongRef(window->multiviewScenes[i]);
  391. // We have a source. Now chose the proper highlight color
  392. uint32_t colorVal = outerColor;
  393. if (src == programSrc)
  394. colorVal = programColor;
  395. else if (src == previewSrc)
  396. colorVal = studioMode ? previewColor : programColor;
  397. // Paint the background
  398. paintAreaWithColor(window->sourceX, window->sourceY,
  399. window->scenesCX, window->scenesCY, colorVal);
  400. paintAreaWithColor(window->siX, window->siY, window->siCX,
  401. window->siCY, backgroundColor);
  402. /* ----------- */
  403. // Render the source
  404. gs_matrix_push();
  405. gs_matrix_translate3f(window->siX, window->siY, 0.0f);
  406. gs_matrix_scale3f(window->siScaleX, window->siScaleY, 1.0f);
  407. setRegion(window->siX, window->siY, window->siCX, window->siCY);
  408. obs_source_video_render(src);
  409. endRegion();
  410. gs_matrix_pop();
  411. /* ----------- */
  412. // Render the label
  413. if (!drawLabel)
  414. continue;
  415. obs_source *label = window->multiviewLabels[i + 2];
  416. if (!label)
  417. continue;
  418. window->offset = labelOffset(label, window->scenesCX);
  419. gs_matrix_push();
  420. gs_matrix_translate3f(window->sourceX + window->offset,
  421. (window->scenesCY * 0.85f) + window->sourceY,
  422. 0.0f);
  423. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  424. drawBox(obs_source_get_width(label),
  425. obs_source_get_height(label) +
  426. int(window->sourceY * 0.015f), labelColor);
  427. obs_source_video_render(label);
  428. gs_matrix_pop();
  429. }
  430. /* ----------------------------- */
  431. /* draw preview */
  432. obs_source_t *previewLabel = window->multiviewLabels[0];
  433. window->offset = labelOffset(previewLabel, window->pvwprgCX);
  434. calcPreviewProgram(false);
  435. // Paint the background
  436. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  437. window->ppiCY, backgroundColor);
  438. // Scale and Draw the preview
  439. gs_matrix_push();
  440. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  441. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  442. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  443. window->ppiCY);
  444. if (studioMode)
  445. obs_source_video_render(previewSrc);
  446. else
  447. obs_render_main_texture();
  448. if (drawSafeArea) {
  449. renderVB(window->actionSafeMargin, targetCX, targetCY,
  450. outerColor);
  451. renderVB(window->graphicsSafeMargin, targetCX, targetCY,
  452. outerColor);
  453. renderVB(window->fourByThreeSafeMargin, targetCX, targetCY,
  454. outerColor);
  455. renderVB(window->leftLine, targetCX, targetCY, outerColor);
  456. renderVB(window->topLine, targetCX, targetCY, outerColor);
  457. renderVB(window->rightLine, targetCX, targetCY, outerColor);
  458. }
  459. endRegion();
  460. gs_matrix_pop();
  461. /* ----------- */
  462. // Draw the Label
  463. if (drawLabel) {
  464. gs_matrix_push();
  465. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  466. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  467. drawBox(obs_source_get_width(previewLabel),
  468. obs_source_get_height(previewLabel) +
  469. int(window->pvwprgCX * 0.015f), labelColor);
  470. obs_source_video_render(previewLabel);
  471. gs_matrix_pop();
  472. }
  473. /* ----------------------------- */
  474. /* draw program */
  475. obs_source_t *programLabel = window->multiviewLabels[1];
  476. window->offset = labelOffset(programLabel, window->pvwprgCX);
  477. calcPreviewProgram(true);
  478. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  479. window->ppiCY, backgroundColor);
  480. // Scale and Draw the program
  481. gs_matrix_push();
  482. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  483. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  484. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  485. window->ppiCY);
  486. obs_render_main_texture();
  487. endRegion();
  488. gs_matrix_pop();
  489. /* ----------- */
  490. // Draw the Label
  491. if (drawLabel) {
  492. gs_matrix_push();
  493. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  494. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  495. drawBox(obs_source_get_width(programLabel),
  496. obs_source_get_height(programLabel) +
  497. int(window->pvwprgCX * 0.015f), labelColor);
  498. obs_source_video_render(programLabel);
  499. gs_matrix_pop();
  500. }
  501. // Region for future usage with aditional info.
  502. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  503. // Just paint the background for now
  504. paintAreaWithColor(window->thickness, window->thickness,
  505. window->siCX, window->siCY * 2 +
  506. window->thicknessx2, backgroundColor);
  507. paintAreaWithColor(window->thickness + 2.5 * (
  508. window->thicknessx2 + window->ppiCX),
  509. window->thickness, window->siCX,
  510. window->siCY * 2 + window->thicknessx2,
  511. backgroundColor);
  512. }
  513. endRegion();
  514. }
  515. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  516. {
  517. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  518. if (!window->ready)
  519. return;
  520. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  521. OBSSource source = window->source;
  522. uint32_t targetCX;
  523. uint32_t targetCY;
  524. int x, y;
  525. int newCX, newCY;
  526. float scale;
  527. if (source) {
  528. targetCX = std::max(obs_source_get_width(source), 1u);
  529. targetCY = std::max(obs_source_get_height(source), 1u);
  530. } else {
  531. struct obs_video_info ovi;
  532. obs_get_video_info(&ovi);
  533. targetCX = ovi.base_width;
  534. targetCY = ovi.base_height;
  535. }
  536. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  537. newCX = int(scale * float(targetCX));
  538. newCY = int(scale * float(targetCY));
  539. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  540. float(targetCY));
  541. if (window->type == ProjectorType::Preview &&
  542. main->IsPreviewProgramMode()) {
  543. OBSSource curSource = main->GetCurrentSceneSource();
  544. if (source != curSource) {
  545. obs_source_dec_showing(source);
  546. obs_source_inc_showing(curSource);
  547. source = curSource;
  548. window->source = source;
  549. }
  550. }
  551. if (source)
  552. obs_source_video_render(source);
  553. else
  554. obs_render_main_texture();
  555. endRegion();
  556. }
  557. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  558. {
  559. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  560. window->deleteLater();
  561. UNUSED_PARAMETER(params);
  562. }
  563. static int getSourceByPosition(int x, int y, float ratio)
  564. {
  565. int pos = -1;
  566. QWidget *rec = QApplication::activeWindow();
  567. if (!rec)
  568. return pos;
  569. int cx = rec->width();
  570. int cy = rec->height();
  571. int minX = 0;
  572. int minY = 0;
  573. int maxX = cx;
  574. int maxY = cy;
  575. switch (multiviewLayout) {
  576. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  577. if (float(cx) / float(cy) > ratio) {
  578. int validX = cy * ratio;
  579. minX = (cx / 2) - (validX / 2);
  580. maxX = (cx / 2) + (validX / 2);
  581. minY = cy / 3;
  582. } else {
  583. int validY = cx / ratio;
  584. maxY = (cy / 2) + (validY / 2);
  585. minY = (cy / 2) - (validY / 6);
  586. }
  587. if (x < minX || x > maxX || y < minY || y > maxY)
  588. break;
  589. pos = (x - minX) / ((maxX - minX) / 6);
  590. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  591. break;
  592. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  593. if (float(cx) / float(cy) > ratio) {
  594. int validX = cy * ratio;
  595. maxX = (cx / 2) + (validX / 2);
  596. } else {
  597. int validY = cx / ratio;
  598. minY = (cy / 2) - (validY / 2);
  599. maxY = (cy / 2) + (validY / 2);
  600. }
  601. minX = cx / 2;
  602. if (x < minX || x > maxX || y < minY || y > maxY)
  603. break;
  604. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  605. if (x > minX + ((maxX - minX) / 2))
  606. pos++;
  607. break;
  608. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  609. if (float(cx) / float(cy) > ratio) {
  610. int validX = cy * ratio;
  611. minX = (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. maxX = (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::HORIZONTAL_BOTTOM_8_SCENES:
  625. if (float(cx) / float(cy) > ratio) {
  626. int validX = cy * ratio;
  627. minX = (cx / 2) - (validX / 2);
  628. maxX = (cx / 2) + (validX / 2);
  629. } else {
  630. int validY = cx / ratio;
  631. minY = (cy / 2) - (validY / 2);
  632. }
  633. maxY = (cy / 2);
  634. if (x < minX || x > maxX || y < minY || y > maxY)
  635. break;
  636. pos = (x - minX) / ((maxX - minX) / 4);
  637. if (y > minY + ((maxY - minY) / 2))
  638. pos += 4;
  639. break;
  640. default: // MultiviewLayout::HORIZONTAL_TOP_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. maxY = (cy / 2) + (validY / 2);
  648. }
  649. minY = (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. }
  656. return pos;
  657. }
  658. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  659. {
  660. OBSQTDisplay::mouseDoubleClickEvent(event);
  661. if (!mouseSwitching)
  662. return;
  663. if (!transitionOnDoubleClick)
  664. return;
  665. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  666. if (!main->IsPreviewProgramMode())
  667. return;
  668. if (event->button() == Qt::LeftButton) {
  669. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  670. if (pos < 0 || pos >= (int)numSrcs)
  671. return;
  672. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  673. if (!src)
  674. return;
  675. if (main->GetProgramSource() != src)
  676. main->TransitionToScene(src);
  677. }
  678. }
  679. void OBSProjector::mousePressEvent(QMouseEvent *event)
  680. {
  681. OBSQTDisplay::mousePressEvent(event);
  682. if (event->button() == Qt::RightButton) {
  683. QMenu popup(this);
  684. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  685. popup.exec(QCursor::pos());
  686. }
  687. if (!mouseSwitching)
  688. return;
  689. if (event->button() == Qt::LeftButton) {
  690. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  691. if (pos < 0 || pos >= (int)numSrcs)
  692. return;
  693. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  694. if (!src)
  695. return;
  696. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  697. if (main->GetCurrentSceneSource() != src)
  698. main->SetCurrentScene(src, false);
  699. }
  700. }
  701. void OBSProjector::EscapeTriggered()
  702. {
  703. deleteLater();
  704. }
  705. void OBSProjector::UpdateMultiview()
  706. {
  707. multiviewScenes.clear();
  708. multiviewLabels.clear();
  709. struct obs_video_info ovi;
  710. obs_get_video_info(&ovi);
  711. uint32_t w = ovi.base_width;
  712. uint32_t h = ovi.base_height;
  713. fw = float(w);
  714. fh = float(h);
  715. ratio = fw / fh;
  716. struct obs_frontend_source_list scenes = {};
  717. obs_frontend_get_scenes(&scenes);
  718. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Preview"),
  719. h / 2));
  720. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Program"),
  721. h / 2));
  722. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  723. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  724. drawLabel = config_get_bool(GetGlobalConfig(),
  725. "BasicWindow", "MultiviewDrawNames");
  726. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  727. "MultiviewDrawAreas");
  728. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  729. "MultiviewMouseSwitch");
  730. transitionOnDoubleClick = config_get_bool(GetGlobalConfig(),
  731. "BasicWindow", "TransitionOnDoubleClick");
  732. switch(multiviewLayout) {
  733. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  734. pvwprgCX = fw / 3;
  735. pvwprgCY = fh / 3;
  736. maxSrcs = 24;
  737. break;
  738. default:
  739. pvwprgCX = fw / 2;
  740. pvwprgCY = fh / 2;
  741. maxSrcs = 8;
  742. }
  743. ppiCX = pvwprgCX - thicknessx2;
  744. ppiCY = pvwprgCY - thicknessx2;
  745. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  746. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  747. scenesCX = pvwprgCX / 2;
  748. scenesCY = pvwprgCY / 2;
  749. siCX = scenesCX - thicknessx2;
  750. siCY = scenesCY - thicknessx2;
  751. siScaleX = (scenesCX - thicknessx2) / fw;
  752. siScaleY = (scenesCY - thicknessx2) / fh;
  753. numSrcs = 0;
  754. size_t i = 0;
  755. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  756. obs_source_t *src = scenes.sources.array[i++];
  757. OBSData data = obs_source_get_private_settings(src);
  758. obs_data_release(data);
  759. obs_data_set_default_bool(data, "show_in_multiview", true);
  760. if (!obs_data_get_bool(data, "show_in_multiview"))
  761. continue;
  762. // We have a displayable source.
  763. numSrcs++;
  764. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  765. obs_source_inc_showing(src);
  766. std::string name = std::to_string(numSrcs) + " - " +
  767. obs_source_get_name(src);
  768. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  769. }
  770. obs_frontend_source_list_free(&scenes);
  771. }
  772. void OBSProjector::UpdateProjectorTitle(QString name)
  773. {
  774. projectorTitle = name;
  775. QString title = nullptr;
  776. switch (type) {
  777. case ProjectorType::Scene:
  778. title = QTStr("SceneWindow") + " - " + name;
  779. break;
  780. case ProjectorType::Source:
  781. title = QTStr("SourceWindow") + " - " + name;
  782. break;
  783. default:
  784. title = name;
  785. break;
  786. }
  787. setWindowTitle(title);
  788. }
  789. OBSSource OBSProjector::GetSource()
  790. {
  791. return source;
  792. }
  793. ProjectorType OBSProjector::GetProjectorType()
  794. {
  795. return type;
  796. }
  797. int OBSProjector::GetMonitor()
  798. {
  799. return savedMonitor;
  800. }
  801. void OBSProjector::UpdateMultiviewProjectors()
  802. {
  803. obs_enter_graphics();
  804. updatingMultiview = true;
  805. obs_leave_graphics();
  806. for (auto &projector : multiviewProjectors)
  807. projector->UpdateMultiview();
  808. obs_enter_graphics();
  809. updatingMultiview = false;
  810. obs_leave_graphics();
  811. }
  812. void OBSProjector::RenameProjector(QString oldName, QString newName)
  813. {
  814. for (auto &projector : windowedProjectors)
  815. if (projector->projectorTitle == oldName)
  816. projector->UpdateProjectorTitle(newName);
  817. }