multiview.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. #include "multiview.hpp"
  2. #include "window-basic-main.hpp"
  3. #include "obs-app.hpp"
  4. #include "platform.hpp"
  5. #include "display-helpers.hpp"
  6. Multiview::Multiview()
  7. {
  8. InitSafeAreas(&actionSafeMargin, &graphicsSafeMargin,
  9. &fourByThreeSafeMargin, &leftLine, &topLine, &rightLine);
  10. }
  11. Multiview::~Multiview()
  12. {
  13. for (OBSWeakSource &weakSrc : multiviewScenes) {
  14. OBSSource src = OBSGetStrongRef(weakSrc);
  15. if (src)
  16. obs_source_dec_showing(src);
  17. }
  18. obs_enter_graphics();
  19. gs_vertexbuffer_destroy(actionSafeMargin);
  20. gs_vertexbuffer_destroy(graphicsSafeMargin);
  21. gs_vertexbuffer_destroy(fourByThreeSafeMargin);
  22. gs_vertexbuffer_destroy(leftLine);
  23. gs_vertexbuffer_destroy(topLine);
  24. gs_vertexbuffer_destroy(rightLine);
  25. obs_leave_graphics();
  26. }
  27. static OBSSource CreateLabel(const char *name, size_t h)
  28. {
  29. OBSDataAutoRelease settings = obs_data_create();
  30. OBSDataAutoRelease font = obs_data_create();
  31. std::string text;
  32. text += " ";
  33. text += name;
  34. text += " ";
  35. #if defined(_WIN32)
  36. obs_data_set_string(font, "face", "Arial");
  37. #elif defined(__APPLE__)
  38. obs_data_set_string(font, "face", "Helvetica");
  39. #else
  40. obs_data_set_string(font, "face", "Monospace");
  41. #endif
  42. obs_data_set_int(font, "flags", 1); // Bold text
  43. obs_data_set_int(font, "size", int(h / 9.81));
  44. obs_data_set_obj(settings, "font", font);
  45. obs_data_set_string(settings, "text", text.c_str());
  46. obs_data_set_bool(settings, "outline", false);
  47. #ifdef _WIN32
  48. const char *text_source_id = "text_gdiplus";
  49. #else
  50. const char *text_source_id = "text_ft2_source";
  51. #endif
  52. OBSSourceAutoRelease txtSource =
  53. obs_source_create_private(text_source_id, name, settings);
  54. return txtSource.Get();
  55. }
  56. void Multiview::Update(MultiviewLayout multiviewLayout, bool drawLabel,
  57. bool drawSafeArea)
  58. {
  59. this->multiviewLayout = multiviewLayout;
  60. this->drawLabel = drawLabel;
  61. this->drawSafeArea = drawSafeArea;
  62. multiviewScenes.clear();
  63. multiviewLabels.clear();
  64. struct obs_video_info ovi;
  65. obs_get_video_info(&ovi);
  66. uint32_t w = ovi.base_width;
  67. uint32_t h = ovi.base_height;
  68. fw = float(w);
  69. fh = float(h);
  70. ratio = fw / fh;
  71. struct obs_frontend_source_list scenes = {};
  72. obs_frontend_get_scenes(&scenes);
  73. multiviewLabels.emplace_back(
  74. CreateLabel(Str("StudioMode.Preview"), h / 2));
  75. multiviewLabels.emplace_back(
  76. CreateLabel(Str("StudioMode.Program"), h / 2));
  77. switch (multiviewLayout) {
  78. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  79. pvwprgCX = fw / 2;
  80. pvwprgCY = fh / 2;
  81. maxSrcs = 18;
  82. break;
  83. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  84. pvwprgCX = fw / 3;
  85. pvwprgCY = fh / 3;
  86. maxSrcs = 24;
  87. break;
  88. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  89. pvwprgCX = fw / 2;
  90. pvwprgCY = fh / 2;
  91. maxSrcs = 4;
  92. break;
  93. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  94. pvwprgCX = fw / 3;
  95. pvwprgCY = fh / 3;
  96. maxSrcs = 9;
  97. break;
  98. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  99. pvwprgCX = fw / 4;
  100. pvwprgCY = fh / 4;
  101. maxSrcs = 16;
  102. break;
  103. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  104. pvwprgCX = fw / 5;
  105. pvwprgCY = fh / 5;
  106. maxSrcs = 25;
  107. break;
  108. default:
  109. pvwprgCX = fw / 2;
  110. pvwprgCY = fh / 2;
  111. maxSrcs = 8;
  112. }
  113. ppiCX = pvwprgCX - thicknessx2;
  114. ppiCY = pvwprgCY - thicknessx2;
  115. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  116. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  117. switch (multiviewLayout) {
  118. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  119. scenesCX = pvwprgCX / 3;
  120. scenesCY = pvwprgCY / 3;
  121. break;
  122. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  123. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  124. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  125. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  126. scenesCX = pvwprgCX;
  127. scenesCY = pvwprgCY;
  128. break;
  129. default:
  130. scenesCX = pvwprgCX / 2;
  131. scenesCY = pvwprgCY / 2;
  132. }
  133. siCX = scenesCX - thicknessx2;
  134. siCY = scenesCY - thicknessx2;
  135. siScaleX = (scenesCX - thicknessx2) / fw;
  136. siScaleY = (scenesCY - thicknessx2) / fh;
  137. numSrcs = 0;
  138. size_t i = 0;
  139. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  140. obs_source_t *src = scenes.sources.array[i++];
  141. OBSDataAutoRelease data = obs_source_get_private_settings(src);
  142. obs_data_set_default_bool(data, "show_in_multiview", true);
  143. if (!obs_data_get_bool(data, "show_in_multiview"))
  144. continue;
  145. // We have a displayable source.
  146. numSrcs++;
  147. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  148. obs_source_inc_showing(src);
  149. multiviewLabels.emplace_back(
  150. CreateLabel(obs_source_get_name(src), h / 3));
  151. }
  152. obs_frontend_source_list_free(&scenes);
  153. }
  154. static inline uint32_t labelOffset(MultiviewLayout multiviewLayout,
  155. obs_source_t *label, uint32_t cx)
  156. {
  157. uint32_t w = obs_source_get_width(label);
  158. int n; // Twice of scale factor of preview and program scenes
  159. switch (multiviewLayout) {
  160. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  161. n = 6;
  162. break;
  163. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  164. n = 10;
  165. break;
  166. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  167. n = 8;
  168. break;
  169. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  170. n = 6;
  171. break;
  172. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  173. n = 4;
  174. break;
  175. default:
  176. n = 4;
  177. break;
  178. }
  179. w = uint32_t(w * ((1.0f) / n));
  180. return (cx / 2) - w;
  181. }
  182. void Multiview::Render(uint32_t cx, uint32_t cy)
  183. {
  184. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  185. uint32_t targetCX, targetCY;
  186. int x, y;
  187. float scale;
  188. targetCX = (uint32_t)fw;
  189. targetCY = (uint32_t)fh;
  190. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  191. OBSSource previewSrc = main->GetCurrentSceneSource();
  192. OBSSource programSrc = main->GetProgramSource();
  193. bool studioMode = main->IsPreviewProgramMode();
  194. auto drawBox = [&](float cx, float cy, uint32_t colorVal) {
  195. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  196. gs_eparam_t *color =
  197. gs_effect_get_param_by_name(solid, "color");
  198. gs_effect_set_color(color, colorVal);
  199. while (gs_effect_loop(solid, "Solid"))
  200. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  201. };
  202. auto setRegion = [&](float bx, float by, float cx, float cy) {
  203. float vX = int(x + bx * scale);
  204. float vY = int(y + by * scale);
  205. float vCX = int(cx * scale);
  206. float vCY = int(cy * scale);
  207. float oL = bx;
  208. float oT = by;
  209. float oR = (bx + cx);
  210. float oB = (by + cy);
  211. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  212. };
  213. auto calcBaseSource = [&](size_t i) {
  214. switch (multiviewLayout) {
  215. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  216. sourceX = (i % 6) * scenesCX;
  217. sourceY = pvwprgCY + (i / 6) * scenesCY;
  218. break;
  219. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  220. sourceX = (i % 6) * scenesCX;
  221. sourceY = pvwprgCY + (i / 6) * scenesCY;
  222. break;
  223. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  224. sourceX = pvwprgCX;
  225. sourceY = (i / 2) * scenesCY;
  226. if (i % 2 != 0)
  227. sourceX += scenesCX;
  228. break;
  229. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  230. sourceX = 0;
  231. sourceY = (i / 2) * scenesCY;
  232. if (i % 2 != 0)
  233. sourceX = scenesCX;
  234. break;
  235. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  236. if (i < 4) {
  237. sourceX = (float(i) * scenesCX);
  238. sourceY = 0;
  239. } else {
  240. sourceX = (float(i - 4) * scenesCX);
  241. sourceY = scenesCY;
  242. }
  243. break;
  244. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  245. sourceX = (i % 2) * scenesCX;
  246. sourceY = (i / 2) * scenesCY;
  247. break;
  248. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  249. sourceX = (i % 3) * scenesCX;
  250. sourceY = (i / 3) * scenesCY;
  251. break;
  252. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  253. sourceX = (i % 4) * scenesCX;
  254. sourceY = (i / 4) * scenesCY;
  255. break;
  256. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  257. sourceX = (i % 5) * scenesCX;
  258. sourceY = (i / 5) * scenesCY;
  259. break;
  260. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  261. if (i < 4) {
  262. sourceX = (float(i) * scenesCX);
  263. sourceY = pvwprgCY;
  264. } else {
  265. sourceX = (float(i - 4) * scenesCX);
  266. sourceY = pvwprgCY + scenesCY;
  267. }
  268. }
  269. siX = sourceX + thickness;
  270. siY = sourceY + thickness;
  271. };
  272. auto calcPreviewProgram = [&](bool program) {
  273. switch (multiviewLayout) {
  274. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  275. sourceX = thickness + pvwprgCX / 2;
  276. sourceY = thickness;
  277. labelX = offset + pvwprgCX / 2;
  278. labelY = pvwprgCY * 0.85f;
  279. if (program) {
  280. sourceX += pvwprgCX;
  281. labelX += pvwprgCX;
  282. }
  283. break;
  284. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  285. sourceX = thickness;
  286. sourceY = pvwprgCY + thickness;
  287. labelX = offset;
  288. labelY = pvwprgCY * 1.85f;
  289. if (program) {
  290. sourceY = thickness;
  291. labelY = pvwprgCY * 0.85f;
  292. }
  293. break;
  294. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  295. sourceX = pvwprgCX + thickness;
  296. sourceY = pvwprgCY + thickness;
  297. labelX = pvwprgCX + offset;
  298. labelY = pvwprgCY * 1.85f;
  299. if (program) {
  300. sourceY = thickness;
  301. labelY = pvwprgCY * 0.85f;
  302. }
  303. break;
  304. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  305. sourceX = thickness;
  306. sourceY = pvwprgCY + thickness;
  307. labelX = offset;
  308. labelY = pvwprgCY * 1.85f;
  309. if (program) {
  310. sourceX += pvwprgCX;
  311. labelX += pvwprgCX;
  312. }
  313. break;
  314. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  315. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  316. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  317. sourceX = thickness;
  318. sourceY = thickness;
  319. labelX = offset;
  320. break;
  321. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES and 18_SCENES
  322. sourceX = thickness;
  323. sourceY = thickness;
  324. labelX = offset;
  325. labelY = pvwprgCY * 0.85f;
  326. if (program) {
  327. sourceX += pvwprgCX;
  328. labelX += pvwprgCX;
  329. }
  330. }
  331. };
  332. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  333. uint32_t color) {
  334. gs_matrix_push();
  335. gs_matrix_translate3f(tx, ty, 0.0f);
  336. drawBox(cx, cy, color);
  337. gs_matrix_pop();
  338. };
  339. // Define the whole usable region for the multiview
  340. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, fw, 0.0f,
  341. fh);
  342. // Change the background color to highlight all sources
  343. drawBox(fw, fh, outerColor);
  344. /* ----------------------------- */
  345. /* draw sources */
  346. for (size_t i = 0; i < maxSrcs; i++) {
  347. // Handle all the offsets
  348. calcBaseSource(i);
  349. if (i >= numSrcs) {
  350. // Just paint the background and continue
  351. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  352. outerColor);
  353. paintAreaWithColor(siX, siY, siCX, siCY,
  354. backgroundColor);
  355. continue;
  356. }
  357. OBSSource src = OBSGetStrongRef(multiviewScenes[i]);
  358. // We have a source. Now chose the proper highlight color
  359. uint32_t colorVal = outerColor;
  360. if (src == programSrc)
  361. colorVal = programColor;
  362. else if (src == previewSrc)
  363. colorVal = studioMode ? previewColor : programColor;
  364. // Paint the background
  365. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  366. colorVal);
  367. paintAreaWithColor(siX, siY, siCX, siCY, backgroundColor);
  368. /* ----------- */
  369. // Render the source
  370. gs_matrix_push();
  371. gs_matrix_translate3f(siX, siY, 0.0f);
  372. gs_matrix_scale3f(siScaleX, siScaleY, 1.0f);
  373. setRegion(siX, siY, siCX, siCY);
  374. obs_source_video_render(src);
  375. endRegion();
  376. gs_matrix_pop();
  377. /* ----------- */
  378. // Render the label
  379. if (!drawLabel)
  380. continue;
  381. obs_source *label = multiviewLabels[i + 2];
  382. if (!label)
  383. continue;
  384. offset = labelOffset(multiviewLayout, label, scenesCX);
  385. gs_matrix_push();
  386. gs_matrix_translate3f(sourceX + offset,
  387. (scenesCY * 0.85f) + sourceY, 0.0f);
  388. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  389. drawBox(obs_source_get_width(label),
  390. obs_source_get_height(label) + int(sourceY * 0.015f),
  391. labelColor);
  392. obs_source_video_render(label);
  393. gs_matrix_pop();
  394. }
  395. if (multiviewLayout == MultiviewLayout::SCENES_ONLY_4_SCENES ||
  396. multiviewLayout == MultiviewLayout::SCENES_ONLY_9_SCENES ||
  397. multiviewLayout == MultiviewLayout::SCENES_ONLY_16_SCENES ||
  398. multiviewLayout == MultiviewLayout::SCENES_ONLY_25_SCENES) {
  399. endRegion();
  400. return;
  401. }
  402. /* ----------------------------- */
  403. /* draw preview */
  404. obs_source_t *previewLabel = multiviewLabels[0];
  405. offset = labelOffset(multiviewLayout, previewLabel, pvwprgCX);
  406. calcPreviewProgram(false);
  407. // Paint the background
  408. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  409. // Scale and Draw the preview
  410. gs_matrix_push();
  411. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  412. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  413. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  414. if (studioMode)
  415. obs_source_video_render(previewSrc);
  416. else
  417. obs_render_main_texture();
  418. if (drawSafeArea) {
  419. RenderSafeAreas(actionSafeMargin, targetCX, targetCY);
  420. RenderSafeAreas(graphicsSafeMargin, targetCX, targetCY);
  421. RenderSafeAreas(fourByThreeSafeMargin, targetCX, targetCY);
  422. RenderSafeAreas(leftLine, targetCX, targetCY);
  423. RenderSafeAreas(topLine, targetCX, targetCY);
  424. RenderSafeAreas(rightLine, targetCX, targetCY);
  425. }
  426. endRegion();
  427. gs_matrix_pop();
  428. /* ----------- */
  429. // Draw the Label
  430. if (drawLabel) {
  431. gs_matrix_push();
  432. gs_matrix_translate3f(labelX, labelY, 0.0f);
  433. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  434. drawBox(obs_source_get_width(previewLabel),
  435. obs_source_get_height(previewLabel) +
  436. int(pvwprgCX * 0.015f),
  437. labelColor);
  438. obs_source_video_render(previewLabel);
  439. gs_matrix_pop();
  440. }
  441. /* ----------------------------- */
  442. /* draw program */
  443. obs_source_t *programLabel = multiviewLabels[1];
  444. offset = labelOffset(multiviewLayout, programLabel, pvwprgCX);
  445. calcPreviewProgram(true);
  446. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  447. // Scale and Draw the program
  448. gs_matrix_push();
  449. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  450. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  451. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  452. obs_render_main_texture();
  453. endRegion();
  454. gs_matrix_pop();
  455. /* ----------- */
  456. // Draw the Label
  457. if (drawLabel) {
  458. gs_matrix_push();
  459. gs_matrix_translate3f(labelX, labelY, 0.0f);
  460. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  461. drawBox(obs_source_get_width(programLabel),
  462. obs_source_get_height(programLabel) +
  463. int(pvwprgCX * 0.015f),
  464. labelColor);
  465. obs_source_video_render(programLabel);
  466. gs_matrix_pop();
  467. }
  468. // Region for future usage with additional info.
  469. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  470. // Just paint the background for now
  471. paintAreaWithColor(thickness, thickness, siCX,
  472. siCY * 2 + thicknessx2, backgroundColor);
  473. paintAreaWithColor(thickness + 2.5 * (thicknessx2 + ppiCX),
  474. thickness, siCX, siCY * 2 + thicknessx2,
  475. backgroundColor);
  476. }
  477. endRegion();
  478. }
  479. OBSSource Multiview::GetSourceByPosition(int x, int y)
  480. {
  481. int pos = -1;
  482. QWidget *rec = QApplication::activeWindow();
  483. if (!rec)
  484. return nullptr;
  485. int cx = rec->width();
  486. int cy = rec->height();
  487. int minX = 0;
  488. int minY = 0;
  489. int maxX = cx;
  490. int maxY = cy;
  491. switch (multiviewLayout) {
  492. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  493. if (float(cx) / float(cy) > ratio) {
  494. int validX = cy * ratio;
  495. minX = (cx / 2) - (validX / 2);
  496. maxX = (cx / 2) + (validX / 2);
  497. } else {
  498. int validY = cx / ratio;
  499. maxY = (cy / 2) + (validY / 2);
  500. }
  501. minY = cy / 2;
  502. if (x < minX || x > maxX || y < minY || y > maxY)
  503. break;
  504. pos = (x - minX) / ((maxX - minX) / 6);
  505. pos += ((y - minY) / ((maxY - minY) / 3)) * 6;
  506. break;
  507. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  508. if (float(cx) / float(cy) > ratio) {
  509. int validX = cy * ratio;
  510. minX = (cx / 2) - (validX / 2);
  511. maxX = (cx / 2) + (validX / 2);
  512. minY = cy / 3;
  513. } else {
  514. int validY = cx / ratio;
  515. maxY = (cy / 2) + (validY / 2);
  516. minY = (cy / 2) - (validY / 6);
  517. }
  518. if (x < minX || x > maxX || y < minY || y > maxY)
  519. break;
  520. pos = (x - minX) / ((maxX - minX) / 6);
  521. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  522. break;
  523. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  524. if (float(cx) / float(cy) > ratio) {
  525. int validX = cy * ratio;
  526. maxX = (cx / 2) + (validX / 2);
  527. } else {
  528. int validY = cx / ratio;
  529. minY = (cy / 2) - (validY / 2);
  530. maxY = (cy / 2) + (validY / 2);
  531. }
  532. minX = cx / 2;
  533. if (x < minX || x > maxX || y < minY || y > maxY)
  534. break;
  535. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  536. if (x > minX + ((maxX - minX) / 2))
  537. pos++;
  538. break;
  539. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  540. if (float(cx) / float(cy) > ratio) {
  541. int validX = cy * ratio;
  542. minX = (cx / 2) - (validX / 2);
  543. } else {
  544. int validY = cx / ratio;
  545. minY = (cy / 2) - (validY / 2);
  546. maxY = (cy / 2) + (validY / 2);
  547. }
  548. maxX = (cx / 2);
  549. if (x < minX || x > maxX || y < minY || y > maxY)
  550. break;
  551. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  552. if (x > minX + ((maxX - minX) / 2))
  553. pos++;
  554. break;
  555. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  556. if (float(cx) / float(cy) > ratio) {
  557. int validX = cy * ratio;
  558. minX = (cx / 2) - (validX / 2);
  559. maxX = (cx / 2) + (validX / 2);
  560. } else {
  561. int validY = cx / ratio;
  562. minY = (cy / 2) - (validY / 2);
  563. }
  564. maxY = (cy / 2);
  565. if (x < minX || x > maxX || y < minY || y > maxY)
  566. break;
  567. pos = (x - minX) / ((maxX - minX) / 4);
  568. if (y > minY + ((maxY - minY) / 2))
  569. pos += 4;
  570. break;
  571. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  572. if (float(cx) / float(cy) > ratio) {
  573. int validX = cy * ratio;
  574. minX = (cx / 2) - (validX / 2);
  575. maxX = (cx / 2) + (validX / 2);
  576. } else {
  577. int validY = cx / ratio;
  578. maxY = (cy / 2) + (validY / 2);
  579. minY = (cy / 2) - (validY / 2);
  580. }
  581. if (x < minX || x > maxX || y < minY || y > maxY)
  582. break;
  583. pos = (x - minX) / ((maxX - minX) / 2);
  584. pos += ((y - minY) / ((maxY - minY) / 2)) * 2;
  585. break;
  586. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  587. if (float(cx) / float(cy) > ratio) {
  588. int validX = cy * ratio;
  589. minX = (cx / 2) - (validX / 2);
  590. maxX = (cx / 2) + (validX / 2);
  591. } else {
  592. int validY = cx / ratio;
  593. maxY = (cy / 2) + (validY / 2);
  594. minY = (cy / 2) - (validY / 2);
  595. }
  596. if (x < minX || x > maxX || y < minY || y > maxY)
  597. break;
  598. pos = (x - minX) / ((maxX - minX) / 3);
  599. pos += ((y - minY) / ((maxY - minY) / 3)) * 3;
  600. break;
  601. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  602. if (float(cx) / float(cy) > ratio) {
  603. int validX = cy * ratio;
  604. minX = (cx / 2) - (validX / 2);
  605. maxX = (cx / 2) + (validX / 2);
  606. } else {
  607. int validY = cx / ratio;
  608. maxY = (cy / 2) + (validY / 2);
  609. minY = (cy / 2) - (validY / 2);
  610. }
  611. if (x < minX || x > maxX || y < minY || y > maxY)
  612. break;
  613. pos = (x - minX) / ((maxX - minX) / 4);
  614. pos += ((y - minY) / ((maxY - minY) / 4)) * 4;
  615. break;
  616. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  617. if (float(cx) / float(cy) > ratio) {
  618. int validX = cy * ratio;
  619. minX = (cx / 2) - (validX / 2);
  620. maxX = (cx / 2) + (validX / 2);
  621. } else {
  622. int validY = cx / ratio;
  623. maxY = (cy / 2) + (validY / 2);
  624. minY = (cy / 2) - (validY / 2);
  625. }
  626. if (x < minX || x > maxX || y < minY || y > maxY)
  627. break;
  628. pos = (x - minX) / ((maxX - minX) / 5);
  629. pos += ((y - minY) / ((maxY - minY) / 5)) * 5;
  630. break;
  631. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  632. if (float(cx) / float(cy) > ratio) {
  633. int validX = cy * ratio;
  634. minX = (cx / 2) - (validX / 2);
  635. maxX = (cx / 2) + (validX / 2);
  636. } else {
  637. int validY = cx / ratio;
  638. maxY = (cy / 2) + (validY / 2);
  639. }
  640. minY = (cy / 2);
  641. if (x < minX || x > maxX || y < minY || y > maxY)
  642. break;
  643. pos = (x - minX) / ((maxX - minX) / 4);
  644. if (y > minY + ((maxY - minY) / 2))
  645. pos += 4;
  646. }
  647. if (pos < 0 || pos >= (int)numSrcs)
  648. return nullptr;
  649. return OBSGetStrongRef(multiviewScenes[pos]);
  650. }