window-projector.cpp 26 KB

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