obs-scene.c 21 KB

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