OBSBasicProperties.cpp 14 KB

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