window-basic-properties.cpp 15 KB

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