Add transition selection widget

This commit is contained in:
WarmUpTill 2021-09-30 21:44:51 +02:00 committed by WarmUpTill
parent d3220c40dc
commit 588f90f0b1
3 changed files with 219 additions and 0 deletions

View File

@ -252,6 +252,7 @@ set(advanced-scene-switcher_HEADERS
src/headers/hotkey.hpp
src/headers/scene-selection.hpp
src/headers/screenshot-helper.hpp
src/headers/transition-selection.hpp
src/headers/name-dialog.hpp
src/headers/duration-control.hpp
src/headers/file-selection.hpp
@ -340,6 +341,7 @@ set(advanced-scene-switcher_SOURCES
src/curl-helper.cpp
src/scene-selection.cpp
src/screenshot-helper.cpp
src/transition-selection.cpp
src/name-dialog.cpp
src/duration-control.cpp
src/section.cpp

View File

@ -0,0 +1,46 @@
#pragma once
#include "utility.hpp"
#include <QComboBox>
enum class TransitionSelectionType {
TRANSITION,
CURRENT,
ANY,
};
class TransitionSelection {
public:
void Save(obs_data_t *obj, const char *name = "transition",
const char *typeName = "transitionType");
void Load(obs_data_t *obj, const char *name = "transition",
const char *typeName = "transitionType");
TransitionSelectionType GetType() { return _type; }
OBSWeakSource GetTransition();
std::string ToString();
private:
OBSWeakSource _transition;
TransitionSelectionType _type = TransitionSelectionType::TRANSITION;
friend class TransitionSelectionWidget;
};
class TransitionSelectionWidget : public QComboBox {
Q_OBJECT
public:
TransitionSelectionWidget(QWidget *parent, bool current = true,
bool any = false);
void SetTransition(TransitionSelection &);
void Repopulate(bool current, bool any);
signals:
void TransitionChanged(const TransitionSelection &);
private slots:
void SelectionChanged(const QString &name);
private:
bool IsCurrentTransitionSelected(const QString &name);
bool IsAnyTransitionSelected(const QString &name);
};

View File

@ -0,0 +1,171 @@
#include "headers/transition-selection.hpp"
#include "headers/advanced-scene-switcher.hpp"
void TransitionSelection::Save(obs_data_t *obj, const char *name,
const char *typeName)
{
obs_data_set_int(obj, typeName, static_cast<int>(_type));
switch (_type) {
case TransitionSelectionType::TRANSITION:
obs_data_set_string(obj, name,
GetWeakSourceName(_transition).c_str());
break;
default:
break;
}
}
void TransitionSelection::Load(obs_data_t *obj, const char *name,
const char *typeName)
{
_type = static_cast<TransitionSelectionType>(
obs_data_get_int(obj, typeName));
auto target = obs_data_get_string(obj, name);
switch (_type) {
case TransitionSelectionType::TRANSITION:
_transition = GetWeakTransitionByName(target);
break;
default:
break;
}
}
OBSWeakSource TransitionSelection::GetTransition()
{
switch (_type) {
case TransitionSelectionType::TRANSITION:
return _transition;
case TransitionSelectionType::CURRENT: {
auto source = obs_frontend_get_current_transition();
auto weakSource = obs_source_get_weak_source(source);
obs_weak_source_release(weakSource);
obs_source_release(source);
return weakSource;
}
default:
break;
}
return nullptr;
}
std::string TransitionSelection::ToString()
{
switch (_type) {
case TransitionSelectionType::TRANSITION:
return GetWeakSourceName(_transition);
case TransitionSelectionType::CURRENT:
return obs_module_text("AdvSceneSwitcher.currentTransition");
case TransitionSelectionType::ANY:
return obs_module_text("AdvSceneSwitcher.anyTransition");
default:
break;
}
return "";
}
TransitionSelectionWidget::TransitionSelectionWidget(QWidget *parent,
bool current, bool any)
: QComboBox(parent)
{
// For the rare occasion of a name conflict with current / previous
setDuplicatesEnabled(true);
populateTransitionSelection(this, current, any);
QWidget::connect(this, SIGNAL(currentTextChanged(const QString &)),
this, SLOT(SelectionChanged(const QString &)));
}
void TransitionSelectionWidget::SetTransition(TransitionSelection &t)
{
// Order of entries
// 1. Any transition
// 2. Current transition
// 3. Previous transition
// 4. Transitions
int idx;
switch (t.GetType()) {
case TransitionSelectionType::TRANSITION:
setCurrentText(QString::fromStdString(t.ToString()));
break;
case TransitionSelectionType::CURRENT:
idx = findText(QString::fromStdString(
obs_module_text("AdvSceneSwitcher.currentTransition")));
if (idx != -1) {
setCurrentIndex(idx);
}
break;
case TransitionSelectionType::ANY:
idx = findText(QString::fromStdString(
obs_module_text("AdvSceneSwitcher.anyTransition")));
if (idx != -1) {
setCurrentIndex(idx);
}
break;
default:
setCurrentIndex(0);
break;
}
}
void TransitionSelectionWidget::Repopulate(bool current, bool any)
{
this->clear();
populateTransitionSelection(this, current, any);
setCurrentIndex(0);
TransitionSelection t;
emit TransitionChanged(t);
}
static bool isFirstEntry(QComboBox *l, QString name, int idx)
{
for (auto i = l->count() - 1; i >= 0; i--) {
if (l->itemText(i) == name) {
return idx == i;
}
}
// If entry cannot be found we dont want the selection to be empty
return false;
}
bool TransitionSelectionWidget::IsCurrentTransitionSelected(const QString &name)
{
if (name == QString::fromStdString((obs_module_text(
"AdvSceneSwitcher.currentTransition")))) {
return isFirstEntry(this, name, currentIndex());
}
return false;
}
bool TransitionSelectionWidget::IsAnyTransitionSelected(const QString &name)
{
if (name == QString::fromStdString((obs_module_text(
"AdvSceneSwitcher.anyTransition")))) {
return isFirstEntry(this, name, currentIndex());
}
return false;
}
void TransitionSelectionWidget::SelectionChanged(const QString &name)
{
TransitionSelection t;
auto transition = GetWeakTransitionByQString(name);
if (transition) {
t._type = TransitionSelectionType::TRANSITION;
t._transition = transition;
}
if (!transition) {
if (IsCurrentTransitionSelected(name)) {
t._type = TransitionSelectionType::CURRENT;
}
if (IsAnyTransitionSelected(name)) {
t._type = TransitionSelectionType::ANY;
}
}
emit TransitionChanged(t);
}