obs-scene.c 20.7 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

J
jp9000 已提交
18
#include "util/threading.h"
J
jp9000 已提交
19 20 21
#include "graphics/math-defs.h"
#include "obs-scene.h"

22 23 24
static const char *obs_scene_signals[] = {
	"void item_add(ptr scene, ptr item)",
	"void item_remove(ptr scene, ptr item)",
J
jp9000 已提交
25 26 27 28
	"void item_move_up(ptr scene, ptr item)",
	"void item_move_down(ptr scene, ptr item)",
	"void item_move_top(ptr scene, ptr item)",
	"void item_move_bottom(ptr scene, ptr item)",
29 30 31
	"void item_select(ptr scene, ptr item)",
	"void item_deselect(ptr scene, ptr item)",
	"void item_transform(ptr scene, ptr item)",
32 33 34
	NULL
};

J
jp9000 已提交
35
static inline void signal_item_remove(struct obs_scene_item *item)
J
jp9000 已提交
36
{
J
jp9000 已提交
37 38 39 40
	struct calldata params = {0};
	calldata_setptr(&params, "scene", item->parent);
	calldata_setptr(&params, "item", item);

41 42
	signal_handler_signal(item->parent->source->context.signals,
			"item_remove", &params);
J
jp9000 已提交
43
	calldata_free(&params);
J
jp9000 已提交
44 45
}

46 47
static const char *scene_getname(const char *locale)
{
48
	/* TODO: locale */
J
jp9000 已提交
49
	UNUSED_PARAMETER(locale);
50
	return "Scene";
51 52
}

53
static void *scene_create(obs_data_t settings, struct obs_source *source)
J
jp9000 已提交
54
{
P
Palana 已提交
55
	pthread_mutexattr_t attr;
J
jp9000 已提交
56
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
57 58 59
	scene->source     = source;
	scene->first_item = NULL;

60 61 62
	signal_handler_add_array(obs_source_signalhandler(source),
			obs_scene_signals);

P
Palana 已提交
63 64 65 66 67
	if (pthread_mutexattr_init(&attr) != 0)
		goto fail;
	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		goto fail;
	if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
68
		blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
P
Palana 已提交
69
		goto fail;
70
	}
J
jp9000 已提交
71

J
jp9000 已提交
72
	UNUSED_PARAMETER(settings);
J
jp9000 已提交
73
	return scene;
P
Palana 已提交
74 75 76 77 78

fail:
	pthread_mutexattr_destroy(&attr);
	bfree(scene);
	return NULL;
J
jp9000 已提交
79 80
}

81
static void remove_all_items(struct obs_scene *scene)
J
jp9000 已提交
82
{
J
jp9000 已提交
83
	struct obs_scene_item *item;
J
jp9000 已提交
84 85

	pthread_mutex_lock(&scene->mutex);
J
jp9000 已提交
86

J
jp9000 已提交
87 88
	item = scene->first_item;

89 90 91 92
	while (item) {
		struct obs_scene_item *del_item = item;
		item = item->next;

J
jp9000 已提交
93
		obs_sceneitem_remove(del_item);
94
	}
J
jp9000 已提交
95

J
jp9000 已提交
96
	pthread_mutex_unlock(&scene->mutex);
97 98 99 100 101
}

static void scene_destroy(void *data)
{
	struct obs_scene *scene = data;
J
jp9000 已提交
102

103
	remove_all_items(scene);
104
	pthread_mutex_destroy(&scene->mutex);
J
jp9000 已提交
105 106 107
	bfree(scene);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void scene_enum_sources(void *data,
		obs_source_enum_proc_t enum_callback,
		void *param)
{
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);
		enum_callback(scene->source, item->source, param);
		obs_sceneitem_release(item);

		item = next;
	}

	pthread_mutex_unlock(&scene->mutex);
}

