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
|
#include <QMessageBox>
#include "window-basic-transform.hpp"
#include "window-basic-main.hpp"
Q_DECLARE_METATYPE(OBSSceneItem);
static bool find_sel(obs_scene_t *, obs_sceneitem_t *item, void *param)
{
OBSSceneItem &dst = *reinterpret_cast<OBSSceneItem*>(param);
if (obs_sceneitem_selected(item)) {
dst = item;
return false;
}
if (obs_sceneitem_is_group(item)) {
obs_sceneitem_group_enum_items(item, find_sel, param);
if (!!dst) {
return false;
}
}
return true;
};
static OBSSceneItem FindASelectedItem(OBSScene scene)
{
OBSSceneItem item;
obs_scene_enum_items(scene, find_sel, &item);
return item;
}
void OBSBasicTransform::HookWidget(QWidget *widget, const char *signal,
const char *slot)
{
QObject::connect(widget, signal, this, slot);
}
#define COMBO_CHANGED SIGNAL(currentIndexChanged(int))
#define ISCROLL_CHANGED SIGNAL(valueChanged(int))
#define DSCROLL_CHANGED SIGNAL(valueChanged(double))
OBSBasicTransform::OBSBasicTransform(OBSBasic *parent)
: QDialog (parent),
ui (new Ui::OBSBasicTransform),
main (parent)
{
ui->setupUi(this);
HookWidget(ui->positionX, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->positionY, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->rotation, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->sizeX, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->sizeY, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->align, COMBO_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->boundsType, COMBO_CHANGED, SLOT(OnBoundsType(int)));
HookWidget(ui->boundsAlign, COMBO_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->boundsWidth, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->boundsHeight, DSCROLL_CHANGED, SLOT(OnControlChanged()));
HookWidget(ui->cropLeft, ISCROLL_CHANGED, SLOT(OnCropChanged()));
HookWidget(ui->cropRight, ISCROLL_CHANGED, SLOT(OnCropChanged()));
HookWidget(ui->cropTop, ISCROLL_CHANGED, SLOT(OnCropChanged()));
HookWidget(ui->cropBottom, ISCROLL_CHANGED, SLOT(OnCropChanged()));
ui->buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
connect(ui->buttonBox->button(QDialogButtonBox::Reset),
SIGNAL(clicked()), this, SLOT(on_resetButton_clicked()));
installEventFilter(CreateShortcutFilter());
OBSSceneItem item = FindASelectedItem(main->GetCurrentScene());
OBSScene scene = obs_sceneitem_get_scene(item);
SetScene(scene);
SetItem(item);
channelChangedSignal.Connect(obs_get_signal_handler(), "channel_change",
OBSChannelChanged, this);
}
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 OBSBasicTransform::SetItem(OBSSceneItem newItem)
{
QMetaObject::invokeMethod(this, "SetItemQt",
Q_ARG(OBSSceneItem, OBSSceneItem(newItem)));
}
void OBSBasicTransform::SetItemQt(OBSSceneItem newItem)
{
item = newItem;
if (item)
RefreshControls();
setEnabled(!!item);
}
void OBSBasicTransform::OBSChannelChanged(void *param, calldata_t *data)
{
OBSBasicTransform *window = reinterpret_cast<OBSBasicTransform*>(param);
uint32_t channel = (uint32_t)calldata_int(data, "channel");
OBSSource source = (obs_source_t*)calldata_ptr(data, "source");
if (channel == 0) {
OBSScene scene = obs_scene_from_source(source);
window->SetScene(scene);
if (!scene)
window->SetItem(nullptr);
else
window->SetItem(FindASelectedItem(scene));
}
}
void OBSBasicTransform::OBSSceneItemTransform(void *param, calldata_t *data)
{
OBSBasicTransform *window = reinterpret_cast<OBSBasicTransform*>(param);
OBSSceneItem item = (obs_sceneitem_t*)calldata_ptr(data, "item");
if (item == window->item && !window->ignoreTransformSignal)
QMetaObject::invokeMethod(window, "RefreshControls");
}
void OBSBasicTransform::OBSSceneItemRemoved(void *param, calldata_t *data)
{
OBSBasicTransform *window = reinterpret_cast<OBSBasicTransform*>(param);
OBSScene scene = (obs_scene_t*)calldata_ptr(data, "scene");
OBSSceneItem item = (obs_sceneitem_t*)calldata_ptr(data, "item");
if (item == window->item)
window->SetItem(FindASelectedItem(scene));
}
void OBSBasicTransform::OBSSceneItemSelect(void *param, calldata_t *data)
{
OBSBasicTransform *window = reinterpret_cast<OBSBasicTransform*>(param);
OBSSceneItem item = (obs_sceneitem_t*)calldata_ptr(data, "item");
if (item != window->item)
window->SetItem(item);
}
void OBSBasicTransform::OBSSceneItemDeselect(void *param, calldata_t *data)
{
OBSBasicTransform *window = reinterpret_cast<OBSBasicTransform*>(param);
OBSScene scene = (obs_scene_t*)calldata_ptr(data, "scene");
OBSSceneItem item = (obs_sceneitem_t*)calldata_ptr(data, "item");
if (item == window->item)
window->SetItem(FindASelectedItem(scene));
}
static const uint32_t listToAlign[] = {
OBS_ALIGN_TOP | OBS_ALIGN_LEFT,
OBS_ALIGN_TOP,
OBS_ALIGN_TOP | OBS_ALIGN_RIGHT,
OBS_ALIGN_LEFT,
OBS_ALIGN_CENTER,
OBS_ALIGN_RIGHT,
OBS_ALIGN_BOTTOM | OBS_ALIGN_LEFT,
OBS_ALIGN_BOTTOM,
OBS_ALIGN_BOTTOM | OBS_ALIGN_RIGHT
};
static int AlignToList(uint32_t align)
{
int index = 0;
for (uint32_t curAlign : listToAlign) {
if (curAlign == align)
return index;
index++;
}
return 0;
}
void OBSBasicTransform::RefreshControls()
{
if (!item)
return;
obs_transform_info osi;
obs_sceneitem_crop crop;
obs_sceneitem_get_info(item, &osi);
obs_sceneitem_get_crop(item, &crop);
obs_source_t *source = obs_sceneitem_get_source(item);
float width = float(obs_source_get_width(source));
float height = float(obs_source_get_height(source));
int alignIndex = AlignToList(osi.alignment);
int boundsAlignIndex = AlignToList(osi.bounds_alignment);
ignoreItemChange = true;
ui->positionX->setValue(osi.pos.x);
ui->positionY->setValue(osi.pos.y);
ui->rotation->setValue(osi.rot);
ui->sizeX->setValue(osi.scale.x * width);
ui->sizeY->setValue(osi.scale.y * height);
ui->align->setCurrentIndex(alignIndex);
ui->boundsType->setCurrentIndex(int(osi.bounds_type));
ui->boundsAlign->setCurrentIndex(boundsAlignIndex);
ui->boundsWidth->setValue(osi.bounds.x);
ui->boundsHeight->setValue(osi.bounds.y);
ui->cropLeft->setValue(int(crop.left));
ui->cropRight->setValue(int(crop.right));
ui->cropTop->setValue(int(crop.top));
ui->cropBottom->setValue(int(crop.bottom));
ignoreItemChange = false;
}
void OBSBasicTransform::OnBoundsType(int index)
{
if (index == -1)
return;
obs_bounds_type type = (obs_bounds_type)index;
bool enable = (type != OBS_BOUNDS_NONE);
ui->boundsAlign->setEnabled(enable);
ui->boundsWidth->setEnabled(enable);
ui->boundsHeight->setEnabled(enable);
if (!ignoreItemChange) {
obs_bounds_type lastType = obs_sceneitem_get_bounds_type(item);
if (lastType == OBS_BOUNDS_NONE) {
OBSSource source = obs_sceneitem_get_source(item);
int width = (int)obs_source_get_width(source);
int height = (int)obs_source_get_height(source);
ui->boundsWidth->setValue(width);
ui->boundsHeight->setValue(height);
}
}
OnControlChanged();
}
void OBSBasicTransform::OnControlChanged()
{
if (ignoreItemChange)
return;
obs_source_t *source = obs_sceneitem_get_source(item);
double width = double(obs_source_get_width(source));
double height = double(obs_source_get_height(source));
obs_transform_info oti;
oti.pos.x = float(ui->positionX->value());
oti.pos.y = float(ui->positionY->value());
oti.rot = float(ui->rotation->value());
oti.scale.x = float(ui->sizeX->value() / width);
oti.scale.y = float(ui->sizeY->value() / height);
oti.alignment = listToAlign[ui->align->currentIndex()];
oti.bounds_type = (obs_bounds_type)ui->boundsType->currentIndex();
oti.bounds_alignment = listToAlign[ui->boundsAlign->currentIndex()];
oti.bounds.x = float(ui->boundsWidth->value());
oti.bounds.y = float(ui->boundsHeight->value());
ignoreTransformSignal = true;
obs_sceneitem_set_info(item, &oti);
ignoreTransformSignal = false;
}
void OBSBasicTransform::OnCropChanged()
{
if (ignoreItemChange)
return;
obs_sceneitem_crop crop;
crop.left = uint32_t(ui->cropLeft->value());
crop.right = uint32_t(ui->cropRight->value());
crop.top = uint32_t(ui->cropTop->value());
crop.bottom = uint32_t(ui->cropBottom->value());
ignoreTransformSignal = true;
obs_sceneitem_set_crop(item, &crop);
ignoreTransformSignal = false;
}
void OBSBasicTransform::on_resetButton_clicked()
{
main->on_actionResetTransform_triggered();
}
|