1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
#include "transform-setting.hpp"
#include "json-helpers.hpp"
#include "obs-module-helper.hpp"
#include "math-helpers.hpp"
#include "scene-item-transform-helpers.hpp"
#include <nlohmann/json.hpp>
#include <QLayout>
Q_DECLARE_METATYPE(advss::TransformSetting);
namespace advss {
TransformSetting::TransformSetting(const std::string &id,
const std::string &nestedId)
: _id(id),
_nestedId(nestedId)
{
}
bool TransformSetting::Save(obs_data_t *obj) const
{
OBSDataAutoRelease data = obs_data_create();
obs_data_set_string(data, "id", _id.c_str());
obs_data_set_string(data, "nestedId", _nestedId.c_str());
obs_data_set_obj(obj, "transformSetting", data);
return true;
}
bool TransformSetting::Load(obs_data_t *obj)
{
OBSDataAutoRelease data = obs_data_get_obj(obj, "transformSetting");
_id = obs_data_get_string(data, "id");
_nestedId = obs_data_get_string(data, "nestedId");
return true;
}
bool TransformSetting::operator==(const TransformSetting &other) const
{
return _id == other._id && _nestedId == other._nestedId;
}
static void
getSoruceTransformSettingsHelper(std::vector<TransformSetting> &settings,
const std::string &jsonString,
const std::string &nestedId = "")
{
try {
auto transform = nlohmann::json::parse(jsonString);
for (const auto &[key, value] : transform.items()) {
if (value.is_object() && nestedId.empty()) {
getSoruceTransformSettingsHelper(
settings, value.dump(), key);
} else {
settings.emplace_back(key, nestedId);
}
}
} catch (const nlohmann::json::exception &) {
return;
}
}
std::vector<TransformSetting> GetSoruceTransformSettings(obs_scene_item *source)
{
std::vector<TransformSetting> settings;
auto jsonString = GetSceneItemTransform(source);
getSoruceTransformSettingsHelper(settings, jsonString);
return settings;
}
std::optional<std::string>
GetTransformSettingValue(obs_scene_item *source,
const TransformSetting &setting)
{
auto jsonString = GetSceneItemTransform(source);
if (setting.GetNestedID().empty()) {
return GetJsonField(jsonString, setting.GetID());
}
auto nestedJsonString = GetJsonField(jsonString, setting.GetNestedID());
if (nestedJsonString.has_value()) {
return GetJsonField(*nestedJsonString, setting.GetID());
}
return {};
}
template<class T>
static void logConversionError(T, const char *func, const char *target,
const char *value)
{
if (std::is_same<T, std::invalid_argument>::value) {
blog(LOG_WARNING, "%s invalid %s value (%s)", func, target,
value);
} else {
blog(LOG_WARNING, "%s value out of range for %s (%s)", func,
target, value);
}
}
static void handleCrop(struct obs_sceneitem_crop &crop, const std::string &id,
const std::string &value)
{
int val = 0;
try {
val = std::stoi(value);
} catch (const std::invalid_argument &e) {
logConversionError(e, __func__, id.c_str(), value.c_str());
return;
} catch (const std::out_of_range &e) {
logConversionError(e, __func__, id.c_str(), value.c_str());
return;
}
if (id == "left") {
crop.left = val;
} else if (id == "top") {
crop.top = val;
} else if (id == "right") {
crop.right = val;
} else if (id == "bottom") {
crop.bottom = val;
}
}
static void handlePosOrScaleOrBounds(struct obs_transform_info &info,
const TransformSetting &setting,
const std::string &value)
{
float val = 0;
try {
val = std::stof(value);
} catch (const std::invalid_argument &e) {
logConversionError(
e, __func__,
(setting.GetID() + "-" + setting.GetNestedID()).c_str(),
value.c_str());
return;
} catch (const std::out_of_range &e) {
logConversionError(
e, __func__,
(setting.GetID() + "-" + setting.GetNestedID()).c_str(),
value.c_str());
return;
}
struct vec2 *target = nullptr;
if (setting.GetNestedID() == "scale") {
target = &info.scale;
} else if (setting.GetNestedID() == "bounds") {
target = &info.bounds;
} else if (setting.GetNestedID() == "pos") {
target = &info.pos;
}
if (setting.GetID() == "x") {
target->x = val;
} else if (setting.GetID() == "y") {
target->y = val;
}
}
static void handleRot(struct obs_transform_info &info, const std::string &value)
{
float val = 0;
try {
val = std::stof(value);
} catch (const std::invalid_argument &e) {
logConversionError(e, __func__, "rot", value.c_str());
return;
} catch (const std::out_of_range &e) {
logConversionError(e, __func__, "rot", value.c_str());
return;
}
info.rot = val;
}
void SetTransformSetting(obs_scene_item *source,
const TransformSetting &setting,
const std::string &value)
{
auto id = setting.GetID();
struct obs_transform_info info = {};
struct obs_sceneitem_crop crop = {};
#if (LIBOBS_API_VER >= MAKE_SEMANTIC_VERSION(30, 1, 0))
obs_sceneitem_get_info2(source, &info);
#else
obs_sceneitem_get_info(source, &info);
#endif
obs_sceneitem_get_crop(source, &crop);
if (id == "alignment") {
try {
auto val = std::stoul(value);
info.alignment = val;
} catch (const std::invalid_argument &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
} catch (const std::out_of_range &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
}
} else if (id == "left" || id == "top" || id == "right" ||
id == "bottom") {
handleCrop(crop, id, value);
} else if (id == "bounds_alignment") {
uint32_t val = 0;
try {
val = std::stoul(value);
info.bounds_alignment = val;
} catch (const std::invalid_argument &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
} catch (const std::out_of_range &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
}
} else if (id == "bounds_type") {
try {
obs_bounds_type val =
static_cast<obs_bounds_type>(std::stoul(value));
info.bounds_type = val;
} catch (const std::invalid_argument &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
} catch (const std::out_of_range &e) {
logConversionError(e, __func__, id.c_str(),
value.c_str());
}
} else if (id == "x" || id == "y") {
handlePosOrScaleOrBounds(info, setting, value);
} else if (id == "width" || id == "height") {
float val = 0;
try {
val = std::stof(value);
} catch (const std::invalid_argument &e) {
logConversionError(
e, __func__,
(setting.GetID() + "-" + setting.GetNestedID())
.c_str(),
value.c_str());
return;
} catch (const std::out_of_range &e) {
logConversionError(
e, __func__,
(setting.GetID() + "-" + setting.GetNestedID())
.c_str(),
value.c_str());
return;
}
auto source2 = obs_sceneitem_get_source(source);
if (setting.GetID() == "width") {
val = val / obs_source_get_width(source2);
handlePosOrScaleOrBounds(info,
TransformSetting("x", "scale"),
std::to_string(val));
} else if (setting.GetID() == "height") {
val = val / obs_source_get_height(source2);
handlePosOrScaleOrBounds(info,
TransformSetting("y", "scale"),
std::to_string(val));
}
} else if (id == "rot") {
handleRot(info, value);
}
obs_sceneitem_defer_update_begin(source);
#if (LIBOBS_API_VER >= MAKE_SEMANTIC_VERSION(30, 1, 0))
obs_sceneitem_set_info2(source, &info);
#else
obs_sceneitem_set_info(source, &info);
#endif
obs_sceneitem_set_crop(source, &crop);
obs_sceneitem_defer_update_end(source);
obs_sceneitem_force_update_transform(source);
}
TransformSettingSelection::TransformSettingSelection(QWidget *parent)
: QWidget(parent),
_settings(new FilterComboBox(
this, obs_module_text("AdvSceneSwitcher.setting.select")))
{
_settings->setSizeAdjustPolicy(QComboBox::AdjustToContents);
_settings->setMaximumWidth(350);
QWidget::connect(_settings, SIGNAL(currentIndexChanged(int)), this,
SLOT(SelectionIdxChanged(int)));
auto layout = new QHBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_settings);
setLayout(layout);
}
void TransformSettingSelection::SetSource(obs_scene_item *source)
{
const auto current = _settings->currentData().value<TransformSetting>();
_settings->clear();
Populate(source);
SetSetting(current);
}
void TransformSettingSelection::SetSetting(const TransformSetting &setting)
{
QVariant variant;
variant.setValue(setting);
_settings->setCurrentIndex(_settings->findData(variant));
}
void TransformSettingSelection::SelectionIdxChanged(int idx)
{
if (idx == -1) {
return;
}
auto setting = _settings->itemData(idx).value<TransformSetting>();
emit SelectionChanged(setting);
}
static QString mapIdToLocale(const TransformSetting &setting)
{
static const std::unordered_map<std::string_view, std::string_view>
idMap = {
{"alignment",
"AdvSceneSwitcher.setting.transform.alignment"},
{"bottom",
"AdvSceneSwitcher.setting.transform.cropBottom"},
{"left", "AdvSceneSwitcher.setting.transform.cropLeft"},
{"right",
"AdvSceneSwitcher.setting.transform.cropRight"},
{"top", "AdvSceneSwitcher.setting.transform.cropTop"},
{"bounds_alignment",
"AdvSceneSwitcher.setting.transform.boundingBoxAlign"},
{"bounds_type",
"AdvSceneSwitcher.setting.transform.boundingBoxType"},
{"rot", "AdvSceneSwitcher.setting.transform.rotation"},
{"height", "AdvSceneSwitcher.setting.transform.height"},
{"width", "AdvSceneSwitcher.setting.transform.width"},
{"x", "x"},
{"y", "y"},
};
static const std::unordered_map<std::string_view, std::string_view>
nestedIdMap = {
{"bounds",
"AdvSceneSwitcher.setting.transform.boundingBoxSize"},
{"pos", "AdvSceneSwitcher.setting.transform.position"},
{"scale", "AdvSceneSwitcher.setting.transform.scale"},
{"size", "AdvSceneSwitcher.setting.transform.size"},
};
auto idNameIt = idMap.find(setting.GetID());
QString name = idNameIt == idMap.end()
? QString::fromStdString(setting.GetID())
: obs_module_text(idNameIt->second.data());
if (!setting.GetNestedID().empty()) {
auto idNameIt = nestedIdMap.find(setting.GetNestedID());
const auto nestedName =
idNameIt == nestedIdMap.end()
? QString::fromStdString(setting.GetID())
: obs_module_text(idNameIt->second.data());
name = "[" + nestedName + "] " + name;
}
return name;
}
void TransformSettingSelection::Populate(obs_scene_item *source)
{
auto settings = GetSoruceTransformSettings(source);
for (const auto &setting : settings) {
QVariant variant;
variant.setValue(setting);
auto name = mapIdToLocale(setting);
_settings->addItem(name, variant);
}
_settings->setCurrentIndex(-1);
TransformSetting emptySetting;
emit SelectionChanged(emptySetting);
adjustSize();
updateGeometry();
}
} // namespace advss
|