window-projector.cpp 26 KB

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