canvas_item.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*************************************************************************/
  2. /* canvas_item.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/method_bind_ext.gen.inc"
  32. #include "message_queue.h"
  33. #include "os/input.h"
  34. #include "scene/main/canvas_layer.h"
  35. #include "scene/main/viewport.h"
  36. #include "scene/resources/font.h"
  37. #include "scene/resources/style_box.h"
  38. #include "scene/resources/texture.h"
  39. #include "scene/scene_string_names.h"
  40. #include "servers/visual/visual_server_raster.h"
  41. #include "servers/visual_server.h"
  42. Mutex *CanvasItemMaterial::material_mutex = NULL;
  43. SelfList<CanvasItemMaterial>::List CanvasItemMaterial::dirty_materials;
  44. Map<CanvasItemMaterial::MaterialKey, CanvasItemMaterial::ShaderData> CanvasItemMaterial::shader_map;
  45. void CanvasItemMaterial::init_shaders() {
  46. #ifndef NO_THREADS
  47. material_mutex = Mutex::create();
  48. #endif
  49. }
  50. void CanvasItemMaterial::finish_shaders() {
  51. #ifndef NO_THREADS
  52. memdelete(material_mutex);
  53. #endif
  54. }
  55. void CanvasItemMaterial::_update_shader() {
  56. dirty_materials.remove(&element);
  57. MaterialKey mk = _compute_key();
  58. if (mk.key == current_key.key)
  59. return; //no update required in the end
  60. if (shader_map.has(current_key)) {
  61. shader_map[current_key].users--;
  62. if (shader_map[current_key].users == 0) {
  63. //deallocate shader, as it's no longer in use
  64. VS::get_singleton()->free(shader_map[current_key].shader);
  65. shader_map.erase(current_key);
  66. }
  67. }
  68. current_key = mk;
  69. if (shader_map.has(mk)) {
  70. VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  71. shader_map[mk].users++;
  72. return;
  73. }
  74. //must create a shader!
  75. String code = "shader_type canvas_item;\nrender_mode ";
  76. switch (blend_mode) {
  77. case BLEND_MODE_MIX: code += "blend_mix"; break;
  78. case BLEND_MODE_ADD: code += "blend_add"; break;
  79. case BLEND_MODE_SUB: code += "blend_sub"; break;
  80. case BLEND_MODE_MUL: code += "blend_mul"; break;
  81. case BLEND_MODE_PREMULT_ALPHA: code += "blend_premul_alpha"; break;
  82. }
  83. switch (light_mode) {
  84. case LIGHT_MODE_NORMAL: break;
  85. case LIGHT_MODE_UNSHADED: code += ",unshaded"; break;
  86. case LIGHT_MODE_LIGHT_ONLY: code += ",light_only"; break;
  87. }
  88. code += ";\n"; //thats it.
  89. ShaderData shader_data;
  90. shader_data.shader = VS::get_singleton()->shader_create();
  91. shader_data.users = 1;
  92. VS::get_singleton()->shader_set_code(shader_data.shader, code);
  93. shader_map[mk] = shader_data;
  94. VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  95. }
  96. void CanvasItemMaterial::flush_changes() {
  97. if (material_mutex)
  98. material_mutex->lock();
  99. while (dirty_materials.first()) {
  100. dirty_materials.first()->self()->_update_shader();
  101. }
  102. if (material_mutex)
  103. material_mutex->unlock();
  104. }
  105. void CanvasItemMaterial::_queue_shader_change() {
  106. if (material_mutex)
  107. material_mutex->lock();
  108. if (!element.in_list()) {
  109. dirty_materials.add(&element);
  110. }
  111. if (material_mutex)
  112. material_mutex->unlock();
  113. }
  114. bool CanvasItemMaterial::_is_shader_dirty() const {
  115. bool dirty = false;
  116. if (material_mutex)
  117. material_mutex->lock();
  118. dirty = element.in_list();
  119. if (material_mutex)
  120. material_mutex->unlock();
  121. return dirty;
  122. }
  123. void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
  124. blend_mode = p_blend_mode;
  125. _queue_shader_change();
  126. }
  127. CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
  128. return blend_mode;
  129. }
  130. void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
  131. light_mode = p_light_mode;
  132. _queue_shader_change();
  133. }
  134. CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
  135. return light_mode;
  136. }
  137. void CanvasItemMaterial::_validate_property(PropertyInfo &property) const {
  138. }
  139. void CanvasItemMaterial::_bind_methods() {
  140. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
  141. ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
  142. ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
  143. ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
  144. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul,Premult Alpha"), "set_blend_mode", "get_blend_mode");
  145. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
  146. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  147. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  148. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  149. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  150. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  151. BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
  152. BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
  153. BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
  154. }
  155. CanvasItemMaterial::CanvasItemMaterial()
  156. : element(this) {
  157. blend_mode = BLEND_MODE_MIX;
  158. light_mode = LIGHT_MODE_NORMAL;
  159. current_key.key = 0;
  160. current_key.invalid_key = 1;
  161. _queue_shader_change();
  162. }
  163. CanvasItemMaterial::~CanvasItemMaterial() {
  164. if (material_mutex)
  165. material_mutex->lock();
  166. if (shader_map.has(current_key)) {
  167. shader_map[current_key].users--;
  168. if (shader_map[current_key].users == 0) {
  169. //deallocate shader, as it's no longer in use
  170. VS::get_singleton()->free(shader_map[current_key].shader);
  171. shader_map.erase(current_key);
  172. }
  173. VS::get_singleton()->material_set_shader(_get_material(), RID());
  174. }
  175. if (material_mutex)
  176. material_mutex->unlock();
  177. }
  178. ///////////////////////////////////////////////////////////////////
  179. bool CanvasItem::is_visible_in_tree() const {
  180. if (!is_inside_tree())
  181. return false;
  182. const CanvasItem *p = this;
  183. while (p) {
  184. if (!p->visible)
  185. return false;
  186. p = p->get_parent_item();
  187. }
  188. return true;
  189. }
  190. void CanvasItem::_propagate_visibility_changed(bool p_visible) {
  191. notification(NOTIFICATION_VISIBILITY_CHANGED);
  192. if (p_visible)
  193. update(); //todo optimize
  194. else
  195. emit_signal(SceneStringNames::get_singleton()->hide);
  196. _block();
  197. for (int i = 0; i < get_child_count(); i++) {
  198. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  199. if (c && c->visible) //should the toplevels stop propagation? i think so but..
  200. c->_propagate_visibility_changed(p_visible);
  201. }
  202. _unblock();
  203. }
  204. void CanvasItem::show() {
  205. if (visible)
  206. return;
  207. visible = true;
  208. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, true);
  209. if (!is_inside_tree())
  210. return;
  211. _propagate_visibility_changed(true);
  212. _change_notify("visible");
  213. }
  214. void CanvasItem::hide() {
  215. if (!visible)
  216. return;
  217. visible = false;
  218. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, false);
  219. if (!is_inside_tree())
  220. return;
  221. _propagate_visibility_changed(false);
  222. _change_notify("visible");
  223. }
  224. Variant CanvasItem::edit_get_state() const {
  225. return Variant();
  226. }
  227. void CanvasItem::edit_set_state(const Variant &p_state) {
  228. }
  229. void CanvasItem::edit_set_rect(const Rect2 &p_edit_rect) {
  230. //used by editors, implement at will
  231. }
  232. void CanvasItem::edit_rotate(float p_rot) {
  233. }
  234. Size2 CanvasItem::edit_get_minimum_size() const {
  235. return Size2(-1, -1); //no limit
  236. }
  237. void CanvasItem::_update_callback() {
  238. if (!is_inside_tree()) {
  239. pending_update = false;
  240. return;
  241. }
  242. VisualServer::get_singleton()->canvas_item_clear(get_canvas_item());
  243. //todo updating = true - only allow drawing here
  244. if (is_visible_in_tree()) { //todo optimize this!!
  245. if (first_draw) {
  246. notification(NOTIFICATION_VISIBILITY_CHANGED);
  247. first_draw = false;
  248. }
  249. drawing = true;
  250. notification(NOTIFICATION_DRAW);
  251. emit_signal(SceneStringNames::get_singleton()->draw);
  252. if (get_script_instance()) {
  253. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_draw, NULL, 0);
  254. }
  255. drawing = false;
  256. }
  257. //todo updating = false
  258. pending_update = false; // don't change to false until finished drawing (avoid recursive update)
  259. }
  260. Transform2D CanvasItem::get_global_transform_with_canvas() const {
  261. const CanvasItem *ci = this;
  262. Transform2D xform;
  263. const CanvasItem *last_valid = NULL;
  264. while (ci) {
  265. last_valid = ci;
  266. xform = ci->get_transform() * xform;
  267. ci = ci->get_parent_item();
  268. }
  269. if (last_valid->canvas_layer)
  270. return last_valid->canvas_layer->get_transform() * xform;
  271. else if (is_inside_tree())
  272. return get_viewport()->get_canvas_transform() * xform;
  273. return xform;
  274. }
  275. Transform2D CanvasItem::get_global_transform() const {
  276. #ifdef DEBUG_ENABLED
  277. ERR_FAIL_COND_V(!is_inside_tree(), get_transform());
  278. #endif
  279. if (global_invalid) {
  280. const CanvasItem *pi = get_parent_item();
  281. if (pi)
  282. global_transform = pi->get_global_transform() * get_transform();
  283. else
  284. global_transform = get_transform();
  285. global_invalid = false;
  286. }
  287. return global_transform;
  288. }
  289. void CanvasItem::_toplevel_raise_self() {
  290. if (!is_inside_tree())
  291. return;
  292. if (canvas_layer)
  293. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, canvas_layer->get_sort_index());
  294. else
  295. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_viewport()->gui_get_canvas_sort_index());
  296. }
  297. void CanvasItem::_enter_canvas() {
  298. if ((!Object::cast_to<CanvasItem>(get_parent())) || toplevel) {
  299. Node *n = this;
  300. canvas_layer = NULL;
  301. while (n) {
  302. canvas_layer = Object::cast_to<CanvasLayer>(n);
  303. if (canvas_layer) {
  304. break;
  305. }
  306. n = n->get_parent();
  307. }
  308. RID canvas;
  309. if (canvas_layer)
  310. canvas = canvas_layer->get_world_2d()->get_canvas();
  311. else
  312. canvas = get_viewport()->find_world_2d()->get_canvas();
  313. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  314. group = "root_canvas" + itos(canvas.get_id());
  315. add_to_group(group);
  316. if (canvas_layer)
  317. canvas_layer->reset_sort_index();
  318. else
  319. get_viewport()->gui_reset_canvas_sort_index();
  320. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
  321. } else {
  322. CanvasItem *parent = get_parent_item();
  323. canvas_layer = parent->canvas_layer;
  324. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, parent->get_canvas_item());
  325. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  326. }
  327. pending_update = false;
  328. update();
  329. notification(NOTIFICATION_ENTER_CANVAS);
  330. }
  331. void CanvasItem::_exit_canvas() {
  332. notification(NOTIFICATION_EXIT_CANVAS, true); //reverse the notification
  333. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, RID());
  334. canvas_layer = NULL;
  335. group = "";
  336. }
  337. void CanvasItem::_notification(int p_what) {
  338. switch (p_what) {
  339. case NOTIFICATION_ENTER_TREE: {
  340. first_draw = true;
  341. if (get_parent()) {
  342. CanvasItem *ci = Object::cast_to<CanvasItem>(get_parent());
  343. if (ci)
  344. C = ci->children_items.push_back(this);
  345. }
  346. _enter_canvas();
  347. if (!block_transform_notify && !xform_change.in_list()) {
  348. get_tree()->xform_change_list.add(&xform_change);
  349. }
  350. } break;
  351. case NOTIFICATION_MOVED_IN_PARENT: {
  352. if (!is_inside_tree())
  353. break;
  354. if (group != "") {
  355. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
  356. } else {
  357. CanvasItem *p = get_parent_item();
  358. ERR_FAIL_COND(!p);
  359. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  360. }
  361. } break;
  362. case NOTIFICATION_EXIT_TREE: {
  363. if (xform_change.in_list())
  364. get_tree()->xform_change_list.remove(&xform_change);
  365. _exit_canvas();
  366. if (C) {
  367. Object::cast_to<CanvasItem>(get_parent())->children_items.erase(C);
  368. C = NULL;
  369. }
  370. global_invalid = true;
  371. } break;
  372. case NOTIFICATION_DRAW: {
  373. } break;
  374. case NOTIFICATION_TRANSFORM_CHANGED: {
  375. } break;
  376. case NOTIFICATION_VISIBILITY_CHANGED: {
  377. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  378. } break;
  379. }
  380. }
  381. void CanvasItem::set_visible(bool p_visible) {
  382. if (p_visible)
  383. show();
  384. else
  385. hide();
  386. }
  387. bool CanvasItem::is_visible() const {
  388. return visible;
  389. }
  390. void CanvasItem::update() {
  391. if (!is_inside_tree())
  392. return;
  393. if (pending_update)
  394. return;
  395. pending_update = true;
  396. MessageQueue::get_singleton()->push_call(this, "_update_callback");
  397. }
  398. void CanvasItem::set_modulate(const Color &p_modulate) {
  399. modulate = p_modulate;
  400. VisualServer::get_singleton()->canvas_item_set_modulate(canvas_item, modulate);
  401. }
  402. Color CanvasItem::get_modulate() const {
  403. return modulate;
  404. }
  405. void CanvasItem::set_as_toplevel(bool p_toplevel) {
  406. if (toplevel == p_toplevel)
  407. return;
  408. if (!is_inside_tree()) {
  409. toplevel = p_toplevel;
  410. return;
  411. }
  412. _exit_canvas();
  413. toplevel = p_toplevel;
  414. _enter_canvas();
  415. }
  416. bool CanvasItem::is_set_as_toplevel() const {
  417. return toplevel;
  418. }
  419. CanvasItem *CanvasItem::get_parent_item() const {
  420. if (toplevel)
  421. return NULL;
  422. return Object::cast_to<CanvasItem>(get_parent());
  423. }
  424. void CanvasItem::set_self_modulate(const Color &p_self_modulate) {
  425. self_modulate = p_self_modulate;
  426. VisualServer::get_singleton()->canvas_item_set_self_modulate(canvas_item, self_modulate);
  427. }
  428. Color CanvasItem::get_self_modulate() const {
  429. return self_modulate;
  430. }
  431. void CanvasItem::set_light_mask(int p_light_mask) {
  432. light_mask = p_light_mask;
  433. VS::get_singleton()->canvas_item_set_light_mask(canvas_item, p_light_mask);
  434. }
  435. int CanvasItem::get_light_mask() const {
  436. return light_mask;
  437. }
  438. void CanvasItem::item_rect_changed(bool p_size_changed) {
  439. if (p_size_changed)
  440. update();
  441. emit_signal(SceneStringNames::get_singleton()->item_rect_changed);
  442. }
  443. void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
  444. if (!drawing) {
  445. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  446. ERR_FAIL();
  447. }
  448. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
  449. }
  450. void CanvasItem::draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width, bool p_antialiased) {
  451. if (!drawing) {
  452. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  453. ERR_FAIL();
  454. }
  455. Vector<Color> colors;
  456. colors.push_back(p_color);
  457. VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, colors, p_width, p_antialiased);
  458. }
  459. void CanvasItem::draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  460. if (!drawing) {
  461. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  462. ERR_FAIL();
  463. }
  464. VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  465. }
  466. void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled) {
  467. if (!drawing) {
  468. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  469. ERR_FAIL();
  470. }
  471. if (p_filled) {
  472. VisualServer::get_singleton()->canvas_item_add_rect(canvas_item, p_rect, p_color);
  473. } else {
  474. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_rect.position, p_rect.position + Size2(p_rect.size.width, 0), p_color);
  475. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_rect.position, p_rect.position + Size2(0, p_rect.size.height), p_color);
  476. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_rect.position + Point2(0, p_rect.size.height), p_rect.position + p_rect.size, p_color);
  477. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_rect.position + Point2(p_rect.size.width, 0), p_rect.position + p_rect.size, p_color);
  478. }
  479. }
  480. void CanvasItem::draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color) {
  481. if (!drawing) {
  482. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  483. ERR_FAIL();
  484. }
  485. VisualServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
  486. }
  487. void CanvasItem::draw_texture(const Ref<Texture> &p_texture, const Point2 &p_pos, const Color &p_modulate, const Ref<Texture> &p_normal_map) {
  488. if (!drawing) {
  489. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  490. ERR_FAIL();
  491. }
  492. ERR_FAIL_COND(p_texture.is_null());
  493. p_texture->draw(canvas_item, p_pos, p_modulate);
  494. }
  495. void CanvasItem::draw_texture_rect(const Ref<Texture> &p_texture, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) {
  496. if (!drawing) {
  497. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  498. ERR_FAIL();
  499. }
  500. ERR_FAIL_COND(p_texture.is_null());
  501. p_texture->draw_rect(canvas_item, p_rect, p_tile, p_modulate, p_transpose, p_normal_map);
  502. }
  503. void CanvasItem::draw_texture_rect_region(const Ref<Texture> &p_texture, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) {
  504. if (!drawing) {
  505. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  506. ERR_FAIL();
  507. }
  508. ERR_FAIL_COND(p_texture.is_null());
  509. p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_normal_map, p_clip_uv);
  510. }
  511. void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
  512. if (!drawing) {
  513. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  514. ERR_FAIL();
  515. }
  516. ERR_FAIL_COND(p_style_box.is_null());
  517. p_style_box->draw(canvas_item, p_rect);
  518. }
  519. void CanvasItem::draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, float p_width, const Ref<Texture> &p_normal_map) {
  520. if (!drawing) {
  521. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  522. ERR_FAIL();
  523. }
  524. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  525. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  526. VisualServer::get_singleton()->canvas_item_add_primitive(canvas_item, p_points, p_colors, p_uvs, rid, p_width, rid_normal);
  527. }
  528. void CanvasItem::draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale) {
  529. if (!drawing) {
  530. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  531. ERR_FAIL();
  532. }
  533. Transform2D xform(p_rot, p_offset);
  534. xform.scale_basis(p_scale);
  535. VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, xform);
  536. }
  537. void CanvasItem::draw_set_transform_matrix(const Transform2D &p_matrix) {
  538. if (!drawing) {
  539. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  540. ERR_FAIL();
  541. }
  542. VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, p_matrix);
  543. }
  544. void CanvasItem::draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, const Ref<Texture> &p_normal_map, bool p_antialiased) {
  545. if (!drawing) {
  546. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  547. ERR_FAIL();
  548. }
  549. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  550. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  551. VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid, rid_normal, p_antialiased);
  552. }
  553. void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs, Ref<Texture> p_texture, const Ref<Texture> &p_normal_map, bool p_antialiased) {
  554. if (!drawing) {
  555. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  556. ERR_FAIL();
  557. }
  558. Vector<Color> colors;
  559. colors.push_back(p_color);
  560. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  561. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  562. VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid, rid_normal, p_antialiased);
  563. }
  564. void CanvasItem::draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, const Color &p_modulate, int p_clip_w) {
  565. if (!drawing) {
  566. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  567. ERR_FAIL();
  568. }
  569. ERR_FAIL_COND(p_font.is_null());
  570. p_font->draw(canvas_item, p_pos, p_text, p_modulate, p_clip_w);
  571. }
  572. float CanvasItem::draw_char(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_char, const String &p_next, const Color &p_modulate) {
  573. if (!drawing) {
  574. ERR_EXPLAIN("Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  575. ERR_FAIL_V(0);
  576. }
  577. ERR_FAIL_COND_V(p_char.length() != 1, 0);
  578. ERR_FAIL_COND_V(p_font.is_null(), 0);
  579. return p_font->draw_char(canvas_item, p_pos, p_char[0], p_next.c_str()[0], p_modulate);
  580. }
  581. void CanvasItem::_notify_transform(CanvasItem *p_node) {
  582. if (/*p_node->xform_change.in_list() &&*/ p_node->global_invalid) {
  583. return; //nothing to do
  584. }
  585. p_node->global_invalid = true;
  586. if (p_node->notify_transform && !p_node->xform_change.in_list()) {
  587. if (!p_node->block_transform_notify) {
  588. if (p_node->is_inside_tree())
  589. get_tree()->xform_change_list.add(&p_node->xform_change);
  590. }
  591. }
  592. for (List<CanvasItem *>::Element *E = p_node->children_items.front(); E; E = E->next()) {
  593. CanvasItem *ci = E->get();
  594. if (ci->toplevel)
  595. continue;
  596. _notify_transform(ci);
  597. }
  598. }
  599. Rect2 CanvasItem::get_viewport_rect() const {
  600. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  601. return get_viewport()->get_visible_rect();
  602. }
  603. RID CanvasItem::get_canvas() const {
  604. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  605. if (canvas_layer)
  606. return canvas_layer->get_world_2d()->get_canvas();
  607. else
  608. return get_viewport()->find_world_2d()->get_canvas();
  609. }
  610. CanvasItem *CanvasItem::get_toplevel() const {
  611. CanvasItem *ci = const_cast<CanvasItem *>(this);
  612. while (!ci->toplevel && Object::cast_to<CanvasItem>(ci->get_parent())) {
  613. ci = Object::cast_to<CanvasItem>(ci->get_parent());
  614. }
  615. return ci;
  616. }
  617. Ref<World2D> CanvasItem::get_world_2d() const {
  618. ERR_FAIL_COND_V(!is_inside_tree(), Ref<World2D>());
  619. CanvasItem *tl = get_toplevel();
  620. if (tl->canvas_layer) {
  621. return tl->canvas_layer->get_world_2d();
  622. } else if (tl->get_viewport()) {
  623. return tl->get_viewport()->find_world_2d();
  624. } else {
  625. return Ref<World2D>();
  626. }
  627. }
  628. RID CanvasItem::get_viewport_rid() const {
  629. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  630. return get_viewport()->get_viewport_rid();
  631. }
  632. void CanvasItem::set_block_transform_notify(bool p_enable) {
  633. block_transform_notify = p_enable;
  634. }
  635. bool CanvasItem::is_block_transform_notify_enabled() const {
  636. return block_transform_notify;
  637. }
  638. void CanvasItem::set_draw_behind_parent(bool p_enable) {
  639. if (behind == p_enable)
  640. return;
  641. behind = p_enable;
  642. VisualServer::get_singleton()->canvas_item_set_draw_behind_parent(canvas_item, behind);
  643. }
  644. bool CanvasItem::is_draw_behind_parent_enabled() const {
  645. return behind;
  646. }
  647. void CanvasItem::set_material(const Ref<Material> &p_material) {
  648. material = p_material;
  649. RID rid;
  650. if (material.is_valid())
  651. rid = material->get_rid();
  652. VS::get_singleton()->canvas_item_set_material(canvas_item, rid);
  653. _change_notify(); //properties for material exposed
  654. }
  655. void CanvasItem::set_use_parent_material(bool p_use_parent_material) {
  656. use_parent_material = p_use_parent_material;
  657. VS::get_singleton()->canvas_item_set_use_parent_material(canvas_item, p_use_parent_material);
  658. }
  659. bool CanvasItem::get_use_parent_material() const {
  660. return use_parent_material;
  661. }
  662. Ref<Material> CanvasItem::get_material() const {
  663. return material;
  664. }
  665. Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) const {
  666. ERR_FAIL_COND_V(!is_inside_tree(), screen_point);
  667. Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
  668. return local_matrix.xform(screen_point);
  669. }
  670. Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
  671. ERR_FAIL_COND_V(!is_inside_tree(), p_event);
  672. return p_event->xformed_by((get_canvas_transform() * get_global_transform()).affine_inverse());
  673. }
  674. Vector2 CanvasItem::get_global_mouse_position() const {
  675. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  676. return get_canvas_transform().affine_inverse().xform(get_viewport()->get_mouse_position());
  677. }
  678. Vector2 CanvasItem::get_local_mouse_position() const {
  679. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  680. return get_global_transform().affine_inverse().xform(get_global_mouse_position());
  681. }
  682. void CanvasItem::_bind_methods() {
  683. ClassDB::bind_method(D_METHOD("_toplevel_raise_self"), &CanvasItem::_toplevel_raise_self);
  684. ClassDB::bind_method(D_METHOD("_update_callback"), &CanvasItem::_update_callback);
  685. ClassDB::bind_method(D_METHOD("edit_set_state", "state"), &CanvasItem::edit_set_state);
  686. ClassDB::bind_method(D_METHOD("edit_get_state"), &CanvasItem::edit_get_state);
  687. ClassDB::bind_method(D_METHOD("edit_set_rect", "rect"), &CanvasItem::edit_set_rect);
  688. ClassDB::bind_method(D_METHOD("edit_rotate", "degrees"), &CanvasItem::edit_rotate);
  689. ClassDB::bind_method(D_METHOD("get_item_rect"), &CanvasItem::get_item_rect);
  690. ClassDB::bind_method(D_METHOD("get_item_and_children_rect"), &CanvasItem::get_item_and_children_rect);
  691. //ClassDB::bind_method(D_METHOD("get_transform"),&CanvasItem::get_transform);
  692. ClassDB::bind_method(D_METHOD("get_canvas_item"), &CanvasItem::get_canvas_item);
  693. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasItem::set_visible);
  694. ClassDB::bind_method(D_METHOD("is_visible"), &CanvasItem::is_visible);
  695. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &CanvasItem::is_visible_in_tree);
  696. ClassDB::bind_method(D_METHOD("show"), &CanvasItem::show);
  697. ClassDB::bind_method(D_METHOD("hide"), &CanvasItem::hide);
  698. ClassDB::bind_method(D_METHOD("update"), &CanvasItem::update);
  699. ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &CanvasItem::set_as_toplevel);
  700. ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &CanvasItem::is_set_as_toplevel);
  701. ClassDB::bind_method(D_METHOD("set_light_mask", "light_mask"), &CanvasItem::set_light_mask);
  702. ClassDB::bind_method(D_METHOD("get_light_mask"), &CanvasItem::get_light_mask);
  703. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &CanvasItem::set_modulate);
  704. ClassDB::bind_method(D_METHOD("get_modulate"), &CanvasItem::get_modulate);
  705. ClassDB::bind_method(D_METHOD("set_self_modulate", "self_modulate"), &CanvasItem::set_self_modulate);
  706. ClassDB::bind_method(D_METHOD("get_self_modulate"), &CanvasItem::get_self_modulate);
  707. ClassDB::bind_method(D_METHOD("set_draw_behind_parent", "enable"), &CanvasItem::set_draw_behind_parent);
  708. ClassDB::bind_method(D_METHOD("is_draw_behind_parent_enabled"), &CanvasItem::is_draw_behind_parent_enabled);
  709. ClassDB::bind_method(D_METHOD("_set_on_top", "on_top"), &CanvasItem::_set_on_top);
  710. ClassDB::bind_method(D_METHOD("_is_on_top"), &CanvasItem::_is_on_top);
  711. //ClassDB::bind_method(D_METHOD("get_transform"),&CanvasItem::get_transform);
  712. ClassDB::bind_method(D_METHOD("draw_line", "from", "to", "color", "width", "antialiased"), &CanvasItem::draw_line, DEFVAL(1.0), DEFVAL(false));
  713. ClassDB::bind_method(D_METHOD("draw_polyline", "points", "color", "width", "antialiased"), &CanvasItem::draw_polyline, DEFVAL(1.0), DEFVAL(false));
  714. ClassDB::bind_method(D_METHOD("draw_polyline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_polyline_colors, DEFVAL(1.0), DEFVAL(false));
  715. ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled"), &CanvasItem::draw_rect, DEFVAL(true));
  716. ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color"), &CanvasItem::draw_circle);
  717. ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate", "normal_map"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(Variant()));
  718. ClassDB::bind_method(D_METHOD("draw_texture_rect", "texture", "rect", "tile", "modulate", "transpose", "normal_map"), &CanvasItem::draw_texture_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()));
  719. ClassDB::bind_method(D_METHOD("draw_texture_rect_region", "texture", "rect", "src_rect", "modulate", "transpose", "normal_map", "clip_uv"), &CanvasItem::draw_texture_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()), DEFVAL(true));
  720. ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
  721. ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width", "normal_map"), &CanvasItem::draw_primitive, DEFVAL(Variant()), DEFVAL(1.0), DEFVAL(Variant()));
  722. ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
  723. ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_colored_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
  724. ClassDB::bind_method(D_METHOD("draw_string", "font", "position", "text", "modulate", "clip_w"), &CanvasItem::draw_string, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
  725. ClassDB::bind_method(D_METHOD("draw_char", "font", "position", "char", "next", "modulate"), &CanvasItem::draw_char, DEFVAL(Color(1, 1, 1)));
  726. ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform);
  727. ClassDB::bind_method(D_METHOD("draw_set_transform_matrix", "xform"), &CanvasItem::draw_set_transform_matrix);
  728. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasItem::get_transform);
  729. ClassDB::bind_method(D_METHOD("get_global_transform"), &CanvasItem::get_global_transform);
  730. ClassDB::bind_method(D_METHOD("get_global_transform_with_canvas"), &CanvasItem::get_global_transform_with_canvas);
  731. ClassDB::bind_method(D_METHOD("get_viewport_transform"), &CanvasItem::get_viewport_transform);
  732. ClassDB::bind_method(D_METHOD("get_viewport_rect"), &CanvasItem::get_viewport_rect);
  733. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &CanvasItem::get_canvas_transform);
  734. ClassDB::bind_method(D_METHOD("get_local_mouse_position"), &CanvasItem::get_local_mouse_position);
  735. ClassDB::bind_method(D_METHOD("get_global_mouse_position"), &CanvasItem::get_global_mouse_position);
  736. ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasItem::get_canvas);
  737. ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasItem::get_world_2d);
  738. //ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasItem::get_viewport);
  739. ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
  740. ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);
  741. ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
  742. ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);
  743. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &CanvasItem::set_notify_local_transform);
  744. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &CanvasItem::is_local_transform_notification_enabled);
  745. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &CanvasItem::set_notify_transform);
  746. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &CanvasItem::is_transform_notification_enabled);
  747. ClassDB::bind_method(D_METHOD("make_canvas_position_local", "screen_point"), &CanvasItem::make_canvas_position_local);
  748. ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);
  749. BIND_VMETHOD(MethodInfo("_draw"));
  750. ADD_GROUP("Visibility", "");
  751. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  752. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  753. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "self_modulate"), "set_self_modulate", "get_self_modulate");
  754. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "show_behind_parent"), "set_draw_behind_parent", "is_draw_behind_parent_enabled");
  755. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_on_top", PROPERTY_HINT_NONE, "", 0), "_set_on_top", "_is_on_top"); //compatibility
  756. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_light_mask", "get_light_mask");
  757. ADD_GROUP("Material", "");
  758. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial"), "set_material", "get_material");
  759. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "use_parent_material"), "set_use_parent_material", "get_use_parent_material");
  760. //exporting these two things doesn't really make much sense i think
  761. //ADD_PROPERTY( PropertyInfo(Variant::BOOL,"transform/toplevel"), "set_as_toplevel","is_set_as_toplevel") ;
  762. //ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),"set_transform_notify","is_transform_notify_enabled");
  763. ADD_SIGNAL(MethodInfo("draw"));
  764. ADD_SIGNAL(MethodInfo("visibility_changed"));
  765. ADD_SIGNAL(MethodInfo("hide"));
  766. ADD_SIGNAL(MethodInfo("item_rect_changed"));
  767. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  768. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  769. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  770. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  771. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  772. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  773. BIND_CONSTANT(NOTIFICATION_DRAW);
  774. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  775. BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
  776. BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
  777. }
  778. Transform2D CanvasItem::get_canvas_transform() const {
  779. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  780. if (canvas_layer)
  781. return canvas_layer->get_transform();
  782. else if (Object::cast_to<CanvasItem>(get_parent()))
  783. return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
  784. else
  785. return get_viewport()->get_canvas_transform();
  786. }
  787. Transform2D CanvasItem::get_viewport_transform() const {
  788. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  789. if (canvas_layer) {
  790. if (get_viewport()) {
  791. return get_viewport()->get_final_transform() * canvas_layer->get_transform();
  792. } else {
  793. return canvas_layer->get_transform();
  794. }
  795. } else {
  796. return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
  797. }
  798. }
  799. void CanvasItem::set_notify_local_transform(bool p_enable) {
  800. notify_local_transform = p_enable;
  801. }
  802. bool CanvasItem::is_local_transform_notification_enabled() const {
  803. return notify_local_transform;
  804. }
  805. void CanvasItem::set_notify_transform(bool p_enable) {
  806. if (notify_transform == p_enable)
  807. return;
  808. notify_transform = p_enable;
  809. if (notify_transform && is_inside_tree()) {
  810. //this ensures that invalid globals get resolved, so notifications can be received
  811. get_global_transform();
  812. }
  813. }
  814. bool CanvasItem::is_transform_notification_enabled() const {
  815. return notify_transform;
  816. }
  817. int CanvasItem::get_canvas_layer() const {
  818. if (canvas_layer)
  819. return canvas_layer->get_layer();
  820. else
  821. return 0;
  822. }
  823. Rect2 CanvasItem::get_item_and_children_rect() const {
  824. Rect2 rect = get_item_rect();
  825. for (int i = 0; i < get_child_count(); i++) {
  826. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  827. if (c) {
  828. Rect2 sir = c->get_transform().xform(c->get_item_and_children_rect());
  829. rect = rect.merge(sir);
  830. }
  831. }
  832. return rect;
  833. }
  834. CanvasItem::CanvasItem()
  835. : xform_change(this) {
  836. canvas_item = VisualServer::get_singleton()->canvas_item_create();
  837. visible = true;
  838. pending_update = false;
  839. modulate = Color(1, 1, 1, 1);
  840. self_modulate = Color(1, 1, 1, 1);
  841. toplevel = false;
  842. first_draw = false;
  843. drawing = false;
  844. behind = false;
  845. block_transform_notify = false;
  846. //viewport=NULL;
  847. canvas_layer = NULL;
  848. use_parent_material = false;
  849. global_invalid = true;
  850. notify_local_transform = false;
  851. notify_transform = false;
  852. light_mask = 1;
  853. C = NULL;
  854. }
  855. CanvasItem::~CanvasItem() {
  856. VisualServer::get_singleton()->free(canvas_item);
  857. }