131 132 133 134 135 136 137 138 139
static inline void detach_sceneitem(struct obs_scene_item *item)
{
	if (item->prev)
		item->prev->next = item->next;
	else
		item->parent->first_item = item->next;

	if (item->next)
		item->next->prev = item->prev;
J
jp9000 已提交
140 141

	item->parent = NULL;
142 143
}

J
jp9000 已提交
144 145
static inline void attach_sceneitem(struct obs_scene *parent,
		struct obs_scene_item *item, struct obs_scene_item *prev)
146
{
J
jp9000 已提交
147 148
	item->prev   = prev;
	item->parent = parent;
149 150 151 152 153 154 155 156 157 158 159 160

	if (prev) {
		item->next = prev->next;
		if (prev->next)
			prev->next->prev = item;
		prev->next = item;
	} else {
		item->next = item->parent->first_item;
		item->parent->first_item = item;
	}
}

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
static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
{
	if (align & OBS_ALIGN_RIGHT)
		v->x += (float)cx;
	else if ((align & OBS_ALIGN_LEFT) == 0)
		v->x += (float)(cx / 2);

	if (align & OBS_ALIGN_BOTTOM)
		v->y += (float)cy;
	else if ((align & OBS_ALIGN_TOP) == 0)
		v->y += (float)(cy / 2);
}

static void calculate_bounds_data(struct obs_scene_item *item,
		struct vec2 *origin, struct vec2 *scale,
		uint32_t *cx, uint32_t *cy)
{
	float width         = (float)(*cx) * fabsf(scale->x);
	float height        = (float)(*cy) * fabsf(scale->y);
	float item_aspect   = width / height;
	float bounds_aspect = item->bounds.x / item->bounds.y;
	float width_diff, height_diff;

	if (item->bounds_type == OBS_BOUNDS_SCALE_INNER ||
	    item->bounds_type == OBS_BOUNDS_SCALE_OUTER) {
		bool  use_width = (bounds_aspect < item_aspect);
		float mul;

		if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
			use_width = !use_width;

		mul = use_width ?
			item->bounds.x / width :
			item->bounds.y / height;

		vec2_mulf(scale, scale, mul);

	} else if (item->bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
		vec2_mulf(scale, scale, item->bounds.x / width);

	} else if (item->bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
		vec2_mulf(scale, scale, item->bounds.y / height);

	} else if (item->bounds_type == OBS_BOUNDS_STRETCH) {
		scale->x = item->bounds.x / (float)(*cx);
		scale->y = item->bounds.y / (float)(*cy);
	}

	width       = (float)(*cx) * scale->x;
	height      = (float)(*cy) * scale->y;
	width_diff  = item->bounds.x - width;
	height_diff = item->bounds.y - height;
	*cx         = (uint32_t)item->bounds.x;
	*cy         = (uint32_t)item->bounds.y;

	add_alignment(origin, item->bounds_align,
			(int)-width_diff, (int)-height_diff);
}

static void update_item_transform(struct obs_scene_item *item)
{
	uint32_t        width         = obs_source_getwidth(item->source);
	uint32_t        height        = obs_source_getheight(item->source);
	uint32_t        cx            = width;
	uint32_t        cy            = height;
	struct vec2     base_origin   = {0.0f, 0.0f};
	struct vec2     origin        = {0.0f, 0.0f};
	struct vec2     scale         = item->scale;
	struct calldata params        = {0};

	/* ----------------------- */

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		calculate_bounds_data(item, &origin, &scale, &cx, &cy);
	} else {
		cx = (uint32_t)((float)cx * scale.x);
		cy = (uint32_t)((float)cy * scale.y);
	}

	add_alignment(&origin, item->align, (int)cx, (int)cy);

	matrix4_identity(&item->draw_transform);
	matrix4_scale3f(&item->draw_transform, &item->draw_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			-origin.x, -origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			item->pos.x, item->pos.y, 0.0f);

	/* ----------------------- */

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		vec2_copy(&scale, &item->bounds);
	} else {
		scale.x = (float)width  * item->scale.x;
		scale.y = (float)height * item->scale.y;
	}

	add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);

	matrix4_identity(&item->box_transform);
	matrix4_scale3f(&item->box_transform, &item->box_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			-base_origin.x, -base_origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			item->pos.x, item->pos.y, 0.0f);

	/* ----------------------- */

	item->last_width  = width;
	item->last_height = height;

	calldata_setptr(&params, "scene", item->parent);
	calldata_setptr(&params, "item", item);
	signal_handler_signal(item->parent->source->context.signals,
			"item_transform", &params);
	calldata_free(&params);
}

