canvas_item.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*************************************************************************/
  2. /* canvas_item.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "canvas_item.h"
  31. #include "core/object/message_queue.h"
  32. #include "scene/2d/canvas_group.h"
  33. #include "scene/main/canvas_layer.h"
  34. #include "scene/main/window.h"
  35. #include "scene/resources/canvas_item_material.h"
  36. #include "scene/resources/font.h"
  37. #include "scene/resources/multimesh.h"
  38. #include "scene/resources/style_box.h"
  39. #include "scene/resources/world_2d.h"
  40. #include "scene/scene_string_names.h"
  41. #ifdef TOOLS_ENABLED
  42. bool CanvasItem::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  43. if (_edit_use_rect()) {
  44. return _edit_get_rect().has_point(p_point);
  45. } else {
  46. return p_point.length() < p_tolerance;
  47. }
  48. }
  49. Transform2D CanvasItem::_edit_get_transform() const {
  50. return Transform2D(_edit_get_rotation(), _edit_get_position() + _edit_get_pivot());
  51. }
  52. #endif
  53. bool CanvasItem::is_visible_in_tree() const {
  54. return visible && parent_visible_in_tree;
  55. }
  56. void CanvasItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
  57. parent_visible_in_tree = p_parent_visible_in_tree;
  58. if (!visible) {
  59. return;
  60. }
  61. _handle_visibility_change(p_parent_visible_in_tree);
  62. }
  63. void CanvasItem::set_visible(bool p_visible) {
  64. if (visible == p_visible) {
  65. return;
  66. }
  67. visible = p_visible;
  68. if (!parent_visible_in_tree) {
  69. notification(NOTIFICATION_VISIBILITY_CHANGED);
  70. return;
  71. }
  72. _handle_visibility_change(p_visible);
  73. }
  74. void CanvasItem::_handle_visibility_change(bool p_visible) {
  75. RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, p_visible);
  76. notification(NOTIFICATION_VISIBILITY_CHANGED);
  77. if (p_visible) {
  78. queue_redraw();
  79. } else {
  80. emit_signal(SceneStringNames::get_singleton()->hidden);
  81. }
  82. _block();
  83. for (int i = 0; i < get_child_count(); i++) {
  84. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  85. if (c) { // Should the top_levels stop propagation? I think so, but...
  86. c->_propagate_visibility_changed(p_visible);
  87. }
  88. }
  89. _unblock();
  90. }
  91. void CanvasItem::show() {
  92. set_visible(true);
  93. }
  94. void CanvasItem::hide() {
  95. set_visible(false);
  96. }
  97. bool CanvasItem::is_visible() const {
  98. return visible;
  99. }
  100. CanvasItem *CanvasItem::current_item_drawn = nullptr;
  101. CanvasItem *CanvasItem::get_current_item_drawn() {
  102. return current_item_drawn;
  103. }
  104. void CanvasItem::_redraw_callback() {
  105. if (!is_inside_tree()) {
  106. pending_update = false;
  107. return;
  108. }
  109. RenderingServer::get_singleton()->canvas_item_clear(get_canvas_item());
  110. //todo updating = true - only allow drawing here
  111. if (is_visible_in_tree()) {
  112. drawing = true;
  113. current_item_drawn = this;
  114. notification(NOTIFICATION_DRAW);
  115. emit_signal(SceneStringNames::get_singleton()->draw);
  116. GDVIRTUAL_CALL(_draw);
  117. current_item_drawn = nullptr;
  118. drawing = false;
  119. }
  120. //todo updating = false
  121. pending_update = false; // don't change to false until finished drawing (avoid recursive update)
  122. }
  123. Transform2D CanvasItem::get_global_transform_with_canvas() const {
  124. if (canvas_layer) {
  125. return canvas_layer->get_transform() * get_global_transform();
  126. } else if (is_inside_tree()) {
  127. return get_viewport()->get_canvas_transform() * get_global_transform();
  128. } else {
  129. return get_global_transform();
  130. }
  131. }
  132. Transform2D CanvasItem::get_screen_transform() const {
  133. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  134. Transform2D xform = get_global_transform_with_canvas();
  135. Window *w = Object::cast_to<Window>(get_viewport());
  136. if (w && !w->is_embedding_subwindows()) {
  137. Transform2D s;
  138. s.set_origin(w->get_position());
  139. xform = s * xform;
  140. }
  141. return xform;
  142. }
  143. Transform2D CanvasItem::get_global_transform() const {
  144. #ifdef DEBUG_ENABLED
  145. ERR_FAIL_COND_V(!is_inside_tree(), get_transform());
  146. #endif
  147. if (global_invalid) {
  148. const CanvasItem *pi = get_parent_item();
  149. if (pi) {
  150. global_transform = pi->get_global_transform() * get_transform();
  151. } else {
  152. global_transform = get_transform();
  153. }
  154. global_invalid = false;
  155. }
  156. return global_transform;
  157. }
  158. void CanvasItem::_top_level_raise_self() {
  159. if (!is_inside_tree()) {
  160. return;
  161. }
  162. if (canvas_layer) {
  163. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, canvas_layer->get_sort_index());
  164. } else {
  165. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_viewport()->gui_get_canvas_sort_index());
  166. }
  167. }
  168. void CanvasItem::_enter_canvas() {
  169. if ((!Object::cast_to<CanvasItem>(get_parent())) || top_level) {
  170. Node *n = this;
  171. canvas_layer = nullptr;
  172. while (n) {
  173. canvas_layer = Object::cast_to<CanvasLayer>(n);
  174. if (canvas_layer) {
  175. break;
  176. }
  177. if (Object::cast_to<Viewport>(n)) {
  178. break;
  179. }
  180. n = n->get_parent();
  181. }
  182. RID canvas;
  183. if (canvas_layer) {
  184. canvas = canvas_layer->get_canvas();
  185. } else {
  186. canvas = get_viewport()->find_world_2d()->get_canvas();
  187. }
  188. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  189. canvas_group = "root_canvas" + itos(canvas.get_id());
  190. add_to_group(canvas_group);
  191. if (canvas_layer) {
  192. canvas_layer->reset_sort_index();
  193. } else {
  194. get_viewport()->gui_reset_canvas_sort_index();
  195. }
  196. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE | SceneTree::GROUP_CALL_DEFERRED, canvas_group, SNAME("_top_level_raise_self"));
  197. } else {
  198. CanvasItem *parent = get_parent_item();
  199. canvas_layer = parent->canvas_layer;
  200. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, parent->get_canvas_item());
  201. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  202. }
  203. pending_update = false;
  204. queue_redraw();
  205. notification(NOTIFICATION_ENTER_CANVAS);
  206. }
  207. void CanvasItem::_exit_canvas() {
  208. notification(NOTIFICATION_EXIT_CANVAS, true); //reverse the notification
  209. RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, RID());
  210. canvas_layer = nullptr;
  211. if (canvas_group != StringName()) {
  212. remove_from_group(canvas_group);
  213. canvas_group = StringName();
  214. }
  215. }
  216. void CanvasItem::_notification(int p_what) {
  217. switch (p_what) {
  218. case NOTIFICATION_ENTER_TREE: {
  219. ERR_FAIL_COND(!is_inside_tree());
  220. Node *parent = get_parent();
  221. if (parent) {
  222. CanvasItem *ci = Object::cast_to<CanvasItem>(parent);
  223. if (ci) {
  224. parent_visible_in_tree = ci->is_visible_in_tree();
  225. C = ci->children_items.push_back(this);
  226. } else {
  227. CanvasLayer *cl = Object::cast_to<CanvasLayer>(parent);
  228. if (cl) {
  229. parent_visible_in_tree = cl->is_visible();
  230. } else {
  231. // Look for a window.
  232. Viewport *viewport = nullptr;
  233. while (parent) {
  234. viewport = Object::cast_to<Viewport>(parent);
  235. if (viewport) {
  236. break;
  237. }
  238. parent = parent->get_parent();
  239. }
  240. ERR_FAIL_COND(!viewport);
  241. window = Object::cast_to<Window>(viewport);
  242. if (window) {
  243. window->connect(SceneStringNames::get_singleton()->visibility_changed, callable_mp(this, &CanvasItem::_window_visibility_changed));
  244. parent_visible_in_tree = window->is_visible();
  245. } else {
  246. parent_visible_in_tree = true;
  247. }
  248. }
  249. }
  250. }
  251. RenderingServer::get_singleton()->canvas_item_set_visible(canvas_item, is_visible_in_tree()); // The visibility of the parent may change.
  252. if (is_visible_in_tree()) {
  253. notification(NOTIFICATION_VISIBILITY_CHANGED); // Considered invisible until entered.
  254. }
  255. _enter_canvas();
  256. _update_texture_filter_changed(false);
  257. _update_texture_repeat_changed(false);
  258. if (!block_transform_notify && !xform_change.in_list()) {
  259. get_tree()->xform_change_list.add(&xform_change);
  260. }
  261. } break;
  262. case NOTIFICATION_MOVED_IN_PARENT: {
  263. if (!is_inside_tree()) {
  264. break;
  265. }
  266. if (canvas_group != StringName()) {
  267. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE | SceneTree::GROUP_CALL_DEFERRED, canvas_group, "_top_level_raise_self");
  268. } else {
  269. CanvasItem *p = get_parent_item();
  270. ERR_FAIL_COND(!p);
  271. RenderingServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  272. }
  273. } break;
  274. case NOTIFICATION_EXIT_TREE: {
  275. if (xform_change.in_list()) {
  276. get_tree()->xform_change_list.remove(&xform_change);
  277. }
  278. _exit_canvas();
  279. if (C) {
  280. Object::cast_to<CanvasItem>(get_parent())->children_items.erase(C);
  281. C = nullptr;
  282. }
  283. if (window) {
  284. window->disconnect(SceneStringNames::get_singleton()->visibility_changed, callable_mp(this, &CanvasItem::_window_visibility_changed));
  285. }
  286. global_invalid = true;
  287. parent_visible_in_tree = false;
  288. } break;
  289. case NOTIFICATION_VISIBILITY_CHANGED: {
  290. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  291. } break;
  292. }
  293. }
  294. void CanvasItem::_window_visibility_changed() {
  295. if (visible) {
  296. _propagate_visibility_changed(window->is_visible());
  297. }
  298. }
  299. void CanvasItem::queue_redraw() {
  300. if (!is_inside_tree()) {
  301. return;
  302. }
  303. if (pending_update) {
  304. return;
  305. }
  306. pending_update = true;
  307. MessageQueue::get_singleton()->push_callable(callable_mp(this, &CanvasItem::_redraw_callback));
  308. }
  309. void CanvasItem::set_modulate(const Color &p_modulate) {
  310. if (modulate == p_modulate) {
  311. return;
  312. }
  313. modulate = p_modulate;
  314. RenderingServer::get_singleton()->canvas_item_set_modulate(canvas_item, modulate);
  315. }
  316. Color CanvasItem::get_modulate() const {
  317. return modulate;
  318. }
  319. void CanvasItem::set_as_top_level(bool p_top_level) {
  320. if (top_level == p_top_level) {
  321. return;
  322. }
  323. if (!is_inside_tree()) {
  324. top_level = p_top_level;
  325. return;
  326. }
  327. _exit_canvas();
  328. top_level = p_top_level;
  329. _enter_canvas();
  330. _notify_transform();
  331. }
  332. bool CanvasItem::is_set_as_top_level() const {
  333. return top_level;
  334. }
  335. CanvasItem *CanvasItem::get_parent_item() const {
  336. if (top_level) {
  337. return nullptr;
  338. }
  339. return Object::cast_to<CanvasItem>(get_parent());
  340. }
  341. void CanvasItem::set_self_modulate(const Color &p_self_modulate) {
  342. if (self_modulate == p_self_modulate) {
  343. return;
  344. }
  345. self_modulate = p_self_modulate;
  346. RenderingServer::get_singleton()->canvas_item_set_self_modulate(canvas_item, self_modulate);
  347. }
  348. Color CanvasItem::get_self_modulate() const {
  349. return self_modulate;
  350. }
  351. void CanvasItem::set_light_mask(int p_light_mask) {
  352. if (light_mask == p_light_mask) {
  353. return;
  354. }
  355. light_mask = p_light_mask;
  356. RS::get_singleton()->canvas_item_set_light_mask(canvas_item, p_light_mask);
  357. }
  358. int CanvasItem::get_light_mask() const {
  359. return light_mask;
  360. }
  361. void CanvasItem::item_rect_changed(bool p_size_changed) {
  362. if (p_size_changed) {
  363. queue_redraw();
  364. }
  365. emit_signal(SceneStringNames::get_singleton()->item_rect_changed);
  366. }
  367. 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) {
  368. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  369. float length = (p_to - p_from).length();
  370. if (length < p_dash) {
  371. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width);
  372. return;
  373. }
  374. Point2 off = p_from;
  375. Vector2 step = p_dash * (p_to - p_from).normalized();
  376. int steps = length / p_dash / 2;
  377. for (int i = 0; i < steps; i++) {
  378. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, off, (off + step), p_color, p_width);
  379. off += 2 * step;
  380. }
  381. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, off, p_to, p_color, p_width);
  382. }
  383. void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, real_t p_width, bool p_antialiased) {
  384. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  385. RenderingServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
  386. }
  387. void CanvasItem::draw_polyline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width, bool p_antialiased) {
  388. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  389. Vector<Color> colors;
  390. colors.push_back(p_color);
  391. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, colors, p_width, p_antialiased);
  392. }
  393. void CanvasItem::draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width, bool p_antialiased) {
  394. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  395. RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  396. }
  397. 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) {
  398. Vector<Point2> points;
  399. points.resize(p_point_count);
  400. const real_t delta_angle = p_end_angle - p_start_angle;
  401. for (int i = 0; i < p_point_count; i++) {
  402. real_t theta = (i / (p_point_count - 1.0f)) * delta_angle + p_start_angle;
  403. points.set(i, p_center + Vector2(Math::cos(theta), Math::sin(theta)) * p_radius);
  404. }
  405. draw_polyline(points, p_color, p_width, p_antialiased);
  406. }
  407. void CanvasItem::draw_multiline(const Vector<Point2> &p_points, const Color &p_color, real_t p_width) {
  408. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  409. Vector<Color> colors;
  410. colors.push_back(p_color);
  411. RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, colors, p_width);
  412. }
  413. void CanvasItem::draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, real_t p_width) {
  414. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  415. RenderingServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, p_colors, p_width);
  416. }
  417. void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled, real_t p_width) {
  418. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  419. if (p_filled) {
  420. if (p_width != 1.0) {
  421. WARN_PRINT("The draw_rect() \"width\" argument has no effect when \"filled\" is \"true\".");
  422. }
  423. RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, p_rect, p_color);
  424. } else {
  425. // Thick lines are offset depending on their width to avoid partial overlapping.
  426. // Thin lines don't require an offset, so don't apply one in this case
  427. real_t offset;
  428. if (p_width >= 2) {
  429. offset = p_width / 2.0;
  430. } else {
  431. offset = 0.0;
  432. }
  433. RenderingServer::get_singleton()->canvas_item_add_line(
  434. canvas_item,
  435. p_rect.position + Size2(-offset, 0),
  436. p_rect.position + Size2(p_rect.size.width + offset, 0),
  437. p_color,
  438. p_width);
  439. RenderingServer::get_singleton()->canvas_item_add_line(
  440. canvas_item,
  441. p_rect.position + Size2(p_rect.size.width, offset),
  442. p_rect.position + Size2(p_rect.size.width, p_rect.size.height - offset),
  443. p_color,
  444. p_width);
  445. RenderingServer::get_singleton()->canvas_item_add_line(
  446. canvas_item,
  447. p_rect.position + Size2(p_rect.size.width + offset, p_rect.size.height),
  448. p_rect.position + Size2(-offset, p_rect.size.height),
  449. p_color,
  450. p_width);
  451. RenderingServer::get_singleton()->canvas_item_add_line(
  452. canvas_item,
  453. p_rect.position + Size2(0, p_rect.size.height - offset),
  454. p_rect.position + Size2(0, offset),
  455. p_color,
  456. p_width);
  457. }
  458. }
  459. void CanvasItem::draw_circle(const Point2 &p_pos, real_t p_radius, const Color &p_color) {
  460. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  461. RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
  462. }
  463. void CanvasItem::draw_texture(const Ref<Texture2D> &p_texture, const Point2 &p_pos, const Color &p_modulate) {
  464. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  465. ERR_FAIL_COND(p_texture.is_null());
  466. p_texture->draw(canvas_item, p_pos, p_modulate, false);
  467. }
  468. void CanvasItem::draw_texture_rect(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) {
  469. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  470. ERR_FAIL_COND(p_texture.is_null());
  471. p_texture->draw_rect(canvas_item, p_rect, p_tile, p_modulate, p_transpose);
  472. }
  473. void CanvasItem::draw_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) {
  474. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  475. ERR_FAIL_COND(p_texture.is_null());
  476. p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  477. }
  478. void CanvasItem::draw_msdf_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, double p_outline, double p_pixel_range) {
  479. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  480. ERR_FAIL_COND(p_texture.is_null());
  481. 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);
  482. }
  483. void CanvasItem::draw_lcd_texture_rect_region(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate) {
  484. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  485. ERR_FAIL_COND(p_texture.is_null());
  486. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(canvas_item, p_rect, p_texture->get_rid(), p_src_rect, p_modulate);
  487. }
  488. void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
  489. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  490. ERR_FAIL_COND(p_style_box.is_null());
  491. p_style_box->draw(canvas_item, p_rect);
  492. }
  493. void CanvasItem::draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture, real_t p_width) {
  494. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  495. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  496. RenderingServer::get_singleton()->canvas_item_add_primitive(canvas_item, p_points, p_colors, p_uvs, rid, p_width);
  497. }
  498. void CanvasItem::draw_set_transform(const Point2 &p_offset, real_t p_rot, const Size2 &p_scale) {
  499. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  500. Transform2D xform(p_rot, p_offset);
  501. xform.scale_basis(p_scale);
  502. RenderingServer::get_singleton()->canvas_item_add_set_transform(canvas_item, xform);
  503. }
  504. void CanvasItem::draw_set_transform_matrix(const Transform2D &p_matrix) {
  505. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  506. RenderingServer::get_singleton()->canvas_item_add_set_transform(canvas_item, p_matrix);
  507. }
  508. void CanvasItem::draw_animation_slice(double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset) {
  509. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  510. RenderingServer::get_singleton()->canvas_item_add_animation_slice(canvas_item, p_animation_length, p_slice_begin, p_slice_end, p_offset);
  511. }
  512. void CanvasItem::draw_end_animation() {
  513. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  514. RenderingServer::get_singleton()->canvas_item_add_animation_slice(canvas_item, 1, 0, 2, 0);
  515. }
  516. void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
  517. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  518. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  519. RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid);
  520. }
  521. void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture2D> p_texture) {
  522. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  523. Vector<Color> colors;
  524. colors.push_back(p_color);
  525. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  526. RenderingServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid);
  527. }
  528. void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture2D> &p_texture, const Transform2D &p_transform, const Color &p_modulate) {
  529. ERR_FAIL_COND(p_mesh.is_null());
  530. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  531. RenderingServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), p_transform, p_modulate, texture_rid);
  532. }
  533. void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture2D> &p_texture) {
  534. ERR_FAIL_COND(p_multimesh.is_null());
  535. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  536. RenderingServer::get_singleton()->canvas_item_add_multimesh(canvas_item, p_multimesh->get_rid(), texture_rid);
  537. }
  538. void CanvasItem::draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  539. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  540. ERR_FAIL_COND(p_font.is_null());
  541. 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);
  542. }
  543. void CanvasItem::draw_multiline_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  544. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  545. ERR_FAIL_COND(p_font.is_null());
  546. 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);
  547. }
  548. void CanvasItem::draw_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  549. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  550. ERR_FAIL_COND(p_font.is_null());
  551. 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);
  552. }
  553. void CanvasItem::draw_multiline_string_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, int p_size, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  554. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  555. ERR_FAIL_COND(p_font.is_null());
  556. 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);
  557. }
  558. void CanvasItem::draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size, const Color &p_modulate) const {
  559. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  560. ERR_FAIL_COND(p_char.length() != 1);
  561. ERR_FAIL_COND(p_font.is_null());
  562. p_font->draw_char(canvas_item, p_pos, p_char[0], p_font_size, p_modulate);
  563. }
  564. void CanvasItem::draw_char_outline(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, int p_font_size, int p_size, const Color &p_modulate) const {
  565. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  566. ERR_FAIL_COND(p_char.length() != 1);
  567. ERR_FAIL_COND(p_font.is_null());
  568. p_font->draw_char_outline(canvas_item, p_pos, p_char[0], p_font_size, p_size, p_modulate);
  569. }
  570. void CanvasItem::_notify_transform(CanvasItem *p_node) {
  571. /* This check exists to avoid re-propagating the transform
  572. * notification down the tree on dirty nodes. It provides
  573. * optimization by avoiding redundancy (nodes are dirty, will get the
  574. * notification anyway).
  575. */
  576. if (/*p_node->xform_change.in_list() &&*/ p_node->global_invalid) {
  577. return; //nothing to do
  578. }
  579. p_node->global_invalid = true;
  580. if (p_node->notify_transform && !p_node->xform_change.in_list()) {
  581. if (!p_node->block_transform_notify) {
  582. if (p_node->is_inside_tree()) {
  583. get_tree()->xform_change_list.add(&p_node->xform_change);
  584. }
  585. }
  586. }
  587. for (CanvasItem *ci : p_node->children_items) {
  588. if (ci->top_level) {
  589. continue;
  590. }
  591. _notify_transform(ci);
  592. }
  593. }
  594. Rect2 CanvasItem::get_viewport_rect() const {
  595. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  596. return get_viewport()->get_visible_rect();
  597. }
  598. RID CanvasItem::get_canvas() const {
  599. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  600. if (canvas_layer) {
  601. return canvas_layer->get_canvas();
  602. } else {
  603. return get_viewport()->find_world_2d()->get_canvas();
  604. }
  605. }
  606. ObjectID CanvasItem::get_canvas_layer_instance_id() const {
  607. if (canvas_layer) {
  608. return canvas_layer->get_instance_id();
  609. } else {
  610. return ObjectID();
  611. }
  612. }
  613. CanvasItem *CanvasItem::get_top_level() const {
  614. CanvasItem *ci = const_cast<CanvasItem *>(this);
  615. while (!ci->top_level && Object::cast_to<CanvasItem>(ci->get_parent())) {
  616. ci = Object::cast_to<CanvasItem>(ci->get_parent());
  617. }
  618. return ci;
  619. }
  620. Ref<World2D> CanvasItem::get_world_2d() const {
  621. ERR_FAIL_COND_V(!is_inside_tree(), Ref<World2D>());
  622. CanvasItem *tl = get_top_level();
  623. if (tl->get_viewport()) {
  624. return tl->get_viewport()->find_world_2d();
  625. } else {
  626. return Ref<World2D>();
  627. }
  628. }
  629. RID CanvasItem::get_viewport_rid() const {
  630. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  631. return get_viewport()->get_viewport_rid();
  632. }
  633. void CanvasItem::set_block_transform_notify(bool p_enable) {
  634. block_transform_notify = p_enable;
  635. }
  636. bool CanvasItem::is_block_transform_notify_enabled() const {
  637. return block_transform_notify;
  638. }
  639. void CanvasItem::set_draw_behind_parent(bool p_enable) {
  640. if (behind == p_enable) {
  641. return;
  642. }
  643. behind = p_enable;
  644. RenderingServer::get_singleton()->canvas_item_set_draw_behind_parent(canvas_item, behind);
  645. }
  646. bool CanvasItem::is_draw_behind_parent_enabled() const {
  647. return behind;
  648. }
  649. void CanvasItem::set_material(const Ref<Material> &p_material) {
  650. material = p_material;
  651. RID rid;
  652. if (material.is_valid()) {
  653. rid = material->get_rid();
  654. }
  655. RS::get_singleton()->canvas_item_set_material(canvas_item, rid);
  656. notify_property_list_changed(); //properties for material exposed
  657. }
  658. void CanvasItem::set_use_parent_material(bool p_use_parent_material) {
  659. use_parent_material = p_use_parent_material;
  660. RS::get_singleton()->canvas_item_set_use_parent_material(canvas_item, p_use_parent_material);
  661. }
  662. bool CanvasItem::get_use_parent_material() const {
  663. return use_parent_material;
  664. }
  665. Ref<Material> CanvasItem::get_material() const {
  666. return material;
  667. }
  668. Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) const {
  669. ERR_FAIL_COND_V(!is_inside_tree(), screen_point);
  670. Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
  671. return local_matrix.xform(screen_point);
  672. }
  673. Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
  674. ERR_FAIL_COND_V(p_event.is_null(), p_event);
  675. ERR_FAIL_COND_V(!is_inside_tree(), p_event);
  676. return p_event->xformed_by((get_canvas_transform() * get_global_transform()).affine_inverse());
  677. }
  678. Vector2 CanvasItem::get_global_mouse_position() const {
  679. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  680. return get_canvas_transform().affine_inverse().xform(get_viewport()->get_mouse_position());
  681. }
  682. Vector2 CanvasItem::get_local_mouse_position() const {
  683. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  684. return get_global_transform().affine_inverse().xform(get_global_mouse_position());
  685. }
  686. void CanvasItem::force_update_transform() {
  687. ERR_FAIL_COND(!is_inside_tree());
  688. if (!xform_change.in_list()) {
  689. return;
  690. }
  691. get_tree()->xform_change_list.remove(&xform_change);
  692. notification(NOTIFICATION_TRANSFORM_CHANGED);
  693. }
  694. void CanvasItem::_bind_methods() {
  695. ClassDB::bind_method(D_METHOD("_top_level_raise_self"), &CanvasItem::_top_level_raise_self);
  696. #ifdef TOOLS_ENABLED
  697. ClassDB::bind_method(D_METHOD("_edit_set_state", "state"), &CanvasItem::_edit_set_state);
  698. ClassDB::bind_method(D_METHOD("_edit_get_state"), &CanvasItem::_edit_get_state);
  699. ClassDB::bind_method(D_METHOD("_edit_set_position", "position"), &CanvasItem::_edit_set_position);
  700. ClassDB::bind_method(D_METHOD("_edit_get_position"), &CanvasItem::_edit_get_position);
  701. ClassDB::bind_method(D_METHOD("_edit_set_scale", "scale"), &CanvasItem::_edit_set_scale);
  702. ClassDB::bind_method(D_METHOD("_edit_get_scale"), &CanvasItem::_edit_get_scale);
  703. ClassDB::bind_method(D_METHOD("_edit_set_rect", "rect"), &CanvasItem::_edit_set_rect);
  704. ClassDB::bind_method(D_METHOD("_edit_get_rect"), &CanvasItem::_edit_get_rect);
  705. ClassDB::bind_method(D_METHOD("_edit_use_rect"), &CanvasItem::_edit_use_rect);
  706. ClassDB::bind_method(D_METHOD("_edit_set_rotation", "degrees"), &CanvasItem::_edit_set_rotation);
  707. ClassDB::bind_method(D_METHOD("_edit_get_rotation"), &CanvasItem::_edit_get_rotation);
  708. ClassDB::bind_method(D_METHOD("_edit_use_rotation"), &CanvasItem::_edit_use_rotation);
  709. ClassDB::bind_method(D_METHOD("_edit_set_pivot", "pivot"), &CanvasItem::_edit_set_pivot);
  710. ClassDB::bind_method(D_METHOD("_edit_get_pivot"), &CanvasItem::_edit_get_pivot);
  711. ClassDB::bind_method(D_METHOD("_edit_use_pivot"), &CanvasItem::_edit_use_pivot);
  712. ClassDB::bind_method(D_METHOD("_edit_get_transform"), &CanvasItem::_edit_get_transform);
  713. #endif
  714. ClassDB::bind_method(D_METHOD("get_canvas_item"), &CanvasItem::get_canvas_item);
  715. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasItem::set_visible);
  716. ClassDB::bind_method(D_METHOD("is_visible"), &CanvasItem::is_visible);
  717. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &CanvasItem::is_visible_in_tree);
  718. ClassDB::bind_method(D_METHOD("show"), &CanvasItem::show);
  719. ClassDB::bind_method(D_METHOD("hide"), &CanvasItem::hide);
  720. ClassDB::bind_method(D_METHOD("queue_redraw"), &CanvasItem::queue_redraw);
  721. ClassDB::bind_method(D_METHOD("set_as_top_level", "enable"), &CanvasItem::set_as_top_level);
  722. ClassDB::bind_method(D_METHOD("is_set_as_top_level"), &CanvasItem::is_set_as_top_level);
  723. ClassDB::bind_method(D_METHOD("set_light_mask", "light_mask"), &CanvasItem::set_light_mask);
  724. ClassDB::bind_method(D_METHOD("get_light_mask"), &CanvasItem::get_light_mask);
  725. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &CanvasItem::set_modulate);
  726. ClassDB::bind_method(D_METHOD("get_modulate"), &CanvasItem::get_modulate);
  727. ClassDB::bind_method(D_METHOD("set_self_modulate", "self_modulate"), &CanvasItem::set_self_modulate);
  728. ClassDB::bind_method(D_METHOD("get_self_modulate"), &CanvasItem::get_self_modulate);
  729. ClassDB::bind_method(D_METHOD("set_draw_behind_parent", "enable"), &CanvasItem::set_draw_behind_parent);
  730. ClassDB::bind_method(D_METHOD("is_draw_behind_parent_enabled"), &CanvasItem::is_draw_behind_parent_enabled);
  731. ClassDB::bind_method(D_METHOD("draw_line", "from", "to", "color", "width", "antialiased"), &CanvasItem::draw_line, DEFVAL(1.0), DEFVAL(false));
  732. ClassDB::bind_method(D_METHOD("draw_dashed_line", "from", "to", "color", "width", "dash"), &CanvasItem::draw_dashed_line, DEFVAL(1.0), DEFVAL(2.0));
  733. ClassDB::bind_method(D_METHOD("draw_polyline", "points", "color", "width", "antialiased"), &CanvasItem::draw_polyline, DEFVAL(1.0), DEFVAL(false));
  734. ClassDB::bind_method(D_METHOD("draw_polyline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_polyline_colors, DEFVAL(1.0), DEFVAL(false));
  735. 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));
  736. ClassDB::bind_method(D_METHOD("draw_multiline", "points", "color", "width"), &CanvasItem::draw_multiline, DEFVAL(1.0));
  737. ClassDB::bind_method(D_METHOD("draw_multiline_colors", "points", "colors", "width"), &CanvasItem::draw_multiline_colors, DEFVAL(1.0));
  738. ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled", "width"), &CanvasItem::draw_rect, DEFVAL(true), DEFVAL(1.0));
  739. ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color"), &CanvasItem::draw_circle);
  740. ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)));
  741. 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));
  742. 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));
  743. ClassDB::bind_method(D_METHOD("draw_msdf_texture_rect_region", "texture", "rect", "src_rect", "modulate", "outline", "pixel_range"), &CanvasItem::draw_msdf_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(0.0), DEFVAL(4.0));
  744. 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)));
  745. ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
  746. ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width"), &CanvasItem::draw_primitive, DEFVAL(Ref<Texture2D>()), DEFVAL(1.0));
  747. ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture"), &CanvasItem::draw_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref<Texture2D>()));
  748. ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture"), &CanvasItem::draw_colored_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref<Texture2D>()));
  749. ClassDB::bind_method(D_METHOD("draw_string", "font", "pos", "text", "alignment", "width", "font_size", "modulate", "jst_flags", "direction", "orientation"), &CanvasItem::draw_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  750. ClassDB::bind_method(D_METHOD("draw_multiline_string", "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "jst_flags", "direction", "orientation"), &CanvasItem::draw_multiline_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  751. ClassDB::bind_method(D_METHOD("draw_string_outline", "font", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "jst_flags", "direction", "orientation"), &CanvasItem::draw_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  752. ClassDB::bind_method(D_METHOD("draw_multiline_string_outline", "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "jst_flags", "direction", "orientation"), &CanvasItem::draw_multiline_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  753. ClassDB::bind_method(D_METHOD("draw_char", "font", "pos", "char", "font_size", "modulate"), &CanvasItem::draw_char, DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)));
  754. ClassDB::bind_method(D_METHOD("draw_char_outline", "font", "pos", "char", "font_size", "size", "modulate"), &CanvasItem::draw_char_outline, DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)));
  755. ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "transform", "modulate"), &CanvasItem::draw_mesh, DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1, 1)));
  756. ClassDB::bind_method(D_METHOD("draw_multimesh", "multimesh", "texture"), &CanvasItem::draw_multimesh);
  757. ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform, DEFVAL(0.0), DEFVAL(Size2(1.0, 1.0)));
  758. ClassDB::bind_method(D_METHOD("draw_set_transform_matrix", "xform"), &CanvasItem::draw_set_transform_matrix);
  759. ClassDB::bind_method(D_METHOD("draw_animation_slice", "animation_length", "slice_begin", "slice_end", "offset"), &CanvasItem::draw_animation_slice, DEFVAL(0.0));
  760. ClassDB::bind_method(D_METHOD("draw_end_animation"), &CanvasItem::draw_end_animation);
  761. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasItem::get_transform);
  762. ClassDB::bind_method(D_METHOD("get_global_transform"), &CanvasItem::get_global_transform);
  763. ClassDB::bind_method(D_METHOD("get_global_transform_with_canvas"), &CanvasItem::get_global_transform_with_canvas);
  764. ClassDB::bind_method(D_METHOD("get_viewport_transform"), &CanvasItem::get_viewport_transform);
  765. ClassDB::bind_method(D_METHOD("get_viewport_rect"), &CanvasItem::get_viewport_rect);
  766. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &CanvasItem::get_canvas_transform);
  767. ClassDB::bind_method(D_METHOD("get_screen_transform"), &CanvasItem::get_screen_transform);
  768. ClassDB::bind_method(D_METHOD("get_local_mouse_position"), &CanvasItem::get_local_mouse_position);
  769. ClassDB::bind_method(D_METHOD("get_global_mouse_position"), &CanvasItem::get_global_mouse_position);
  770. ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasItem::get_canvas);
  771. ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasItem::get_world_2d);
  772. //ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasItem::get_viewport);
  773. ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
  774. ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);
  775. ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
  776. ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);
  777. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &CanvasItem::set_notify_local_transform);
  778. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &CanvasItem::is_local_transform_notification_enabled);
  779. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &CanvasItem::set_notify_transform);
  780. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &CanvasItem::is_transform_notification_enabled);
  781. ClassDB::bind_method(D_METHOD("force_update_transform"), &CanvasItem::force_update_transform);
  782. ClassDB::bind_method(D_METHOD("make_canvas_position_local", "screen_point"), &CanvasItem::make_canvas_position_local);
  783. ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);
  784. ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &CanvasItem::set_texture_filter);
  785. ClassDB::bind_method(D_METHOD("get_texture_filter"), &CanvasItem::get_texture_filter);
  786. ClassDB::bind_method(D_METHOD("set_texture_repeat", "mode"), &CanvasItem::set_texture_repeat);
  787. ClassDB::bind_method(D_METHOD("get_texture_repeat"), &CanvasItem::get_texture_repeat);
  788. ClassDB::bind_method(D_METHOD("set_clip_children", "enable"), &CanvasItem::set_clip_children);
  789. ClassDB::bind_method(D_METHOD("is_clipping_children"), &CanvasItem::is_clipping_children);
  790. GDVIRTUAL_BIND(_draw);
  791. ADD_GROUP("Visibility", "");
  792. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  793. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  794. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "self_modulate"), "set_self_modulate", "get_self_modulate");
  795. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_behind_parent"), "set_draw_behind_parent", "is_draw_behind_parent_enabled");
  796. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "top_level"), "set_as_top_level", "is_set_as_top_level");
  797. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_children"), "set_clip_children", "is_clipping_children");
  798. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_light_mask", "get_light_mask");
  799. ADD_GROUP("Texture", "texture_");
  800. 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");
  801. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_repeat", PROPERTY_HINT_ENUM, "Inherit,Disabled,Enabled,Mirror"), "set_texture_repeat", "get_texture_repeat");
  802. ADD_GROUP("Material", "");
  803. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial"), "set_material", "get_material");
  804. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_parent_material"), "set_use_parent_material", "get_use_parent_material");
  805. // ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),"set_transform_notify","is_transform_notify_enabled");
  806. ADD_SIGNAL(MethodInfo("draw"));
  807. ADD_SIGNAL(MethodInfo("visibility_changed"));
  808. ADD_SIGNAL(MethodInfo("hidden"));
  809. ADD_SIGNAL(MethodInfo("item_rect_changed"));
  810. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  811. BIND_CONSTANT(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
  812. BIND_CONSTANT(NOTIFICATION_DRAW);
  813. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  814. BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
  815. BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
  816. BIND_ENUM_CONSTANT(TEXTURE_FILTER_PARENT_NODE);
  817. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST);
  818. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR);
  819. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  820. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
  821. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC);
  822. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC);
  823. BIND_ENUM_CONSTANT(TEXTURE_FILTER_MAX);
  824. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_PARENT_NODE);
  825. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_DISABLED);
  826. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_ENABLED);
  827. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_MIRROR);
  828. BIND_ENUM_CONSTANT(TEXTURE_REPEAT_MAX);
  829. }
  830. Transform2D CanvasItem::get_canvas_transform() const {
  831. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  832. if (canvas_layer) {
  833. return canvas_layer->get_transform();
  834. } else if (Object::cast_to<CanvasItem>(get_parent())) {
  835. return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
  836. } else {
  837. return get_viewport()->get_canvas_transform();
  838. }
  839. }
  840. Transform2D CanvasItem::get_viewport_transform() const {
  841. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  842. if (canvas_layer) {
  843. return get_viewport()->get_final_transform() * canvas_layer->get_transform();
  844. } else {
  845. return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
  846. }
  847. }
  848. void CanvasItem::set_notify_local_transform(bool p_enable) {
  849. notify_local_transform = p_enable;
  850. }
  851. bool CanvasItem::is_local_transform_notification_enabled() const {
  852. return notify_local_transform;
  853. }
  854. void CanvasItem::set_notify_transform(bool p_enable) {
  855. if (notify_transform == p_enable) {
  856. return;
  857. }
  858. notify_transform = p_enable;
  859. if (notify_transform && is_inside_tree()) {
  860. // This ensures that invalid globals get resolved, so notifications can be received.
  861. _ALLOW_DISCARD_ get_global_transform();
  862. }
  863. }
  864. bool CanvasItem::is_transform_notification_enabled() const {
  865. return notify_transform;
  866. }
  867. int CanvasItem::get_canvas_layer() const {
  868. if (canvas_layer) {
  869. return canvas_layer->get_layer();
  870. } else {
  871. return 0;
  872. }
  873. }
  874. void CanvasItem::_update_texture_filter_changed(bool p_propagate) {
  875. if (!is_inside_tree()) {
  876. return;
  877. }
  878. if (texture_filter == TEXTURE_FILTER_PARENT_NODE) {
  879. CanvasItem *parent_item = get_parent_item();
  880. if (parent_item) {
  881. texture_filter_cache = parent_item->texture_filter_cache;
  882. } else {
  883. texture_filter_cache = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  884. }
  885. } else {
  886. texture_filter_cache = RS::CanvasItemTextureFilter(texture_filter);
  887. }
  888. RS::get_singleton()->canvas_item_set_default_texture_filter(get_canvas_item(), texture_filter_cache);
  889. queue_redraw();
  890. if (p_propagate) {
  891. for (CanvasItem *E : children_items) {
  892. if (!E->top_level && E->texture_filter == TEXTURE_FILTER_PARENT_NODE) {
  893. E->_update_texture_filter_changed(true);
  894. }
  895. }
  896. }
  897. }
  898. void CanvasItem::set_texture_filter(TextureFilter p_texture_filter) {
  899. ERR_FAIL_INDEX(p_texture_filter, TEXTURE_FILTER_MAX);
  900. if (texture_filter == p_texture_filter) {
  901. return;
  902. }
  903. texture_filter = p_texture_filter;
  904. _update_texture_filter_changed(true);
  905. notify_property_list_changed();
  906. }
  907. CanvasItem::TextureFilter CanvasItem::get_texture_filter() const {
  908. return texture_filter;
  909. }
  910. void CanvasItem::_update_texture_repeat_changed(bool p_propagate) {
  911. if (!is_inside_tree()) {
  912. return;
  913. }
  914. if (texture_repeat == TEXTURE_REPEAT_PARENT_NODE) {
  915. CanvasItem *parent_item = get_parent_item();
  916. if (parent_item) {
  917. texture_repeat_cache = parent_item->texture_repeat_cache;
  918. } else {
  919. texture_repeat_cache = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  920. }
  921. } else {
  922. texture_repeat_cache = RS::CanvasItemTextureRepeat(texture_repeat);
  923. }
  924. RS::get_singleton()->canvas_item_set_default_texture_repeat(get_canvas_item(), texture_repeat_cache);
  925. queue_redraw();
  926. if (p_propagate) {
  927. for (CanvasItem *E : children_items) {
  928. if (!E->top_level && E->texture_repeat == TEXTURE_REPEAT_PARENT_NODE) {
  929. E->_update_texture_repeat_changed(true);
  930. }
  931. }
  932. }
  933. }
  934. void CanvasItem::set_texture_repeat(TextureRepeat p_texture_repeat) {
  935. ERR_FAIL_INDEX(p_texture_repeat, TEXTURE_REPEAT_MAX);
  936. if (texture_repeat == p_texture_repeat) {
  937. return;
  938. }
  939. texture_repeat = p_texture_repeat;
  940. _update_texture_repeat_changed(true);
  941. notify_property_list_changed();
  942. }
  943. void CanvasItem::set_clip_children(bool p_enabled) {
  944. if (clip_children == p_enabled) {
  945. return;
  946. }
  947. clip_children = p_enabled;
  948. if (Object::cast_to<CanvasGroup>(this) != nullptr) {
  949. //avoid accidental bugs, make this not work on CanvasGroup
  950. return;
  951. }
  952. RS::get_singleton()->canvas_item_set_canvas_group_mode(get_canvas_item(), clip_children ? RS::CANVAS_GROUP_MODE_OPAQUE : RS::CANVAS_GROUP_MODE_DISABLED);
  953. }
  954. bool CanvasItem::is_clipping_children() const {
  955. return clip_children;
  956. }
  957. CanvasItem::TextureRepeat CanvasItem::get_texture_repeat() const {
  958. return texture_repeat;
  959. }
  960. CanvasItem::CanvasItem() :
  961. xform_change(this) {
  962. canvas_item = RenderingServer::get_singleton()->canvas_item_create();
  963. }
  964. CanvasItem::~CanvasItem() {
  965. RenderingServer::get_singleton()->free(canvas_item);
  966. }
  967. ///////////////////////////////////////////////////////////////////
  968. void CanvasTexture::set_diffuse_texture(const Ref<Texture2D> &p_diffuse) {
  969. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_diffuse.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  970. diffuse_texture = p_diffuse;
  971. RID tex_rid = diffuse_texture.is_valid() ? diffuse_texture->get_rid() : RID();
  972. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE, tex_rid);
  973. emit_changed();
  974. }
  975. Ref<Texture2D> CanvasTexture::get_diffuse_texture() const {
  976. return diffuse_texture;
  977. }
  978. void CanvasTexture::set_normal_texture(const Ref<Texture2D> &p_normal) {
  979. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_normal.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  980. normal_texture = p_normal;
  981. RID tex_rid = normal_texture.is_valid() ? normal_texture->get_rid() : RID();
  982. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_NORMAL, tex_rid);
  983. }
  984. Ref<Texture2D> CanvasTexture::get_normal_texture() const {
  985. return normal_texture;
  986. }
  987. void CanvasTexture::set_specular_texture(const Ref<Texture2D> &p_specular) {
  988. ERR_FAIL_COND_MSG(Object::cast_to<CanvasTexture>(p_specular.ptr()) != nullptr, "Can't self-assign a CanvasTexture");
  989. specular_texture = p_specular;
  990. RID tex_rid = specular_texture.is_valid() ? specular_texture->get_rid() : RID();
  991. RS::get_singleton()->canvas_texture_set_channel(canvas_texture, RS::CANVAS_TEXTURE_CHANNEL_SPECULAR, tex_rid);
  992. }
  993. Ref<Texture2D> CanvasTexture::get_specular_texture() const {
  994. return specular_texture;
  995. }
  996. void CanvasTexture::set_specular_color(const Color &p_color) {
  997. specular = p_color;
  998. RS::get_singleton()->canvas_texture_set_shading_parameters(canvas_texture, specular, shininess);
  999. }
  1000. Color CanvasTexture::get_specular_color() const {
  1001. return specular;
  1002. }
  1003. void CanvasTexture::set_specular_shininess(real_t p_shininess) {
  1004. shininess = p_shininess;
  1005. RS::get_singleton()->canvas_texture_set_shading_parameters(canvas_texture, specular, shininess);
  1006. }
  1007. real_t CanvasTexture::get_specular_shininess() const {
  1008. return shininess;
  1009. }
  1010. void CanvasTexture::set_texture_filter(CanvasItem::TextureFilter p_filter) {
  1011. texture_filter = p_filter;
  1012. RS::get_singleton()->canvas_texture_set_texture_filter(canvas_texture, RS::CanvasItemTextureFilter(p_filter));
  1013. }
  1014. CanvasItem::TextureFilter CanvasTexture::get_texture_filter() const {
  1015. return texture_filter;
  1016. }
  1017. void CanvasTexture::set_texture_repeat(CanvasItem::TextureRepeat p_repeat) {
  1018. texture_repeat = p_repeat;
  1019. RS::get_singleton()->canvas_texture_set_texture_repeat(canvas_texture, RS::CanvasItemTextureRepeat(p_repeat));
  1020. }
  1021. CanvasItem::TextureRepeat CanvasTexture::get_texture_repeat() const {
  1022. return texture_repeat;
  1023. }
  1024. int CanvasTexture::get_width() const {
  1025. if (diffuse_texture.is_valid()) {
  1026. return diffuse_texture->get_width();
  1027. } else {
  1028. return 1;
  1029. }
  1030. }
  1031. int CanvasTexture::get_height() const {
  1032. if (diffuse_texture.is_valid()) {
  1033. return diffuse_texture->get_height();
  1034. } else {
  1035. return 1;
  1036. }
  1037. }
  1038. bool CanvasTexture::is_pixel_opaque(int p_x, int p_y) const {
  1039. if (diffuse_texture.is_valid()) {
  1040. return diffuse_texture->is_pixel_opaque(p_x, p_y);
  1041. } else {
  1042. return false;
  1043. }
  1044. }
  1045. bool CanvasTexture::has_alpha() const {
  1046. if (diffuse_texture.is_valid()) {
  1047. return diffuse_texture->has_alpha();
  1048. } else {
  1049. return false;
  1050. }
  1051. }
  1052. Ref<Image> CanvasTexture::get_image() const {
  1053. if (diffuse_texture.is_valid()) {
  1054. return diffuse_texture->get_image();
  1055. } else {
  1056. return Ref<Image>();
  1057. }
  1058. }
  1059. RID CanvasTexture::get_rid() const {
  1060. return canvas_texture;
  1061. }
  1062. void CanvasTexture::_bind_methods() {
  1063. ClassDB::bind_method(D_METHOD("set_diffuse_texture", "texture"), &CanvasTexture::set_diffuse_texture);
  1064. ClassDB::bind_method(D_METHOD("get_diffuse_texture"), &CanvasTexture::get_diffuse_texture);
  1065. ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &CanvasTexture::set_normal_texture);
  1066. ClassDB::bind_method(D_METHOD("get_normal_texture"), &CanvasTexture::get_normal_texture);
  1067. ClassDB::bind_method(D_METHOD("set_specular_texture", "texture"), &CanvasTexture::set_specular_texture);
  1068. ClassDB::bind_method(D_METHOD("get_specular_texture"), &CanvasTexture::get_specular_texture);
  1069. ClassDB::bind_method(D_METHOD("set_specular_color", "color"), &CanvasTexture::set_specular_color);
  1070. ClassDB::bind_method(D_METHOD("get_specular_color"), &CanvasTexture::get_specular_color);
  1071. ClassDB::bind_method(D_METHOD("set_specular_shininess", "shininess"), &CanvasTexture::set_specular_shininess);
  1072. ClassDB::bind_method(D_METHOD("get_specular_shininess"), &CanvasTexture::get_specular_shininess);
  1073. ClassDB::bind_method(D_METHOD("set_texture_filter", "filter"), &CanvasTexture::set_texture_filter);
  1074. ClassDB::bind_method(D_METHOD("get_texture_filter"), &CanvasTexture::get_texture_filter);
  1075. ClassDB::bind_method(D_METHOD("set_texture_repeat", "repeat"), &CanvasTexture::set_texture_repeat);
  1076. ClassDB::bind_method(D_METHOD("get_texture_repeat"), &CanvasTexture::get_texture_repeat);
  1077. ADD_GROUP("Diffuse", "diffuse_");
  1078. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "diffuse_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_diffuse_texture", "get_diffuse_texture");
  1079. ADD_GROUP("NormalMap", "normal_");
  1080. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_normal_texture", "get_normal_texture");
  1081. ADD_GROUP("Specular", "specular_");
  1082. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "specular_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_specular_texture", "get_specular_texture");
  1083. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "specular_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_specular_color", "get_specular_color");
  1084. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "specular_shininess", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_specular_shininess", "get_specular_shininess");
  1085. ADD_GROUP("Texture", "texture_");
  1086. 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");
  1087. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_repeat", PROPERTY_HINT_ENUM, "Inherit,Disabled,Enabled,Mirror"), "set_texture_repeat", "get_texture_repeat");
  1088. }
  1089. CanvasTexture::CanvasTexture() {
  1090. canvas_texture = RS::get_singleton()->canvas_texture_create();
  1091. }
  1092. CanvasTexture::~CanvasTexture() {
  1093. RS::get_singleton()->free(canvas_texture);
  1094. }