window-projector.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. #define HORIZONTAL_TOP 0
  12. #define HORIZONTAL_BOTTOM 1
  13. #define VERTICAL_LEFT 2
  14. #define VERTICAL_RIGHT 3
  15. static QList<OBSProjector *> windowedProjectors;
  16. static QList<OBSProjector *> multiviewProjectors;
  17. static bool updatingMultiview = false;
  18. static int multiviewLayout = HORIZONTAL_TOP;
  19. OBSProjector::OBSProjector(QWidget *widget, obs_source_t *source_, int monitor,
  20. QString title, ProjectorType type_)
  21. : OBSQTDisplay (widget,
  22. Qt::Window),
  23. source (source_),
  24. removedSignal (obs_source_get_signal_handler(source),
  25. "remove", OBSSourceRemoved, this)
  26. {
  27. projectorTitle = title;
  28. savedMonitor = monitor;
  29. isWindow = savedMonitor < 0;
  30. type = type_;
  31. if (isWindow) {
  32. setWindowIcon(QIcon(":/res/images/obs.png"));
  33. } else {
  34. setWindowFlags(Qt::FramelessWindowHint |
  35. Qt::X11BypassWindowManagerHint);
  36. }
  37. setAttribute(Qt::WA_DeleteOnClose, true);
  38. //disable application quit when last window closed
  39. setAttribute(Qt::WA_QuitOnClose, false);
  40. installEventFilter(CreateShortcutFilter());
  41. auto addDrawCallback = [this] ()
  42. {
  43. bool isMultiview = type == ProjectorType::Multiview;
  44. obs_display_add_draw_callback(GetDisplay(),
  45. isMultiview ? OBSRenderMultiview : OBSRender,
  46. this);
  47. obs_display_set_background_color(GetDisplay(), 0x000000);
  48. };
  49. connect(this, &OBSQTDisplay::DisplayCreated, addDrawCallback);
  50. bool hideCursor = config_get_bool(GetGlobalConfig(),
  51. "BasicWindow", "HideProjectorCursor");
  52. if (hideCursor && !isWindow) {
  53. QPixmap empty(16, 16);
  54. empty.fill(Qt::transparent);
  55. setCursor(QCursor(empty));
  56. }
  57. if (type == ProjectorType::Multiview) {
  58. obs_enter_graphics();
  59. gs_render_start(true);
  60. gs_vertex2f(0.001f, 0.001f);
  61. gs_vertex2f(0.001f, 0.997f);
  62. gs_vertex2f(0.997f, 0.997f);
  63. gs_vertex2f(0.997f, 0.001f);
  64. gs_vertex2f(0.001f, 0.001f);
  65. outerBox = gs_render_save();
  66. gs_render_start(true);
  67. gs_vertex2f(0.04f, 0.04f);
  68. gs_vertex2f(0.04f, 0.96f);
  69. gs_vertex2f(0.96f, 0.96f);
  70. gs_vertex2f(0.96f, 0.04f);
  71. gs_vertex2f(0.04f, 0.04f);
  72. innerBox = gs_render_save();
  73. gs_render_start(true);
  74. gs_vertex2f(0.15f, 0.04f);
  75. gs_vertex2f(0.15f, 0.96f);
  76. leftVLine = gs_render_save();
  77. gs_render_start(true);
  78. gs_vertex2f(0.85f, 0.04f);
  79. gs_vertex2f(0.85f, 0.96f);
  80. rightVLine = gs_render_save();
  81. gs_render_start(true);
  82. gs_vertex2f(0.0f, 0.5f);
  83. gs_vertex2f(0.075f, 0.5f);
  84. leftLine = gs_render_save();
  85. gs_render_start(true);
  86. gs_vertex2f(0.5f, 0.0f);
  87. gs_vertex2f(0.5f, 0.09f);
  88. topLine = gs_render_save();
  89. gs_render_start(true);
  90. gs_vertex2f(0.925f, 0.5f);
  91. gs_vertex2f(1.0f, 0.5f);
  92. rightLine = gs_render_save();
  93. obs_leave_graphics();
  94. UpdateMultiview();
  95. multiviewProjectors.push_back(this);
  96. }
  97. App()->IncrementSleepInhibition();
  98. resize(480, 270);
  99. }
  100. OBSProjector::~OBSProjector()
  101. {
  102. bool isMultiview = type == ProjectorType::Multiview;
  103. obs_display_remove_draw_callback(GetDisplay(),
  104. isMultiview ? OBSRenderMultiview : OBSRender, this);
  105. if (source)
  106. obs_source_dec_showing(source);
  107. if (isMultiview) {
  108. for (OBSWeakSource &weakSrc : multiviewScenes) {
  109. OBSSource src = OBSGetStrongRef(weakSrc);
  110. if (src)
  111. obs_source_dec_showing(src);
  112. }
  113. obs_enter_graphics();
  114. gs_vertexbuffer_destroy(outerBox);
  115. gs_vertexbuffer_destroy(innerBox);
  116. gs_vertexbuffer_destroy(leftVLine);
  117. gs_vertexbuffer_destroy(rightVLine);
  118. gs_vertexbuffer_destroy(leftLine);
  119. gs_vertexbuffer_destroy(topLine);
  120. gs_vertexbuffer_destroy(rightLine);
  121. obs_leave_graphics();
  122. }
  123. if (type == ProjectorType::Multiview)
  124. multiviewProjectors.removeAll(this);
  125. if (isWindow)
  126. windowedProjectors.removeAll(this);
  127. App()->DecrementSleepInhibition();
  128. }
  129. static OBSSource CreateLabel(const char *name, size_t h)
  130. {
  131. obs_data_t *settings = obs_data_create();
  132. obs_data_t *font = obs_data_create();
  133. std::string text;
  134. text += " ";
  135. text += name;
  136. text += " ";
  137. #if defined(_WIN32)
  138. obs_data_set_string(font, "face", "Arial");
  139. #elif defined(__APPLE__)
  140. obs_data_set_string(font, "face", "Helvetica");
  141. #else
  142. obs_data_set_string(font, "face", "Monospace");
  143. #endif
  144. obs_data_set_int(font, "flags", 1); // Bold text
  145. obs_data_set_int(font, "size", int(h / 9.81));
  146. obs_data_set_obj(settings, "font", font);
  147. obs_data_set_string(settings, "text", text.c_str());
  148. obs_data_set_bool(settings, "outline", false);
  149. #ifdef _WIN32
  150. const char *text_source_id = "text_gdiplus";
  151. #else
  152. const char *text_source_id = "text_ft2_source";
  153. #endif
  154. OBSSource txtSource = obs_source_create_private(text_source_id, name,
  155. settings);
  156. obs_source_release(txtSource);
  157. obs_data_release(font);
  158. obs_data_release(settings);
  159. return txtSource;
  160. }
  161. void OBSProjector::Init()
  162. {
  163. bool alwaysOnTop = config_get_bool(GetGlobalConfig(),
  164. "BasicWindow", "ProjectorAlwaysOnTop");
  165. if (alwaysOnTop && !isWindow)
  166. SetAlwaysOnTop(this, true);
  167. show();
  168. if (isWindow) {
  169. UpdateProjectorTitle(projectorTitle);
  170. windowedProjectors.push_back(this);
  171. } else {
  172. QScreen *screen = QGuiApplication::screens()[savedMonitor];
  173. setGeometry(screen->geometry());
  174. QAction *action = new QAction(this);
  175. action->setShortcut(Qt::Key_Escape);
  176. addAction(action);
  177. connect(action, SIGNAL(triggered()), this,
  178. SLOT(EscapeTriggered()));
  179. activateWindow();
  180. }
  181. if (source)
  182. obs_source_inc_showing(source);
  183. ready = true;
  184. }
  185. static inline void renderVB(gs_effect_t *effect, gs_vertbuffer_t *vb,
  186. int cx, int cy)
  187. {
  188. if (!vb)
  189. return;
  190. matrix4 transform;
  191. matrix4_identity(&transform);
  192. transform.x.x = cx;
  193. transform.y.y = cy;
  194. gs_load_vertexbuffer(vb);
  195. gs_matrix_push();
  196. gs_matrix_mul(&transform);
  197. while (gs_effect_loop(effect, "Solid"))
  198. gs_draw(GS_LINESTRIP, 0, 0);
  199. gs_matrix_pop();
  200. }
  201. static inline uint32_t labelOffset(obs_source_t *label, uint32_t cx)
  202. {
  203. uint32_t w = obs_source_get_width(label);
  204. w = uint32_t(float(w) * 0.5f);
  205. return (cx / 2) - w;
  206. }
  207. void OBSProjector::OBSRenderMultiview(void *data, uint32_t cx, uint32_t cy)
  208. {
  209. OBSProjector *window = (OBSProjector *)data;
  210. if (updatingMultiview || !window->ready)
  211. return;
  212. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  213. uint32_t targetCX, targetCY;
  214. int x, y;
  215. float fX, fY, halfCX, halfCY, sourceX, sourceY, labelX, labelY,
  216. quarterCX, quarterCY, scale, targetCXF, targetCYF,
  217. hiCX, hiCY, qiX, qiY, qiCX, qiCY, hiScaleX, hiScaleY,
  218. qiScaleX, qiScaleY;
  219. uint32_t offset;
  220. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  221. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  222. struct obs_video_info ovi;
  223. obs_get_video_info(&ovi);
  224. targetCX = ovi.base_width;
  225. targetCY = ovi.base_height;
  226. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  227. targetCXF = float(targetCX);
  228. targetCYF = float(targetCY);
  229. fX = float(x);
  230. fY = float(y);
  231. halfCX = (targetCXF + 1) / 2;
  232. halfCY = (targetCYF + 1) / 2;
  233. hiCX = (halfCX - 4.0);
  234. hiCY = (halfCY - 4.0);
  235. hiScaleX = hiCX / targetCXF;
  236. hiScaleY = hiCY / targetCYF;
  237. quarterCX = (halfCX + 1) / 2;
  238. quarterCY = (halfCY + 1) / 2;
  239. qiCX = (quarterCX - 8.0);
  240. qiCY = (quarterCY - 8.0);
  241. qiScaleX = qiCX / targetCXF;
  242. qiScaleY = qiCY / targetCYF;
  243. OBSSource previewSrc = main->GetCurrentSceneSource();
  244. OBSSource programSrc = main->GetProgramSource();
  245. bool studioMode = main->IsPreviewProgramMode();
  246. auto drawBox = [solid, color] (float cx, float cy,
  247. uint32_t colorVal)
  248. {
  249. gs_effect_set_color(color, colorVal);
  250. while (gs_effect_loop(solid, "Solid"))
  251. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  252. };
  253. auto setRegion = [fX, fY, scale] (float x, float y, float cx, float cy)
  254. {
  255. float vX = int(fX + x * scale);
  256. float vY = int(fY + y * scale);
  257. float vCX = int(cx * scale);
  258. float vCY = int(cy * scale);
  259. float oL = x;
  260. float oT = y;
  261. float oR = (x + cx);
  262. float oB = (y + cy);
  263. gs_projection_push();
  264. gs_viewport_push();
  265. gs_set_viewport(vX, vY, vCX, vCY);
  266. gs_ortho(oL, oR, oT, oB, -100.0f, 100.0f);
  267. };
  268. auto resetRegion = [] ()
  269. {
  270. gs_viewport_pop();
  271. gs_projection_pop();
  272. };
  273. auto calcBaseSource = [&](size_t i)
  274. {
  275. switch (multiviewLayout) {
  276. case VERTICAL_LEFT:
  277. sourceX = halfCX;
  278. sourceY = (i / 2 ) * quarterCY;
  279. if (i % 2 != 0)
  280. sourceX = halfCX + quarterCX;
  281. break;
  282. case VERTICAL_RIGHT:
  283. sourceX = 0;
  284. sourceY = (i / 2 ) * quarterCY;
  285. if (i % 2 != 0)
  286. sourceX = quarterCX;
  287. break;
  288. case HORIZONTAL_BOTTOM:
  289. if (i < 4) {
  290. sourceX = (float(i) * quarterCX);
  291. sourceY = 0;
  292. } else {
  293. sourceX = (float(i - 4) * quarterCX);
  294. sourceY = quarterCY;
  295. }
  296. break;
  297. default: //HORIZONTAL_TOP:
  298. if (i < 4) {
  299. sourceX = (float(i) * quarterCX);
  300. sourceY = halfCY;
  301. } else {
  302. sourceX = (float(i - 4) * quarterCX);
  303. sourceY = halfCY + quarterCY;
  304. }
  305. }
  306. };
  307. auto calcPreviewProgram = [&](bool program)
  308. {
  309. switch (multiviewLayout) {
  310. case VERTICAL_LEFT:
  311. sourceX = 2.0f;
  312. sourceY = halfCY + 2.0f;
  313. labelX = offset;
  314. labelY = halfCY * 1.8f;
  315. if (program) {
  316. sourceY = 2.0f;
  317. labelY = halfCY * 0.8f;
  318. }
  319. break;
  320. case VERTICAL_RIGHT:
  321. sourceX = halfCX + 2.0f;
  322. sourceY = halfCY + 2.0f;
  323. labelX = halfCX + offset;
  324. labelY = halfCY * 1.8f;
  325. if (program) {
  326. sourceY = 2.0f;
  327. labelY = halfCY * 0.8f;
  328. }
  329. break;
  330. case HORIZONTAL_BOTTOM:
  331. sourceX = 2.0f;
  332. sourceY = halfCY + 2.0f;
  333. labelX = offset;
  334. labelY = halfCY * 1.8f;
  335. if (program) {
  336. sourceX = halfCX + 2.0f;
  337. labelX = halfCX + offset;
  338. }
  339. break;
  340. default: //HORIZONTAL_TOP:
  341. sourceX = 2.0f;
  342. sourceY = 2.0f;
  343. labelX = offset;
  344. labelY = halfCY * 0.8f;
  345. if (program) {
  346. sourceX = halfCX + 2.0f;
  347. labelX = halfCX + offset;
  348. }
  349. }
  350. };
  351. /* ----------------------------- */
  352. /* draw sources */
  353. gs_projection_push();
  354. gs_viewport_push();
  355. gs_set_viewport(x, y, targetCX * scale, targetCY * scale);
  356. gs_ortho(0.0f, targetCXF, 0.0f, targetCYF, -100.0f, 100.0f);
  357. for (size_t i = 0; i < 8; i++) {
  358. OBSSource src = OBSGetStrongRef(window->multiviewScenes[i]);
  359. obs_source *label = window->multiviewLabels[i + 2];
  360. if (!src)
  361. continue;
  362. if (!label)
  363. continue;
  364. calcBaseSource(i);
  365. qiX = sourceX + 4.0f;
  366. qiY = sourceY + 4.0f;
  367. /* ----------- */
  368. if (src == previewSrc || src == programSrc) {
  369. uint32_t colorVal = src == programSrc
  370. ? 0xFFFF0000
  371. : 0xFF00FF00;
  372. gs_matrix_push();
  373. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  374. drawBox(quarterCX, quarterCY, colorVal);
  375. gs_matrix_pop();
  376. gs_matrix_push();
  377. gs_matrix_translate3f(qiX, qiY, 0.0f);
  378. drawBox(qiCX, qiCY, 0xFF000000);
  379. gs_matrix_pop();
  380. }
  381. /* ----------- */
  382. gs_matrix_push();
  383. gs_matrix_translate3f(qiX, qiY, 0.0f);
  384. gs_matrix_scale3f(qiScaleX, qiScaleY, 1.0f);
  385. setRegion(qiX, qiY, qiCX, qiCY);
  386. obs_source_video_render(src);
  387. resetRegion();
  388. gs_effect_set_color(color, 0xFFFFFFFF);
  389. renderVB(solid, window->outerBox, targetCX, targetCY);
  390. gs_matrix_pop();
  391. /* ----------- */
  392. offset = labelOffset(label, quarterCX);
  393. cx = obs_source_get_width(label);
  394. cy = obs_source_get_height(label);
  395. gs_matrix_push();
  396. gs_matrix_translate3f(sourceX + offset,
  397. (quarterCY * 0.8f) + sourceY, 0.0f);
  398. drawBox(cx, cy + int(quarterCX * 0.015f), 0xD91F1F1F);
  399. obs_source_video_render(label);
  400. gs_matrix_pop();
  401. }
  402. gs_effect_set_color(color, 0xFFFFFFFF);
  403. /* ----------------------------- */
  404. /* draw preview */
  405. obs_source_t *previewLabel = window->multiviewLabels[0];
  406. offset = labelOffset(previewLabel, halfCX);
  407. calcPreviewProgram(false);
  408. gs_matrix_push();
  409. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  410. gs_matrix_scale3f(hiScaleX, hiScaleY, 1.0f);
  411. setRegion(sourceX, sourceY, hiCX, hiCY);
  412. if (studioMode) {
  413. obs_source_video_render(previewSrc);
  414. } else {
  415. obs_render_main_texture();
  416. }
  417. resetRegion();
  418. gs_matrix_pop();
  419. /* ----------- */
  420. gs_matrix_push();
  421. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  422. gs_matrix_scale3f(hiScaleX, hiScaleY, 1.0f);
  423. renderVB(solid, window->outerBox, targetCX, targetCY);
  424. renderVB(solid, window->innerBox, targetCX, targetCY);
  425. renderVB(solid, window->leftVLine, targetCX, targetCY);
  426. renderVB(solid, window->rightVLine, targetCX, targetCY);
  427. renderVB(solid, window->leftLine, targetCX, targetCY);
  428. renderVB(solid, window->topLine, targetCX, targetCY);
  429. renderVB(solid, window->rightLine, targetCX, targetCY);
  430. gs_matrix_pop();
  431. /* ----------- */
  432. cx = obs_source_get_width(previewLabel);
  433. cy = obs_source_get_height(previewLabel);
  434. gs_matrix_push();
  435. gs_matrix_translate3f(labelX, labelY, 0.0f);
  436. drawBox(cx, cy + int(halfCX * 0.015f), 0xD91F1F1F);
  437. obs_source_video_render(previewLabel);
  438. gs_matrix_pop();
  439. /* ----------------------------- */
  440. /* draw program */
  441. obs_source_t *programLabel = window->multiviewLabels[1];
  442. offset = labelOffset(programLabel, halfCX);
  443. calcPreviewProgram(true);
  444. gs_matrix_push();
  445. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  446. gs_matrix_scale3f(hiScaleX, hiScaleY, 1.0f);
  447. setRegion(sourceX, sourceY, hiCX, hiCY);
  448. obs_render_main_texture();
  449. resetRegion();
  450. gs_matrix_pop();
  451. /* ----------- */
  452. gs_matrix_push();
  453. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  454. gs_matrix_scale3f(hiScaleX, hiScaleY, 1.0f);
  455. renderVB(solid, window->outerBox, targetCX, targetCY);
  456. gs_matrix_pop();
  457. /* ----------- */
  458. cx = obs_source_get_width(programLabel);
  459. cy = obs_source_get_height(programLabel);
  460. gs_matrix_push();
  461. gs_matrix_translate3f(labelX, labelY, 0.0f);
  462. drawBox(cx, cy + int(halfCX * 0.015f), 0xD91F1F1F);
  463. obs_source_video_render(programLabel);
  464. gs_matrix_pop();
  465. /* ----------------------------- */
  466. gs_viewport_pop();
  467. gs_projection_pop();
  468. }
  469. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  470. {
  471. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  472. if (!window->ready)
  473. return;
  474. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  475. OBSSource source = window->source;
  476. uint32_t targetCX;
  477. uint32_t targetCY;
  478. int x, y;
  479. int newCX, newCY;
  480. float scale;
  481. if (source) {
  482. targetCX = std::max(obs_source_get_width(source), 1u);
  483. targetCY = std::max(obs_source_get_height(source), 1u);
  484. } else {
  485. struct obs_video_info ovi;
  486. obs_get_video_info(&ovi);
  487. targetCX = ovi.base_width;
  488. targetCY = ovi.base_height;
  489. }
  490. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  491. newCX = int(scale * float(targetCX));
  492. newCY = int(scale * float(targetCY));
  493. gs_viewport_push();
  494. gs_projection_push();
  495. gs_ortho(0.0f, float(targetCX), 0.0f, float(targetCY), -100.0f, 100.0f);
  496. gs_set_viewport(x, y, newCX, newCY);
  497. if (window->type == ProjectorType::Preview &&
  498. main->IsPreviewProgramMode()) {
  499. OBSSource curSource = main->GetCurrentSceneSource();
  500. if (source != curSource) {
  501. obs_source_dec_showing(source);
  502. obs_source_inc_showing(curSource);
  503. source = curSource;
  504. }
  505. }
  506. if (source) {
  507. obs_source_video_render(source);
  508. } else {
  509. obs_render_main_texture();
  510. }
  511. gs_projection_pop();
  512. gs_viewport_pop();
  513. }
  514. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  515. {
  516. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  517. window->deleteLater();
  518. UNUSED_PARAMETER(params);
  519. }
  520. static int getSourceByPosition(int x, int y)
  521. {
  522. struct obs_video_info ovi;
  523. obs_get_video_info(&ovi);
  524. float ratio = float(ovi.base_width) / float(ovi.base_height);
  525. QWidget *rec = QApplication::activeWindow();
  526. int cx = rec->width();
  527. int cy = rec->height();
  528. int minX = 0;
  529. int minY = 0;
  530. int maxX = cx;
  531. int maxY = cy;
  532. int halfX = cx / 2;
  533. int halfY = cy / 2;
  534. int pos = -1;
  535. switch (multiviewLayout) {
  536. case VERTICAL_LEFT:
  537. if (float(cx) / float(cy) > ratio) {
  538. int validX = cy * ratio;
  539. maxX = halfX + (validX / 2);
  540. } else {
  541. int validY = cx / ratio;
  542. minY = halfY - (validY / 2);
  543. maxY = halfY + (validY / 2);
  544. }
  545. minX = halfX;
  546. if (x < minX || x > maxX || y < minY || y > maxY)
  547. break;
  548. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  549. if (x > minX + ((maxX - minX) / 2))
  550. pos++;
  551. break;
  552. case VERTICAL_RIGHT:
  553. if (float(cx) / float(cy) > ratio) {
  554. int validX = cy * ratio;
  555. minX = halfX - (validX / 2);
  556. } else {
  557. int validY = cx / ratio;
  558. minY = halfY - (validY / 2);
  559. maxY = halfY + (validY / 2);
  560. }
  561. maxX = halfX;
  562. if (x < minX || x > maxX || y < minY || y > maxY)
  563. break;
  564. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  565. if (x > minX + ((maxX - minX) / 2))
  566. pos++;
  567. break;
  568. case HORIZONTAL_BOTTOM:
  569. if (float(cx) / float(cy) > ratio) {
  570. int validX = cy * ratio;
  571. minX = halfX - (validX / 2);
  572. maxX = halfX + (validX / 2);
  573. } else {
  574. int validY = cx / ratio;
  575. minY = halfY - (validY / 2);
  576. }
  577. maxY = halfY;
  578. if (x < minX || x > maxX || y < minY || y > maxY)
  579. break;
  580. pos = (x - minX) / ((maxX - minX) / 4);
  581. if (y > minY + ((maxY - minY) / 2))
  582. pos += 4;
  583. break;
  584. default: // HORIZONTAL_TOP
  585. if (float(cx) / float(cy) > ratio) {
  586. int validX = cy * ratio;
  587. minX = halfX - (validX / 2);
  588. maxX = halfX + (validX / 2);
  589. } else {
  590. int validY = cx / ratio;
  591. maxY = halfY + (validY / 2);
  592. }
  593. minY = halfY;
  594. if (x < minX || x > maxX || y < minY || y > maxY)
  595. break;
  596. pos = (x - minX) / ((maxX - minX) / 4);
  597. if (y > minY + ((maxY - minY) / 2))
  598. pos += 4;
  599. }
  600. return pos;
  601. }
  602. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  603. {
  604. OBSQTDisplay::mouseDoubleClickEvent(event);
  605. if (!config_get_bool(GetGlobalConfig(), "BasicWindow",
  606. "TransitionOnDoubleClick"))
  607. return;
  608. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  609. if (!main->IsPreviewProgramMode())
  610. return;
  611. if (event->button() == Qt::LeftButton) {
  612. int pos = getSourceByPosition(event->x(), event->y());
  613. if (pos < 0)
  614. return;
  615. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  616. if (!src)
  617. return;
  618. if (main->GetProgramSource() != src)
  619. main->TransitionToScene(src);
  620. }
  621. }
  622. void OBSProjector::mousePressEvent(QMouseEvent *event)
  623. {
  624. OBSQTDisplay::mousePressEvent(event);
  625. if (event->button() == Qt::RightButton) {
  626. QMenu popup(this);
  627. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  628. popup.exec(QCursor::pos());
  629. }
  630. if (event->button() == Qt::LeftButton) {
  631. int pos = getSourceByPosition(event->x(), event->y());
  632. if (pos < 0)
  633. return;
  634. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  635. if (!src)
  636. return;
  637. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  638. if (main->GetCurrentSceneSource() != src)
  639. main->SetCurrentScene(src, false);
  640. }
  641. }
  642. void OBSProjector::EscapeTriggered()
  643. {
  644. deleteLater();
  645. }
  646. void OBSProjector::UpdateMultiview()
  647. {
  648. for (OBSWeakSource &val : multiviewScenes)
  649. val = nullptr;
  650. for (OBSSource &val : multiviewLabels)
  651. val = nullptr;
  652. struct obs_video_info ovi;
  653. obs_get_video_info(&ovi);
  654. uint32_t h = ovi.base_height;
  655. struct obs_frontend_source_list scenes = {};
  656. obs_frontend_get_scenes(&scenes);
  657. int curIdx = 0;
  658. multiviewLabels[0] = CreateLabel(Str("StudioMode.Preview"), h / 2);
  659. multiviewLabels[1] = CreateLabel(Str("StudioMode.Program"), h / 2);
  660. for (size_t i = 0; i < scenes.sources.num && curIdx < 8; i++) {
  661. obs_source_t *src = scenes.sources.array[i];
  662. OBSData data = obs_source_get_private_settings(src);
  663. obs_data_release(data);
  664. obs_data_set_default_bool(data, "show_in_multiview", true);
  665. if (!obs_data_get_bool(data, "show_in_multiview"))
  666. continue;
  667. multiviewScenes[curIdx] = OBSGetWeakRef(src);
  668. obs_source_inc_showing(src);
  669. std::string name;
  670. name += std::to_string(curIdx + 1);
  671. name += " - ";
  672. name += obs_source_get_name(src);
  673. multiviewLabels[curIdx + 2] = CreateLabel(name.c_str(), h / 3);
  674. curIdx++;
  675. }
  676. obs_frontend_source_list_free(&scenes);
  677. const char *multiviewLayoutText = config_get_string(GetGlobalConfig(),
  678. "BasicWindow", "MultiviewLayout");
  679. if (astrcmpi(multiviewLayoutText, "horizontalbottom") == 0)
  680. multiviewLayout = HORIZONTAL_BOTTOM;
  681. else if (astrcmpi(multiviewLayoutText, "verticalleft") == 0)
  682. multiviewLayout = VERTICAL_LEFT;
  683. else if (astrcmpi(multiviewLayoutText, "verticalright") == 0)
  684. multiviewLayout = VERTICAL_RIGHT;
  685. else
  686. multiviewLayout = HORIZONTAL_TOP;
  687. }
  688. void OBSProjector::UpdateProjectorTitle(QString name)
  689. {
  690. projectorTitle = name;
  691. QString title = nullptr;
  692. switch (type) {
  693. case ProjectorType::Scene:
  694. title = QTStr("SceneWindow") + " - " + name;
  695. break;
  696. case ProjectorType::Source:
  697. title = QTStr("SourceWindow") + " - " + name;
  698. break;
  699. default:
  700. title = name;
  701. break;
  702. }
  703. setWindowTitle(title);
  704. }
  705. OBSSource OBSProjector::GetSource()
  706. {
  707. return source;
  708. }
  709. ProjectorType OBSProjector::GetProjectorType()
  710. {
  711. return type;
  712. }
  713. int OBSProjector::GetMonitor()
  714. {
  715. return savedMonitor;
  716. }
  717. void OBSProjector::UpdateMultiviewProjectors()
  718. {
  719. obs_enter_graphics();
  720. updatingMultiview = true;
  721. obs_leave_graphics();
  722. for (auto &projector : multiviewProjectors)
  723. projector->UpdateMultiview();
  724. obs_enter_graphics();
  725. updatingMultiview = false;
  726. obs_leave_graphics();
  727. }
  728. void OBSProjector::RenameProjector(QString oldName, QString newName)
  729. {
  730. for (auto &projector : windowedProjectors)
  731. if (projector->projectorTitle == oldName)
  732. projector->UpdateProjectorTitle(newName);
  733. }