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