obs-scene.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. 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_getwidth(item->source);
  182. uint32_t height = obs_source_getheight(item->source);
  183. uint32_t cx = width;
  184. uint32_t cy = height;
  185. struct vec2 base_origin = {0.0f, 0.0f};
  186. struct vec2 origin = {0.0f, 0.0f};
  187. struct vec2 scale = item->scale;
  188. struct calldata params = {0};
  189. /* ----------------------- */
  190. if (item->bounds_type != OBS_BOUNDS_NONE) {
  191. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  192. } else {
  193. cx = (uint32_t)((float)cx * scale.x);
  194. cy = (uint32_t)((float)cy * scale.y);
  195. }
  196. add_alignment(&origin, item->align, (int)cx, (int)cy);
  197. matrix4_identity(&item->draw_transform);
  198. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  199. scale.x, scale.y, 1.0f);
  200. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  201. -origin.x, -origin.y, 0.0f);
  202. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  203. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  204. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  205. item->pos.x, item->pos.y, 0.0f);
  206. /* ----------------------- */
  207. if (item->bounds_type != OBS_BOUNDS_NONE) {
  208. vec2_copy(&scale, &item->bounds);
  209. } else {
  210. scale.x = (float)width * item->scale.x;
  211. scale.y = (float)height * item->scale.y;
  212. }
  213. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  214. matrix4_identity(&item->box_transform);
  215. matrix4_scale3f(&item->box_transform, &item->box_transform,
  216. scale.x, scale.y, 1.0f);
  217. matrix4_translate3f(&item->box_transform, &item->box_transform,
  218. -base_origin.x, -base_origin.y, 0.0f);
  219. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  220. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  221. matrix4_translate3f(&item->box_transform, &item->box_transform,
  222. item->pos.x, item->pos.y, 0.0f);
  223. /* ----------------------- */
  224. item->last_width = width;
  225. item->last_height = height;
  226. calldata_setptr(&params, "scene", item->parent);
  227. calldata_setptr(&params, "item", item);
  228. signal_handler_signal(item->parent->source->context.signals,
  229. "item_transform", &params);
  230. calldata_free(&params);
  231. }
  232. static inline bool source_size_changed(struct obs_scene_item *item)
  233. {
  234. uint32_t width = obs_source_getwidth(item->source);
  235. uint32_t height = obs_source_getheight(item->source);
  236. return item->last_width != width || item->last_height != height;
  237. }
  238. static void scene_video_render(void *data, effect_t effect)
  239. {
  240. struct obs_scene *scene = data;
  241. struct obs_scene_item *item;
  242. pthread_mutex_lock(&scene->mutex);
  243. item = scene->first_item;
  244. while (item) {
  245. if (obs_source_removed(item->source)) {
  246. struct obs_scene_item *del_item = item;
  247. item = item->next;
  248. obs_sceneitem_remove(del_item);
  249. continue;
  250. }
  251. if (source_size_changed(item))
  252. update_item_transform(item);
  253. gs_matrix_push();
  254. gs_matrix_mul(&item->draw_transform);
  255. obs_source_video_render(item->source);
  256. gs_matrix_pop();
  257. item = item->next;
  258. }
  259. pthread_mutex_unlock(&scene->mutex);
  260. UNUSED_PARAMETER(effect);
  261. }
  262. static void scene_load_item(struct obs_scene *scene, obs_data_t item_data)
  263. {
  264. const char *name = obs_data_getstring(item_data, "name");
  265. obs_source_t source = obs_get_source_by_name(name);
  266. struct obs_scene_item *item;
  267. if (!source) {
  268. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  269. name);
  270. return;
  271. }
  272. item = obs_scene_add(scene, source);
  273. obs_data_set_default_int(item_data, "align",
  274. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  275. item->rot = (float)obs_data_getdouble(item_data, "rot");
  276. item->align = (uint32_t)obs_data_getint(item_data, "align");
  277. item->visible = obs_data_getbool(item_data, "visible");
  278. obs_data_get_vec2(item_data, "pos", &item->pos);
  279. obs_data_get_vec2(item_data, "scale", &item->scale);
  280. item->bounds_type =
  281. (enum obs_bounds_type)obs_data_getint(item_data, "bounds_type");
  282. item->bounds_align =
  283. (uint32_t)obs_data_getint(item_data, "bounds_align");
  284. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  285. obs_source_release(source);
  286. update_item_transform(item);
  287. }
  288. static void scene_load(void *scene, obs_data_t settings)
  289. {
  290. obs_data_array_t items = obs_data_getarray(settings, "items");
  291. size_t count, i;
  292. remove_all_items(scene);
  293. if (!items) return;
  294. count = obs_data_array_count(items);
  295. for (i = 0; i < count; i++) {
  296. obs_data_t item_data = obs_data_array_item(items, i);
  297. scene_load_item(scene, item_data);
  298. obs_data_release(item_data);
  299. }
  300. obs_data_array_release(items);
  301. }
  302. static void scene_save_item(obs_data_array_t array, struct obs_scene_item *item)
  303. {
  304. obs_data_t item_data = obs_data_create();
  305. const char *name = obs_source_getname(item->source);
  306. obs_data_setstring(item_data, "name", name);
  307. obs_data_setbool (item_data, "visible", item->visible);
  308. obs_data_setdouble(item_data, "rot", item->rot);
  309. obs_data_set_vec2 (item_data, "pos", &item->pos);
  310. obs_data_set_vec2 (item_data, "scale", &item->scale);
  311. obs_data_setint (item_data, "align", (int)item->align);
  312. obs_data_setint (item_data, "bounds_type", (int)item->bounds_type);
  313. obs_data_setint (item_data, "bounds_align", (int)item->bounds_align);
  314. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  315. obs_data_array_push_back(array, item_data);
  316. obs_data_release(item_data);
  317. }
  318. static void scene_save(void *data, obs_data_t settings)
  319. {
  320. struct obs_scene *scene = data;
  321. obs_data_array_t array = obs_data_array_create();
  322. struct obs_scene_item *item;
  323. pthread_mutex_lock(&scene->mutex);
  324. item = scene->first_item;
  325. while (item) {
  326. scene_save_item(array, item);
  327. item = item->next;
  328. }
  329. pthread_mutex_unlock(&scene->mutex);
  330. obs_data_setarray(settings, "items", array);
  331. obs_data_array_release(array);
  332. }
  333. static uint32_t scene_getwidth(void *data)
  334. {
  335. UNUSED_PARAMETER(data);
  336. return obs->video.base_width;
  337. }
  338. static uint32_t scene_getheight(void *data)
  339. {
  340. UNUSED_PARAMETER(data);
  341. return obs->video.base_height;
  342. }
  343. const struct obs_source_info scene_info =
  344. {
  345. .id = "scene",
  346. .type = OBS_SOURCE_TYPE_INPUT,
  347. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  348. .getname = scene_getname,
  349. .create = scene_create,
  350. .destroy = scene_destroy,
  351. .video_render = scene_video_render,
  352. .getwidth = scene_getwidth,
  353. .getheight = scene_getheight,
  354. .load = scene_load,
  355. .save = scene_save,
  356. .enum_sources = scene_enum_sources
  357. };
  358. obs_scene_t obs_scene_create(const char *name)
  359. {
  360. struct obs_source *source =
  361. obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
  362. return source->context.data;
  363. }
  364. void obs_scene_addref(obs_scene_t scene)
  365. {
  366. if (scene)
  367. obs_source_addref(scene->source);
  368. }
  369. void obs_scene_release(obs_scene_t scene)
  370. {
  371. if (scene)
  372. obs_source_release(scene->source);
  373. }
  374. obs_source_t obs_scene_getsource(obs_scene_t scene)
  375. {
  376. return scene ? scene->source : NULL;
  377. }
  378. obs_scene_t obs_scene_fromsource(obs_source_t source)
  379. {
  380. if (!source || source->info.id != scene_info.id)
  381. return NULL;
  382. return source->context.data;
  383. }
  384. obs_sceneitem_t obs_scene_findsource(obs_scene_t scene, const char *name)
  385. {
  386. struct obs_scene_item *item;
  387. if (!scene)
  388. return NULL;
  389. pthread_mutex_lock(&scene->mutex);
  390. item = scene->first_item;
  391. while (item) {
  392. if (strcmp(item->source->context.name, name) == 0)
  393. break;
  394. item = item->next;
  395. }
  396. pthread_mutex_unlock(&scene->mutex);
  397. return item;
  398. }
  399. void obs_scene_enum_items(obs_scene_t scene,
  400. bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
  401. void *param)
  402. {
  403. struct obs_scene_item *item;
  404. if (!scene || !callback)
  405. return;
  406. pthread_mutex_lock(&scene->mutex);
  407. item = scene->first_item;
  408. while (item) {
  409. struct obs_scene_item *next = item->next;
  410. obs_sceneitem_addref(item);
  411. if (!callback(scene, item, param)) {
  412. obs_sceneitem_release(item);
  413. break;
  414. }
  415. obs_sceneitem_release(item);
  416. item = next;
  417. }
  418. pthread_mutex_unlock(&scene->mutex);
  419. }
  420. obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
  421. {
  422. struct obs_scene_item *last;
  423. struct obs_scene_item *item;
  424. struct calldata params = {0};
  425. if (!scene)
  426. return NULL;
  427. if (!source) {
  428. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  429. return NULL;
  430. }
  431. item = bzalloc(sizeof(struct obs_scene_item));
  432. item->source = source;
  433. item->visible = true;
  434. item->parent = scene;
  435. item->ref = 1;
  436. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  437. vec2_set(&item->scale, 1.0f, 1.0f);
  438. matrix4_identity(&item->draw_transform);
  439. matrix4_identity(&item->box_transform);
  440. obs_source_addref(source);
  441. obs_source_add_child(scene->source, source);
  442. pthread_mutex_lock(&scene->mutex);
  443. last = scene->first_item;
  444. if (!last) {
  445. scene->first_item = item;
  446. } else {
  447. while (last->next)
  448. last = last->next;
  449. last->next = item;
  450. item->prev = last;
  451. }
  452. pthread_mutex_unlock(&scene->mutex);
  453. calldata_setptr(&params, "scene", scene);
  454. calldata_setptr(&params, "item", item);
  455. signal_handler_signal(scene->source->context.signals, "item_add",
  456. &params);
  457. calldata_free(&params);
  458. return item;
  459. }
  460. static void obs_sceneitem_destroy(obs_sceneitem_t item)
  461. {
  462. if (item) {
  463. if (item->source)
  464. obs_source_release(item->source);
  465. bfree(item);
  466. }
  467. }
  468. void obs_sceneitem_addref(obs_sceneitem_t item)
  469. {
  470. if (item)
  471. os_atomic_inc_long(&item->ref);
  472. }
  473. void obs_sceneitem_release(obs_sceneitem_t item)
  474. {
  475. if (!item)
  476. return;
  477. if (os_atomic_dec_long(&item->ref) == 0)
  478. obs_sceneitem_destroy(item);
  479. }
  480. void obs_sceneitem_remove(obs_sceneitem_t item)
  481. {
  482. obs_scene_t scene;
  483. if (!item)
  484. return;
  485. scene = item->parent;
  486. if (scene)
  487. pthread_mutex_lock(&scene->mutex);
  488. if (item->removed) {
  489. if (scene)
  490. pthread_mutex_unlock(&scene->mutex);
  491. return;
  492. }
  493. item->removed = true;
  494. assert(scene != NULL);
  495. assert(scene->source != NULL);
  496. obs_source_remove_child(scene->source, item->source);
  497. signal_item_remove(item);
  498. detach_sceneitem(item);
  499. pthread_mutex_unlock(&scene->mutex);
  500. obs_sceneitem_release(item);
  501. }
  502. obs_scene_t obs_sceneitem_getscene(obs_sceneitem_t item)
  503. {
  504. return item ? item->parent : NULL;
  505. }
  506. obs_source_t obs_sceneitem_getsource(obs_sceneitem_t item)
  507. {
  508. return item ? item->source : NULL;
  509. }
  510. void obs_sceneitem_select(obs_sceneitem_t item, bool select)
  511. {
  512. struct calldata params = {0};
  513. const char *command = select ? "item_select" : "item_deselect";
  514. if (!item || item->selected == select)
  515. return;
  516. item->selected = select;
  517. calldata_setptr(&params, "scene", item->parent);
  518. calldata_setptr(&params, "item", item);
  519. signal_handler_signal(item->parent->source->context.signals,
  520. command, &params);
  521. calldata_free(&params);
  522. }
  523. bool obs_sceneitem_selected(obs_sceneitem_t item)
  524. {
  525. return item ? item->selected : false;
  526. }
  527. void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
  528. {
  529. if (item) {
  530. vec2_copy(&item->pos, pos);
  531. update_item_transform(item);
  532. }
  533. }
  534. void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
  535. {
  536. if (item) {
  537. item->rot = rot;
  538. update_item_transform(item);
  539. }
  540. }
  541. void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
  542. {
  543. if (item) {
  544. vec2_copy(&item->scale, scale);
  545. update_item_transform(item);
  546. }
  547. }
  548. void obs_sceneitem_setalignment(obs_sceneitem_t item, uint32_t alignment)
  549. {
  550. if (item) {
  551. item->align = alignment;
  552. update_item_transform(item);
  553. }
  554. }
  555. static inline void signal_move_dir(struct obs_scene_item *item,
  556. enum order_movement movement)
  557. {
  558. const char *command;
  559. struct calldata params = {0};
  560. switch (movement) {
  561. case ORDER_MOVE_UP: command = "item_move_up"; break;
  562. case ORDER_MOVE_DOWN: command = "item_move_down"; break;
  563. case ORDER_MOVE_TOP: command = "item_move_top"; break;
  564. case ORDER_MOVE_BOTTOM: command = "item_move_bottom"; break;
  565. }
  566. calldata_setptr(&params, "scene", item->parent);
  567. calldata_setptr(&params, "item", item);
  568. signal_handler_signal(item->parent->source->context.signals,
  569. command, &params);
  570. calldata_free(&params);
  571. }
  572. void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
  573. {
  574. if (!item) return;
  575. struct obs_scene_item *next, *prev;
  576. struct obs_scene *scene = item->parent;
  577. obs_scene_addref(scene);
  578. pthread_mutex_lock(&scene->mutex);
  579. next = item->next;
  580. prev = item->prev;
  581. detach_sceneitem(item);
  582. if (movement == ORDER_MOVE_DOWN) {
  583. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  584. } else if (movement == ORDER_MOVE_UP) {
  585. attach_sceneitem(scene, item, next ? next : prev);
  586. } else if (movement == ORDER_MOVE_TOP) {
  587. struct obs_scene_item *last = next;
  588. if (!last) {
  589. last = prev;
  590. } else {
  591. while (last->next)
  592. last = last->next;
  593. }
  594. attach_sceneitem(scene, item, last);
  595. } else if (movement == ORDER_MOVE_BOTTOM) {
  596. attach_sceneitem(scene, item, NULL);
  597. }
  598. signal_move_dir(item, movement);
  599. pthread_mutex_unlock(&scene->mutex);
  600. obs_scene_release(scene);
  601. }
  602. void obs_sceneitem_set_bounds_type(obs_sceneitem_t item,
  603. enum obs_bounds_type type)
  604. {
  605. if (item) {
  606. item->bounds_type = type;
  607. update_item_transform(item);
  608. }
  609. }
  610. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t item,
  611. uint32_t alignment)
  612. {
  613. if (item) {
  614. item->bounds_align = alignment;
  615. update_item_transform(item);
  616. }
  617. }
  618. void obs_sceneitem_set_bounds(obs_sceneitem_t item, const struct vec2 *bounds)
  619. {
  620. if (item) {
  621. item->bounds = *bounds;
  622. update_item_transform(item);
  623. }
  624. }
  625. void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
  626. {
  627. if (item)
  628. vec2_copy(pos, &item->pos);
  629. }
  630. float obs_sceneitem_getrot(obs_sceneitem_t item)
  631. {
  632. return item ? item->rot : 0.0f;
  633. }
  634. void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
  635. {
  636. if (item)
  637. vec2_copy(scale, &item->scale);
  638. }
  639. uint32_t obs_sceneitem_getalignment(obs_sceneitem_t item)
  640. {
  641. return item ? item->align : 0;
  642. }
  643. enum obs_bounds_type obs_sceneitem_get_bounds_type(obs_sceneitem_t item)
  644. {
  645. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  646. }
  647. uint32_t obs_sceneitem_get_bounds_alignment(obs_sceneitem_t item)
  648. {
  649. return item ? item->bounds_align : 0;
  650. }
  651. void obs_sceneitem_get_bounds(obs_sceneitem_t item, struct vec2 *bounds)
  652. {
  653. if (item)
  654. *bounds = item->bounds;
  655. }
  656. void obs_sceneitem_get_info(obs_sceneitem_t item,
  657. struct obs_sceneitem_info *info)
  658. {
  659. if (item && info) {
  660. info->pos = item->pos;
  661. info->rot = item->rot;
  662. info->scale = item->scale;
  663. info->alignment = item->align;
  664. info->bounds_type = item->bounds_type;
  665. info->bounds_alignment = item->bounds_align;
  666. info->bounds = item->bounds;
  667. }
  668. }
  669. void obs_sceneitem_set_info(obs_sceneitem_t item,
  670. const struct obs_sceneitem_info *info)
  671. {
  672. if (item && info) {
  673. item->pos = info->pos;
  674. item->rot = info->rot;
  675. item->scale = info->scale;
  676. item->align = info->alignment;
  677. item->bounds_type = info->bounds_type;
  678. item->bounds_align = info->bounds_alignment;
  679. item->bounds = info->bounds;
  680. update_item_transform(item);
  681. }
  682. }
  683. void obs_sceneitem_get_draw_transform(obs_sceneitem_t item,
  684. struct matrix4 *transform)
  685. {
  686. if (item)
  687. matrix4_copy(transform, &item->draw_transform);
  688. }
  689. void obs_sceneitem_get_box_transform(obs_sceneitem_t item,
  690. struct matrix4 *transform)
  691. {
  692. if (item)
  693. matrix4_copy(transform, &item->box_transform);
  694. }