canvas_item.cpp 71 KB

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