obs-scene.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. 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
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. static const char *obs_scene_signals[] = {
  19. "void item_add(ptr scene, ptr item)",
  20. "void item_remove(ptr scene, ptr item)",
  21. "void reorder(ptr scene)",
  22. "void item_visible(ptr scene, ptr item, bool visible)",
  23. "void item_select(ptr scene, ptr item)",
  24. "void item_deselect(ptr scene, ptr item)",
  25. "void item_transform(ptr scene, ptr item)",
  26. NULL
  27. };
  28. static inline void signal_item_remove(struct obs_scene_item *item)
  29. {
  30. struct calldata params = {0};
  31. calldata_set_ptr(&params, "scene", item->parent);
  32. calldata_set_ptr(&params, "item", item);
  33. signal_handler_signal(item->parent->source->context.signals,
  34. "item_remove", &params);
  35. calldata_free(&params);
  36. }
  37. static const char *scene_getname(void)
  38. {
  39. /* TODO: locale */
  40. return "Scene";
  41. }
  42. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  43. {
  44. pthread_mutexattr_t attr;
  45. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  46. scene->source = source;
  47. scene->first_item = NULL;
  48. signal_handler_add_array(obs_source_get_signal_handler(source),
  49. obs_scene_signals);
  50. if (pthread_mutexattr_init(&attr) != 0)
  51. goto fail;
  52. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  53. goto fail;
  54. if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
  55. blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
  56. goto fail;
  57. }
  58. UNUSED_PARAMETER(settings);
  59. return scene;
  60. fail:
  61. pthread_mutexattr_destroy(&attr);
  62. bfree(scene);
  63. return NULL;
  64. }
  65. static void remove_all_items(struct obs_scene *scene)
  66. {
  67. struct obs_scene_item *item;
  68. pthread_mutex_lock(&scene->mutex);
  69. item = scene->first_item;
  70. while (item) {
  71. struct obs_scene_item *del_item = item;
  72. item = item->next;
  73. obs_sceneitem_remove(del_item);
  74. }
  75. pthread_mutex_unlock(&scene->mutex);
  76. }
  77. static void scene_destroy(void *data)
  78. {
  79. struct obs_scene *scene = data;
  80. remove_all_items(scene);
  81. pthread_mutex_destroy(&scene->mutex);
  82. bfree(scene);
  83. }
  84. static void scene_enum_sources(void *data,
  85. obs_source_enum_proc_t enum_callback,
  86. void *param)
  87. {
  88. struct obs_scene *scene = data;
  89. struct obs_scene_item *item;
  90. pthread_mutex_lock(&scene->mutex);
  91. item = scene->first_item;
  92. while (item) {
  93. struct obs_scene_item *next = item->next;
  94. obs_sceneitem_addref(item);
  95. enum_callback(scene->source, item->source, param);
  96. obs_sceneitem_release(item);
  97. item = next;
  98. }
  99. pthread_mutex_unlock(&scene->mutex);
  100. }
  101. static inline void detach_sceneitem(struct obs_scene_item *item)
  102. {
  103. if (item->prev)
  104. item->prev->next = item->next;
  105. else
  106. item->parent->first_item = item->next;
  107. if (item->next)
  108. item->next->prev = item->prev;
  109. item->parent = NULL;
  110. }
  111. static inline void attach_sceneitem(struct obs_scene *parent,
  112. struct obs_scene_item *item, struct obs_scene_item *prev)
  113. {
  114. item->prev = prev;
  115. item->parent = parent;
  116. if (prev) {
  117. item->next = prev->next;
  118. if (prev->next)
  119. prev->next->prev = item;
  120. prev->next = item;
  121. } else {
  122. item->next = parent->first_item;
  123. if (parent->first_item)
  124. parent->first_item->prev = item;
  125. parent->first_item = item;
  126. }
  127. }
  128. static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  129. {
  130. if (align & OBS_ALIGN_RIGHT)
  131. v->x += (float)cx;
  132. else if ((align & OBS_ALIGN_LEFT) == 0)
  133. v->x += (float)(cx / 2);
  134. if (align & OBS_ALIGN_BOTTOM)
  135. v->y += (float)cy;
  136. else if ((align & OBS_ALIGN_TOP) == 0)
  137. v->y += (float)(cy / 2);
  138. }
  139. static void calculate_bounds_data(struct obs_scene_item *item,
  140. struct vec2 *origin, struct vec2 *scale,
  141. uint32_t *cx, uint32_t *cy)
  142. {
  143. float width = (float)(*cx) * fabsf(scale->x);
  144. float height = (float)(*cy) * fabsf(scale->y);
  145. float item_aspect = width / height;
  146. float bounds_aspect = item->bounds.x / item->bounds.y;
  147. uint32_t bounds_type = item->bounds_type;
  148. float width_diff, height_diff;
  149. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  150. if (width > item->bounds.x || height > item->bounds.y)
  151. bounds_type = OBS_BOUNDS_SCALE_INNER;
  152. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  153. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  154. bool use_width = (bounds_aspect < item_aspect);
  155. float mul;
  156. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  157. use_width = !use_width;
  158. mul = use_width ?
  159. item->bounds.x / width :
  160. item->bounds.y / height;
  161. vec2_mulf(scale, scale, mul);
  162. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  163. vec2_mulf(scale, scale, item->bounds.x / width);
  164. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  165. vec2_mulf(scale, scale, item->bounds.y / height);
  166. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  167. scale->x = item->bounds.x / (float)(*cx);
  168. scale->y = item->bounds.y / (float)(*cy);
  169. }
  170. width = (float)(*cx) * scale->x;
  171. height = (float)(*cy) * scale->y;
  172. width_diff = item->bounds.x - width;
  173. height_diff = item->bounds.y - height;
  174. *cx = (uint32_t)item->bounds.x;
  175. *cy = (uint32_t)item->bounds.y;
  176. add_alignment(origin, item->bounds_align,
  177. (int)-width_diff, (int)-height_diff);
  178. }
  179. static void update_item_transform(struct obs_scene_item *item)
  180. {
  181. uint32_t width = obs_source_get_width(item->source);
  182. uint32_t height = obs_source_get_height(item->source);
  183. uint32_t cx = width;
  184. uint32_t cy = height;
  185. struct vec2 base_origin;
  186. struct vec2 origin;
  187. struct vec2 scale = item->scale;
  188. struct calldata params = {0};
  189. vec2_zero(&base_origin);
  190. vec2_zero(&origin);
  191. /* ----------------------- */
  192. if (item->bounds_type != OBS_BOUNDS_NONE) {
  193. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  194. } else {
  195. cx = (uint32_t)((float)cx * scale.x);
  196. cy = (uint32_t)((float)cy * scale.y);
  197. }
  198. add_alignment(&origin, item->align, (int)cx, (int)cy);
  199. matrix4_identity(&item->draw_transform);
  200. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  201. scale.x, scale.y, 1.0f);
  202. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  203. -origin.x, -origin.y, 0.0f);
  204. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  205. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  206. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  207. item->pos.x, item->pos.y, 0.0f);
  208. /* ----------------------- */
  209. if (item->bounds_type != OBS_BOUNDS_NONE) {
  210. vec2_copy(&scale, &item->bounds);
  211. } else {
  212. scale.x = (float)width * item->scale.x;
  213. scale.y = (float)height * item->scale.y;
  214. }
  215. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  216. matrix4_identity(&item->box_transform);
  217. matrix4_scale3f(&item->box_transform, &item->box_transform,
  218. scale.x, scale.y, 1.0f);
  219. matrix4_translate3f(&item->box_transform, &item->box_transform,
  220. -base_origin.x, -base_origin.y, 0.0f);
  221. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  222. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  223. matrix4_translate3f(&item->box_transform, &item->box_transform,
  224. item->pos.x, item->pos.y, 0.0f);
  225. /* ----------------------- */
  226. item->last_width = width;
  227. item->last_height = height;
  228. calldata_set_ptr(&params, "scene", item->parent);
  229. calldata_set_ptr(&params, "item", item);
  230. signal_handler_signal(item->parent->source->context.signals,
  231. "item_transform", &params);
  232. calldata_free(&params);
  233. }
  234. static inline bool source_size_changed(struct obs_scene_item *item)
  235. {
  236. uint32_t width = obs_source_get_width(item->source);
  237. uint32_t height = obs_source_get_height(item->source);
  238. return item->last_width != width || item->last_height != height;
  239. }
  240. static void scene_video_render(void *data, gs_effect_t *effect)
  241. {
  242. struct obs_scene *scene = data;
  243. struct obs_scene_item *item;
  244. pthread_mutex_lock(&scene->mutex);
  245. item = scene->first_item;
  246. gs_blend_state_push();
  247. gs_reset_blend_state();
  248. while (item) {
  249. if (obs_source_removed(item->source)) {
  250. struct obs_scene_item *del_item = item;
  251. item = item->next;
  252. obs_sceneitem_remove(del_item);
  253. continue;
  254. }
  255. if (source_size_changed(item))
  256. update_item_transform(item);
  257. if (item->visible) {
  258. gs_matrix_push();
  259. gs_matrix_mul(&item->draw_transform);
  260. obs_source_video_render(item->source);
  261. gs_matrix_pop();
  262. }
  263. item = item->next;
  264. }
  265. gs_blend_state_pop();
  266. pthread_mutex_unlock(&scene->mutex);
  267. UNUSED_PARAMETER(effect);
  268. }
  269. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  270. {
  271. const char *name = obs_data_get_string(item_data, "name");
  272. obs_source_t *source = obs_get_source_by_name(name);
  273. struct obs_scene_item *item;
  274. if (!source) {
  275. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  276. name);
  277. return;
  278. }
  279. item = obs_scene_add(scene, source);
  280. if (!item) {
  281. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  282. "to scene '%s'!",
  283. name, obs_source_get_name(scene->source));
  284. obs_source_release(source);
  285. return;
  286. }
  287. obs_data_set_default_int(item_data, "align",
  288. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  289. item->rot = (float)obs_data_get_double(item_data, "rot");
  290. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  291. item->visible = obs_data_get_bool(item_data, "visible");
  292. obs_data_get_vec2(item_data, "pos", &item->pos);
  293. obs_data_get_vec2(item_data, "scale", &item->scale);
  294. item->bounds_type =
  295. (enum obs_bounds_type)obs_data_get_int(item_data,
  296. "bounds_type");
  297. item->bounds_align =
  298. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  299. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  300. obs_source_release(source);
  301. update_item_transform(item);
  302. }
  303. static void scene_load(void *scene, obs_data_t *settings)
  304. {
  305. obs_data_array_t *items = obs_data_get_array(settings, "items");
  306. size_t count, i;
  307. remove_all_items(scene);
  308. if (!items) return;
  309. count = obs_data_array_count(items);
  310. for (i = 0; i < count; i++) {
  311. obs_data_t *item_data = obs_data_array_item(items, i);
  312. scene_load_item(scene, item_data);
  313. obs_data_release(item_data);
  314. }
  315. obs_data_array_release(items);
  316. }
  317. static void scene_save_item(obs_data_array_t *array,
  318. struct obs_scene_item *item)
  319. {
  320. obs_data_t *item_data = obs_data_create();
  321. const char *name = obs_source_get_name(item->source);
  322. obs_data_set_string(item_data, "name", name);
  323. obs_data_set_bool (item_data, "visible", item->visible);
  324. obs_data_set_double(item_data, "rot", item->rot);
  325. obs_data_set_vec2 (item_data, "pos", &item->pos);
  326. obs_data_set_vec2 (item_data, "scale", &item->scale);
  327. obs_data_set_int (item_data, "align", (int)item->align);
  328. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  329. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  330. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  331. obs_data_array_push_back(array, item_data);
  332. obs_data_release(item_data);
  333. }
  334. static void scene_save(void *data, obs_data_t *settings)
  335. {
  336. struct obs_scene *scene = data;
  337. obs_data_array_t *array = obs_data_array_create();
  338. struct obs_scene_item *item;
  339. pthread_mutex_lock(&scene->mutex);
  340. item = scene->first_item;
  341. while (item) {
  342. scene_save_item(array, item);
  343. item = item->next;
  344. }
  345. pthread_mutex_unlock(&scene->mutex);
  346. obs_data_set_array(settings, "items", array);
  347. obs_data_array_release(array);
  348. }
  349. static uint32_t scene_getwidth(void *data)
  350. {
  351. UNUSED_PARAMETER(data);
  352. return obs->video.base_width;
  353. }
  354. static uint32_t scene_getheight(void *data)
  355. {
  356. UNUSED_PARAMETER(data);
  357. return obs->video.base_height;
  358. }
  359. const struct obs_source_info scene_info =
  360. {
  361. .id = "scene",
  362. .type = OBS_SOURCE_TYPE_INPUT,
  363. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  364. .get_name = scene_getname,
  365. .create = scene_create,
  366. .destroy = scene_destroy,
  367. .video_render = scene_video_render,
  368. .get_width = scene_getwidth,
  369. .get_height = scene_getheight,
  370. .load = scene_load,
  371. .save = scene_save,
  372. .enum_sources = scene_enum_sources
  373. };
  374. obs_scene_t *obs_scene_create(const char *name)
  375. {
  376. struct obs_source *source =
  377. obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
  378. return source->context.data;
  379. }
  380. void obs_scene_addref(obs_scene_t *scene)
  381. {
  382. if (scene)
  383. obs_source_addref(scene->source);
  384. }
  385. void obs_scene_release(obs_scene_t *scene)
  386. {
  387. if (scene)
  388. obs_source_release(scene->source);
  389. }
  390. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  391. {
  392. return scene ? scene->source : NULL;
  393. }
  394. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  395. {
  396. if (!source || source->info.id != scene_info.id)
  397. return NULL;
  398. return source->context.data;
  399. }
  400. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  401. {
  402. struct obs_scene_item *item;
  403. if (!scene)
  404. return NULL;
  405. pthread_mutex_lock(&scene->mutex);
  406. item = scene->first_item;
  407. while (item) {
  408. if (strcmp(item->source->context.name, name) == 0)
  409. break;
  410. item = item->next;
  411. }
  412. pthread_mutex_unlock(&scene->mutex);
  413. return item;
  414. }
  415. void obs_scene_enum_items(obs_scene_t *scene,
  416. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  417. void *param)
  418. {
  419. struct obs_scene_item *item;
  420. if (!scene || !callback)
  421. return;
  422. pthread_mutex_lock(&scene->mutex);
  423. item = scene->first_item;
  424. while (item) {
  425. struct obs_scene_item *next = item->next;
  426. obs_sceneitem_addref(item);
  427. if (!callback(scene, item, param)) {
  428. obs_sceneitem_release(item);
  429. break;
  430. }
  431. obs_sceneitem_release(item);
  432. item = next;
  433. }
  434. pthread_mutex_unlock(&scene->mutex);
  435. }
  436. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  437. {
  438. struct obs_scene_item *last;
  439. struct obs_scene_item *item;
  440. struct calldata params = {0};
  441. if (!scene)
  442. return NULL;
  443. if (!source) {
  444. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  445. return NULL;
  446. }
  447. if (!obs_source_add_child(scene->source, source)) {
  448. blog(LOG_WARNING, "Failed to add source to scene due to "
  449. "infinite source recursion");
  450. return NULL;
  451. }
  452. item = bzalloc(sizeof(struct obs_scene_item));
  453. item->source = source;
  454. item->visible = true;
  455. item->parent = scene;
  456. item->ref = 1;
  457. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  458. vec2_set(&item->scale, 1.0f, 1.0f);
  459. matrix4_identity(&item->draw_transform);
  460. matrix4_identity(&item->box_transform);
  461. obs_source_addref(source);
  462. pthread_mutex_lock(&scene->mutex);
  463. last = scene->first_item;
  464. if (!last) {
  465. scene->first_item = item;
  466. } else {
  467. while (last->next)
  468. last = last->next;
  469. last->next = item;
  470. item->prev = last;
  471. }
  472. pthread_mutex_unlock(&scene->mutex);
  473. calldata_set_ptr(&params, "scene", scene);
  474. calldata_set_ptr(&params, "item", item);
  475. signal_handler_signal(scene->source->context.signals, "item_add",
  476. &params);
  477. calldata_free(&params);
  478. return item;
  479. }
  480. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  481. {
  482. if (item) {
  483. if (item->source)
  484. obs_source_release(item->source);
  485. bfree(item);
  486. }
  487. }
  488. void obs_sceneitem_addref(obs_sceneitem_t *item)
  489. {
  490. if (item)
  491. os_atomic_inc_long(&item->ref);
  492. }
  493. void obs_sceneitem_release(obs_sceneitem_t *item)
  494. {
  495. if (!item)
  496. return;
  497. if (os_atomic_dec_long(&item->ref) == 0)
  498. obs_sceneitem_destroy(item);
  499. }
  500. void obs_sceneitem_remove(obs_sceneitem_t *item)
  501. {
  502. obs_scene_t *scene;
  503. if (!item)
  504. return;
  505. scene = item->parent;
  506. if (scene)
  507. pthread_mutex_lock(&scene->mutex);
  508. if (item->removed) {
  509. if (scene)
  510. pthread_mutex_unlock(&scene->mutex);
  511. return;
  512. }
  513. item->removed = true;
  514. assert(scene != NULL);
  515. assert(scene->source != NULL);
  516. obs_source_remove_child(scene->source, item->source);
  517. signal_item_remove(item);
  518. detach_sceneitem(item);
  519. pthread_mutex_unlock(&scene->mutex);
  520. obs_sceneitem_release(item);
  521. }
  522. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  523. {
  524. return item ? item->parent : NULL;
  525. }
  526. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  527. {
  528. return item ? item->source : NULL;
  529. }
  530. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  531. {
  532. struct calldata params = {0};
  533. const char *command = select ? "item_select" : "item_deselect";
  534. if (!item || item->selected == select)
  535. return;
  536. item->selected = select;
  537. calldata_set_ptr(&params, "scene", item->parent);
  538. calldata_set_ptr(&params, "item", item);
  539. signal_handler_signal(item->parent->source->context.signals,
  540. command, &params);
  541. calldata_free(&params);
  542. }
  543. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  544. {
  545. return item ? item->selected : false;
  546. }
  547. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  548. {
  549. if (item) {
  550. vec2_copy(&item->pos, pos);
  551. update_item_transform(item);
  552. }
  553. }
  554. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  555. {
  556. if (item) {
  557. item->rot = rot;
  558. update_item_transform(item);
  559. }
  560. }
  561. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  562. {
  563. if (item) {
  564. vec2_copy(&item->scale, scale);
  565. update_item_transform(item);
  566. }
  567. }
  568. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  569. {
  570. if (item) {
  571. item->align = alignment;
  572. update_item_transform(item);
  573. }
  574. }
  575. static inline void signal_reorder(struct obs_scene_item *item)
  576. {
  577. const char *command = NULL;
  578. struct calldata params = {0};
  579. command = "reorder";
  580. calldata_set_ptr(&params, "scene", item->parent);
  581. signal_handler_signal(item->parent->source->context.signals,
  582. command, &params);
  583. calldata_free(&params);
  584. }
  585. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  586. enum obs_order_movement movement)
  587. {
  588. if (!item) return;
  589. struct obs_scene_item *next, *prev;
  590. struct obs_scene *scene = item->parent;
  591. obs_scene_addref(scene);
  592. pthread_mutex_lock(&scene->mutex);
  593. next = item->next;
  594. prev = item->prev;
  595. detach_sceneitem(item);
  596. if (movement == OBS_ORDER_MOVE_DOWN) {
  597. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  598. } else if (movement == OBS_ORDER_MOVE_UP) {
  599. attach_sceneitem(scene, item, next ? next : prev);
  600. } else if (movement == OBS_ORDER_MOVE_TOP) {
  601. struct obs_scene_item *last = next;
  602. if (!last) {
  603. last = prev;
  604. } else {
  605. while (last->next)
  606. last = last->next;
  607. }
  608. attach_sceneitem(scene, item, last);
  609. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  610. attach_sceneitem(scene, item, NULL);
  611. }
  612. signal_reorder(item);
  613. pthread_mutex_unlock(&scene->mutex);
  614. obs_scene_release(scene);
  615. }
  616. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  617. int position)
  618. {
  619. if (!item) return;
  620. struct obs_scene *scene = item->parent;
  621. struct obs_scene_item *next;
  622. obs_scene_addref(scene);
  623. pthread_mutex_lock(&scene->mutex);
  624. detach_sceneitem(item);
  625. next = scene->first_item;
  626. if (position == 0) {
  627. attach_sceneitem(scene, item, NULL);
  628. } else {
  629. for (int i = position; i > 1; --i) {
  630. if (next->next == NULL)
  631. break;
  632. next = next->next;
  633. }
  634. attach_sceneitem(scene, item, next);
  635. }
  636. signal_reorder(item);
  637. pthread_mutex_unlock(&scene->mutex);
  638. obs_scene_release(scene);
  639. }
  640. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  641. enum obs_bounds_type type)
  642. {
  643. if (item) {
  644. item->bounds_type = type;
  645. update_item_transform(item);
  646. }
  647. }
  648. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  649. uint32_t alignment)
  650. {
  651. if (item) {
  652. item->bounds_align = alignment;
  653. update_item_transform(item);
  654. }
  655. }
  656. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  657. {
  658. if (item) {
  659. item->bounds = *bounds;
  660. update_item_transform(item);
  661. }
  662. }
  663. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  664. {
  665. if (item)
  666. vec2_copy(pos, &item->pos);
  667. }
  668. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  669. {
  670. return item ? item->rot : 0.0f;
  671. }
  672. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  673. {
  674. if (item)
  675. vec2_copy(scale, &item->scale);
  676. }
  677. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  678. {
  679. return item ? item->align : 0;
  680. }
  681. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  682. {
  683. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  684. }
  685. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  686. {
  687. return item ? item->bounds_align : 0;
  688. }
  689. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  690. {
  691. if (item)
  692. *bounds = item->bounds;
  693. }
  694. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  695. struct obs_transform_info *info)
  696. {
  697. if (item && info) {
  698. info->pos = item->pos;
  699. info->rot = item->rot;
  700. info->scale = item->scale;
  701. info->alignment = item->align;
  702. info->bounds_type = item->bounds_type;
  703. info->bounds_alignment = item->bounds_align;
  704. info->bounds = item->bounds;
  705. }
  706. }
  707. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  708. const struct obs_transform_info *info)
  709. {
  710. if (item && info) {
  711. item->pos = info->pos;
  712. item->rot = info->rot;
  713. item->scale = info->scale;
  714. item->align = info->alignment;
  715. item->bounds_type = info->bounds_type;
  716. item->bounds_align = info->bounds_alignment;
  717. item->bounds = info->bounds;
  718. update_item_transform(item);
  719. }
  720. }
  721. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  722. struct matrix4 *transform)
  723. {
  724. if (item)
  725. matrix4_copy(transform, &item->draw_transform);
  726. }
  727. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  728. struct matrix4 *transform)
  729. {
  730. if (item)
  731. matrix4_copy(transform, &item->box_transform);
  732. }
  733. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  734. {
  735. return item ? item->visible : false;
  736. }
  737. void obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  738. {
  739. struct calldata cd = {0};
  740. if (!item)
  741. return;
  742. item->visible = visible;
  743. if (!item->parent)
  744. return;
  745. calldata_set_ptr(&cd, "scene", item->parent);
  746. calldata_set_ptr(&cd, "item", item);
  747. calldata_set_bool(&cd, "visible", visible);
  748. signal_handler_signal(item->parent->source->context.signals,
  749. "item_visible", &cd);
  750. calldata_free(&cd);
  751. }