obs-scene.c 21 KB

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