1
0

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