canvas_item.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. /**************************************************************************/
  2. /* canvas_item.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "canvas_item.h"
  31. #include "canvas_item.compat.inc"
  32. #include "scene/2d/canvas_group.h"
  33. #include "scene/main/canvas_layer.h"
  34. #include "scene/main/window.h"
  35. #include "scene/resources/atlas_texture.h"
  36. #include "scene/resources/font.h"
  37. #include "scene/resources/multimesh.h"
  38. #include "scene/resources/style_box.h"
  39. #include "scene/resources/world_2d.h"
  40. #define ERR_DRAW_GUARD \
  41. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside this node's `_draw()`, functions connected to its `draw` signal, or when it receives NOTIFICATION_DRAW.")
  42. #ifdef DEBUG_ENABLED
  43. bool CanvasItem::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  44. if (_edit_use_rect()) {
  45. return _edit_get_rect().has_point(p_point);
  46. } else {
  47. return p_point.length() < p_tolerance;
  48. }
  49. }
  50. #endif // DEBUG_ENABLED
  51. #ifdef TOOLS_ENABLED
  52. Transform2D CanvasItem::_edit_get_transform() const {
  53. return Transform2D(_edit_get_rotation(), _edit_get_position() + _edit_get_pivot());
  54. }
  55. #endif //TOOLS_ENABLED
  56. bool CanvasItem::is_visible_in_tree() const {
  57. ERR_READ_THREAD_GUARD_V(false);
  58. return visible && parent_visible_in_tree;
  59. }
  60. void CanvasItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
  61. parent_visible_in_tree = p_parent_visible_in_tree;
  62. if (!visible) {
  63. return;
  64. }
  65. _handle_visibility_change(p_parent_visible_in_tree);
  66. }
  67. void CanvasItem::set_visible(bool p_visible) {
  68. ERR_MAIN_THREAD_GUARD;
  69. if (visible == p_visible) {
  70. return;
  71. }
  72. visible = p_visible;
  73. if (!parent_visible_in_tree) {
  74. notification(NOTIFICATION_VISIBILITY_CHANGED);
  75. return;
  76. }
  77. _handle_visibility_change(p_visible);
  78. }
  79. void CanvasItem::_handle_visibility_change(bool p_visible) {
  80. RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, p_visible);
  81. notification(NOTIFICATION_VISIBILITY_CHANGED);
  82. if (p_visible) {
  83. queue_redraw();
  84. } else {
  85. emit_signal(SceneStringName(hidden));
  86. }
  87. _block();
  88. for (int i = 0; i < get_child_count(); i++) {
  89. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  90. if (c) { // Should the top_levels stop propagation? I think so, but...
  91. c->_propagate_visibility_changed(p_visible);
  92. }
  93. }
  94. _unblock();
  95. }
  96. void CanvasItem::show() {
  97. ERR_MAIN_THREAD_GUARD;
  98. set_visible(true);
  99. }
  100. void CanvasItem::hide() {
  101. ERR_MAIN_THREAD_GUARD;
  102. set_visible(false);
  103. }
  104. bool CanvasItem::is_visible() const {
  105. ERR_READ_THREAD_GUARD_V(false);
  106. return visible;
  107. }
  108. CanvasItem *CanvasItem::current_item_drawn = nullptr;
  109. CanvasItem *CanvasItem::get_current_item_drawn() {
  110. return current_item_drawn;
  111. }
  112. void CanvasItem::_redraw_callback() {
  113. if (!is_inside_tree()) {
  114. pending_update = false;
  115. return;
  116. }
  117. RenderingServer::get_singleton()->canvas_item_clear(get_canvas_item());
  118. //todo updating = true - only allow drawing here
  119. if (is_visible_in_tree()) {
  120. drawing = true;
  121. current_item_drawn = this;
  122. notification(NOTIFICATION_DRAW);
  123. emit_signal(SceneStringName(draw));
  124. GDVIRTUAL_CALL(_draw);
  125. current_item_drawn = nullptr;
  126. drawing = false;
  127. }
  128. //todo updating = false
  129. pending_update = false; // don't change to false until finished drawing (avoid recursive update)
  130. }
  131. Transform2D CanvasItem::get_global_transform_with_canvas() const {
  132. ERR_READ_THREAD_GUARD_V(Transform2D());
  133. if (canvas_layer) {
  134. return canvas_layer->get_final_transform() * get_global_transform();
  135. } else if (is_inside_tree()) {
  136. return get_viewport()->get_canvas_transform() * get_global_transform();
  137. } else {
  138. return get_global_transform();
  139. }
  140. }
  141. Transform2D CanvasItem::get_screen_transform() const {
  142. ERR_READ_THREAD_GUARD_V(Transform2D());
  143. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  144. return get_viewport()->get_popup_base_transform() * get_global_transform_with_canvas();
  145. }
  146. Transform2D CanvasItem::get_global_transform() const {
  147. ERR_READ_THREAD_GUARD_V(Transform2D());
  148. if (_is_global_invalid()) {
  149. // This code can enter multiple times from threads if dirty, this is expected.
  150. const CanvasItem *pi = get_parent_item();
  151. Transform2D new_global;
  152. if (pi) {
  153. new_global = pi->get_global_transform() * get_transform();
  154. } else {
  155. new_global = get_transform();
  156. }
  157. global_transform = new_global;
  158. _set_global_invalid(false);
  159. }
  160. return global_transform;
  161. }
  162. void CanvasItem::_set_global_invalid(bool p_invalid) const {
  163. if (is_group_processing()) {
  164. if (p_invalid) {
  165. global_invalid.mt.set();
  166. } else {
  167. global_invalid.mt.clear();
  168. }
  169. } else {
  170. global_invalid.st = p_invalid;
  171. }
  172. }
  173. void CanvasItem::_top_level_raise_self() {
  174. if (!is_inside_tree()) {
  175. return;
  176. }
  177. if (canvas_layer) {
  178. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, canvas_layer->get_sort_index());
  179. } else {
  180. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_viewport()->gui_get_canvas_sort_index());
  181. }
  182. }
  183. void CanvasItem::_enter_canvas() {
  184. // Resolves to nullptr if the node is top_level.
  185. CanvasItem *parent_item = get_parent_item();
  186. if (get_parent()) {
  187. get_viewport()->canvas_parent_mark_dirty(get_parent());
  188. }
  189. if (parent_item) {
  190. canvas_layer = parent_item->canvas_layer;
  191. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, parent_item->get_canvas_item());
  192. RenderingServer::get_singleton()->canvas_item_set_visibility_layer(canvas_item, visibility_layer);
  193. } else {
  194. Node *n = this;
  195. canvas_layer = nullptr;
  196. while (n) {
  197. canvas_layer = Object::cast_to<CanvasLayer>(n);
  198. if (canvas_layer) {
  199. break;
  200. }
  201. if (Object::cast_to<Viewport>(n)) {
  202. break;
  203. }
  204. n = n->get_parent();
  205. }
  206. RID canvas;
  207. if (canvas_layer) {
  208. canvas = canvas_layer->get_canvas();
  209. } else {
  210. canvas = get_viewport()->find_world_2d()->get_canvas();
  211. }
  212. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  213. RenderingServer::get_singleton()->canvas_item_set_visibility_layer(canvas_item, visibility_layer);
  214. canvas_group = "_root_canvas" + itos(canvas.get_id());
  215. add_to_group(canvas_group);
  216. if (canvas_layer) {
  217. canvas_layer->reset_sort_index();
  218. } else {
  219. get_viewport()->gui_reset_canvas_sort_index();
  220. }
  221. }
  222. pending_update = false;
  223. queue_redraw();
  224. notification(NOTIFICATION_ENTER_CANVAS);
  225. }
  226. void CanvasItem::_exit_canvas() {
  227. notification(NOTIFICATION_EXIT_CANVAS, true); //reverse the notification
  228. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, RID());
  229. canvas_layer = nullptr;
  230. if (canvas_group != StringName()) {
  231. remove_from_group(canvas_group);
  232. canvas_group = StringName();
  233. }
  234. }
  235. void CanvasItem::_notification(int p_what) {
  236. switch (p_what) {
  237. case NOTIFICATION_ENTER_TREE: {
  238. ERR_MAIN_THREAD_GUARD;
  239. ERR_FAIL_COND(!is_inside_tree());
  240. Node *parent = get_parent();
  241. if (parent) {
  242. CanvasItem *ci = Object::cast_to<CanvasItem>(parent);
  243. if (ci) {
  244. parent_visible_in_tree = ci->is_visible_in_tree();
  245. C = ci->children_items.push_back(this);
  246. } else {
  247. CanvasLayer *cl = Object::cast_to<CanvasLayer>(parent);
  248. if (cl) {
  249. parent_visible_in_tree = cl->is_visible();
  250. } else {
  251. // Look for a window.
  252. Viewport *viewport = nullptr;
  253. while (parent) {
  254. viewport = Object::cast_to<Viewport>(parent);
  255. if (viewport) {
  256. break;
  257. }
  258. parent = parent->get_parent();
  259. }
  260. ERR_FAIL_NULL(viewport);
  261. window = Object::cast_to<Window>(viewport);
  262. if (window) {
  263. window->connect(SceneStringName(visibility_changed), callable_mp(this, &CanvasItem::_window_visibility_changed));
  264. parent_visible_in_tree = window->is_visible();
  265. } else {
  266. parent_visible_in_tree = true;
  267. }
  268. }
  269. }
  270. }
  271. _set_global_invalid(true);
  272. _enter_canvas();
  273. RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, is_visible_in_tree()); // The visibility of the parent may change.
  274. if (is_visible_in_tree()) {
  275. notification(NOTIFICATION_VISIBILITY_CHANGED); // Considered invisible until entered.
  276. }
  277. _update_texture_filter_changed(false);
  278. _update_texture_repeat_changed(false);
  279. if (!block_transform_notify && !xform_change.in_list()) {
  280. get_tree()->xform_change_list.add(&xform_change);
  281. }
  282. if (get_viewport()) {
  283. get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::canvas_parent_mark_dirty).bind(get_parent()), CONNECT_REFERENCE_COUNTED);
  284. }
  285. // If using physics interpolation, reset for this node only,
  286. // as a helper, as in most cases, users will want items reset when
  287. // adding to the tree.
  288. // In cases where they move immediately after adding,
  289. // there will be little cost in having two resets as these are cheap,
  290. // and it is worth it for convenience.
  291. // Do not propagate to children, as each child of an added branch
  292. // receives its own NOTIFICATION_ENTER_TREE, and this would
  293. // cause unnecessary duplicate resets.
  294. if (is_physics_interpolated_and_enabled()) {
  295. notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  296. }
  297. } break;
  298. case NOTIFICATION_EXIT_TREE: {
  299. ERR_MAIN_THREAD_GUARD;
  300. if (xform_change.in_list()) {
  301. get_tree()->xform_change_list.remove(&xform_change);
  302. }
  303. _exit_canvas();
  304. if (C) {
  305. Object::cast_to<CanvasItem>(get_parent())->children_items.erase(C);
  306. C = nullptr;
  307. }
  308. if (window) {
  309. window->disconnect(SceneStringName(visibility_changed), callable_mp(this, &CanvasItem::_window_visibility_changed));
  310. window = nullptr;
  311. }
  312. _set_global_invalid(true);
  313. parent_visible_in_tree = false;
  314. if (get_viewport()) {
  315. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::canvas_parent_mark_dirty).bind(get_parent()));
  316. }
  317. } break;
  318. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  319. if (is_visible_in_tree() && is_physics_interpolated()) {
  320. RenderingServer::get_singleton()->canvas_item_reset_physics_interpolation(canvas_item);
  321. }
  322. } break;
  323. case NOTIFICATION_VISIBILITY_CHANGED: {
  324. ERR_MAIN_THREAD_GUARD;
  325. emit_signal(SceneStringName(visibility_changed));
  326. } break;
  327. case NOTIFICATION_WORLD_2D_CHANGED: {
  328. ERR_MAIN_THREAD_GUARD;
  329. _exit_canvas();
  330. _enter_canvas();
  331. } break;
  332. case NOTIFICATION_PARENTED: {
  333. // The node is not inside the tree during this notification.
  334. ERR_MAIN_THREAD_GUARD;
  335. _notify_transform();
  336. } break;
  337. }
  338. }
  339. void CanvasItem::update_draw_order() {
  340. ERR_MAIN_THREAD_GUARD;
  341. if (!is_inside_tree()) {
  342. return;
  343. }
  344. if (canvas_group != StringName()) {
  345. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE | SceneTree::GROUP_CALL_DEFERRED, canvas_group, "_top_level_raise_self");
  346. } else {
  347. ERR_FAIL_NULL_MSG(get_parent_item(), "Moved child is in incorrect state (no canvas group, no canvas item parent).");
  348. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  349. }
  350. }
  351. void CanvasItem::_window_visibility_changed() {
  352. _propagate_visibility_changed(window->is_visible());
  353. }
  354. void CanvasItem::queue_redraw() {
  355. ERR_THREAD_GUARD; // Calling from thread is safe.
  356. if (!is_inside_tree()) {
  357. return;
  358. }
  359. if (pending_update) {
  360. return;
  361. }
  362. pending_update = true;
  363. callable_mp(this, &CanvasItem::_redraw_callback).call_deferred();
  364. }
  365. void CanvasItem::move_to_front() {
  366. ERR_MAIN_THREAD_GUARD;
  367. if (!get_parent()) {
  368. return;
  369. }
  370. get_parent()->move_child(this, -1);
  371. }
  372. void CanvasItem::set_modulate(const Color &p_modulate) {
  373. ERR_THREAD_GUARD;
  374. if (modulate == p_modulate) {
  375. return;
  376. }
  377. modulate = p_modulate;
  378. RenderingServer::get_singleton()->canvas_item_set_modulate(canvas_item, modulate);
  379. }
  380. Color CanvasItem::get_modulate() const {
  381. ERR_READ_THREAD_GUARD_V(Color());
  382. return modulate;
  383. }
  384. Color CanvasItem::get_modulate_in_tree() const {
  385. ERR_READ_THREAD_GUARD_V(Color());
  386. Color final_modulate = modulate;
  387. CanvasItem *parent_item = get_parent_item();
  388. while (parent_item) {
  389. final_modulate *= parent_item->get_modulate();
  390. parent_item = parent_item->get_parent_item();
  391. }
  392. return final_modulate;
  393. }
  394. void CanvasItem::set_as_top_level(bool p_top_level) {
  395. ERR_MAIN_THREAD_GUARD;
  396. if (top_level == p_top_level) {
  397. return;
  398. }
  399. if (!is_inside_tree()) {
  400. top_level = p_top_level;
  401. _notify_transform();
  402. return;
  403. }
  404. _exit_canvas();
  405. top_level = p_top_level;
  406. _top_level_changed();
  407. _enter_canvas();
  408. _notify_transform();
  409. if (get_viewport()) {
  410. get_viewport()->canvas_item_top_level_changed();
  411. }
  412. }
  413. void CanvasItem::_top_level_changed() {
  414. // Inform children that top_level status has changed on a parent.
  415. int children = get_child_count();
  416. for (int i = 0; i < children; i++) {
  417. CanvasItem *child = Object::cast_to<CanvasItem>(get_child(i));
  418. if (child) {
  419. child->_top_level_changed_on_parent();
  420. }
  421. }
  422. }
  423. void CanvasItem::_top_level_changed_on_parent() {
  424. // Inform children that top_level status has changed on a parent.
  425. _top_level_changed();
  426. }
  427. bool CanvasItem::is_set_as_top_level() const {
  428. return top_level;
  429. }
  430. CanvasItem *CanvasItem::get_parent_item() const {
  431. ERR_READ_THREAD_GUARD_V(nullptr);
  432. if (top_level) {
  433. return nullptr;
  434. }
  435. return Object::cast_to<CanvasItem>(get_parent());
  436. }
  437. void CanvasItem::set_self_modulate(const Color &p_self_modulate) {
  438. ERR_THREAD_GUARD;
  439. if (self_modulate == p_self_modulate) {
  440. return;
  441. }
  442. self_modulate = p_self_modulate;
  443. RenderingServer::get_singleton()->canvas_item_set_self_modulate(canvas_item, self_modulate);
  444. }
  445. Color CanvasItem::get_self_modulate() const {
  446. ERR_READ_THREAD_GUARD_V(Color());
  447. return self_modulate;
  448. }
  449. void CanvasItem::set_light_mask(int p_light_mask) {
  450. ERR_THREAD_GUARD;
  451. if (light_mask == p_light_mask) {
  452. return;
  453. }
  454. light_mask = p_light_mask;
  455. RS::get_singleton()->canvas_item_set_light_mask(canvas_item, p_light_mask);
  456. }
  457. int CanvasItem::get_light_mask() const {
  458. ERR_READ_THREAD_GUARD_V(0);
  459. return light_mask;
  460. }
  461. const StringName *CanvasItem::_instance_shader_parameter_get_remap(const StringName &p_name) const {
  462. StringName *r = instance_shader_parameter_property_remap.getptr(p_name);
  463. if (!r) {
  464. String s = p_name;
  465. if (s.begins_with("instance_shader_parameters/")) {
  466. StringName name = s.trim_prefix("instance_shader_parameters/");
  467. instance_shader_parameter_property_remap[p_name] = name;
  468. return instance_shader_parameter_property_remap.getptr(p_name);
  469. }
  470. return nullptr;
  471. }
  472. return r;
  473. }
  474. bool CanvasItem::_set(const StringName &p_name, const Variant &p_value) {
  475. const StringName *r = _instance_shader_parameter_get_remap(p_name);
  476. if (r) {
  477. set_instance_shader_parameter(*r, p_value);
  478. return true;
  479. }
  480. return false;
  481. }
  482. bool CanvasItem::_get(const StringName &p_name, Variant &r_ret) const {
  483. const StringName *r = _instance_shader_parameter_get_remap(p_name);
  484. if (r) {
  485. r_ret = get_instance_shader_parameter(*r);
  486. return true;
  487. }
  488. return false;
  489. }
  490. void CanvasItem::_get_property_list(List<PropertyInfo> *p_list) const {
  491. List<PropertyInfo> pinfo;
  492. RS::get_singleton()->canvas_item_get_instance_shader_parameter_list(get_canvas_item(), &pinfo);
  493. for (PropertyInfo &pi : pinfo) {
  494. bool has_def_value = false;
  495. Variant def_value = RS::get_singleton()->canvas_item_get_instance_shader_parameter_default_value(get_canvas_item(), pi.name);
  496. if (def_value.get_type() != Variant::NIL) {
  497. has_def_value = true;
  498. }
  499. if (instance_shader_parameters.has(pi.name)) {
  500. pi.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | (has_def_value ? (PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED) : PROPERTY_USAGE_NONE);
  501. } else {
  502. pi.usage = PROPERTY_USAGE_EDITOR | (has_def_value ? PROPERTY_USAGE_CHECKABLE : PROPERTY_USAGE_NONE); // Do not save if not changed.
  503. }
  504. pi.name = "instance_shader_parameters/" + pi.name;
  505. p_list->push_back(pi);
  506. }
  507. }
  508. void CanvasItem::item_rect_changed(bool p_size_changed) {
  509. ERR_MAIN_THREAD_GUARD;
  510. if (p_size_changed) {
  511. queue_redraw();
  512. }
  513. emit_signal(SceneStringName(item_rect_changed));
  514. }
  515. void CanvasItem::set_z_index(int p_z) {
  516. ERR_THREAD_GUARD;
  517. ERR_FAIL_COND(p_z < RS::CANVAS_ITEM_Z_MIN);
  518. ERR_FAIL_COND(p_z > RS::CANVAS_ITEM_Z_MAX);
  519. z_index = p_z;
  520. RS::get_singleton()->canvas_item_set_z_index(canvas_item, z_index);
  521. update_configuration_warnings();
  522. }
  523. void CanvasItem::set_z_as_relative(bool p_enabled) {
  524. ERR_THREAD_GUARD;
  525. if (z_relative == p_enabled) {
  526. return;
  527. }
  528. z_relative = p_enabled;
  529. RS::get_singleton()->canvas_item_set_z_as_relative_to_parent(canvas_item, p_enabled);
  530. }
  531. bool CanvasItem::is_z_relative() const {
  532. ERR_READ_THREAD_GUARD_V(false);
  533. return z_relative;
  534. }
  535. int CanvasItem::get_z_index() const {
  536. ERR_READ_THREAD_GUARD_V(0);
  537. return z_index;
  538. }
  539. int CanvasItem::get_effective_z_index() const {
  540. ERR_READ_THREAD_GUARD_V(0);
  541. int effective_z_index = z_index;
  542. if (is_z_relative()) {
  543. CanvasItem *p = get_parent_item();
  544. if (p) {
  545. effective_z_index += p->get_effective_z_index();
  546. }
  547. }
  548. return effective_z_index;
  549. }
  550. void CanvasItem::set_y_sort_enabled(bool p_enabled) {
  551. ERR_THREAD_GUARD;
  552. y_sort_enabled = p_enabled;
  553. RS::get_singleton()->canvas_item_set_sort_children_by_y(canvas_item, y_sort_enabled);
  554. }
  555. bool CanvasItem::is_y_sort_enabled() const {
  556. ERR_READ_THREAD_GUARD_V(false);
  557. return y_sort_enabled;
  558. }
  559. void CanvasItem::draw_dashed_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width, real_t p_dash, bool p_aligned, bool p_antialiased) {
  560. ERR_THREAD_GUARD;
  561. ERR_DRAW_GUARD;
  562. ERR_FAIL_COND(p_dash <= 0.0);
  563. float length = (p_to - p_from).length();
  564. Vector2 step = p_dash * (p_to - p_from).normalized();
  565. if (length < p_dash || step == Vector2()) {
  566. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
  567. return;
  568. }
  569. int steps = (p_aligned) ? Math::ceil(length / p_dash) : Math::floor(length / p_dash);
  570. if (steps % 2 == 0) {
  571. steps--;
  572. }
  573. Point2 off = p_from;
  574. if (p_aligned) {
  575. off += (p_to - p_from).normalized() * (length - steps * p_dash) / 2.0;
  576. }
  577. Vector<Vector2> points;
  578. points.resize(steps + 1);
  579. for (int i = 0; i < steps; i += 2) {
  580. points.write[i] = (i == 0) ? p_from : off;
  581. points.write[i + 1] = (p_aligned && i == steps - 1) ? p_to : (off + step);
  582. off += step * 2;
  583. }
  584. Vector<Color> colors = { p_color };
  585. RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, points, colors, p_width, p_antialiased);
  586. }
  587. void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width, bool p_antialiased) {
  588. ERR_THREAD_GUARD;
  589. ERR_DRAW_GUARD;
  590. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
  591. }
  592. void CanvasItem::draw_polyline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width, bool p_antialiased) {
  593. ERR_THREAD_GUARD;
  594. ERR_DRAW_GUARD;
  595. Vector<Color> colors = { p_color };
  596. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, colors, p_width, p_antialiased);
  597. }
  598. void CanvasItem::draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width, bool p_antialiased) {
  599. ERR_THREAD_GUARD;
  600. ERR_DRAW_GUARD;
  601. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  602. }
  603. void CanvasItem::draw_arc(const Vector2 &p_center, real_t p_radius, real_t p_start_angle, real_t p_end_angle, int p_point_count, const Color &p_color, real_t p_width, bool p_antialiased) {
  604. ERR_THREAD_GUARD;
  605. Vector<Point2> points;
  606. points.resize(p_point_count);
  607. Point2 *points_ptr = points.ptrw();
  608. // Clamp angle difference to full circle so arc won't overlap itself.
  609. const real_t delta_angle = CLAMP(p_end_angle - p_start_angle, -Math_TAU, Math_TAU);
  610. for (int i = 0; i < p_point_count; i++) {
  611. real_t theta = (i / (p_point_count - 1.0f)) * delta_angle + p_start_angle;
  612. points_ptr[i] = p_center + Vector2(Math::cos(theta), Math::sin(theta)) * p_radius;
  613. }
  614. draw_polyline(points, p_color, p_width, p_antialiased);
  615. }
  616. void CanvasItem::draw_multiline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width, bool p_antialiased) {
  617. ERR_THREAD_GUARD;
  618. ERR_DRAW_GUARD;
  619. Vector<Color> colors = { p_color };
  620. RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, colors, p_width, p_antialiased);
  621. }
  622. void CanvasItem::draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width, bool p_antialiased) {
  623. ERR_THREAD_GUARD;
  624. ERR_DRAW_GUARD;
  625. RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  626. }
  627. void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled, real_t p_width, bool p_antialiased) {
  628. ERR_THREAD_GUARD;
  629. ERR_DRAW_GUARD;
  630. Rect2 rect = p_rect.abs();
  631. if (p_filled) {
  632. if (p_width != -1.0) {
  633. WARN_PRINT("The draw_rect() \"width\" argument has no effect when \"filled\" is \"true\".");
  634. }
  635. RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, rect, p_color, p_antialiased);
  636. } else if (p_width >= rect.size.width || p_width >= rect.size.height) {
  637. RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, rect.grow(0.5f * p_width), p_color, p_antialiased);
  638. } else {
  639. Vector<Vector2> points;
  640. points.resize(5);
  641. points.write[0] = rect.position;
  642. points.write[1] = rect.position + Vector2(rect.size.x, 0);
  643. points.write[2] = rect.position + rect.size;
  644. points.write[3] = rect.position + Vector2(0, rect.size.y);
  645. points.write[4] = rect.position;
  646. Vector<Color> colors = { p_color };
  647. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, points, colors, p_width, p_antialiased);
  648. }
  649. }
  650. void CanvasItem::draw_circle(const Point2 &p_pos, real_t p_radius, const Color &p_color, bool p_filled, real_t p_width, bool p_antialiased) {
  651. ERR_THREAD_GUARD;
  652. ERR_DRAW_GUARD;
  653. if (p_filled) {
  654. if (p_width != -1.0) {
  655. WARN_PRINT("The draw_circle() \"width\" argument has no effect when \"filled\" is \"true\".");
  656. }
  657. RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color, p_antialiased);
  658. } else if (p_width >= 2.0 * p_radius) {
  659. RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius + 0.5 * p_width, p_color, p_antialiased);
  660. } else {
  661. // Tessellation count is hardcoded. Keep in sync with the same variable in `RendererCanvasCull::canvas_item_add_circle()`.
  662. const int circle_segments = 64;
  663. Vector<Vector2> points;
  664. points.resize(circle_segments + 1);
  665. Vector2 *points_ptr = points.ptrw();
  666. const real_t circle_point_step = Math_TAU / circle_segments;
  667. for (int i = 0; i < circle_segments; i++) {
  668. float angle = i * circle_point_step;
  669. points_ptr[i].x = Math::cos(angle) * p_radius;
  670. points_ptr[i].y = Math::sin(angle) * p_radius;
  671. points_ptr[i] += p_pos;
  672. }
  673. points_ptr[circle_segments] = points_ptr[0];
  674. Vector<Color> colors = { p_color };
  675. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, points, colors, p_width, p_antialiased);
  676. }
  677. }
  678. void CanvasItem::draw_texture(const Ref<Texture2D> &p_texture, const Point2 &p_pos, const Color &p_modulate) {
  679. ERR_THREAD_GUARD;
  680. ERR_DRAW_GUARD;
  681. ERR_FAIL_COND(p_texture.is_null());
  682. p_texture->draw(canvas_item, p_pos, p_modulate, false);
  683. }
  684. void CanvasItem::draw_texture_rect(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) {
  685. ERR_THREAD_GUARD;
  686. ERR_DRAW_GUARD;
  687. ERR_FAIL_COND(p_texture.is_null());
  688. p_texture->draw_rect(canvas_item, p_rect, p_tile, p_modulate, p_transpose);
  689. }
  690. void CanvasItem::draw_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) {
  691. ERR_THREAD_GUARD;
  692. ERR_DRAW_GUARD;
  693. ERR_FAIL_COND(p_texture.is_null());
  694. p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  695. }
  696. void CanvasItem::draw_msdf_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, double p_outline, double p_pixel_range, double p_scale) {
  697. ERR_THREAD_GUARD;
  698. ERR_DRAW_GUARD;
  699. ERR_FAIL_COND(p_texture.is_null());
  700. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(canvas_item, p_rect, p_texture->get_rid(), p_src_rect, p_modulate, p_outline, p_pixel_range, p_scale);
  701. }
  702. void CanvasItem::draw_lcd_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate) {
  703. ERR_THREAD_GUARD;
  704. ERR_DRAW_GUARD;
  705. ERR_FAIL_COND(p_texture.is_null());
  706. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(canvas_item, p_rect, p_texture->get_rid(), p_src_rect, p_modulate);
  707. }
  708. void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
  709. ERR_THREAD_GUARD;
  710. ERR_DRAW_GUARD;
  711. ERR_FAIL_COND(p_style_box.is_null());
  712. p_style_box->draw(canvas_item, p_rect);
  713. }
  714. void CanvasItem::draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
  715. ERR_THREAD_GUARD;
  716. ERR_DRAW_GUARD;
  717. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  718. RenderingServer::get_singleton()->canvas_item_add_primitive(canvas_item, p_points, p_colors, p_uvs, rid);
  719. }
  720. void CanvasItem::draw_set_transform(const Point2 &p_offset, real_t p_rot, const Size2 &p_scale) {
  721. ERR_THREAD_GUARD;
  722. ERR_DRAW_GUARD;
  723. Transform2D xform(p_rot, p_offset);
  724. xform.scale_basis(p_scale);
  725. RenderingServer::get_singleton()->canvas_item_add_set_transform(canvas_item, xform);
  726. }
  727. void CanvasItem::draw_set_transform_matrix(const Transform2D &p_matrix) {
  728. ERR_THREAD_GUARD;
  729. ERR_DRAW_GUARD;
  730. RenderingServer::get_singleton()->canvas_item_add_set_transform(canvas_item, p_matrix);
  731. }
  732. void CanvasItem::draw_animation_slice(double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset) {
  733. ERR_THREAD_GUARD;
  734. ERR_DRAW_GUARD;
  735. RenderingServer::get_singleton()->canvas_item_add_animation_slice(canvas_item, p_animation_length, p_slice_begin, p_slice_end, p_offset);
  736. }
  737. void CanvasItem::draw_end_animation() {
  738. ERR_THREAD_GUARD;
  739. ERR_DRAW_GUARD;
  740. RenderingServer::get_singleton()->canvas_item_add_animation_slice(canvas_item, 1, 0, 2, 0);
  741. }
  742. void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
  743. ERR_THREAD_GUARD;
  744. ERR_DRAW_GUARD;
  745. const Ref<AtlasTexture> atlas = p_texture;
  746. if (atlas.is_valid() && atlas->get_atlas().is_valid()) {
  747. const Ref<Texture2D> &texture = atlas->get_atlas();
  748. const Vector2 atlas_size = texture->get_size();
  749. const Vector2 remap_min = atlas->get_region().position / atlas_size;
  750. const Vector2 remap_max = atlas->get_region().get_end() / atlas_size;
  751. PackedVector2Array uvs = p_uvs;
  752. for (Vector2 &p : uvs) {
  753. p.x = Math::remap(p.x, 0, 1, remap_min.x, remap_max.x);
  754. p.y = Math::remap(p.y, 0, 1, remap_min.y, remap_max.y);
  755. }
  756. RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, uvs, texture->get_rid());
  757. } else {
  758. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  759. RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, texture_rid);
  760. }
  761. }
  762. void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
  763. draw_polygon(p_points, { p_color }, p_uvs, p_texture);
  764. }
  765. void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture2D> &p_texture, const Transform2D &p_transform, const Color &p_modulate) {
  766. ERR_THREAD_GUARD;
  767. ERR_FAIL_COND(p_mesh.is_null());
  768. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  769. RenderingServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), p_transform, p_modulate, texture_rid);
  770. }
  771. void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture2D> &p_texture) {
  772. ERR_THREAD_GUARD;
  773. ERR_FAIL_COND(p_multimesh.is_null());
  774. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  775. RenderingServer::get_singleton()->canvas_item_add_multimesh(canvas_item, p_multimesh->get_rid(), texture_rid);
  776. }
  777. void CanvasItem::draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  778. ERR_THREAD_GUARD;
  779. ERR_DRAW_GUARD;
  780. ERR_FAIL_COND(p_font.is_null());
  781. p_font->draw_string(canvas_item, p_pos, p_text, p_alignment, p_width, p_font_size, p_modulate, p_jst_flags, p_direction, p_orientation);
  782. }
  783. void CanvasItem::draw_multiline_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  784. ERR_THREAD_GUARD;
  785. ERR_DRAW_GUARD;
  786. ERR_FAIL_COND(p_font.is_null());
  787. p_font->draw_multiline_string(canvas_item, p_pos, p_text, p_alignment, p_width, p_font_size, p_max_lines, p_modulate, p_brk_flags, p_jst_flags, p_direction, p_orientation);
  788. }
  789. void CanvasItem::draw_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  790. ERR_THREAD_GUARD;
  791. ERR_DRAW_GUARD;
  792. ERR_FAIL_COND(p_font.is_null());
  793. p_font->draw_string_outline(canvas_item, p_pos, p_text, p_alignment, p_width, p_font_size, p_size, p_modulate, p_jst_flags, p_direction, p_orientation);
  794. }
  795. void CanvasItem::draw_multiline_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, int p_size, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  796. ERR_THREAD_GUARD;
  797. ERR_DRAW_GUARD;
  798. ERR_FAIL_COND(p_font.is_null());
  799. p_font->draw_multiline_string_outline(canvas_item, p_pos, p_text, p_alignment, p_width, p_font_size, p_max_lines, p_size, p_modulate, p_brk_flags, p_jst_flags, p_direction, p_orientation);
  800. }
  801. void CanvasItem::draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size, const Color &p_modulate) const {
  802. ERR_THREAD_GUARD;
  803. ERR_DRAW_GUARD;
  804. ERR_FAIL_COND(p_char.length() != 1);
  805. ERR_FAIL_COND(p_font.is_null());
  806. p_font->draw_char(canvas_item, p_pos, p_char[0], p_font_size, p_modulate);
  807. }
  808. void CanvasItem::draw_char_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size, int p_size, const Color &p_modulate) const {
  809. ERR_THREAD_GUARD;
  810. ERR_DRAW_GUARD;
  811. ERR_FAIL_COND(p_char.length() != 1);
  812. ERR_FAIL_COND(p_font.is_null());
  813. p_font->draw_char_outline(canvas_item, p_pos, p_char[0], p_font_size, p_size, p_modulate);
  814. }
  815. void CanvasItem::_notify_transform_deferred() {
  816. if (is_inside_tree() && notify_transform && !xform_change.in_list()) {
  817. get_tree()->xform_change_list.add(&xform_change);
  818. }
  819. }
  820. void CanvasItem::_notify_transform(CanvasItem *p_node) {
  821. /* This check exists to avoid re-propagating the transform
  822. * notification down the tree on dirty nodes. It provides
  823. * optimization by avoiding redundancy (nodes are dirty, will get the
  824. * notification anyway).
  825. */
  826. if (/*p_node->xform_change.in_list() &&*/ p_node->_is_global_invalid()) {
  827. return; //nothing to do
  828. }
  829. p_node->_set_global_invalid(true);
  830. if (p_node->notify_transform && !p_node->xform_change.in_list()) {
  831. if (!p_node->block_transform_notify) {
  832. if (p_node->is_inside_tree()) {
  833. if (is_accessible_from_caller_thread()) {
  834. get_tree()->xform_change_list.add(&p_node->xform_change);
  835. } else {
  836. // Should be rare, but still needs to be handled.
  837. callable_mp(p_node, &CanvasItem::_notify_transform_deferred).call_deferred();
  838. }
  839. }
  840. }
  841. }
  842. for (CanvasItem *ci : p_node->children_items) {
  843. if (ci->top_level) {
  844. continue;
  845. }
  846. _notify_transform(ci);
  847. }
  848. }
  849. void CanvasItem::_physics_interpolated_changed() {
  850. RenderingServer::get_singleton()->canvas_item_set_interpolated(canvas_item, is_physics_interpolated());
  851. }
  852. Rect2 CanvasItem::get_viewport_rect() const {
  853. ERR_READ_THREAD_GUARD_V(Rect2());
  854. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  855. return get_viewport()->get_visible_rect();
  856. }
  857. RID CanvasItem::get_canvas() const {
  858. ERR_READ_THREAD_GUARD_V(RID());
  859. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  860. if (canvas_layer) {
  861. return canvas_layer->get_canvas();
  862. } else {
  863. return get_viewport()->find_world_2d()->get_canvas();
  864. }
  865. }
  866. ObjectID CanvasItem::get_canvas_layer_instance_id() const {
  867. ERR_READ_THREAD_GUARD_V(ObjectID());
  868. if (canvas_layer) {
  869. return canvas_layer->get_instance_id();
  870. } else {
  871. return ObjectID();
  872. }
  873. }
  874. CanvasItem *CanvasItem::get_top_level() const {
  875. ERR_READ_THREAD_GUARD_V(nullptr);
  876. CanvasItem *ci = const_cast<CanvasItem *>(this);
  877. while (!ci->top_level && Object::cast_to<CanvasItem>(ci->get_parent())) {
  878. ci = Object::cast_to<CanvasItem>(ci->get_parent());
  879. }
  880. return ci;
  881. }
  882. Ref<World2D> CanvasItem::get_world_2d() const {
  883. ERR_READ_THREAD_GUARD_V(Ref<World2D>());
  884. ERR_FAIL_COND_V(!is_inside_tree(), Ref<World2D>());
  885. CanvasItem *tl = get_top_level();
  886. if (tl->get_viewport()) {
  887. return tl->get_viewport()->find_world_2d();
  888. } else {
  889. return Ref<World2D>();
  890. }
  891. }
  892. RID CanvasItem::get_viewport_rid() const {
  893. ERR_READ_THREAD_GUARD_V(RID());
  894. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  895. return get_viewport()->get_viewport_rid();
  896. }
  897. void CanvasItem::set_block_transform_notify(bool p_enable) {
  898. ERR_THREAD_GUARD;
  899. block_transform_notify = p_enable;
  900. }
  901. bool CanvasItem::is_block_transform_notify_enabled() const {
  902. ERR_READ_THREAD_GUARD_V(false);
  903. return block_transform_notify;
  904. }
  905. void CanvasItem::set_draw_behind_parent(bool p_enable) {
  906. ERR_THREAD_GUARD;
  907. if (behind == p_enable) {
  908. return;
  909. }
  910. behind = p_enable;
  911. RenderingServer::get_singleton()->canvas_item_set_draw_behind_parent(canvas_item, behind);
  912. }
  913. bool CanvasItem::is_draw_behind_parent_enabled() const {
  914. ERR_READ_THREAD_GUARD_V(false);
  915. return behind;
  916. }
  917. void CanvasItem::set_material(const Ref<Material> &p_material) {
  918. ERR_THREAD_GUARD;
  919. material = p_material;
  920. RID rid;
  921. if (material.is_valid()) {
  922. rid = material->get_rid();
  923. }
  924. RS::get_singleton()->canvas_item_set_material(canvas_item, rid);
  925. notify_property_list_changed(); //properties for material exposed
  926. }
  927. void CanvasItem::set_use_parent_material(bool p_use_parent_material) {
  928. ERR_THREAD_GUARD;
  929. use_parent_material = p_use_parent_material;
  930. RS::get_singleton()->canvas_item_set_use_parent_material(canvas_item, p_use_parent_material);
  931. }
  932. void CanvasItem::set_instance_shader_parameter(const StringName &p_name, const Variant &p_value) {
  933. if (p_value.get_type() == Variant::NIL) {
  934. Variant def_value = RS::get_singleton()->canvas_item_get_instance_shader_parameter_default_value(get_canvas_item(), p_name);
  935. RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, def_value);
  936. instance_shader_parameters.erase(p_value);
  937. } else {
  938. instance_shader_parameters[p_name] = p_value;
  939. if (p_value.get_type() == Variant::OBJECT) {
  940. RID tex_id = p_value;
  941. RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, tex_id);
  942. } else {
  943. RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, p_value);
  944. }
  945. }
  946. }
  947. Variant CanvasItem::get_instance_shader_parameter(const StringName &p_name) const {
  948. return RS::get_singleton()->canvas_item_get_instance_shader_parameter(get_canvas_item(), p_name);
  949. }
  950. bool CanvasItem::get_use_parent_material() const {
  951. ERR_READ_THREAD_GUARD_V(false);
  952. return use_parent_material;
  953. }
  954. Ref<Material> CanvasItem::get_material() const {
  955. ERR_READ_THREAD_GUARD_V(Ref<Material>());
  956. return material;
  957. }
  958. Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) const {
  959. ERR_READ_THREAD_GUARD_V(Vector2());
  960. ERR_FAIL_COND_V(!is_inside_tree(), screen_point);
  961. Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
  962. return local_matrix.xform(screen_point);
  963. }
  964. Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
  965. ERR_READ_THREAD_GUARD_V(Ref<InputEvent>());
  966. ERR_FAIL_COND_V(p_event.is_null(), p_event);
  967. ERR_FAIL_COND_V(!is_inside_tree(), p_event);
  968. return p_event->xformed_by((get_canvas_transform() * get_global_transform()).affine_inverse());
  969. }
  970. Vector2 CanvasItem::get_global_mouse_position() const {
  971. ERR_READ_THREAD_GUARD_V(Vector2());
  972. ERR_FAIL_NULL_V(get_viewport(), Vector2());
  973. return get_canvas_transform().affine_inverse().xform(get_viewport()->get_mouse_position());
  974. }
  975. Vector2 CanvasItem::get_local_mouse_position() const {
  976. ERR_READ_THREAD_GUARD_V(Vector2());
  977. ERR_FAIL_NULL_V(get_viewport(), Vector2());
  978. return get_global_transform().affine_inverse().xform(get_global_mouse_position());
  979. }
  980. void CanvasItem::force_update_transform() {
  981. ERR_THREAD_GUARD;
  982. ERR_FAIL_COND(!is_inside_tree());
  983. if (!xform_change.in_list()) {
  984. return;
  985. }
  986. get_tree()->xform_change_list.remove(&xform_change);
  987. notification(NOTIFICATION_TRANSFORM_CHANGED);
  988. }
  989. void CanvasItem::_validate_property(PropertyInfo &p_property) const {
  990. if (hide_clip_children && p_property.name == "clip_children") {
  991. p_property.usage = PROPERTY_USAGE_NONE;
  992. }
  993. }
  994. void CanvasItem::_bind_methods() {
  995. ClassDB::bind_method(D_METHOD("_top_level_raise_self"), &CanvasItem::_top_level_raise_self);
  996. #ifdef TOOLS_ENABLED
  997. ClassDB::bind_method(D_METHOD("_edit_set_state", "state"), &CanvasItem::_edit_set_state);
  998. ClassDB::bind_method(D_METHOD("_edit_get_state"), &CanvasItem::_edit_get_state);
  999. ClassDB::bind_method(D_METHOD("_edit_set_position", "position"), &CanvasItem::_edit_set_position);
  1000. ClassDB::bind_method(D_METHOD("_edit_get_position"), &CanvasItem::_edit_get_position);
  1001. ClassDB::bind_method(D_METHOD("_edit_set_scale", "scale"), &CanvasItem::_edit_set_scale);
  1002. ClassDB::bind_method(D_METHOD("_edit_get_scale"), &CanvasItem::_edit_get_scale);
  1003. ClassDB::bind_method(D_METHOD("_edit_set_rect", "rect"), &CanvasItem::_edit_set_rect);
  1004. ClassDB::bind_method(D_METHOD("_edit_get_rect"), &CanvasItem::_edit_get_rect);
  1005. ClassDB::bind_method(D_METHOD("_edit_use_rect"), &CanvasItem::_edit_use_rect);
  1006. ClassDB::bind_method(D_METHOD("_edit_set_rotation", "degrees"), &CanvasItem::_edit_set_rotation);
  1007. ClassDB::bind_method(D_METHOD("_edit_get_rotation"), &CanvasItem::_edit_get_rotation);
  1008. ClassDB::bind_method(D_METHOD("_edit_use_rotation"), &CanvasItem::_edit_use_rotation);
  1009. ClassDB::bind_method(D_METHOD("_edit_set_pivot", "pivot"), &CanvasItem::_edit_set_pivot);
  1010. ClassDB::bind_method(D_METHOD("_edit_get_pivot"), &CanvasItem::_edit_get_pivot);
  1011. ClassDB::bind_method(D_METHOD("_edit_use_pivot"), &CanvasItem::_edit_use_pivot);
  1012. ClassDB::bind_method(D_METHOD("_edit_get_transform"), &CanvasItem::_edit_get_transform);
  1013. #endif //TOOLS_ENABLED
  1014. ClassDB::bind_method(D_METHOD("get_canvas_item"), &CanvasItem::get_canvas_item);
  1015. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasItem::set_visible);
  1016. ClassDB::bind_method(D_METHOD("is_visible"), &CanvasItem::is_visible);
  1017. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &CanvasItem::is_visible_in_tree);
  1018. ClassDB::bind_method(D_METHOD("show"), &CanvasItem::show);
  1019. ClassDB::bind_method(D_METHOD("hide"), &CanvasItem::hide);
  1020. ClassDB::bind_method(D_METHOD("queue_redraw"), &CanvasItem::queue_redraw);
  1021. ClassDB::bind_method(D_METHOD("move_to_front"), &CanvasItem::move_to_front);
  1022. ClassDB::bind_method(D_METHOD("set_as_top_level", "enable"), &CanvasItem::set_as_top_level);
  1023. ClassDB::bind_method(D_METHOD("is_set_as_top_level"), &CanvasItem::is_set_as_top_level);
  1024. ClassDB::bind_method(D_METHOD("set_light_mask", "light_mask"), &CanvasItem::set_light_mask);
  1025. ClassDB::bind_method(D_METHOD("get_light_mask"), &CanvasItem::get_light_mask);
  1026. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &CanvasItem::set_modulate);
  1027. ClassDB::bind_method(D_METHOD("get_modulate"), &CanvasItem::get_modulate);
  1028. ClassDB::bind_method(D_METHOD("set_self_modulate", "self_modulate"), &CanvasItem::set_self_modulate);
  1029. ClassDB::bind_method(D_METHOD("get_self_modulate"), &CanvasItem::get_self_modulate);
  1030. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &CanvasItem::set_z_index);
  1031. ClassDB::bind_method(D_METHOD("get_z_index"), &CanvasItem::get_z_index);
  1032. ClassDB::bind_method(D_METHOD("set_z_as_relative", "enable"), &CanvasItem::set_z_as_relative);
  1033. ClassDB::bind_method(D_METHOD("is_z_relative"), &CanvasItem::is_z_relative);
  1034. ClassDB::bind_method(D_METHOD("set_y_sort_enabled", "enabled"), &CanvasItem::set_y_sort_enabled);
  1035. ClassDB::bind_method(D_METHOD("is_y_sort_enabled"), &CanvasItem::is_y_sort_enabled);
  1036. ClassDB::bind_method(D_METHOD("set_draw_behind_parent", "enable"), &CanvasItem::set_draw_behind_parent);
  1037. ClassDB::bind_method(D_METHOD("is_draw_behind_parent_enabled"), &CanvasItem::is_draw_behind_parent_enabled);
  1038. ClassDB::bind_method(D_METHOD("draw_line", "from", "to", "color", "width", "antialiased"), &CanvasItem::draw_line, DEFVAL(-1.0), DEFVAL(false));
  1039. ClassDB::bind_method(D_METHOD("draw_dashed_line", "from", "to", "color", "width", "dash", "aligned", "antialiased"), &CanvasItem::draw_dashed_line, DEFVAL(-1.0), DEFVAL(2.0), DEFVAL(true), DEFVAL(false));
  1040. ClassDB::bind_method(D_METHOD("draw_polyline", "points", "color", "width", "antialiased"), &CanvasItem::draw_polyline, DEFVAL(-1.0), DEFVAL(false));
  1041. ClassDB::bind_method(D_METHOD("draw_polyline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_polyline_colors, DEFVAL(-1.0), DEFVAL(false));
  1042. ClassDB::bind_method(D_METHOD("draw_arc", "center", "radius", "start_angle", "end_angle", "point_count", "color", "width", "antialiased"), &CanvasItem::draw_arc, DEFVAL(-1.0), DEFVAL(false));
  1043. ClassDB::bind_method(D_METHOD("draw_multiline", "points", "color", "width", "antialiased"), &CanvasItem::draw_multiline, DEFVAL(-1.0), DEFVAL(false));
  1044. ClassDB::bind_method(D_METHOD("draw_multiline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_multiline_colors, DEFVAL(-1.0), DEFVAL(false));
  1045. ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled", "width", "antialiased"), &CanvasItem::draw_rect, DEFVAL(true), DEFVAL(-1.0), DEFVAL(false));
  1046. ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color", "filled", "width", "antialiased"), &CanvasItem::draw_circle, DEFVAL(true), DEFVAL(-1.0), DEFVAL(false));
  1047. ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)));
  1048. ClassDB::bind_method(D_METHOD("draw_texture_rect", "texture", "rect", "tile", "modulate", "transpose"), &CanvasItem::draw_texture_rect, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false));
  1049. ClassDB::bind_method(D_METHOD("draw_texture_rect_region", "texture", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &CanvasItem::draw_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false), DEFVAL(true));
  1050. ClassDB::bind_method(D_METHOD("draw_msdf_texture_rect_region", "texture", "rect", "src_rect", "modulate", "outline", "pixel_range", "scale"), &CanvasItem::draw_msdf_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(0.0), DEFVAL(4.0), DEFVAL(1.0));
  1051. ClassDB::bind_method(D_METHOD("draw_lcd_texture_rect_region", "texture", "rect", "src_rect", "modulate"), &CanvasItem::draw_lcd_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)));
  1052. ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
  1053. ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture"), &CanvasItem::draw_primitive, DEFVAL(Ref<Texture2D>()));
  1054. ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture"), &CanvasItem::draw_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref<Texture2D>()));
  1055. ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture"), &CanvasItem::draw_colored_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref<Texture2D>()));
  1056. ClassDB::bind_method(D_METHOD("draw_string", "font", "pos", "text", "alignment", "width", "font_size", "modulate", "justification_flags", "direction", "orientation"), &CanvasItem::draw_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  1057. ClassDB::bind_method(D_METHOD("draw_multiline_string", "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &CanvasItem::draw_multiline_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  1058. ClassDB::bind_method(D_METHOD("draw_string_outline", "font", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "justification_flags", "direction", "orientation"), &CanvasItem::draw_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  1059. ClassDB::bind_method(D_METHOD("draw_multiline_string_outline", "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &CanvasItem::draw_multiline_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  1060. ClassDB::bind_method(D_METHOD("draw_char", "font", "pos", "char", "font_size", "modulate"), &CanvasItem::draw_char, DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)));
  1061. ClassDB::bind_method(D_METHOD("draw_char_outline", "font", "pos", "char", "font_size", "size", "modulate"), &CanvasItem::draw_char_outline, DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)));
  1062. ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "transform", "modulate"), &CanvasItem::draw_mesh, DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1, 1)));
  1063. ClassDB::bind_method(D_METHOD("draw_multimesh", "multimesh", "texture"), &CanvasItem::draw_multimesh);
  1064. ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform, DEFVAL(0.0), DEFVAL(Size2(1.0, 1.0)));
  1065. ClassDB::bind_method(D_METHOD("draw_set_transform_matrix", "xform"), &CanvasItem::draw_set_transform_matrix);
  1066. ClassDB::bind_method(D_METHOD("draw_animation_slice", "animation_length", "slice_begin", "slice_end", "offset"), &CanvasItem::draw_animation_slice, DEFVAL(0.0));
  1067. ClassDB::bind_method(D_METHOD("draw_end_animation"), &CanvasItem::draw_end_animation);
  1068. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasItem::get_transform);
  1069. ClassDB::bind_method(D_METHOD("get_global_transform"), &CanvasItem::get_global_transform);
  1070. ClassDB::bind_method(D_METHOD("get_global_transform_with_canvas"), &CanvasItem::get_global_transform_with_canvas);
  1071. ClassDB::bind_method(D_METHOD("get_viewport_transform"), &CanvasItem::get_viewport_transform);
  1072. ClassDB::bind_method(D_METHOD("get_viewport_rect"), &CanvasItem::get_viewport_rect);
  1073. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &CanvasItem::get_canvas_transform);
  1074. ClassDB::bind_method(D_METHOD("get_screen_transform"), &CanvasItem::get_screen_transform);
  1075. ClassDB::bind_method(D_METHOD("get_local_mouse_position"), &CanvasItem::get_local_mouse_position);
  1076. ClassDB::bind_method(D_METHOD("get_global_mouse_position"), &CanvasItem::get_global_mouse_position);
  1077. ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasItem::get_canvas);
  1078. ClassDB::bind_method(D_METHOD("get_canvas_layer_node"), &CanvasItem::get_canvas_layer_node);
  1079. ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasItem::get_world_2d);
  1080. //ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasItem::get_viewport);
  1081. ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
  1082. ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);
  1083. ClassDB::bind_method(D_METHOD("set_instance_shader_parameter", "name", "value"), &CanvasItem::set_instance_shader_parameter);
  1084. ClassDB::bind_method(D_METHOD("get_instance_shader_parameter", "name"), &CanvasItem::get_instance_shader_parameter);
  1085. ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
  1086. ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);
  1087. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &CanvasItem::set_notify_local_transform);
  1088. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &CanvasItem::is_local_transform_notification_enabled);
  1089. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &CanvasItem::set_notify_transform);
  1090. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &CanvasItem::is_transform_notification_enabled);
  1091. ClassDB::bind_method(D_METHOD("force_update_transform"), &CanvasItem::force_update_transform);
  1092. ClassDB::bind_method(D_METHOD("make_canvas_position_local", "viewport_point"), &CanvasItem::make_canvas_position_local);
  1093. ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);
  1094. ClassDB::bind_method(D_METHOD("set_visibility_layer", "layer"), &CanvasItem::set_visibility_layer);
  1095. ClassDB::bind_method(D_METHOD("get_visibility_layer"), &CanvasItem::get_visibility_layer);
  1096. ClassDB::bind_method(D_METHOD("set_visibility_layer_bit", "layer", "enabled"), &CanvasItem::set_visibility_layer_bit);
  1097. ClassDB::bind_method(D_METHOD("get_visibility_layer_bit", "layer"), &CanvasItem::get_visibility_layer_bit);
  1098. ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &CanvasItem::set_texture_filter);
  1099. ClassDB::bind_method(D_METHOD("get_texture_filter"), &CanvasItem::get_texture_filter);
  1100. ClassDB::bind_method(D_METHOD("set_texture_repeat", "mode"), &CanvasItem::set_texture_repeat);
  1101. ClassDB::bind_method(D_METHOD("get_texture_repeat"), &CanvasItem::get_texture_repeat);
  1102. ClassDB::bind_method(D_METHOD("set_clip_children_mode", "mode"), &CanvasItem::set_clip_children_mode);
  1103. ClassDB::bind_method(D_METHOD("get_clip_children_mode"), &CanvasItem::get_clip_children_mode);
  1104. GDVIRTUAL_BIND(_draw);
  1105. ADD_GROUP("Visibility", "");
  1106. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1107. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  1108. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "self_modulate"), "set_self_modulate", "get_self_modulate");
  1109. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_behind_parent"), "set_draw_behind_parent", "is_draw_behind_parent_enabled");
  1110. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "top_level"), "set_as_top_level", "is_set_as_top_level");
  1111. ADD_PROPERTY(PropertyInfo(Variant::INT, "clip_children", PROPERTY_HINT_ENUM, "Disabled,Clip Only,Clip + Draw"), "set_clip_children_mode", "get_clip_children_mode");
  1112. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_light_mask", "get_light_mask");
  1113. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_layer", PROPERTY_HINT_LAYERS_2D_RENDER), "set_visibility_layer", "get_visibility_layer");
  1114. ADD_GROUP("Ordering", "");
  1115. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_index", "get_z_index");
  1116. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "z_as_relative"), "set_z_as_relative", "is_z_relative");
  1117. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "y_sort_enabled"), "set_y_sort_enabled", "is_y_sort_enabled");
  1118. ADD_GROUP("Texture", "texture_");
  1119. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Inherit,Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter");
  1120. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_repeat", PROPERTY_HINT_ENUM, "Inherit,Disabled,Enabled,Mirror"), "set_texture_repeat", "get_texture_repeat");
  1121. ADD_GROUP("Material", "");
  1122. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial"), "set_material", "get_material");
  1123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_parent_material"), "set_use_parent_material", "get_use_parent_material");
  1124. // ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),"set_transform_notify","is_transform_notify_enabled");
  1125. ADD_SIGNAL(MethodInfo("draw"));
  1126. ADD_SIGNAL(MethodInfo("visibility_changed"));
  1127. ADD_SIGNAL(MethodInfo("hidden"));
  1128. ADD_SIGNAL(MethodInfo("item_rect_changed"));
  1129. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  1130. BIND_CONSTANT(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  1131. BIND_CONSTANT(NOTIFICATION_DRAW);
  1132. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  1133. BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
  1134. BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
  1135. BIND_CONSTANT(NOTIFICATION_WORLD_2D_CHANGED);
  1136. BIND_ENUM_CONSTANT(TEXTURE_FILTER_PARENT_NODE);
  1137. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST);
  1138. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR);
  1139. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1140. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
  1141. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC);
  1142. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC);
  1143. BIND_ENUM_CONSTANT(TEXTURE_FILTER_MAX);
  1144. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_PARENT_NODE);
  1145. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_DISABLED);
  1146. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_ENABLED);
  1147. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_MIRROR);
  1148. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_MAX);
  1149. BIND_ENUM_CONSTANT(CLIP_CHILDREN_DISABLED);
  1150. BIND_ENUM_CONSTANT(CLIP_CHILDREN_ONLY);
  1151. BIND_ENUM_CONSTANT(CLIP_CHILDREN_AND_DRAW);
  1152. BIND_ENUM_CONSTANT(CLIP_CHILDREN_MAX);
  1153. }
  1154. Transform2D CanvasItem::get_canvas_transform() const {
  1155. ERR_READ_THREAD_GUARD_V(Transform2D());
  1156. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  1157. if (canvas_layer) {
  1158. return canvas_layer->get_final_transform();
  1159. } else if (Object::cast_to<CanvasItem>(get_parent())) {
  1160. return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
  1161. } else {
  1162. return get_viewport()->get_canvas_transform();
  1163. }
  1164. }
  1165. Transform2D CanvasItem::get_viewport_transform() const {
  1166. ERR_READ_THREAD_GUARD_V(Transform2D());
  1167. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  1168. if (canvas_layer) {
  1169. return get_viewport()->get_final_transform() * canvas_layer->get_final_transform();
  1170. } else {
  1171. return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
  1172. }
  1173. }
  1174. void CanvasItem::set_notify_local_transform(bool p_enable) {
  1175. ERR_THREAD_GUARD;
  1176. notify_local_transform = p_enable;
  1177. }
  1178. bool CanvasItem::is_local_transform_notification_enabled() const {
  1179. ERR_READ_THREAD_GUARD_V(false);
  1180. return notify_local_transform;
  1181. }
  1182. void CanvasItem::set_notify_transform(bool p_enable) {
  1183. ERR_THREAD_GUARD;
  1184. if (notify_transform == p_enable) {
  1185. return;
  1186. }
  1187. notify_transform = p_enable;
  1188. if (notify_transform && is_inside_tree()) {
  1189. // This ensures that invalid globals get resolved, so notifications can be received.
  1190. _ALLOW_DISCARD_ get_global_transform();
  1191. }
  1192. }
  1193. bool CanvasItem::is_transform_notification_enabled() const {
  1194. ERR_READ_THREAD_GUARD_V(false);
  1195. return notify_transform;
  1196. }
  1197. int CanvasItem::get_canvas_layer() const {
  1198. ERR_READ_THREAD_GUARD_V(0);
  1199. if (canvas_layer) {
  1200. return canvas_layer->get_layer();
  1201. } else {
  1202. return 0;
  1203. }
  1204. }
  1205. CanvasLayer *CanvasItem::get_canvas_layer_node() const {
  1206. ERR_READ_THREAD_GUARD_V(nullptr);
  1207. return canvas_layer;
  1208. }
  1209. void CanvasItem::set_visibility_layer(uint32_t p_visibility_layer) {
  1210. ERR_THREAD_GUARD;
  1211. visibility_layer = p_visibility_layer;
  1212. RenderingServer::get_singleton()->canvas_item_set_visibility_layer(canvas_item, p_visibility_layer);
  1213. }
  1214. uint32_t CanvasItem::get_visibility_layer() const {
  1215. ERR_READ_THREAD_GUARD_V(0);
  1216. return visibility_layer;
  1217. }
  1218. void CanvasItem::set_visibility_layer_bit(uint32_t p_visibility_layer, bool p_enable) {
  1219. ERR_THREAD_GUARD;
  1220. ERR_FAIL_UNSIGNED_INDEX(p_visibility_layer, 32);
  1221. if (p_enable) {
  1222. set_visibility_layer(visibility_layer | (1 << p_visibility_layer));
  1223. } else {
  1224. set_visibility_layer(visibility_layer & (~(1 << p_visibility_layer)));
  1225. }
  1226. }
  1227. bool CanvasItem::get_visibility_layer_bit(uint32_t p_visibility_layer) const {
  1228. ERR_READ_THREAD_GUARD_V(false);
  1229. ERR_FAIL_UNSIGNED_INDEX_V(p_visibility_layer, 32, false);
  1230. return (visibility_layer & (1 << p_visibility_layer));
  1231. }
  1232. void CanvasItem::_refresh_texture_filter_cache() const {
  1233. if (!is_inside_tree()) {
  1234. return;
  1235. }
  1236. if (texture_filter == TEXTURE_FILTER_PARENT_NODE) {
  1237. CanvasItem *parent_item = get_parent_item();
  1238. if (parent_item) {
  1239. texture_filter_cache = parent_item->texture_filter_cache;
  1240. } else {
  1241. texture_filter_cache = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  1242. }
  1243. } else {
  1244. texture_filter_cache = RS::CanvasItemTextureFilter(texture_filter);
  1245. }
  1246. }
  1247. void CanvasItem::_update_self_texture_filter(RS::CanvasItemTextureFilter p_texture_filter) {
  1248. RS::get_singleton()->canvas_item_set_default_texture_filter(get_canvas_item(), p_texture_filter);
  1249. queue_redraw();
  1250. }
  1251. void CanvasItem::_update_texture_filter_changed(bool p_propagate) {
  1252. if (!is_inside_tree()) {
  1253. return;
  1254. }
  1255. _refresh_texture_filter_cache();
  1256. _update_self_texture_filter(texture_filter_cache);
  1257. if (p_propagate) {
  1258. for (CanvasItem *E : children_items) {
  1259. if (!E->top_level && E->texture_filter == TEXTURE_FILTER_PARENT_NODE) {
  1260. E->_update_texture_filter_changed(true);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. void CanvasItem::set_texture_filter(TextureFilter p_texture_filter) {
  1266. ERR_MAIN_THREAD_GUARD; // Goes down in the tree, so only main thread can set.
  1267. ERR_FAIL_INDEX(p_texture_filter, TEXTURE_FILTER_MAX);
  1268. if (texture_filter == p_texture_filter) {
  1269. return;
  1270. }
  1271. texture_filter = p_texture_filter;
  1272. _update_texture_filter_changed(true);
  1273. notify_property_list_changed();
  1274. }
  1275. CanvasItem::TextureFilter CanvasItem::get_texture_filter() const {
  1276. ERR_READ_THREAD_GUARD_V(TEXTURE_FILTER_NEAREST);
  1277. return texture_filter;
  1278. }
  1279. void CanvasItem::_refresh_texture_repeat_cache() const {
  1280. if (!is_inside_tree()) {
  1281. return;
  1282. }
  1283. if (texture_repeat == TEXTURE_REPEAT_PARENT_NODE) {
  1284. CanvasItem *parent_item = get_parent_item();
  1285. if (parent_item) {
  1286. texture_repeat_cache = parent_item->texture_repeat_cache;
  1287. } else {
  1288. texture_repeat_cache = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  1289. }
  1290. } else {
  1291. texture_repeat_cache = RS::CanvasItemTextureRepeat(texture_repeat);
  1292. }
  1293. }
  1294. void CanvasItem::_update_self_texture_repeat(RS::CanvasItemTextureRepeat p_texture_repeat) {
  1295. RS::get_singleton()->canvas_item_set_default_texture_repeat(get_canvas_item(), p_texture_repeat);
  1296. queue_redraw();
  1297. }
  1298. void CanvasItem::_update_texture_repeat_changed(bool p_propagate) {
  1299. if (!is_inside_tree()) {
  1300. return;
  1301. }
  1302. _refresh_texture_repeat_cache();
  1303. _update_self_texture_repeat(texture_repeat_cache);
  1304. if (p_propagate) {
  1305. for (CanvasItem *E : children_items) {
  1306. if (!E->top_level && E->texture_repeat == TEXTURE_REPEAT_PARENT_NODE) {
  1307. E->_update_texture_repeat_changed(true);
  1308. }
  1309. }
  1310. }
  1311. }
  1312. void CanvasItem::set_texture_repeat(TextureRepeat p_texture_repeat) {
  1313. ERR_MAIN_THREAD_GUARD; // Goes down in the tree, so only main thread can set.
  1314. ERR_FAIL_INDEX(p_texture_repeat, TEXTURE_REPEAT_MAX);
  1315. if (texture_repeat == p_texture_repeat) {
  1316. return;
  1317. }
  1318. texture_repeat = p_texture_repeat;
  1319. _update_texture_repeat_changed(true);
  1320. notify_property_list_changed();
  1321. }
  1322. void CanvasItem::set_clip_children_mode(ClipChildrenMode p_clip_mode) {
  1323. ERR_THREAD_GUARD;
  1324. ERR_FAIL_COND(p_clip_mode >= CLIP_CHILDREN_MAX);
  1325. if (clip_children_mode == p_clip_mode) {
  1326. return;
  1327. }
  1328. clip_children_mode = p_clip_mode;
  1329. if (Object::cast_to<CanvasGroup>(this) != nullptr) {
  1330. //avoid accidental bugs, make this not work on CanvasGroup
  1331. return;
  1332. }
  1333. RS::get_singleton()->canvas_item_set_canvas_group_mode(get_canvas_item(), RS::CanvasGroupMode(clip_children_mode));
  1334. }
  1335. CanvasItem::ClipChildrenMode CanvasItem::get_clip_children_mode() const {
  1336. ERR_READ_THREAD_GUARD_V(CLIP_CHILDREN_DISABLED);
  1337. return clip_children_mode;
  1338. }
  1339. CanvasItem::TextureRepeat CanvasItem::get_texture_repeat() const {
  1340. ERR_READ_THREAD_GUARD_V(TEXTURE_REPEAT_DISABLED);
  1341. return texture_repeat;
  1342. }
  1343. CanvasItem::TextureFilter CanvasItem::get_texture_filter_in_tree() const {
  1344. ERR_READ_THREAD_GUARD_V(TEXTURE_FILTER_NEAREST);
  1345. _refresh_texture_filter_cache();
  1346. return (TextureFilter)texture_filter_cache;
  1347. }
  1348. CanvasItem::TextureRepeat CanvasItem::get_texture_repeat_in_tree() const {
  1349. ERR_READ_THREAD_GUARD_V(TEXTURE_REPEAT_DISABLED);
  1350. _refresh_texture_repeat_cache();
  1351. return (TextureRepeat)texture_repeat_cache;
  1352. }
  1353. CanvasItem::CanvasItem() :
  1354. xform_change(this) {
  1355. canvas_item = RenderingServer::get_singleton()->canvas_item_create();
  1356. }
  1357. CanvasItem::~CanvasItem() {
  1358. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1359. RenderingServer::get_singleton()->free(canvas_item);
  1360. }
  1361. ///////////////////////////////////////////////////////////////////
  1362. void CanvasTexture::set_diffuse_texture(const Ref<Texture2D> &p_diffuse) {
  1363. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_diffuse.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  1364. if (diffuse_texture == p_diffuse) {
  1365. return;
  1366. }
  1367. diffuse_texture = p_diffuse;
  1368. RID tex_rid = diffuse_texture.is_valid() ? diffuse_texture->get_rid() : RID();
  1369. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE, tex_rid);
  1370. emit_changed();
  1371. }
  1372. Ref<Texture2D> CanvasTexture::get_diffuse_texture() const {
  1373. return diffuse_texture;
  1374. }
  1375. void CanvasTexture::set_normal_texture(const Ref<Texture2D> &p_normal) {
  1376. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_normal.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  1377. if (normal_texture == p_normal) {
  1378. return;
  1379. }
  1380. normal_texture = p_normal;
  1381. RID tex_rid = normal_texture.is_valid() ? normal_texture->get_rid() : RID();
  1382. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_NORMAL, tex_rid);
  1383. emit_changed();
  1384. }
  1385. Ref<Texture2D> CanvasTexture::get_normal_texture() const {
  1386. return normal_texture;
  1387. }
  1388. void CanvasTexture::set_specular_texture(const Ref<Texture2D> &p_specular) {
  1389. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_specular.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  1390. if (specular_texture == p_specular) {
  1391. return;
  1392. }
  1393. specular_texture = p_specular;
  1394. RID tex_rid = specular_texture.is_valid() ? specular_texture->get_rid() : RID();
  1395. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_SPECULAR, tex_rid);
  1396. emit_changed();
  1397. }
  1398. Ref<Texture2D> CanvasTexture::get_specular_texture() const {
  1399. return specular_texture;
  1400. }
  1401. void CanvasTexture::set_specular_color(const Color &p_color) {
  1402. if (specular == p_color) {
  1403. return;
  1404. }
  1405. specular = p_color;
  1406. RS::get_singleton()->canvas_texture_set_shading_parameters(canvas_texture, specular, shininess);
  1407. emit_changed();
  1408. }
  1409. Color CanvasTexture::get_specular_color() const {
  1410. return specular;
  1411. }
  1412. void CanvasTexture::set_specular_shininess(real_t p_shininess) {
  1413. if (shininess == p_shininess) {
  1414. return;
  1415. }
  1416. shininess = p_shininess;
  1417. RS::get_singleton()->canvas_texture_set_shading_parameters(canvas_texture, specular, shininess);
  1418. emit_changed();
  1419. }
  1420. real_t CanvasTexture::get_specular_shininess() const {
  1421. return shininess;
  1422. }
  1423. void CanvasTexture::set_texture_filter(CanvasItem::TextureFilter p_filter) {
  1424. if (texture_filter == p_filter) {
  1425. return;
  1426. }
  1427. texture_filter = p_filter;
  1428. RS::get_singleton()->canvas_texture_set_texture_filter(canvas_texture, RS::CanvasItemTextureFilter(p_filter));
  1429. emit_changed();
  1430. }
  1431. CanvasItem::TextureFilter CanvasTexture::get_texture_filter() const {
  1432. return texture_filter;
  1433. }
  1434. void CanvasTexture::set_texture_repeat(CanvasItem::TextureRepeat p_repeat) {
  1435. if (texture_repeat == p_repeat) {
  1436. return;
  1437. }
  1438. texture_repeat = p_repeat;
  1439. RS::get_singleton()->canvas_texture_set_texture_repeat(canvas_texture, RS::CanvasItemTextureRepeat(p_repeat));
  1440. emit_changed();
  1441. }
  1442. CanvasItem::TextureRepeat CanvasTexture::get_texture_repeat() const {
  1443. return texture_repeat;
  1444. }
  1445. int CanvasTexture::get_width() const {
  1446. if (diffuse_texture.is_valid()) {
  1447. return diffuse_texture->get_width();
  1448. } else {
  1449. return 1;
  1450. }
  1451. }
  1452. int CanvasTexture::get_height() const {
  1453. if (diffuse_texture.is_valid()) {
  1454. return diffuse_texture->get_height();
  1455. } else {
  1456. return 1;
  1457. }
  1458. }
  1459. bool CanvasTexture::is_pixel_opaque(int p_x, int p_y) const {
  1460. if (diffuse_texture.is_valid()) {
  1461. return diffuse_texture->is_pixel_opaque(p_x, p_y);
  1462. } else {
  1463. return false;
  1464. }
  1465. }
  1466. bool CanvasTexture::has_alpha() const {
  1467. if (diffuse_texture.is_valid()) {
  1468. return diffuse_texture->has_alpha();
  1469. } else {
  1470. return false;
  1471. }
  1472. }
  1473. Ref<Image> CanvasTexture::get_image() const {
  1474. if (diffuse_texture.is_valid()) {
  1475. return diffuse_texture->get_image();
  1476. } else {
  1477. return Ref<Image>();
  1478. }
  1479. }
  1480. RID CanvasTexture::get_rid() const {
  1481. return canvas_texture;
  1482. }
  1483. void CanvasTexture::_bind_methods() {
  1484. ClassDB::bind_method(D_METHOD("set_diffuse_texture", "texture"), &CanvasTexture::set_diffuse_texture);
  1485. ClassDB::bind_method(D_METHOD("get_diffuse_texture"), &CanvasTexture::get_diffuse_texture);
  1486. ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &CanvasTexture::set_normal_texture);
  1487. ClassDB::bind_method(D_METHOD("get_normal_texture"), &CanvasTexture::get_normal_texture);
  1488. ClassDB::bind_method(D_METHOD("set_specular_texture", "texture"), &CanvasTexture::set_specular_texture);
  1489. ClassDB::bind_method(D_METHOD("get_specular_texture"), &CanvasTexture::get_specular_texture);
  1490. ClassDB::bind_method(D_METHOD("set_specular_color", "color"), &CanvasTexture::set_specular_color);
  1491. ClassDB::bind_method(D_METHOD("get_specular_color"), &CanvasTexture::get_specular_color);
  1492. ClassDB::bind_method(D_METHOD("set_specular_shininess", "shininess"), &CanvasTexture::set_specular_shininess);
  1493. ClassDB::bind_method(D_METHOD("get_specular_shininess"), &CanvasTexture::get_specular_shininess);
  1494. ClassDB::bind_method(D_METHOD("set_texture_filter", "filter"), &CanvasTexture::set_texture_filter);
  1495. ClassDB::bind_method(D_METHOD("get_texture_filter"), &CanvasTexture::get_texture_filter);
  1496. ClassDB::bind_method(D_METHOD("set_texture_repeat", "repeat"), &CanvasTexture::set_texture_repeat);
  1497. ClassDB::bind_method(D_METHOD("get_texture_repeat"), &CanvasTexture::get_texture_repeat);
  1498. ADD_GROUP("Diffuse", "diffuse_");
  1499. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "diffuse_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_diffuse_texture", "get_diffuse_texture");
  1500. ADD_GROUP("NormalMap", "normal_");
  1501. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_normal_texture", "get_normal_texture");
  1502. ADD_GROUP("Specular", "specular_");
  1503. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "specular_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_specular_texture", "get_specular_texture");
  1504. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "specular_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_specular_color", "get_specular_color");
  1505. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "specular_shininess", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_specular_shininess", "get_specular_shininess");
  1506. ADD_GROUP("Texture", "texture_");
  1507. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Inherit,Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter");
  1508. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_repeat", PROPERTY_HINT_ENUM, "Inherit,Disabled,Enabled,Mirror"), "set_texture_repeat", "get_texture_repeat");
  1509. }
  1510. CanvasTexture::CanvasTexture() {
  1511. canvas_texture = RS::get_singleton()->canvas_texture_create();
  1512. }
  1513. CanvasTexture::~CanvasTexture() {
  1514. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1515. RS::get_singleton()->free(canvas_texture);
  1516. }