canvas_item.cpp 39 KB

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