canvas_item.cpp 76 KB

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