canvas_item.cpp 66 KB

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