multiview.cpp 19 KB

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