static inline bool source_size_changed(struct obs_scene_item *item)
{
	uint32_t width  = obs_source_getwidth(item->source);
	uint32_t height = obs_source_getheight(item->source);

	return item->last_width != width || item->last_height != height;
}

J
jp9000 已提交
293
static void scene_video_render(void *data, effect_t effect)
J
jp9000 已提交
294
{
295 296 297
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

298 299
	pthread_mutex_lock(&scene->mutex);

300
	item = scene->first_item;
J
jp9000 已提交
301

302
	while (item) {
303
		if (obs_source_removed(item->source)) {
304 305 306
			struct obs_scene_item *del_item = item;
			item = item->next;

J
jp9000 已提交
307
			obs_sceneitem_remove(del_item);
308 309 310
			continue;
		}

311 312
		if (source_size_changed(item))
			update_item_transform(item);
J
jp9000 已提交
313

314 315
		gs_matrix_push();
		gs_matrix_mul(&item->draw_transform);
316
		obs_source_video_render(item->source);
J
jp9000 已提交
317
		gs_matrix_pop();
318 319

		item = item->next;
J
jp9000 已提交
320
	}
321 322

	pthread_mutex_unlock(&scene->mutex);
J
jp9000 已提交
323 324 325 326

	UNUSED_PARAMETER(effect);
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340
static void scene_load_item(struct obs_scene *scene, obs_data_t item_data)
{
	const char            *name = obs_data_getstring(item_data, "name");
	obs_source_t          source = obs_get_source_by_name(name);
	struct obs_scene_item *item;

	if (!source) {
		blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
				name);
		return;
	}

	item = obs_scene_add(scene, source);

341 342 343
	obs_data_set_default_int(item_data, "align",
			OBS_ALIGN_TOP | OBS_ALIGN_LEFT);

344
	item->rot     = (float)obs_data_getdouble(item_data, "rot");
345
	item->align   = (uint32_t)obs_data_getint(item_data, "align");
346 347 348
	item->visible = obs_data_getbool(item_data, "visible");
	obs_data_get_vec2(item_data, "pos",    &item->pos);
	obs_data_get_vec2(item_data, "scale",  &item->scale);
349 350 351 352 353 354 355

	item->bounds_type =
		(enum obs_bounds_type)obs_data_getint(item_data, "bounds_type");
	item->bounds_align =
		(uint32_t)obs_data_getint(item_data, "bounds_align");
	obs_data_get_vec2(item_data, "bounds", &item->bounds);

356
	obs_source_release(source);
357 358

	update_item_transform(item);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

static void scene_load(void *scene, obs_data_t settings)
{
	obs_data_array_t items = obs_data_getarray(settings, "items");
	size_t           count, i;

	remove_all_items(scene);

	if (!items) return;

	count = obs_data_array_count(items);

	for (i = 0; i < count; i++) {
		obs_data_t item_data = obs_data_array_item(items, i);
		scene_load_item(scene, item_data);
		obs_data_release(item_data);
	}

	obs_data_array_release(items);
}

J
jp9000 已提交
381
static void scene_save_item(obs_data_array_t array, struct obs_scene_item *item)
382 383 384 385
{
	obs_data_t item_data = obs_data_create();
	const char *name     = obs_source_getname(item->source);

386 387 388 389 390 391 392 393 394
	obs_data_setstring(item_data, "name",         name);
	obs_data_setbool  (item_data, "visible",      item->visible);
	obs_data_setdouble(item_data, "rot",          item->rot);
	obs_data_set_vec2 (item_data, "pos",          &item->pos);
	obs_data_set_vec2 (item_data, "scale",        &item->scale);
	obs_data_setint   (item_data, "align",        (int)item->align);
	obs_data_setint   (item_data, "bounds_type",  (int)item->bounds_type);
	obs_data_setint   (item_data, "bounds_align", (int)item->bounds_align);
	obs_data_set_vec2 (item_data, "bounds",       &item->bounds);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

	obs_data_array_push_back(array, item_data);
	obs_data_release(item_data);
}

static void scene_save(void *data, obs_data_t settings)
{
	struct obs_scene      *scene = data;
	obs_data_array_t      array  = obs_data_array_create();
	struct obs_scene_item *item;

	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
410
		scene_save_item(array, item);
411 412 413 414 415 416 417 418 419
		item = item->next;
	}

	pthread_mutex_unlock(&scene->mutex);

	obs_data_setarray(settings, "items", array);
	obs_data_array_release(array);
}

