window-basic-properties.cpp 14 KB

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