OBSBasicProperties.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "OBSBasicProperties.hpp"
  15. #include <utility/display-helpers.hpp>
  16. #include <widgets/OBSBasic.hpp>
  17. #include <properties-view.hpp>
  18. #include <qt-wrappers.hpp>
  19. #include <vertical-scroll-area.hpp>
  20. #include <QPushButton>
  21. #ifdef _WIN32
  22. #define WIN32_LEAN_AND_MEAN 1
  23. #include <Windows.h>
  24. #endif
  25. #include "moc_OBSBasicProperties.cpp"
  26. using namespace std;
  27. static void CreateTransitionScene(OBSSource scene, const char *text, uint32_t color);
  28. OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
  29. : QDialog(parent),
  30. ui(new Ui::OBSBasicProperties),
  31. main(qobject_cast<OBSBasic *>(parent)),
  32. acceptClicked(false),
  33. source(source_),
  34. removedSignal(obs_source_get_signal_handler(source), "remove", OBSBasicProperties::SourceRemoved, this),
  35. renamedSignal(obs_source_get_signal_handler(source), "rename", OBSBasicProperties::SourceRenamed, this),
  36. oldSettings(obs_data_create())
  37. {
  38. int cx = (int)config_get_int(App()->GetAppConfig(), "PropertiesWindow", "cx");
  39. int cy = (int)config_get_int(App()->GetAppConfig(), "PropertiesWindow", "cy");
  40. enum obs_source_type type = obs_source_get_type(source);
  41. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  42. ui->setupUi(this);
  43. ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
  44. if (cx > 400 && cy > 400)
  45. resize(cx, cy);
  46. /* The OBSData constructor increments the reference once */
  47. obs_data_release(oldSettings);
  48. OBSDataAutoRelease nd_settings = obs_source_get_settings(source);
  49. obs_data_apply(oldSettings, nd_settings);
  50. view = new OBSPropertiesView(nd_settings.Get(), source, (PropertiesReloadCallback)obs_source_properties,
  51. (PropertiesUpdateCallback) nullptr, // No special handling required for undo/redo
  52. (PropertiesVisualUpdateCb)obs_source_update);
  53. view->setMinimumHeight(150);
  54. ui->propertiesLayout->addWidget(view);
  55. if (type == OBS_SOURCE_TYPE_TRANSITION) {
  56. connect(view, &OBSPropertiesView::PropertiesRefreshed, this, &OBSBasicProperties::AddPreviewButton);
  57. }
  58. view->show();
  59. installEventFilter(CreateShortcutFilter());
  60. const char *name = obs_source_get_name(source);
  61. setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
  62. obs_source_inc_showing(source);
  63. updatePropertiesSignal.Connect(obs_source_get_signal_handler(source), "update_properties",
  64. OBSBasicProperties::UpdateProperties, this);
  65. auto addDrawCallback = [this]() {
  66. obs_display_add_draw_callback(ui->preview->GetDisplay(), OBSBasicProperties::DrawPreview, this);
  67. };
  68. auto addTransitionDrawCallback = [this]() {
  69. obs_display_add_draw_callback(ui->preview->GetDisplay(), OBSBasicProperties::DrawTransitionPreview,
  70. this);
  71. };
  72. uint32_t caps = obs_source_get_output_flags(source);
  73. bool drawable_type = type == OBS_SOURCE_TYPE_INPUT || type == OBS_SOURCE_TYPE_SCENE;
  74. bool drawable_preview = (caps & OBS_SOURCE_VIDEO) != 0;
  75. if (drawable_preview && drawable_type) {
  76. ui->preview->show();
  77. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDrawCallback);
  78. } else if (type == OBS_SOURCE_TYPE_TRANSITION) {
  79. sourceA = obs_source_create_private("scene", "sourceA", nullptr);
  80. sourceB = obs_source_create_private("scene", "sourceB", nullptr);
  81. uint32_t colorA = 0xFFB26F52;
  82. uint32_t colorB = 0xFF6FB252;
  83. CreateTransitionScene(sourceA.Get(), "A", colorA);
  84. CreateTransitionScene(sourceB.Get(), "B", colorB);
  85. /**
  86. * The cloned source is made from scratch, rather than using
  87. * obs_source_duplicate, as the stinger transition would not
  88. * play correctly otherwise.
  89. */
  90. OBSDataAutoRelease settings = obs_source_get_settings(source);
  91. sourceClone = obs_source_create_private(obs_source_get_id(source), "clone", settings);
  92. obs_source_inc_active(sourceClone);
  93. obs_transition_set(sourceClone, sourceA);
  94. auto updateCallback = [=]() {
  95. OBSDataAutoRelease settings = obs_source_get_settings(source);
  96. obs_source_update(sourceClone, settings);
  97. obs_transition_clear(sourceClone);
  98. obs_transition_set(sourceClone, sourceA);
  99. obs_transition_force_stop(sourceClone);
  100. direction = true;
  101. };
  102. connect(view, &OBSPropertiesView::Changed, updateCallback);
  103. ui->preview->show();
  104. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addTransitionDrawCallback);
  105. } else {
  106. ui->preview->hide();
  107. }
  108. }
  109. OBSBasicProperties::~OBSBasicProperties()
  110. {
  111. if (sourceClone) {
  112. obs_source_dec_active(sourceClone);
  113. }
  114. obs_source_dec_showing(source);
  115. main->SaveProject();
  116. main->UpdateContextBarDeferred(true);
  117. }
  118. void OBSBasicProperties::AddPreviewButton()
  119. {
  120. QPushButton *playButton = new QPushButton(QTStr("PreviewTransition"), this);
  121. VScrollArea *area = view;
  122. area->widget()->layout()->addWidget(playButton);
  123. playButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  124. auto play = [=]() {
  125. OBSSource start;
  126. OBSSource end;
  127. if (direction) {
  128. start = sourceA;
  129. end = sourceB;
  130. } else {
  131. start = sourceB;
  132. end = sourceA;
  133. }
  134. obs_transition_set(sourceClone, start);
  135. obs_transition_start(sourceClone, OBS_TRANSITION_MODE_AUTO, main->GetTransitionDuration(), end);
  136. direction = !direction;
  137. start = nullptr;
  138. end = nullptr;
  139. };
  140. connect(playButton, &QPushButton::clicked, play);
  141. }
  142. static obs_source_t *CreateLabel(const char *name, size_t h)
  143. {
  144. OBSDataAutoRelease settings = obs_data_create();
  145. OBSDataAutoRelease font = obs_data_create();
  146. std::string text;
  147. text += " ";
  148. text += name;
  149. text += " ";
  150. #if defined(_WIN32)
  151. obs_data_set_string(font, "face", "Arial");
  152. #elif defined(__APPLE__)
  153. obs_data_set_string(font, "face", "Helvetica");
  154. #else
  155. obs_data_set_string(font, "face", "Monospace");
  156. #endif
  157. obs_data_set_int(font, "flags", 1); // Bold text
  158. obs_data_set_int(font, "size", min(int(h), 300));
  159. obs_data_set_obj(settings, "font", font);
  160. obs_data_set_string(settings, "text", text.c_str());
  161. obs_data_set_bool(settings, "outline", false);
  162. #ifdef _WIN32
  163. const char *text_source_id = "text_gdiplus";
  164. #else
  165. const char *text_source_id = "text_ft2_source";
  166. #endif
  167. obs_source_t *txtSource = obs_source_create_private(text_source_id, name, settings);
  168. return txtSource;
  169. }
  170. static void CreateTransitionScene(OBSSource scene, const char *text, uint32_t color)
  171. {
  172. OBSDataAutoRelease settings = obs_data_create();
  173. obs_data_set_int(settings, "width", obs_source_get_width(scene));
  174. obs_data_set_int(settings, "height", obs_source_get_height(scene));
  175. obs_data_set_int(settings, "color", color);
  176. OBSSourceAutoRelease colorBG = obs_source_create_private("color_source", "background", settings);
  177. obs_scene_add(obs_scene_from_source(scene), colorBG);
  178. OBSSourceAutoRelease label = CreateLabel(text, obs_source_get_height(scene));
  179. obs_sceneitem_t *item = obs_scene_add(obs_scene_from_source(scene), label);
  180. vec2 size;
  181. vec2_set(&size, obs_source_get_width(scene),
  182. #ifdef _WIN32
  183. obs_source_get_height(scene));
  184. #else
  185. obs_source_get_height(scene) * 0.8);
  186. #endif
  187. obs_sceneitem_set_bounds(item, &size);
  188. obs_sceneitem_set_bounds_type(item, OBS_BOUNDS_SCALE_INNER);
  189. }
  190. void OBSBasicProperties::SourceRemoved(void *data, calldata_t *)
  191. {
  192. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data), "close");
  193. }
  194. void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
  195. {
  196. const char *name = calldata_string(params, "new_name");
  197. QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));
  198. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data), "setWindowTitle", Q_ARG(QString, title));
  199. }
  200. void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
  201. {
  202. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data)->view, "ReloadProperties");
  203. }
  204. static bool ConfirmReset(QWidget *parent)
  205. {
  206. QMessageBox::StandardButton button;
  207. button = OBSMessageBox::question(parent, QTStr("ConfirmReset.Title"), QTStr("ConfirmReset.Text"),
  208. QMessageBox::Yes | QMessageBox::No);
  209. return button == QMessageBox::Yes;
  210. }
  211. void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
  212. {
  213. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  214. if (val == QDialogButtonBox::AcceptRole) {
  215. std::string scene_uuid = obs_source_get_uuid(main->GetCurrentSceneSource());
  216. auto undo_redo = [scene_uuid](const std::string &data) {
  217. OBSDataAutoRelease settings = obs_data_create_from_json(data.c_str());
  218. OBSSourceAutoRelease source =
  219. obs_get_source_by_uuid(obs_data_get_string(settings, "undo_uuid"));
  220. obs_source_reset_settings(source, settings);
  221. obs_source_update_properties(source);
  222. OBSSourceAutoRelease scene_source = obs_get_source_by_uuid(scene_uuid.c_str());
  223. OBSBasic::Get()->SetCurrentScene(scene_source.Get(), true);
  224. };
  225. OBSDataAutoRelease new_settings = obs_data_create();
  226. OBSDataAutoRelease curr_settings = obs_source_get_settings(source);
  227. obs_data_apply(new_settings, curr_settings);
  228. obs_data_set_string(new_settings, "undo_uuid", obs_source_get_uuid(source));
  229. obs_data_set_string(oldSettings, "undo_uuid", obs_source_get_uuid(source));
  230. std::string undo_data(obs_data_get_json(oldSettings));
  231. std::string redo_data(obs_data_get_json(new_settings));
  232. if (undo_data.compare(redo_data) != 0)
  233. main->undo_s.add_action(QTStr("Undo.Properties").arg(obs_source_get_name(source)), undo_redo,
  234. undo_redo, undo_data, redo_data);
  235. acceptClicked = true;
  236. close();
  237. if (view->DeferUpdate())
  238. view->UpdateSettings();
  239. } else if (val == QDialogButtonBox::RejectRole) {
  240. OBSDataAutoRelease settings = obs_source_get_settings(source);
  241. obs_data_clear(settings);
  242. if (view->DeferUpdate())
  243. obs_data_apply(settings, oldSettings);
  244. else
  245. obs_source_update(source, oldSettings);
  246. close();
  247. } else if (val == QDialogButtonBox::ResetRole) {
  248. if (!ConfirmReset(this))
  249. return;
  250. OBSDataAutoRelease settings = obs_source_get_settings(source);
  251. obs_data_clear(settings);
  252. if (!view->DeferUpdate())
  253. obs_source_update(source, nullptr);
  254. view->ReloadProperties();
  255. }
  256. }
  257. void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  258. {
  259. OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
  260. if (!window->source)
  261. return;
  262. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  263. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  264. int x, y;
  265. int newCX, newCY;
  266. float scale;
  267. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  268. newCX = int(scale * float(sourceCX));
  269. newCY = int(scale * float(sourceCY));
  270. gs_viewport_push();
  271. gs_projection_push();
  272. const bool previous = gs_set_linear_srgb(true);
  273. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
  274. gs_set_viewport(x, y, newCX, newCY);
  275. obs_source_video_render(window->source);
  276. gs_set_linear_srgb(previous);
  277. gs_projection_pop();
  278. gs_viewport_pop();
  279. }
  280. void OBSBasicProperties::DrawTransitionPreview(void *data, uint32_t cx, uint32_t cy)
  281. {
  282. OBSBasicProperties *window = static_cast<OBSBasicProperties *>(data);
  283. if (!window->sourceClone)
  284. return;
  285. uint32_t sourceCX = max(obs_source_get_width(window->sourceClone), 1u);
  286. uint32_t sourceCY = max(obs_source_get_height(window->sourceClone), 1u);
  287. int x, y;
  288. int newCX, newCY;
  289. float scale;
  290. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  291. newCX = int(scale * float(sourceCX));
  292. newCY = int(scale * float(sourceCY));
  293. gs_viewport_push();
  294. gs_projection_push();
  295. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
  296. gs_set_viewport(x, y, newCX, newCY);
  297. obs_source_video_render(window->sourceClone);
  298. gs_projection_pop();
  299. gs_viewport_pop();
  300. }
  301. void OBSBasicProperties::Cleanup()
  302. {
  303. config_set_int(App()->GetAppConfig(), "PropertiesWindow", "cx", width());
  304. config_set_int(App()->GetAppConfig(), "PropertiesWindow", "cy", height());
  305. obs_display_remove_draw_callback(ui->preview->GetDisplay(), OBSBasicProperties::DrawPreview, this);
  306. obs_display_remove_draw_callback(ui->preview->GetDisplay(), OBSBasicProperties::DrawTransitionPreview, this);
  307. }
  308. void OBSBasicProperties::reject()
  309. {
  310. if (!acceptClicked && (CheckSettings() != 0)) {
  311. if (!ConfirmQuit()) {
  312. return;
  313. }
  314. }
  315. Cleanup();
  316. done(0);
  317. }
  318. void OBSBasicProperties::closeEvent(QCloseEvent *event)
  319. {
  320. QDialog::closeEvent(event);
  321. if (event->isAccepted())
  322. Cleanup();
  323. }
  324. bool OBSBasicProperties::nativeEvent(const QByteArray &, void *message, qintptr *)
  325. {
  326. #ifdef _WIN32
  327. const MSG &msg = *static_cast<MSG *>(message);
  328. switch (msg.message) {
  329. case WM_MOVE:
  330. for (OBSQTDisplay *const display : findChildren<OBSQTDisplay *>()) {
  331. display->OnMove();
  332. }
  333. break;
  334. case WM_DISPLAYCHANGE:
  335. for (OBSQTDisplay *const display : findChildren<OBSQTDisplay *>()) {
  336. display->OnDisplayChange();
  337. }
  338. }
  339. #else
  340. UNUSED_PARAMETER(message);
  341. #endif
  342. return false;
  343. }
  344. void OBSBasicProperties::Init()
  345. {
  346. show();
  347. }
  348. int OBSBasicProperties::CheckSettings()
  349. {
  350. OBSDataAutoRelease currentSettings = obs_source_get_settings(source);
  351. const char *oldSettingsJson = obs_data_get_json(oldSettings);
  352. const char *currentSettingsJson = obs_data_get_json(currentSettings);
  353. return strcmp(currentSettingsJson, oldSettingsJson);
  354. }
  355. bool OBSBasicProperties::ConfirmQuit()
  356. {
  357. QMessageBox::StandardButton button;
  358. button = OBSMessageBox::question(this, QTStr("Basic.PropertiesWindow.ConfirmTitle"),
  359. QTStr("Basic.PropertiesWindow.Confirm"),
  360. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  361. switch (button) {
  362. case QMessageBox::Save:
  363. acceptClicked = true;
  364. if (view->DeferUpdate())
  365. view->UpdateSettings();
  366. // Do nothing because the settings are already updated
  367. break;
  368. case QMessageBox::Discard:
  369. obs_source_update(source, oldSettings);
  370. break;
  371. case QMessageBox::Cancel:
  372. return false;
  373. break;
  374. default:
  375. /* If somehow the dialog fails to show, just default to
  376. * saving the settings. */
  377. break;
  378. }
  379. return true;
  380. }