J
jp9000 已提交
420 421 422 423
static uint32_t scene_getwidth(void *data)
{
	UNUSED_PARAMETER(data);
	return obs->video.base_width;
J
jp9000 已提交
424 425
}

J
jp9000 已提交
426
static uint32_t scene_getheight(void *data)
J
jp9000 已提交
427
{
J
jp9000 已提交
428 429
	UNUSED_PARAMETER(data);
	return obs->video.base_height;
J
jp9000 已提交
430 431
}

432
const struct obs_source_info scene_info =
J
jp9000 已提交
433
{
J
jp9000 已提交
434
	.id           = "scene",
435
	.type         = OBS_SOURCE_TYPE_INPUT,
J
jp9000 已提交
436 437 438 439 440
	.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
	.getname      = scene_getname,
	.create       = scene_create,
	.destroy      = scene_destroy,
	.video_render = scene_video_render,
J
jp9000 已提交
441 442
	.getwidth     = scene_getwidth,
	.getheight    = scene_getheight,
443 444
	.load         = scene_load,
	.save         = scene_save,
445
	.enum_sources = scene_enum_sources
J
jp9000 已提交
446 447
};

448
obs_scene_t obs_scene_create(const char *name)
J
jp9000 已提交
449
{
450 451 452
	struct obs_source *source =
		obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
	return source->context.data;
J
jp9000 已提交
453 454
}

P
Palana 已提交
455
void obs_scene_addref(obs_scene_t scene)
456
{
P
Palana 已提交
457 458
	if (scene)
		obs_source_addref(scene->source);
459 460
}

P
Palana 已提交
461
void obs_scene_release(obs_scene_t scene)
J
jp9000 已提交
462
{
P
Palana 已提交
463 464
	if (scene)
		obs_source_release(scene->source);
J
jp9000 已提交
465 466
}

467
obs_source_t obs_scene_getsource(obs_scene_t scene)
J
jp9000 已提交
468
{
J
jp9000 已提交
469
	return scene ? scene->source : NULL;
J
jp9000 已提交
470 471
}

472 473
obs_scene_t obs_scene_fromsource(obs_source_t source)
{
474
	if (!source || source->info.id != scene_info.id)
475 476
		return NULL;

477
	return source->context.data;
478 479
}

480 481 482 483
obs_sceneitem_t obs_scene_findsource(obs_scene_t scene, const char *name)
{
	struct obs_scene_item *item;

J
jp9000 已提交
484 485 486
	if (!scene)
		return NULL;

487 488 489 490
	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
491
		if (strcmp(item->source->context.name, name) == 0)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
			break;

		item = item->next;
	}

	pthread_mutex_unlock(&scene->mutex);

	return item;
}

