obs-scene.c 21 KB

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