OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
	: QDialog (parent),
	  ui      (new Ui::OBSBasicSourceSelect),
	  id      (id_)
{
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	ui->setupUi(this);

	ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);

	QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};

	QString text{placeHolderText};
	int i = 2;
	obs_source_t *source = nullptr;
	while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
		obs_source_release(source);
		text = QString("%1 %2").arg(placeHolderText).arg(i++);
	}

	ui->sourceName->setText(text);
	ui->sourceName->setFocus();	//Fixes deselect of text.
	ui->sourceName->selectAll();

	installEventFilter(CreateShortcutFilter());

	if (strcmp(id_, "scene") == 0) {
		OBSBasic *main = reinterpret_cast<OBSBasic*>(
				App()->GetMainWindow());
		OBSSource curSceneSource = main->GetCurrentSceneSource();

		ui->selectExisting->setChecked(true);
		ui->createNew->setChecked(false);
		ui->createNew->setEnabled(false);
		ui->sourceName->setEnabled(false);

		int count = main->ui->scenes->count();
		for (int i = 0; i < count; i++) {
			QListWidgetItem *item = main->ui->scenes->item(i);
			OBSScene scene = GetOBSRef<OBSScene>(item);
			OBSSource sceneSource = obs_scene_get_source(scene);

			if (curSceneSource == sceneSource)
				continue;

			const char *name = obs_source_get_name(sceneSource);
			ui->sourceList->addItem(QT_UTF8(name));
		}
	} else if (strcmp(id_, "group") == 0) {
		obs_enum_sources(EnumGroups, this);
	} else {
		obs_enum_sources(EnumSources, this);
	}
}
void OBSBasic::on_switchSceneBtn_clicked()
{
	tbliveLog.Log(lss_info, L"on_switchSceneBtn_clicked");

	OBSScene curScene = GetCurrentScene();
	obs_source_t * source = obs_scene_get_source(curScene);
	SetCurrentScene(source, false);

	TransitionToScene(source, false);

	m_streamingScene = source;
	SetTabStreamingStatus(QT_UTF8(obs_source_get_name(m_streamingScene)));

	CurrentTabSceneUpdateControls();
}
void OBSBasic::on_tabAddSceneBtn_clicked()
{
	QString format{ QTStr("Basic.Main.DefaultSceneName.Text") };

	QString sceneName = format.arg(m_curNewSceneNameIndex++);

	{
		std::string name = QT_TO_UTF8(sceneName);
		obs_scene_t *scene = obs_scene_create(name.c_str());

		obs_source_t *source = obs_scene_get_source(scene);
		AddScene(source);
		SetCurrentScene(source);
		obs_scene_release(scene);
	}
}
BiliSceneWidgetItem::BiliSceneWidgetItem(const char* name, obs_scene_t* pScene, obs_sceneitem_t* pSceneItem)
	: scene(pScene), sceneItem(pSceneItem), itemLabelFullText(name)
{
	setStyleSheet(
		"QWidget { background-color: transparent; } \n"
		"QLabel { background-color: transparent; } \n"
		"QCheckBox { background-color: transparent; } \n"
		);

	setMaximumHeight(28);
	setMinimumHeight(28);

	QHBoxLayout *itemLayout = new QHBoxLayout();
	itemLayout->setContentsMargins(10, 2, 3, 2);

	itemCheckBox = new QCheckBox();
	itemCheckBox->setObjectName("sceneItemWidgetCheckBox");
	itemLabel = new QLabel();

	//itemMoveUpBtn->setStyleSheet(
	//	"QPushButton { background-position: center; background-repeat: no-repeat; background-image: url(:/FucBtn/MoveUp); } \n"
	//	"QPushButton:hover { background-image: url(:/FucBtn/MoveUpHS); } \n"
	//	);
	//itemMoveDownBtn->setStyleSheet(
	//	"QPushButton { background-position: center; background-repeat: no-repeat; background-image: url(:/FucBtn/MoveDown); } \n"
	//	"QPushButton:hover { background-image: url(:/FucBtn/MoveDownHS); } \n"
	//	);

	itemCheckBox->setMaximumSize(24, 24);
	itemCheckBox->setMinimumSize(24, 24);

	itemLayout->addWidget(itemCheckBox);
	itemLayout->addWidget(itemLabel);

	//itemLabel->setText(name);
	limitLabelContext(itemLabel, itemLabelFullText, label_context_length_restriction, QStringLiteral("бн"));

	setLayout(itemLayout);

	sourceVisibleSignal.Connect(obs_source_get_signal_handler(obs_scene_get_source(scene)), "item_visible", &BiliSceneWidgetItem::OnItemVisibleChanged, this);
	QObject::connect(itemCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onCheckStateChanged(int)));
}
void OBSBasicTransform::SetScene(OBSScene scene)
{
	transformSignal.Disconnect();
	selectSignal.Disconnect();
	deselectSignal.Disconnect();
	removeSignal.Disconnect();

	if (scene) {
		OBSSource source = obs_scene_get_source(scene);
		signal_handler_t *signal = obs_source_get_signal_handler(source);

		transformSignal.Connect(signal, "item_transform",
				OBSSceneItemTransform, this);
		removeSignal.Connect(signal, "item_remove",
				OBSSceneItemRemoved, this);
		selectSignal.Connect(signal, "item_select",
				OBSSceneItemSelect, this);
		deselectSignal.Connect(signal, "item_deselect",
				OBSSceneItemDeselect, this);
	}
}
void BiLiOBSMainWid::LoadScene()
{
	obs_data_t* sceneData = BiliConfigFile::LoadSceneData();
	if (sceneData)
	{
		BiliSceneConfig::Set(sceneData);
		obs_data_release(sceneData);
	}

	//如果加载进来的场景不够3个,就补到三个
	std::string firstAddedScene;
	int sceneCount = 0;
	for (OBSSource& src : OBSEnumSources())
	{
		if (strcmp(obs_source_get_id(src), "scene") == 0)
		{
			++sceneCount;
		}
	}

	int nextSceneIndex = 1;
	for (; sceneCount < 3; ++sceneCount)
	{
	restartByNewName:
		std::string sceneName = tr("Scene %1").arg(nextSceneIndex).toUtf8().data();

		obs_source_t* existedSource = obs_get_source_by_name(sceneName.c_str());
		if (existedSource != 0)
		{
			obs_source_release(existedSource);
			++nextSceneIndex;
			goto restartByNewName;
		}

		if (firstAddedScene.empty())
		{
			firstAddedScene = sceneName;
		}

		obs_scene_t* scene = obs_scene_create(sceneName.c_str());
		obs_source_t* sceneSource = obs_scene_get_source(scene);
		obs_add_source(sceneSource);
		obs_scene_release(scene);
	}

	obs_source_t* currentOutputSource = obs_get_output_source(0);
	if (!currentOutputSource)
	{
		if (!firstAddedScene.empty())
		{
			currentOutputSource = obs_get_source_by_name(firstAddedScene.c_str());
			if (currentOutputSource)
			{
				obs_set_output_source(0, currentOutputSource);
				obs_source_release(currentOutputSource);
			}
		}
	}
	else
	{
		obs_source_release(currentOutputSource);
	}

	//更新列表控件
	sceneListWidgetOperator->NotifyCurrentSceneChanged();
}