void obs_scene_enum_items(obs_scene_t scene,
		bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
		void *param)
{
	struct obs_scene_item *item;

J
jp9000 已提交
508 509 510
	if (!scene || !callback)
		return;

511 512 513 514
	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
515 516 517 518
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);

519 520
		if (!callback(scene, item, param)) {
			obs_sceneitem_release(item);
521
			break;
522
		}
523

J
jp9000 已提交
524 525 526
		obs_sceneitem_release(item);

		item = next;
527 528 529 530 531
	}

	pthread_mutex_unlock(&scene->mutex);
}

532
obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
J
jp9000 已提交
533
{
534
	struct obs_scene_item *last;
535
	struct obs_scene_item *item;
J
jp9000 已提交
536
	struct calldata params = {0};
537

538 539 540 541
	if (!scene)
		return NULL;

	if (!source) {
J
jp9000 已提交
542
		blog(LOG_ERROR, "Tried to add a NULL source to a scene");
543 544 545 546
		return NULL;
	}

	item = bzalloc(sizeof(struct obs_scene_item));
J
jp9000 已提交
547 548 549
	item->source  = source;
	item->visible = true;
	item->parent  = scene;
J
jp9000 已提交
550
	item->ref     = 1;
551
	item->align   = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
J
jp9000 已提交
552
	vec2_set(&item->scale, 1.0f, 1.0f);
553 554
	matrix4_identity(&item->draw_transform);
	matrix4_identity(&item->box_transform);
J
jp9000 已提交
555

556 557
	obs_source_addref(source);
	obs_source_add_child(scene->source, source);
558

559 560 561 562 563 564 565 566 567 568 569 570
	pthread_mutex_lock(&scene->mutex);

	last = scene->first_item;
	if (!last) {
		scene->first_item = item;
	} else {
		while (last->next)
			last = last->next;

		last->next = item;
		item->prev = last;
	}
J
jp9000 已提交
571

572 573
	pthread_mutex_unlock(&scene->mutex);

J
jp9000 已提交
574 575
	calldata_setptr(&params, "scene", scene);
	calldata_setptr(&params, "item", item);
576 577
	signal_handler_signal(scene->source->context.signals, "item_add",
			&params);
J
jp9000 已提交
578 579
	calldata_free(&params);

J
jp9000 已提交
580 581 582
	return item;
}

J
jp9000 已提交
583
static void obs_sceneitem_destroy(obs_sceneitem_t item)
J
jp9000 已提交
584
{
585
	if (item) {
586
		if (item->source)
J
jp9000 已提交
587
			obs_source_release(item->source);
J
jp9000 已提交
588
		bfree(item);
J
jp9000 已提交
589 590 591
	}
}

P
Palana 已提交
592
void obs_sceneitem_addref(obs_sceneitem_t item)
J
jp9000 已提交
593
{
P
Palana 已提交
594
	if (item)
J
jp9000 已提交
595
		os_atomic_inc_long(&item->ref);
J
jp9000 已提交
596
}
597

P
Palana 已提交
598
void obs_sceneitem_release(obs_sceneitem_t item)
J
jp9000 已提交
599
{
P
Palana 已提交
600 601
	if (!item)
		return;
P
Palana 已提交
602

J
jp9000 已提交
603
	if (os_atomic_dec_long(&item->ref) == 0)
P
Palana 已提交
604
		obs_sceneitem_destroy(item);
605 606
}

J
jp9000 已提交
607 608 609 610
void obs_sceneitem_remove(obs_sceneitem_t item)
{
	obs_scene_t scene;

611
	if (!item)
J
jp9000 已提交
612 613
		return;

614
	scene = item->parent;
J
jp9000 已提交
615

616 617
	if (scene)
		pthread_mutex_lock(&scene->mutex);
J
jp9000 已提交
618

J
jp9000 已提交
619
	if (item->removed) {
620 621 622 623 624
		if (scene)
			pthread_mutex_unlock(&scene->mutex);
		return;
	}

J
jp9000 已提交
625 626
	item->removed = true;

627 628
	assert(scene != NULL);
	assert(scene->source != NULL);
629 630
	obs_source_remove_child(scene->source, item->source);

631
	signal_item_remove(item);
J
jp9000 已提交
632
	detach_sceneitem(item);
633

634
	pthread_mutex_unlock(&scene->mutex);
J
jp9000 已提交
635 636 637 638

	obs_sceneitem_release(item);
}

