obs-scene.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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(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_signalhandler(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_getwidth(item->source);
  181. uint32_t height = obs_source_getheight(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_setptr(&params, "scene", item->parent);
  228. calldata_setptr(&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_getwidth(item->source);
  236. uint32_t height = obs_source_getheight(item->source);
  237. return item->last_width != width || item->last_height != height;
  238. }
  239. static void scene_video_render(void *data, 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_getstring(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_getdouble(item_data, "rot");
  277. item->align = (uint32_t)obs_data_getint(item_data, "align");
  278. item->visible = obs_data_getbool(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_getint(item_data, "bounds_type");
  283. item->bounds_align =
  284. (uint32_t)obs_data_getint(item_data, "bounds_align");
  285. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  286. obs_source_release(source);
  287. update_item_transform(item);
  288. }
  289. static void scene_load(void *scene, obs_data_t settings)
  290. {
  291. obs_data_array_t items = obs_data_getarray(settings, "items");
  292. size_t count, i;
  293. remove_all_items(scene);
  294. if (!items) return;
  295. count = obs_data_array_count(items);
  296. for (i = 0; i < count; i++) {
  297. obs_data_t item_data = obs_data_array_item(items, i);
  298. scene_load_item(scene, item_data);
  299. obs_data_release(item_data);
  300. }
  301. obs_data_array_release(items);
  302. }
  303. static void scene_save_item(obs_data_array_t array, struct obs_scene_item *item)
  304. {
  305. obs_data_t item_data = obs_data_create();
  306. const char *name = obs_source_getname(item->source);
  307. obs_data_setstring(item_data, "name", name);
  308. obs_data_setbool (item_data, "visible", item->visible);
  309. obs_data_setdouble(item_data, "rot", item->rot);
  310. obs_data_set_vec2 (item_data, "pos", &item->pos);
  311. obs_data_set_vec2 (item_data, "scale", &item->scale);
  312. obs_data_setint (item_data, "align", (int)item->align);
  313. obs_data_setint (item_data, "bounds_type", (int)item->bounds_type);
  314. obs_data_setint (item_data, "bounds_align", (int)item->bounds_align);
  315. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  316. obs_data_array_push_back(array, item_data);
  317. obs_data_release(item_data);
  318. }
  319. static void scene_save(void *data, obs_data_t settings)
  320. {
  321. struct obs_scene *scene = data;
  322. obs_data_array_t array = obs_data_array_create();
  323. struct obs_scene_item *item;
  324. pthread_mutex_lock(&scene->mutex);
  325. item = scene->first_item;
  326. while (item) {
  327. scene_save_item(array, item);
  328. item = item->next;
  329. }
  330. pthread_mutex_unlock(&scene->mutex);
  331. obs_data_setarray(settings, "items", array);
  332. obs_data_array_release(array);
  333. }
  334. static uint32_t scene_getwidth(void *data)
  335. {
  336. UNUSED_PARAMETER(data);
  337. return obs->video.base_width;
  338. }
  339. static uint32_t scene_getheight(void *data)
  340. {
  341. UNUSED_PARAMETER(data);
  342. return obs->video.base_height;
  343. }
  344. const struct obs_source_info scene_info =
  345. {
  346. .id = "scene",
  347. .type = OBS_SOURCE_TYPE_INPUT,
  348. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  349. .getname = scene_getname,
  350. .create = scene_create,
  351. .destroy = scene_destroy,
  352. .video_render = scene_video_render,
  353. .getwidth = scene_getwidth,
  354. .getheight = scene_getheight,
  355. .load = scene_load,
  356. .save = scene_save,
  357. .enum_sources = scene_enum_sources
  358. };
  359. obs_scene_t obs_scene_create(const char *name)
  360. {
  361. struct obs_source *source =
  362. obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
  363. return source->context.data;
  364. }
  365. void obs_scene_addref(obs_scene_t scene)
  366. {
  367. if (scene)
  368. obs_source_addref(scene->source);
  369. }
  370. void obs_scene_release(obs_scene_t scene)
  371. {
  372. if (scene)
  373. obs_source_release(scene->source);
  374. }
  375. obs_source_t obs_scene_getsource(obs_scene_t scene)
  376. {
  377. return scene ? scene->source : NULL;
  378. }
  379. obs_scene_t obs_scene_fromsource(obs_source_t source)
  380. {
  381. if (!source || source->info.id != scene_info.id)
  382. return NULL;
  383. return source->context.data;
  384. }
  385. obs_sceneitem_t obs_scene_find_source(obs_scene_t scene, const char *name)
  386. {
  387. struct obs_scene_item *item;
  388. if (!scene)
  389. return NULL;
  390. pthread_mutex_lock(&scene->mutex);
  391. item = scene->first_item;
  392. while (item) {
  393. if (strcmp(item->source->context.name, name) == 0)
  394. break;
  395. item = item->next;
  396. }
  397. pthread_mutex_unlock(&scene->mutex);
  398. return item;
  399. }
  400. void obs_scene_enum_items(obs_scene_t scene,
  401. bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
  402. void *param)
  403. {
  404. struct obs_scene_item *item;
  405. if (!scene || !callback)
  406. return;
  407. pthread_mutex_lock(&scene->mutex);
  408. item = scene->first_item;
  409. while (item) {
  410. struct obs_scene_item *next = item->next;
  411. obs_sceneitem_addref(item);
  412. if (!callback(scene, item, param)) {
  413. obs_sceneitem_release(item);
  414. break;
  415. }
  416. obs_sceneitem_release(item);
  417. item = next;
  418. }
  419. pthread_mutex_unlock(&scene->mutex);
  420. }
  421. obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
  422. {
  423. struct obs_scene_item *last;
  424. struct obs_scene_item *item;
  425. struct calldata params = {0};
  426. if (!scene)
  427. return NULL;
  428. if (!source) {
  429. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  430. return NULL;
  431. }
  432. item = bzalloc(sizeof(struct obs_scene_item));
  433. item->source = source;
  434. item->visible = true;
  435. item->parent = scene;
  436. item->ref = 1;
  437. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  438. vec2_set(&item->scale, 1.0f, 1.0f);
  439. matrix4_identity(&item->draw_transform);
  440. matrix4_identity(&item->box_transform);
  441. obs_source_addref(source);
  442. obs_source_add_child(scene->source, source);
  443. pthread_mutex_lock(&scene->mutex);
  444. last = scene->first_item;
  445. if (!last) {
  446. scene->first_item = item;
  447. } else {
  448. while (last->next)
  449. last = last->next;
  450. last->next = item;
  451. item->prev = last;
  452. }
  453. pthread_mutex_unlock(&scene->mutex);
  454. calldata_setptr(&params, "scene", scene);
  455. calldata_setptr(&params, "item", item);
  456. signal_handler_signal(scene->source->context.signals, "item_add",
  457. &params);
  458. calldata_free(&params);
  459. return item;
  460. }
  461. static void obs_sceneitem_destroy(obs_sceneitem_t item)
  462. {
  463. if (item) {
  464. if (item->source)
  465. obs_source_release(item->source);
  466. bfree(item);
  467. }
  468. }
  469. void obs_sceneitem_addref(obs_sceneitem_t item)
  470. {
  471. if (item)
  472. os_atomic_inc_long(&item->ref);
  473. }
  474. void obs_sceneitem_release(obs_sceneitem_t item)
  475. {
  476. if (!item)
  477. return;
  478. if (os_atomic_dec_long(&item->ref) == 0)
  479. obs_sceneitem_destroy(item);
  480. }
  481. void obs_sceneitem_remove(obs_sceneitem_t item)
  482. {
  483. obs_scene_t scene;
  484. if (!item)
  485. return;
  486. scene = item->parent;
  487. if (scene)
  488. pthread_mutex_lock(&scene->mutex);
  489. if (item->removed) {
  490. if (scene)
  491. pthread_mutex_unlock(&scene->mutex);
  492. return;
  493. }
  494. item->removed = true;
  495. assert(scene != NULL);
  496. assert(scene->source != NULL);
  497. obs_source_remove_child(scene->source, item->source);
  498. signal_item_remove(item);
  499. detach_sceneitem(item);
  500. pthread_mutex_unlock(&scene->mutex);
  501. obs_sceneitem_release(item);
  502. }
  503. obs_scene_t obs_sceneitem_get_scene(obs_sceneitem_t item)
  504. {
  505. return item ? item->parent : NULL;
  506. }
  507. obs_source_t obs_sceneitem_get_source(obs_sceneitem_t item)
  508. {
  509. return item ? item->source : NULL;
  510. }
  511. void obs_sceneitem_select(obs_sceneitem_t item, bool select)
  512. {
  513. struct calldata params = {0};
  514. const char *command = select ? "item_select" : "item_deselect";
  515. if (!item || item->selected == select)
  516. return;
  517. item->selected = select;
  518. calldata_setptr(&params, "scene", item->parent);
  519. calldata_setptr(&params, "item", item);
  520. signal_handler_signal(item->parent->source->context.signals,
  521. command, &params);
  522. calldata_free(&params);
  523. }
  524. bool obs_sceneitem_selected(obs_sceneitem_t item)
  525. {
  526. return item ? item->selected : false;
  527. }
  528. void obs_sceneitem_set_pos(obs_sceneitem_t item, const struct vec2 *pos)
  529. {
  530. if (item) {
  531. vec2_copy(&item->pos, pos);
  532. update_item_transform(item);
  533. }
  534. }
  535. void obs_sceneitem_set_rot(obs_sceneitem_t item, float rot)
  536. {
  537. if (item) {
  538. item->rot = rot;
  539. update_item_transform(item);
  540. }
  541. }
  542. void obs_sceneitem_set_scale(obs_sceneitem_t item, const struct vec2 *scale)
  543. {
  544. if (item) {
  545. vec2_copy(&item->scale, scale);
  546. update_item_transform(item);
  547. }
  548. }
  549. void obs_sceneitem_set_alignment(obs_sceneitem_t item, uint32_t alignment)
  550. {
  551. if (item) {
  552. item->align = alignment;
  553. update_item_transform(item);
  554. }
  555. }
  556. static inline void signal_move_dir(struct obs_scene_item *item,
  557. enum obs_order_movement movement)
  558. {
  559. const char *command = NULL;
  560. struct calldata params = {0};
  561. switch (movement) {
  562. case OBS_ORDER_MOVE_UP: command = "item_move_up"; break;
  563. case OBS_ORDER_MOVE_DOWN: command = "item_move_down"; break;
  564. case OBS_ORDER_MOVE_TOP: command = "item_move_top"; break;
  565. case OBS_ORDER_MOVE_BOTTOM: command = "item_move_bottom"; break;
  566. }
  567. calldata_setptr(&params, "scene", item->parent);
  568. calldata_setptr(&params, "item", item);
  569. signal_handler_signal(item->parent->source->context.signals,
  570. command, &params);
  571. calldata_free(&params);
  572. }
  573. void obs_sceneitem_set_order(obs_sceneitem_t item,
  574. enum obs_order_movement movement)
  575. {
  576. if (!item) return;
  577. struct obs_scene_item *next, *prev;
  578. struct obs_scene *scene = item->parent;
  579. obs_scene_addref(scene);
  580. pthread_mutex_lock(&scene->mutex);
  581. next = item->next;
  582. prev = item->prev;
  583. detach_sceneitem(item);
  584. if (movement == OBS_ORDER_MOVE_DOWN) {
  585. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  586. } else if (movement == OBS_ORDER_MOVE_UP) {
  587. attach_sceneitem(scene, item, next ? next : prev);
  588. } else if (movement == OBS_ORDER_MOVE_TOP) {
  589. struct obs_scene_item *last = next;
  590. if (!last) {
  591. last = prev;
  592. } else {
  593. while (last->next)
  594. last = last->next;
  595. }
  596. attach_sceneitem(scene, item, last);
  597. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  598. attach_sceneitem(scene, item, NULL);
  599. }
  600. signal_move_dir(item, movement);
  601. pthread_mutex_unlock(&scene->mutex);
  602. obs_scene_release(scene);
  603. }
  604. void obs_sceneitem_set_bounds_type(obs_sceneitem_t item,
  605. enum obs_bounds_type type)
  606. {
  607. if (item) {
  608. item->bounds_type = type;
  609. update_item_transform(item);
  610. }
  611. }
  612. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t item,
  613. uint32_t alignment)
  614. {
  615. if (item) {
  616. item->bounds_align = alignment;
  617. update_item_transform(item);
  618. }
  619. }
  620. void obs_sceneitem_set_bounds(obs_sceneitem_t item, const struct vec2 *bounds)
  621. {
  622. if (item) {
  623. item->bounds = *bounds;
  624. update_item_transform(item);
  625. }
  626. }
  627. void obs_sceneitem_get_pos(obs_sceneitem_t item, struct vec2 *pos)
  628. {
  629. if (item)
  630. vec2_copy(pos, &item->pos);
  631. }
  632. float obs_sceneitem_get_rot(obs_sceneitem_t item)
  633. {
  634. return item ? item->rot : 0.0f;
  635. }
  636. void obs_sceneitem_get_scale(obs_sceneitem_t item, struct vec2 *scale)
  637. {
  638. if (item)
  639. vec2_copy(scale, &item->scale);
  640. }
  641. uint32_t obs_sceneitem_get_alignment(obs_sceneitem_t item)
  642. {
  643. return item ? item->align : 0;
  644. }
  645. enum obs_bounds_type obs_sceneitem_get_bounds_type(obs_sceneitem_t item)
  646. {
  647. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  648. }
  649. uint32_t obs_sceneitem_get_bounds_alignment(obs_sceneitem_t item)
  650. {
  651. return item ? item->bounds_align : 0;
  652. }
  653. void obs_sceneitem_get_bounds(obs_sceneitem_t item, struct vec2 *bounds)
  654. {
  655. if (item)
  656. *bounds = item->bounds;
  657. }
  658. void obs_sceneitem_get_info(obs_sceneitem_t item,
  659. struct obs_transform_info *info)
  660. {
  661. if (item && info) {
  662. info->pos = item->pos;
  663. info->rot = item->rot;
  664. info->scale = item->scale;
  665. info->alignment = item->align;
  666. info->bounds_type = item->bounds_type;
  667. info->bounds_alignment = item->bounds_align;
  668. info->bounds = item->bounds;
  669. }
  670. }
  671. void obs_sceneitem_set_info(obs_sceneitem_t item,
  672. const struct obs_transform_info *info)
  673. {
  674. if (item && info) {
  675. item->pos = info->pos;
  676. item->rot = info->rot;
  677. item->scale = info->scale;
  678. item->align = info->alignment;
  679. item->bounds_type = info->bounds_type;
  680. item->bounds_align = info->bounds_alignment;
  681. item->bounds = info->bounds;
  682. update_item_transform(item);
  683. }
  684. }
  685. void obs_sceneitem_get_draw_transform(obs_sceneitem_t item,
  686. struct matrix4 *transform)
  687. {
  688. if (item)
  689. matrix4_copy(transform, &item->draw_transform);
  690. }
  691. void obs_sceneitem_get_box_transform(obs_sceneitem_t item,
  692. struct matrix4 *transform)
  693. {
  694. if (item)
  695. matrix4_copy(transform, &item->box_transform);
  696. }