window-projector.cpp 26 KB

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