J
jp9000 已提交
639 640
obs_scene_t obs_sceneitem_getscene(obs_sceneitem_t item)
{
J
jp9000 已提交
641
	return item ? item->parent : NULL;
J
jp9000 已提交
642 643
}

644 645
obs_source_t obs_sceneitem_getsource(obs_sceneitem_t item)
{
J
jp9000 已提交
646
	return item ? item->source : NULL;
J
jp9000 已提交
647 648
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
void obs_sceneitem_select(obs_sceneitem_t item, bool select)
{
	struct calldata params = {0};
	const char *command = select ? "item_select" : "item_deselect";

	if (!item || item->selected == select)
		return;

	item->selected = select;

	calldata_setptr(&params, "scene", item->parent);
	calldata_setptr(&params, "item",  item);
	signal_handler_signal(item->parent->source->context.signals,
			command, &params);

	calldata_free(&params);
}

bool obs_sceneitem_selected(obs_sceneitem_t item)
{
	return item ? item->selected : false;
}

672
void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
J
jp9000 已提交
673
{
674
	if (item) {
J
jp9000 已提交
675
		vec2_copy(&item->pos, pos);
676 677
		update_item_transform(item);
	}
J
jp9000 已提交
678 679
}

680
void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
J
jp9000 已提交
681
{
682
	if (item) {
J
jp9000 已提交
683
		item->rot = rot;
684 685
		update_item_transform(item);
	}
J
jp9000 已提交
686 687
}

688
void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
J
jp9000 已提交
689
{
690 691 692 693
	if (item) {
		vec2_copy(&item->scale, scale);
		update_item_transform(item);
	}
J
jp9000 已提交
694 695
}

696
void obs_sceneitem_setalignment(obs_sceneitem_t item, uint32_t alignment)
J
jp9000 已提交
697
{
698 699 700 701
	if (item) {
		item->align = alignment;
		update_item_transform(item);
	}
J
jp9000 已提交
702 703
}

J
jp9000 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
static inline void signal_move_dir(struct obs_scene_item *item,
		enum order_movement movement)
{
	const char *command;
	struct calldata params = {0};

	switch (movement) {
	case ORDER_MOVE_UP:     command = "item_move_up";     break;
	case ORDER_MOVE_DOWN:   command = "item_move_down";   break;
	case ORDER_MOVE_TOP:    command = "item_move_top";    break;
	case ORDER_MOVE_BOTTOM: command = "item_move_bottom"; break;
	}

	calldata_setptr(&params, "scene", item->parent);
	calldata_setptr(&params, "item",  item);

	signal_handler_signal(item->parent->source->context.signals,
			command, &params);

	calldata_free(&params);
}

726
void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
J
jp9000 已提交
727
{
J
jp9000 已提交
728 729
	if (!item) return;

J
jp9000 已提交
730
	struct obs_scene_item *next, *prev;
J
jp9000 已提交
731 732
	struct obs_scene *scene = item->parent;

J
jp9000 已提交
733
	obs_scene_addref(scene);
734
	pthread_mutex_lock(&scene->mutex);
735

J
jp9000 已提交
736 737 738
	next = item->next;
	prev = item->prev;

739 740
	detach_sceneitem(item);

J
jp9000 已提交
741 742
	if (movement == ORDER_MOVE_DOWN) {
		attach_sceneitem(scene, item, prev ? prev->prev : NULL);
J
jp9000 已提交
743

J
jp9000 已提交
744 745
	} else if (movement == ORDER_MOVE_UP) {
		attach_sceneitem(scene, item, next ? next : prev);
J
jp9000 已提交
746 747

	} else if (movement == ORDER_MOVE_TOP) {
J
jp9000 已提交
748
		struct obs_scene_item *last = next;
749
		if (!last) {
J
jp9000 已提交
750
			last = prev;
751 752 753 754
		} else {
			while (last->next)
				last = last->next;
		}
J
jp9000 已提交
755

J
jp9000 已提交
756
		attach_sceneitem(scene, item, last);
757 758

	} else if (movement == ORDER_MOVE_BOTTOM) {
J
jp9000 已提交
759
		attach_sceneitem(scene, item, NULL);
J
jp9000 已提交
760
	}
761

J
jp9000 已提交
762 763
	signal_move_dir(item, movement);

764
	pthread_mutex_unlock(&scene->mutex);
765
	obs_scene_release(scene);
J
jp9000 已提交
766 767
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
void obs_sceneitem_set_bounds_type(obs_sceneitem_t item,
		enum obs_bounds_type type)
{
	if (item) {
		item->bounds_type = type;
		update_item_transform(item);
	}
}

void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t item,
		uint32_t alignment)
{
	if (item) {
		item->bounds_align = alignment;
		update_item_transform(item);
	}
}

void obs_sceneitem_set_bounds(obs_sceneitem_t item, const struct vec2 *bounds)
{
	if (item) {
		item->bounds = *bounds;
		update_item_transform(item);
	}
}

794
void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
J
jp9000 已提交
795
{
J
jp9000 已提交
796 797
	if (item)
		vec2_copy(pos, &item->pos);
J
jp9000 已提交
798 799
}

800
float obs_sceneitem_getrot(obs_sceneitem_t item)
J
jp9000 已提交
801
{
J
jp9000 已提交
802
	return item ? item->rot : 0.0f;
J
jp9000 已提交
803 804
}

805
void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
J
jp9000 已提交
806
{
J
jp9000 已提交
807
	if (item)
808
		vec2_copy(scale, &item->scale);
J
jp9000 已提交
809 810
}

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
uint32_t obs_sceneitem_getalignment(obs_sceneitem_t item)
{
	return item ? item->align : 0;
}

enum obs_bounds_type obs_sceneitem_get_bounds_type(obs_sceneitem_t item)
{
	return item ? item->bounds_type : OBS_BOUNDS_NONE;
}

uint32_t obs_sceneitem_get_bounds_alignment(obs_sceneitem_t item)
{
	return item ? item->bounds_align : 0;
}

void obs_sceneitem_get_bounds(obs_sceneitem_t item, struct vec2 *bounds)
J
jp9000 已提交
827
{
J
jp9000 已提交
828
	if (item)
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
		*bounds = item->bounds;
}

void obs_sceneitem_get_info(obs_sceneitem_t item,
		struct obs_sceneitem_info *info)
{
	if (item && info) {
		info->pos              = item->pos;
		info->rot              = item->rot;
		info->scale            = item->scale;
		info->alignment        = item->align;
		info->bounds_type      = item->bounds_type;
		info->bounds_alignment = item->bounds_align;
		info->bounds           = item->bounds;
	}
}

void obs_sceneitem_set_info(obs_sceneitem_t item,
		const struct obs_sceneitem_info *info)
{
	if (item && info) {
		item->pos          = info->pos;
		item->rot          = info->rot;
		item->scale        = info->scale;
		item->align        = info->alignment;
		item->bounds_type  = info->bounds_type;
		item->bounds_align = info->bounds_alignment;
		item->bounds       = info->bounds;
		update_item_transform(item);
	}
}

void obs_sceneitem_get_draw_transform(obs_sceneitem_t item,
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->draw_transform);
}

void obs_sceneitem_get_box_transform(obs_sceneitem_t item,
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->box_transform);
J
jp9000 已提交
873
}