1
0

multiview.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. std::string name = std::to_string(numSrcs) + " - " +
  150. obs_source_get_name(src);
  151. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  152. }
  153. obs_frontend_source_list_free(&scenes);
  154. }
  155. static inline uint32_t labelOffset(MultiviewLayout multiviewLayout,
  156. obs_source_t *label, uint32_t cx)
  157. {
  158. uint32_t w = obs_source_get_width(label);
  159. int n; // Twice of scale factor of preview and program scenes
  160. switch (multiviewLayout) {
  161. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  162. n = 6;
  163. break;
  164. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  165. n = 5;
  166. break;
  167. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  168. n = 3;
  169. break;
  170. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  171. n = 2;
  172. break;
  173. default:
  174. n = 4;
  175. break;
  176. }
  177. w = uint32_t(w * ((1.0f) / n));
  178. return (cx / 2) - w;
  179. }
  180. void Multiview::Render(uint32_t cx, uint32_t cy)
  181. {
  182. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  183. uint32_t targetCX, targetCY;
  184. int x, y;
  185. float scale;
  186. targetCX = (uint32_t)fw;
  187. targetCY = (uint32_t)fh;
  188. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  189. OBSSource previewSrc = main->GetCurrentSceneSource();
  190. OBSSource programSrc = main->GetProgramSource();
  191. bool studioMode = main->IsPreviewProgramMode();
  192. auto drawBox = [&](float cx, float cy, uint32_t colorVal) {
  193. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  194. gs_eparam_t *color =
  195. gs_effect_get_param_by_name(solid, "color");
  196. gs_effect_set_color(color, colorVal);
  197. while (gs_effect_loop(solid, "Solid"))
  198. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  199. };
  200. auto setRegion = [&](float bx, float by, float cx, float cy) {
  201. float vX = int(x + bx * scale);
  202. float vY = int(y + by * scale);
  203. float vCX = int(cx * scale);
  204. float vCY = int(cy * scale);
  205. float oL = bx;
  206. float oT = by;
  207. float oR = (bx + cx);
  208. float oB = (by + cy);
  209. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  210. };
  211. auto calcBaseSource = [&](size_t i) {
  212. switch (multiviewLayout) {
  213. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  214. sourceX = (i % 6) * scenesCX;
  215. sourceY = pvwprgCY + (i / 6) * scenesCY;
  216. break;
  217. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  218. sourceX = (i % 6) * scenesCX;
  219. sourceY = pvwprgCY + (i / 6) * scenesCY;
  220. break;
  221. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  222. sourceX = pvwprgCX;
  223. sourceY = (i / 2) * scenesCY;
  224. if (i % 2 != 0)
  225. sourceX += scenesCX;
  226. break;
  227. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  228. sourceX = 0;
  229. sourceY = (i / 2) * scenesCY;
  230. if (i % 2 != 0)
  231. sourceX = scenesCX;
  232. break;
  233. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  234. if (i < 4) {
  235. sourceX = (float(i) * scenesCX);
  236. sourceY = 0;
  237. } else {
  238. sourceX = (float(i - 4) * scenesCX);
  239. sourceY = scenesCY;
  240. }
  241. break;
  242. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  243. sourceX = (i % 2) * scenesCX;
  244. sourceY = (i / 2) * scenesCY;
  245. break;
  246. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  247. sourceX = (i % 3) * scenesCX;
  248. sourceY = (i / 3) * scenesCY;
  249. break;
  250. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  251. sourceX = (i % 4) * scenesCX;
  252. sourceY = (i / 4) * scenesCY;
  253. break;
  254. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  255. sourceX = (i % 5) * scenesCX;
  256. sourceY = (i / 5) * scenesCY;
  257. break;
  258. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  259. if (i < 4) {
  260. sourceX = (float(i) * scenesCX);
  261. sourceY = pvwprgCY;
  262. } else {
  263. sourceX = (float(i - 4) * scenesCX);
  264. sourceY = pvwprgCY + scenesCY;
  265. }
  266. }
  267. siX = sourceX + thickness;
  268. siY = sourceY + thickness;
  269. };
  270. auto calcPreviewProgram = [&](bool program) {
  271. switch (multiviewLayout) {
  272. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  273. sourceX = thickness + pvwprgCX / 2;
  274. sourceY = thickness;
  275. labelX = offset + pvwprgCX / 2;
  276. labelY = pvwprgCY * 0.85f;
  277. if (program) {
  278. sourceX += pvwprgCX;
  279. labelX += pvwprgCX;
  280. }
  281. break;
  282. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  283. sourceX = thickness;
  284. sourceY = pvwprgCY + thickness;
  285. labelX = offset;
  286. labelY = pvwprgCY * 1.85f;
  287. if (program) {
  288. sourceY = thickness;
  289. labelY = pvwprgCY * 0.85f;
  290. }
  291. break;
  292. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  293. sourceX = pvwprgCX + thickness;
  294. sourceY = pvwprgCY + thickness;
  295. labelX = pvwprgCX + offset;
  296. labelY = pvwprgCY * 1.85f;
  297. if (program) {
  298. sourceY = thickness;
  299. labelY = pvwprgCY * 0.85f;
  300. }
  301. break;
  302. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  303. sourceX = thickness;
  304. sourceY = pvwprgCY + thickness;
  305. labelX = offset;
  306. labelY = pvwprgCY * 1.85f;
  307. if (program) {
  308. sourceX += pvwprgCX;
  309. labelX += pvwprgCX;
  310. }
  311. break;
  312. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  313. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  314. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  315. sourceX = thickness;
  316. sourceY = thickness;
  317. labelX = offset;
  318. break;
  319. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES and 18_SCENES
  320. sourceX = thickness;
  321. sourceY = thickness;
  322. labelX = offset;
  323. labelY = pvwprgCY * 0.85f;
  324. if (program) {
  325. sourceX += pvwprgCX;
  326. labelX += pvwprgCX;
  327. }
  328. }
  329. };
  330. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  331. uint32_t color) {
  332. gs_matrix_push();
  333. gs_matrix_translate3f(tx, ty, 0.0f);
  334. drawBox(cx, cy, color);
  335. gs_matrix_pop();
  336. };
  337. // Define the whole usable region for the multiview
  338. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, fw, 0.0f,
  339. fh);
  340. // Change the background color to highlight all sources
  341. drawBox(fw, fh, outerColor);
  342. /* ----------------------------- */
  343. /* draw sources */
  344. for (size_t i = 0; i < maxSrcs; i++) {
  345. // Handle all the offsets
  346. calcBaseSource(i);
  347. if (i >= numSrcs) {
  348. // Just paint the background and continue
  349. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  350. outerColor);
  351. paintAreaWithColor(siX, siY, siCX, siCY,
  352. backgroundColor);
  353. continue;
  354. }
  355. OBSSource src = OBSGetStrongRef(multiviewScenes[i]);
  356. // We have a source. Now chose the proper highlight color
  357. uint32_t colorVal = outerColor;
  358. if (src == programSrc)
  359. colorVal = programColor;
  360. else if (src == previewSrc)
  361. colorVal = studioMode ? previewColor : programColor;
  362. // Paint the background
  363. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  364. colorVal);
  365. paintAreaWithColor(siX, siY, siCX, siCY, backgroundColor);
  366. /* ----------- */
  367. // Render the source
  368. gs_matrix_push();
  369. gs_matrix_translate3f(siX, siY, 0.0f);
  370. gs_matrix_scale3f(siScaleX, siScaleY, 1.0f);
  371. setRegion(siX, siY, siCX, siCY);
  372. obs_source_video_render(src);
  373. endRegion();
  374. gs_matrix_pop();
  375. /* ----------- */
  376. // Render the label
  377. if (!drawLabel)
  378. continue;
  379. obs_source *label = multiviewLabels[i + 2];
  380. if (!label)
  381. continue;
  382. offset = labelOffset(multiviewLayout, label, scenesCX);
  383. gs_matrix_push();
  384. gs_matrix_translate3f(sourceX + offset,
  385. (scenesCY * 0.85f) + sourceY, 0.0f);
  386. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  387. drawBox(obs_source_get_width(label),
  388. obs_source_get_height(label) + int(sourceY * 0.015f),
  389. labelColor);
  390. obs_source_video_render(label);
  391. gs_matrix_pop();
  392. }
  393. if (multiviewLayout == MultiviewLayout::SCENES_ONLY_4_SCENES ||
  394. multiviewLayout == MultiviewLayout::SCENES_ONLY_9_SCENES ||
  395. multiviewLayout == MultiviewLayout::SCENES_ONLY_16_SCENES ||
  396. multiviewLayout == MultiviewLayout::SCENES_ONLY_25_SCENES) {
  397. endRegion();
  398. return;
  399. }
  400. /* ----------------------------- */
  401. /* draw preview */
  402. obs_source_t *previewLabel = multiviewLabels[0];
  403. offset = labelOffset(multiviewLayout, previewLabel, pvwprgCX);
  404. calcPreviewProgram(false);
  405. // Paint the background
  406. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  407. // Scale and Draw the preview
  408. gs_matrix_push();
  409. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  410. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  411. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  412. if (studioMode)
  413. obs_source_video_render(previewSrc);
  414. else
  415. obs_render_main_texture();
  416. if (drawSafeArea) {
  417. RenderSafeAreas(actionSafeMargin, targetCX, targetCY);
  418. RenderSafeAreas(graphicsSafeMargin, targetCX, targetCY);
  419. RenderSafeAreas(fourByThreeSafeMargin, targetCX, targetCY);
  420. RenderSafeAreas(leftLine, targetCX, targetCY);
  421. RenderSafeAreas(topLine, targetCX, targetCY);
  422. RenderSafeAreas(rightLine, targetCX, targetCY);
  423. }
  424. endRegion();
  425. gs_matrix_pop();
  426. /* ----------- */
  427. // Draw the Label
  428. if (drawLabel) {
  429. gs_matrix_push();
  430. gs_matrix_translate3f(labelX, labelY, 0.0f);
  431. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  432. drawBox(obs_source_get_width(previewLabel),
  433. obs_source_get_height(previewLabel) +
  434. int(pvwprgCX * 0.015f),
  435. labelColor);
  436. obs_source_video_render(previewLabel);
  437. gs_matrix_pop();
  438. }
  439. /* ----------------------------- */
  440. /* draw program */
  441. obs_source_t *programLabel = multiviewLabels[1];
  442. offset = labelOffset(multiviewLayout, programLabel, pvwprgCX);
  443. calcPreviewProgram(true);
  444. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  445. // Scale and Draw the program
  446. gs_matrix_push();
  447. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  448. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  449. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  450. obs_render_main_texture();
  451. endRegion();
  452. gs_matrix_pop();
  453. /* ----------- */
  454. // Draw the Label
  455. if (drawLabel) {
  456. gs_matrix_push();
  457. gs_matrix_translate3f(labelX, labelY, 0.0f);
  458. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  459. drawBox(obs_source_get_width(programLabel),
  460. obs_source_get_height(programLabel) +
  461. int(pvwprgCX * 0.015f),
  462. labelColor);
  463. obs_source_video_render(programLabel);
  464. gs_matrix_pop();
  465. }
  466. // Region for future usage with additional info.
  467. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  468. // Just paint the background for now
  469. paintAreaWithColor(thickness, thickness, siCX,
  470. siCY * 2 + thicknessx2, backgroundColor);
  471. paintAreaWithColor(thickness + 2.5 * (thicknessx2 + ppiCX),
  472. thickness, siCX, siCY * 2 + thicknessx2,
  473. backgroundColor);
  474. }
  475. endRegion();
  476. }
  477. OBSSource Multiview::GetSourceByPosition(int x, int y)
  478. {
  479. int pos = -1;
  480. QWidget *rec = QApplication::activeWindow();
  481. if (!rec)
  482. return nullptr;
  483. int cx = rec->width();
  484. int cy = rec->height();
  485. int minX = 0;
  486. int minY = 0;
  487. int maxX = cx;
  488. int maxY = cy;
  489. switch (multiviewLayout) {
  490. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  491. if (float(cx) / float(cy) > ratio) {
  492. int validX = cy * ratio;
  493. minX = (cx / 2) - (validX / 2);
  494. maxX = (cx / 2) + (validX / 2);
  495. } else {
  496. int validY = cx / ratio;
  497. maxY = (cy / 2) + (validY / 2);
  498. }
  499. minY = cy / 2;
  500. if (x < minX || x > maxX || y < minY || y > maxY)
  501. break;
  502. pos = (x - minX) / ((maxX - minX) / 6);
  503. pos += ((y - minY) / ((maxY - minY) / 3)) * 6;
  504. break;
  505. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  506. if (float(cx) / float(cy) > ratio) {
  507. int validX = cy * ratio;
  508. minX = (cx / 2) - (validX / 2);
  509. maxX = (cx / 2) + (validX / 2);
  510. minY = cy / 3;
  511. } else {
  512. int validY = cx / ratio;
  513. maxY = (cy / 2) + (validY / 2);
  514. minY = (cy / 2) - (validY / 6);
  515. }
  516. if (x < minX || x > maxX || y < minY || y > maxY)
  517. break;
  518. pos = (x - minX) / ((maxX - minX) / 6);
  519. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  520. break;
  521. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  522. if (float(cx) / float(cy) > ratio) {
  523. int validX = cy * ratio;
  524. maxX = (cx / 2) + (validX / 2);
  525. } else {
  526. int validY = cx / ratio;
  527. minY = (cy / 2) - (validY / 2);
  528. maxY = (cy / 2) + (validY / 2);
  529. }
  530. minX = cx / 2;
  531. if (x < minX || x > maxX || y < minY || y > maxY)
  532. break;
  533. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  534. if (x > minX + ((maxX - minX) / 2))
  535. pos++;
  536. break;
  537. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  538. if (float(cx) / float(cy) > ratio) {
  539. int validX = cy * ratio;
  540. minX = (cx / 2) - (validX / 2);
  541. } else {
  542. int validY = cx / ratio;
  543. minY = (cy / 2) - (validY / 2);
  544. maxY = (cy / 2) + (validY / 2);
  545. }
  546. maxX = (cx / 2);
  547. if (x < minX || x > maxX || y < minY || y > maxY)
  548. break;
  549. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  550. if (x > minX + ((maxX - minX) / 2))
  551. pos++;
  552. break;
  553. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  554. if (float(cx) / float(cy) > ratio) {
  555. int validX = cy * ratio;
  556. minX = (cx / 2) - (validX / 2);
  557. maxX = (cx / 2) + (validX / 2);
  558. } else {
  559. int validY = cx / ratio;
  560. minY = (cy / 2) - (validY / 2);
  561. }
  562. maxY = (cy / 2);
  563. if (x < minX || x > maxX || y < minY || y > maxY)
  564. break;
  565. pos = (x - minX) / ((maxX - minX) / 4);
  566. if (y > minY + ((maxY - minY) / 2))
  567. pos += 4;
  568. break;
  569. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  570. if (float(cx) / float(cy) > ratio) {
  571. int validX = cy * ratio;
  572. minX = (cx / 2) - (validX / 2);
  573. maxX = (cx / 2) + (validX / 2);
  574. } else {
  575. int validY = cx / ratio;
  576. maxY = (cy / 2) + (validY / 2);
  577. minY = (cy / 2) - (validY / 2);
  578. }
  579. if (x < minX || x > maxX || y < minY || y > maxY)
  580. break;
  581. pos = (x - minX) / ((maxX - minX) / 2);
  582. pos += ((y - minY) / ((maxY - minY) / 2)) * 2;
  583. break;
  584. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  585. if (float(cx) / float(cy) > ratio) {
  586. int validX = cy * ratio;
  587. minX = (cx / 2) - (validX / 2);
  588. maxX = (cx / 2) + (validX / 2);
  589. } else {
  590. int validY = cx / ratio;
  591. maxY = (cy / 2) + (validY / 2);
  592. minY = (cy / 2) - (validY / 2);
  593. }
  594. if (x < minX || x > maxX || y < minY || y > maxY)
  595. break;
  596. pos = (x - minX) / ((maxX - minX) / 3);
  597. pos += ((y - minY) / ((maxY - minY) / 3)) * 3;
  598. break;
  599. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  600. if (float(cx) / float(cy) > ratio) {
  601. int validX = cy * ratio;
  602. minX = (cx / 2) - (validX / 2);
  603. maxX = (cx / 2) + (validX / 2);
  604. } else {
  605. int validY = cx / ratio;
  606. maxY = (cy / 2) + (validY / 2);
  607. minY = (cy / 2) - (validY / 2);
  608. }
  609. if (x < minX || x > maxX || y < minY || y > maxY)
  610. break;
  611. pos = (x - minX) / ((maxX - minX) / 4);
  612. pos += ((y - minY) / ((maxY - minY) / 4)) * 4;
  613. break;
  614. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  615. if (float(cx) / float(cy) > ratio) {
  616. int validX = cy * ratio;
  617. minX = (cx / 2) - (validX / 2);
  618. maxX = (cx / 2) + (validX / 2);
  619. } else {
  620. int validY = cx / ratio;
  621. maxY = (cy / 2) + (validY / 2);
  622. minY = (cy / 2) - (validY / 2);
  623. }
  624. if (x < minX || x > maxX || y < minY || y > maxY)
  625. break;
  626. pos = (x - minX) / ((maxX - minX) / 5);
  627. pos += ((y - minY) / ((maxY - minY) / 5)) * 5;
  628. break;
  629. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  630. if (float(cx) / float(cy) > ratio) {
  631. int validX = cy * ratio;
  632. minX = (cx / 2) - (validX / 2);
  633. maxX = (cx / 2) + (validX / 2);
  634. } else {
  635. int validY = cx / ratio;
  636. maxY = (cy / 2) + (validY / 2);
  637. }
  638. minY = (cy / 2);
  639. if (x < minX || x > maxX || y < minY || y > maxY)
  640. break;
  641. pos = (x - minX) / ((maxX - minX) / 4);
  642. if (y > minY + ((maxY - minY) / 2))
  643. pos += 4;
  644. }
  645. if (pos < 0 || pos >= (int)numSrcs)
  646. return nullptr;
  647. return OBSGetStrongRef(multiviewScenes[pos]);
  648. }