canvas_item.cpp 67 KB

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