1
0

OBSBasic_StudioMode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #include "OBSProjector.hpp"
  18. #include <utility/display-helpers.hpp>
  19. #include <utility/QuickTransition.hpp>
  20. #include <qt-wrappers.hpp>
  21. #include <slider-ignorewheel.hpp>
  22. #include <QToolTip>
  23. void OBSBasic::CreateProgramDisplay()
  24. {
  25. program = new OBSQTDisplay();
  26. program->setContextMenuPolicy(Qt::CustomContextMenu);
  27. connect(program.data(), &QWidget::customContextMenuRequested, this, &OBSBasic::ProgramViewContextMenuRequested);
  28. auto displayResize = [this]() {
  29. struct obs_video_info ovi;
  30. if (obs_get_video_info(&ovi))
  31. ResizeProgram(ovi.base_width, ovi.base_height);
  32. };
  33. connect(program.data(), &OBSQTDisplay::DisplayResized, displayResize);
  34. auto addDisplay = [this](OBSQTDisplay *window) {
  35. obs_display_add_draw_callback(window->GetDisplay(), OBSBasic::RenderProgram, this);
  36. struct obs_video_info ovi;
  37. if (obs_get_video_info(&ovi))
  38. ResizeProgram(ovi.base_width, ovi.base_height);
  39. };
  40. connect(program.data(), &OBSQTDisplay::DisplayCreated, addDisplay);
  41. program->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  42. }
  43. #define T_BAR_PRECISION 1024
  44. #define T_BAR_PRECISION_F ((float)T_BAR_PRECISION)
  45. #define T_BAR_CLAMP (T_BAR_PRECISION / 10)
  46. void OBSBasic::CreateProgramOptions()
  47. {
  48. programOptions = new QWidget();
  49. QVBoxLayout *layout = new QVBoxLayout();
  50. layout->setSpacing(4);
  51. QPushButton *configTransitions = new QPushButton();
  52. configTransitions->setProperty("class", "icon-dots-vert");
  53. QHBoxLayout *mainButtonLayout = new QHBoxLayout();
  54. mainButtonLayout->setSpacing(2);
  55. transitionButton = new QPushButton(QTStr("Transition"));
  56. transitionButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  57. QHBoxLayout *quickTransitionsLayout = new QHBoxLayout();
  58. quickTransitionsLayout->setSpacing(2);
  59. QPushButton *addQuickTransition = new QPushButton();
  60. addQuickTransition->setProperty("class", "icon-plus");
  61. QLabel *quickTransitionsLabel = new QLabel(QTStr("QuickTransitions"));
  62. quickTransitionsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  63. quickTransitionsLayout->addWidget(quickTransitionsLabel);
  64. quickTransitionsLayout->addWidget(addQuickTransition);
  65. mainButtonLayout->addWidget(transitionButton);
  66. mainButtonLayout->addWidget(configTransitions);
  67. tBar = new SliderIgnoreClick(Qt::Horizontal);
  68. tBar->setMinimum(0);
  69. tBar->setMaximum(T_BAR_PRECISION - 1);
  70. tBar->setProperty("class", "slider-tbar");
  71. connect(tBar, &QSlider::valueChanged, this, &OBSBasic::TBarChanged);
  72. connect(tBar, &QSlider::sliderReleased, this, &OBSBasic::TBarReleased);
  73. layout->addStretch(0);
  74. layout->addLayout(mainButtonLayout);
  75. layout->addLayout(quickTransitionsLayout);
  76. layout->addWidget(tBar);
  77. layout->addStretch(0);
  78. programOptions->setLayout(layout);
  79. auto onAdd = [this]() {
  80. QScopedPointer<QMenu> menu(CreateTransitionMenu(this, nullptr));
  81. menu->exec(QCursor::pos());
  82. };
  83. auto onConfig = [this]() {
  84. QMenu menu(this);
  85. QAction *action;
  86. auto toggleEditProperties = [this]() {
  87. editPropertiesMode = !editPropertiesMode;
  88. OBSSource actualScene = OBSGetStrongRef(programScene);
  89. if (actualScene)
  90. TransitionToScene(actualScene, true);
  91. };
  92. auto toggleSwapScenesMode = [this]() {
  93. swapScenesMode = !swapScenesMode;
  94. };
  95. auto toggleSceneDuplication = [this]() {
  96. sceneDuplicationMode = !sceneDuplicationMode;
  97. OBSSource actualScene = OBSGetStrongRef(programScene);
  98. if (actualScene)
  99. TransitionToScene(actualScene, true);
  100. };
  101. auto showToolTip = [&]() {
  102. QAction *act = menu.activeAction();
  103. QToolTip::showText(QCursor::pos(), act->toolTip(), &menu, menu.actionGeometry(act));
  104. };
  105. action = menu.addAction(QTStr("QuickTransitions.DuplicateScene"));
  106. action->setToolTip(QTStr("QuickTransitions.DuplicateSceneTT"));
  107. action->setCheckable(true);
  108. action->setChecked(sceneDuplicationMode);
  109. connect(action, &QAction::triggered, toggleSceneDuplication);
  110. connect(action, &QAction::hovered, showToolTip);
  111. action = menu.addAction(QTStr("QuickTransitions.EditProperties"));
  112. action->setToolTip(QTStr("QuickTransitions.EditPropertiesTT"));
  113. action->setCheckable(true);
  114. action->setChecked(editPropertiesMode);
  115. action->setEnabled(sceneDuplicationMode);
  116. connect(action, &QAction::triggered, toggleEditProperties);
  117. connect(action, &QAction::hovered, showToolTip);
  118. action = menu.addAction(QTStr("QuickTransitions.SwapScenes"));
  119. action->setToolTip(QTStr("QuickTransitions.SwapScenesTT"));
  120. action->setCheckable(true);
  121. action->setChecked(swapScenesMode);
  122. connect(action, &QAction::triggered, toggleSwapScenesMode);
  123. connect(action, &QAction::hovered, showToolTip);
  124. menu.exec(QCursor::pos());
  125. };
  126. connect(transitionButton.data(), &QAbstractButton::clicked, this, &OBSBasic::TransitionClicked);
  127. connect(addQuickTransition, &QAbstractButton::clicked, onAdd);
  128. connect(configTransitions, &QAbstractButton::clicked, onConfig);
  129. }
  130. void OBSBasic::TogglePreviewProgramMode()
  131. {
  132. SetPreviewProgramMode(!IsPreviewProgramMode());
  133. }
  134. void OBSBasic::SetPreviewProgramMode(bool enabled)
  135. {
  136. if (IsPreviewProgramMode() == enabled)
  137. return;
  138. os_atomic_set_bool(&previewProgramMode, enabled);
  139. emit PreviewProgramModeChanged(enabled);
  140. if (IsPreviewProgramMode()) {
  141. if (!previewEnabled)
  142. EnablePreviewDisplay(true);
  143. CreateProgramDisplay();
  144. CreateProgramOptions();
  145. OBSScene curScene = GetCurrentScene();
  146. OBSSceneAutoRelease dup;
  147. if (sceneDuplicationMode) {
  148. dup = obs_scene_duplicate(curScene, obs_source_get_name(obs_scene_get_source(curScene)),
  149. editPropertiesMode ? OBS_SCENE_DUP_PRIVATE_COPY
  150. : OBS_SCENE_DUP_PRIVATE_REFS);
  151. } else {
  152. dup = std::move(OBSScene(curScene));
  153. }
  154. OBSSourceAutoRelease transition = obs_get_output_source(0);
  155. obs_source_t *dup_source = obs_scene_get_source(dup);
  156. obs_transition_set(transition, dup_source);
  157. if (curScene) {
  158. obs_source_t *source = obs_scene_get_source(curScene);
  159. obs_source_inc_showing(source);
  160. lastScene = OBSGetWeakRef(source);
  161. programScene = OBSGetWeakRef(source);
  162. }
  163. RefreshQuickTransitions();
  164. programLabel = new QLabel(QTStr("StudioMode.ProgramSceneLabel"), this);
  165. programLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  166. programLabel->setProperty("class", "label-preview-title");
  167. programWidget = new QWidget();
  168. programLayout = new QVBoxLayout();
  169. programLayout->setContentsMargins(0, 0, 0, 0);
  170. programLayout->setSpacing(0);
  171. programLayout->addWidget(programLabel);
  172. programLayout->addWidget(program);
  173. programWidget->setLayout(programLayout);
  174. ui->previewLayout->addWidget(programOptions);
  175. ui->previewLayout->addWidget(programWidget);
  176. ui->previewLayout->setAlignment(programOptions, Qt::AlignCenter);
  177. OnEvent(OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED);
  178. blog(LOG_INFO, "Switched to Preview/Program mode");
  179. blog(LOG_INFO, "-----------------------------"
  180. "-------------------");
  181. } else {
  182. OBSSource actualProgramScene = OBSGetStrongRef(programScene);
  183. if (!actualProgramScene)
  184. actualProgramScene = GetCurrentSceneSource();
  185. else
  186. SetCurrentScene(actualProgramScene, true);
  187. TransitionToScene(actualProgramScene, true);
  188. delete programOptions;
  189. delete program;
  190. delete programLabel;
  191. delete programWidget;
  192. if (lastScene) {
  193. OBSSource actualLastScene = OBSGetStrongRef(lastScene);
  194. if (actualLastScene)
  195. obs_source_dec_showing(actualLastScene);
  196. lastScene = nullptr;
  197. }
  198. programScene = nullptr;
  199. swapScene = nullptr;
  200. prevFTBSource = nullptr;
  201. for (QuickTransition &qt : quickTransitions)
  202. qt.button = nullptr;
  203. if (!previewEnabled)
  204. EnablePreviewDisplay(false);
  205. ui->transitions->setEnabled(true);
  206. tBarActive = false;
  207. OnEvent(OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED);
  208. blog(LOG_INFO, "Switched to regular Preview mode");
  209. blog(LOG_INFO, "-----------------------------"
  210. "-------------------");
  211. }
  212. ResetUI();
  213. UpdateTitleBar();
  214. }
  215. void OBSBasic::RenderProgram(void *data, uint32_t, uint32_t)
  216. {
  217. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DEFAULT, "RenderProgram");
  218. OBSBasic *window = static_cast<OBSBasic *>(data);
  219. obs_video_info ovi;
  220. obs_get_video_info(&ovi);
  221. window->programCX = int(window->programScale * float(ovi.base_width));
  222. window->programCY = int(window->programScale * float(ovi.base_height));
  223. gs_viewport_push();
  224. gs_projection_push();
  225. /* --------------------------------------- */
  226. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height), -100.0f, 100.0f);
  227. gs_set_viewport(window->programX, window->programY, window->programCX, window->programCY);
  228. obs_render_main_texture_src_color_only();
  229. gs_load_vertexbuffer(nullptr);
  230. /* --------------------------------------- */
  231. gs_projection_pop();
  232. gs_viewport_pop();
  233. GS_DEBUG_MARKER_END();
  234. }
  235. void OBSBasic::ResizeProgram(uint32_t cx, uint32_t cy)
  236. {
  237. QSize targetSize;
  238. /* resize program panel to fix to the top section of the window */
  239. targetSize = GetPixelSize(program);
  240. GetScaleAndCenterPos(int(cx), int(cy), targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  241. targetSize.height() - PREVIEW_EDGE_SIZE * 2, programX, programY, programScale);
  242. programX += float(PREVIEW_EDGE_SIZE);
  243. programY += float(PREVIEW_EDGE_SIZE);
  244. }
  245. void OBSBasic::UpdatePreviewProgramIndicators()
  246. {
  247. bool labels = previewProgramMode ? config_get_bool(App()->GetUserConfig(), "BasicWindow", "StudioModeLabels")
  248. : false;
  249. ui->previewLabel->setVisible(labels);
  250. if (programLabel)
  251. programLabel->setVisible(labels);
  252. if (!labels)
  253. return;
  254. QString preview =
  255. QTStr("StudioMode.PreviewSceneName").arg(QT_UTF8(obs_source_get_name(GetCurrentSceneSource())));
  256. QString program = QTStr("StudioMode.ProgramSceneName").arg(QT_UTF8(obs_source_get_name(GetProgramSource())));
  257. if (ui->previewLabel->text() != preview)
  258. ui->previewLabel->setText(preview);
  259. if (programLabel && programLabel->text() != program)
  260. programLabel->setText(program);
  261. }
  262. OBSSource OBSBasic::GetProgramSource()
  263. {
  264. return OBSGetStrongRef(programScene);
  265. }
  266. void OBSBasic::ProgramViewContextMenuRequested()
  267. {
  268. QMenu popup(this);
  269. QPointer<QMenu> studioProgramProjector;
  270. studioProgramProjector = new QMenu(QTStr("Projector.Open.Program"));
  271. AddProjectorMenuMonitors(studioProgramProjector, this, &OBSBasic::OpenStudioProgramProjector);
  272. studioProgramProjector->addSeparator();
  273. studioProgramProjector->addAction(QTStr("Projector.Window"), this, &OBSBasic::OpenStudioProgramWindow);
  274. popup.addMenu(studioProgramProjector);
  275. popup.addSeparator();
  276. popup.addAction(QTStr("Screenshot.StudioProgram"), this, &OBSBasic::ScreenshotProgram);
  277. popup.exec(QCursor::pos());
  278. }
  279. void OBSBasic::EnablePreviewProgram()
  280. {
  281. SetPreviewProgramMode(true);
  282. }
  283. void OBSBasic::DisablePreviewProgram()
  284. {
  285. SetPreviewProgramMode(false);
  286. }
  287. void OBSBasic::OpenStudioProgramProjector()
  288. {
  289. int monitor = sender()->property("monitor").toInt();
  290. OpenProjector(nullptr, monitor, ProjectorType::StudioProgram);
  291. }
  292. void OBSBasic::OpenStudioProgramWindow()
  293. {
  294. OpenProjector(nullptr, -1, ProjectorType::StudioProgram);
  295. }