window-projector.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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::fromTheme("obs",
  31. QIcon(":/res/images/obs.png")));
  32. UpdateProjectorTitle(projectorTitle);
  33. windowedProjectors.push_back(this);
  34. resize(480, 270);
  35. } else {
  36. setWindowFlags(Qt::FramelessWindowHint |
  37. Qt::X11BypassWindowManagerHint);
  38. QScreen *screen = QGuiApplication::screens()[savedMonitor];
  39. setGeometry(screen->geometry());
  40. QAction *action = new QAction(this);
  41. action->setShortcut(Qt::Key_Escape);
  42. addAction(action);
  43. connect(action, SIGNAL(triggered()), this,
  44. SLOT(EscapeTriggered()));
  45. }
  46. SetAlwaysOnTop(this, config_get_bool(GetGlobalConfig(),
  47. "BasicWindow", "ProjectorAlwaysOnTop"));
  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 && type != ProjectorType::Multiview) {
  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. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  496. window->ppiCY, backgroundColor);
  497. // Scale and Draw the program
  498. gs_matrix_push();
  499. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  500. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  501. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  502. window->ppiCY);
  503. obs_render_main_texture();
  504. endRegion();
  505. gs_matrix_pop();
  506. /* ----------- */
  507. // Draw the Label
  508. if (drawLabel) {
  509. gs_matrix_push();
  510. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  511. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  512. drawBox(obs_source_get_width(programLabel),
  513. obs_source_get_height(programLabel) +
  514. int(window->pvwprgCX * 0.015f), labelColor);
  515. obs_source_video_render(programLabel);
  516. gs_matrix_pop();
  517. }
  518. // Region for future usage with aditional info.
  519. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  520. // Just paint the background for now
  521. paintAreaWithColor(window->thickness, window->thickness,
  522. window->siCX, window->siCY * 2 +
  523. window->thicknessx2, backgroundColor);
  524. paintAreaWithColor(window->thickness + 2.5 * (
  525. window->thicknessx2 + window->ppiCX),
  526. window->thickness, window->siCX,
  527. window->siCY * 2 + window->thicknessx2,
  528. backgroundColor);
  529. }
  530. endRegion();
  531. }
  532. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  533. {
  534. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  535. if (!window->ready)
  536. return;
  537. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  538. OBSSource source = window->source;
  539. uint32_t targetCX;
  540. uint32_t targetCY;
  541. int x, y;
  542. int newCX, newCY;
  543. float scale;
  544. if (source) {
  545. targetCX = std::max(obs_source_get_width(source), 1u);
  546. targetCY = std::max(obs_source_get_height(source), 1u);
  547. } else {
  548. struct obs_video_info ovi;
  549. obs_get_video_info(&ovi);
  550. targetCX = ovi.base_width;
  551. targetCY = ovi.base_height;
  552. }
  553. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  554. newCX = int(scale * float(targetCX));
  555. newCY = int(scale * float(targetCY));
  556. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  557. float(targetCY));
  558. if (window->type == ProjectorType::Preview &&
  559. main->IsPreviewProgramMode()) {
  560. OBSSource curSource = main->GetCurrentSceneSource();
  561. if (source != curSource) {
  562. obs_source_dec_showing(source);
  563. obs_source_inc_showing(curSource);
  564. source = curSource;
  565. window->source = source;
  566. }
  567. }
  568. if (source)
  569. obs_source_video_render(source);
  570. else
  571. obs_render_main_texture();
  572. endRegion();
  573. }
  574. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  575. {
  576. OBSProjector *window = reinterpret_cast<OBSProjector*>(data);
  577. window->deleteLater();
  578. UNUSED_PARAMETER(params);
  579. }
  580. static int getSourceByPosition(int x, int y, float ratio)
  581. {
  582. int pos = -1;
  583. QWidget *rec = QApplication::activeWindow();
  584. if (!rec)
  585. return pos;
  586. int cx = rec->width();
  587. int cy = rec->height();
  588. int minX = 0;
  589. int minY = 0;
  590. int maxX = cx;
  591. int maxY = cy;
  592. switch (multiviewLayout) {
  593. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  594. if (float(cx) / float(cy) > ratio) {
  595. int validX = cy * ratio;
  596. minX = (cx / 2) - (validX / 2);
  597. maxX = (cx / 2) + (validX / 2);
  598. minY = cy / 3;
  599. } else {
  600. int validY = cx / ratio;
  601. maxY = (cy / 2) + (validY / 2);
  602. minY = (cy / 2) - (validY / 6);
  603. }
  604. if (x < minX || x > maxX || y < minY || y > maxY)
  605. break;
  606. pos = (x - minX) / ((maxX - minX) / 6);
  607. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  608. break;
  609. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  610. if (float(cx) / float(cy) > ratio) {
  611. int validX = cy * ratio;
  612. maxX = (cx / 2) + (validX / 2);
  613. } else {
  614. int validY = cx / ratio;
  615. minY = (cy / 2) - (validY / 2);
  616. maxY = (cy / 2) + (validY / 2);
  617. }
  618. minX = cx / 2;
  619. if (x < minX || x > maxX || y < minY || y > maxY)
  620. break;
  621. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  622. if (x > minX + ((maxX - minX) / 2))
  623. pos++;
  624. break;
  625. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  626. if (float(cx) / float(cy) > ratio) {
  627. int validX = cy * ratio;
  628. minX = (cx / 2) - (validX / 2);
  629. } else {
  630. int validY = cx / ratio;
  631. minY = (cy / 2) - (validY / 2);
  632. maxY = (cy / 2) + (validY / 2);
  633. }
  634. maxX = (cx / 2);
  635. if (x < minX || x > maxX || y < minY || y > maxY)
  636. break;
  637. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  638. if (x > minX + ((maxX - minX) / 2))
  639. pos++;
  640. break;
  641. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  642. if (float(cx) / float(cy) > ratio) {
  643. int validX = cy * ratio;
  644. minX = (cx / 2) - (validX / 2);
  645. maxX = (cx / 2) + (validX / 2);
  646. } else {
  647. int validY = cx / ratio;
  648. minY = (cy / 2) - (validY / 2);
  649. }
  650. maxY = (cy / 2);
  651. if (x < minX || x > maxX || y < minY || y > maxY)
  652. break;
  653. pos = (x - minX) / ((maxX - minX) / 4);
  654. if (y > minY + ((maxY - minY) / 2))
  655. pos += 4;
  656. break;
  657. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  658. if (float(cx) / float(cy) > ratio) {
  659. int validX = cy * ratio;
  660. minX = (cx / 2) - (validX / 2);
  661. maxX = (cx / 2) + (validX / 2);
  662. } else {
  663. int validY = cx / ratio;
  664. maxY = (cy / 2) + (validY / 2);
  665. }
  666. minY = (cy / 2);
  667. if (x < minX || x > maxX || y < minY || y > maxY)
  668. break;
  669. pos = (x - minX) / ((maxX - minX) / 4);
  670. if (y > minY + ((maxY - minY) / 2))
  671. pos += 4;
  672. }
  673. return pos;
  674. }
  675. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  676. {
  677. OBSQTDisplay::mouseDoubleClickEvent(event);
  678. if (!mouseSwitching)
  679. return;
  680. if (!transitionOnDoubleClick)
  681. return;
  682. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  683. if (!main->IsPreviewProgramMode())
  684. return;
  685. if (event->button() == Qt::LeftButton) {
  686. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  687. if (pos < 0 || pos >= (int)numSrcs)
  688. return;
  689. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  690. if (!src)
  691. return;
  692. if (main->GetProgramSource() != src)
  693. main->TransitionToScene(src);
  694. }
  695. }
  696. void OBSProjector::mousePressEvent(QMouseEvent *event)
  697. {
  698. OBSQTDisplay::mousePressEvent(event);
  699. if (event->button() == Qt::RightButton) {
  700. QMenu popup(this);
  701. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  702. popup.exec(QCursor::pos());
  703. }
  704. if (!mouseSwitching)
  705. return;
  706. if (event->button() == Qt::LeftButton) {
  707. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  708. if (pos < 0 || pos >= (int)numSrcs)
  709. return;
  710. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  711. if (!src)
  712. return;
  713. OBSBasic *main = (OBSBasic*)obs_frontend_get_main_window();
  714. if (main->GetCurrentSceneSource() != src)
  715. main->SetCurrentScene(src, false);
  716. }
  717. }
  718. void OBSProjector::EscapeTriggered()
  719. {
  720. deleteLater();
  721. }
  722. void OBSProjector::UpdateMultiview()
  723. {
  724. multiviewScenes.clear();
  725. multiviewLabels.clear();
  726. struct obs_video_info ovi;
  727. obs_get_video_info(&ovi);
  728. uint32_t w = ovi.base_width;
  729. uint32_t h = ovi.base_height;
  730. fw = float(w);
  731. fh = float(h);
  732. ratio = fw / fh;
  733. struct obs_frontend_source_list scenes = {};
  734. obs_frontend_get_scenes(&scenes);
  735. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Preview"),
  736. h / 2));
  737. multiviewLabels.emplace_back(CreateLabel(Str("StudioMode.Program"),
  738. h / 2));
  739. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  740. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  741. drawLabel = config_get_bool(GetGlobalConfig(),
  742. "BasicWindow", "MultiviewDrawNames");
  743. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  744. "MultiviewDrawAreas");
  745. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  746. "MultiviewMouseSwitch");
  747. transitionOnDoubleClick = config_get_bool(GetGlobalConfig(),
  748. "BasicWindow", "TransitionOnDoubleClick");
  749. switch(multiviewLayout) {
  750. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  751. pvwprgCX = fw / 3;
  752. pvwprgCY = fh / 3;
  753. maxSrcs = 24;
  754. break;
  755. default:
  756. pvwprgCX = fw / 2;
  757. pvwprgCY = fh / 2;
  758. maxSrcs = 8;
  759. }
  760. ppiCX = pvwprgCX - thicknessx2;
  761. ppiCY = pvwprgCY - thicknessx2;
  762. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  763. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  764. scenesCX = pvwprgCX / 2;
  765. scenesCY = pvwprgCY / 2;
  766. siCX = scenesCX - thicknessx2;
  767. siCY = scenesCY - thicknessx2;
  768. siScaleX = (scenesCX - thicknessx2) / fw;
  769. siScaleY = (scenesCY - thicknessx2) / fh;
  770. numSrcs = 0;
  771. size_t i = 0;
  772. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  773. obs_source_t *src = scenes.sources.array[i++];
  774. OBSData data = obs_source_get_private_settings(src);
  775. obs_data_release(data);
  776. obs_data_set_default_bool(data, "show_in_multiview", true);
  777. if (!obs_data_get_bool(data, "show_in_multiview"))
  778. continue;
  779. // We have a displayable source.
  780. numSrcs++;
  781. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  782. obs_source_inc_showing(src);
  783. std::string name = std::to_string(numSrcs) + " - " +
  784. obs_source_get_name(src);
  785. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  786. }
  787. obs_frontend_source_list_free(&scenes);
  788. }
  789. void OBSProjector::UpdateProjectorTitle(QString name)
  790. {
  791. projectorTitle = name;
  792. QString title = nullptr;
  793. switch (type) {
  794. case ProjectorType::Scene:
  795. title = QTStr("SceneWindow") + " - " + name;
  796. break;
  797. case ProjectorType::Source:
  798. title = QTStr("SourceWindow") + " - " + name;
  799. break;
  800. default:
  801. title = name;
  802. break;
  803. }
  804. setWindowTitle(title);
  805. }
  806. OBSSource OBSProjector::GetSource()
  807. {
  808. return source;
  809. }
  810. ProjectorType OBSProjector::GetProjectorType()
  811. {
  812. return type;
  813. }
  814. int OBSProjector::GetMonitor()
  815. {
  816. return savedMonitor;
  817. }
  818. void OBSProjector::UpdateMultiviewProjectors()
  819. {
  820. obs_enter_graphics();
  821. updatingMultiview = true;
  822. obs_leave_graphics();
  823. for (auto &projector : multiviewProjectors)
  824. projector->UpdateMultiview();
  825. obs_enter_graphics();
  826. updatingMultiview = false;
  827. obs_leave_graphics();
  828. }
  829. void OBSProjector::RenameProjector(QString oldName, QString newName)
  830. {
  831. for (auto &projector : windowedProjectors)
  832. if (projector->projectorTitle == oldName)
  833. projector->UpdateProjectorTitle(newName);
  834. }