canvas_item.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*************************************************************************/
  2. /* canvas_item.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/message_queue.h"
  32. #include "core/method_bind_ext.gen.inc"
  33. #include "core/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;
  43. SelfList<CanvasItemMaterial>::List *CanvasItemMaterial::dirty_materials = NULL;
  44. Map<CanvasItemMaterial::MaterialKey, CanvasItemMaterial::ShaderData> CanvasItemMaterial::shader_map;
  45. CanvasItemMaterial::ShaderNames *CanvasItemMaterial::shader_names = NULL;
  46. void CanvasItemMaterial::init_shaders() {
  47. dirty_materials = memnew(SelfList<CanvasItemMaterial>::List);
  48. shader_names = memnew(ShaderNames);
  49. shader_names->particles_anim_h_frames = "particles_anim_h_frames";
  50. shader_names->particles_anim_v_frames = "particles_anim_v_frames";
  51. shader_names->particles_anim_loop = "particles_anim_loop";
  52. }
  53. void CanvasItemMaterial::finish_shaders() {
  54. memdelete(dirty_materials);
  55. memdelete(shader_names);
  56. dirty_materials = NULL;
  57. }
  58. void CanvasItemMaterial::_update_shader() {
  59. dirty_materials->remove(&element);
  60. MaterialKey mk = _compute_key();
  61. if (mk.key == current_key.key)
  62. return; //no update required in the end
  63. if (shader_map.has(current_key)) {
  64. shader_map[current_key].users--;
  65. if (shader_map[current_key].users == 0) {
  66. //deallocate shader, as it's no longer in use
  67. VS::get_singleton()->free(shader_map[current_key].shader);
  68. shader_map.erase(current_key);
  69. }
  70. }
  71. current_key = mk;
  72. if (shader_map.has(mk)) {
  73. VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  74. shader_map[mk].users++;
  75. return;
  76. }
  77. //must create a shader!
  78. String code = "shader_type canvas_item;\nrender_mode ";
  79. switch (blend_mode) {
  80. case BLEND_MODE_MIX: code += "blend_mix"; break;
  81. case BLEND_MODE_ADD: code += "blend_add"; break;
  82. case BLEND_MODE_SUB: code += "blend_sub"; break;
  83. case BLEND_MODE_MUL: code += "blend_mul"; break;
  84. case BLEND_MODE_PREMULT_ALPHA: code += "blend_premul_alpha"; break;
  85. case BLEND_MODE_DISABLED: code += "blend_disabled"; break;
  86. }
  87. switch (light_mode) {
  88. case LIGHT_MODE_NORMAL: break;
  89. case LIGHT_MODE_UNSHADED: code += ",unshaded"; break;
  90. case LIGHT_MODE_LIGHT_ONLY: code += ",light_only"; break;
  91. }
  92. code += ";\n";
  93. if (particles_animation) {
  94. code += "uniform int particles_anim_h_frames;\n";
  95. code += "uniform int particles_anim_v_frames;\n";
  96. code += "uniform bool particles_anim_loop;\n";
  97. code += "void vertex() {\n";
  98. code += "\tfloat h_frames = float(particles_anim_h_frames);\n";
  99. code += "\tfloat v_frames = float(particles_anim_v_frames);\n";
  100. code += "\tVERTEX.xy /= vec2(h_frames, v_frames);\n";
  101. code += "\tfloat particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n";
  102. code += "\tfloat particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n";
  103. code += "\tif (!particles_anim_loop) {\n";
  104. code += "\t\tparticle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n";
  105. code += "\t} else {\n";
  106. code += "\t\tparticle_frame = mod(particle_frame, particle_total_frames);\n";
  107. code += "\t}";
  108. code += "\tUV /= vec2(h_frames, v_frames);\n";
  109. code += "\tUV += vec2(mod(particle_frame, h_frames) / h_frames, floor(particle_frame / h_frames) / v_frames);\n";
  110. code += "}\n";
  111. }
  112. ShaderData shader_data;
  113. shader_data.shader = VS::get_singleton()->shader_create();
  114. shader_data.users = 1;
  115. VS::get_singleton()->shader_set_code(shader_data.shader, code);
  116. shader_map[mk] = shader_data;
  117. VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  118. }
  119. void CanvasItemMaterial::flush_changes() {
  120. material_mutex.lock();
  121. while (dirty_materials->first()) {
  122. dirty_materials->first()->self()->_update_shader();
  123. }
  124. material_mutex.unlock();
  125. }
  126. void CanvasItemMaterial::_queue_shader_change() {
  127. material_mutex.lock();
  128. if (is_initialized && !element.in_list()) {
  129. dirty_materials->add(&element);
  130. }
  131. material_mutex.unlock();
  132. }
  133. bool CanvasItemMaterial::_is_shader_dirty() const {
  134. bool dirty = false;
  135. material_mutex.lock();
  136. dirty = element.in_list();
  137. material_mutex.unlock();
  138. return dirty;
  139. }
  140. void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
  141. blend_mode = p_blend_mode;
  142. _queue_shader_change();
  143. }
  144. CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
  145. return blend_mode;
  146. }
  147. void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
  148. light_mode = p_light_mode;
  149. _queue_shader_change();
  150. }
  151. CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
  152. return light_mode;
  153. }
  154. void CanvasItemMaterial::set_particles_animation(bool p_particles_anim) {
  155. particles_animation = p_particles_anim;
  156. _queue_shader_change();
  157. _change_notify();
  158. }
  159. bool CanvasItemMaterial::get_particles_animation() const {
  160. return particles_animation;
  161. }
  162. void CanvasItemMaterial::set_particles_anim_h_frames(int p_frames) {
  163. particles_anim_h_frames = p_frames;
  164. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
  165. }
  166. int CanvasItemMaterial::get_particles_anim_h_frames() const {
  167. return particles_anim_h_frames;
  168. }
  169. void CanvasItemMaterial::set_particles_anim_v_frames(int p_frames) {
  170. particles_anim_v_frames = p_frames;
  171. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
  172. }
  173. int CanvasItemMaterial::get_particles_anim_v_frames() const {
  174. return particles_anim_v_frames;
  175. }
  176. void CanvasItemMaterial::set_particles_anim_loop(bool p_loop) {
  177. particles_anim_loop = p_loop;
  178. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
  179. }
  180. bool CanvasItemMaterial::get_particles_anim_loop() const {
  181. return particles_anim_loop;
  182. }
  183. void CanvasItemMaterial::_validate_property(PropertyInfo &property) const {
  184. if (property.name.begins_with("particles_anim_") && !particles_animation) {
  185. property.usage = 0;
  186. }
  187. }
  188. RID CanvasItemMaterial::get_shader_rid() const {
  189. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  190. return shader_map[current_key].shader;
  191. }
  192. Shader::Mode CanvasItemMaterial::get_shader_mode() const {
  193. return Shader::MODE_CANVAS_ITEM;
  194. }
  195. void CanvasItemMaterial::_bind_methods() {
  196. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
  197. ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
  198. ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
  199. ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
  200. ClassDB::bind_method(D_METHOD("set_particles_animation", "particles_anim"), &CanvasItemMaterial::set_particles_animation);
  201. ClassDB::bind_method(D_METHOD("get_particles_animation"), &CanvasItemMaterial::get_particles_animation);
  202. ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &CanvasItemMaterial::set_particles_anim_h_frames);
  203. ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &CanvasItemMaterial::get_particles_anim_h_frames);
  204. ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &CanvasItemMaterial::set_particles_anim_v_frames);
  205. ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &CanvasItemMaterial::get_particles_anim_v_frames);
  206. ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &CanvasItemMaterial::set_particles_anim_loop);
  207. ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &CanvasItemMaterial::get_particles_anim_loop);
  208. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul,Premult Alpha"), "set_blend_mode", "get_blend_mode");
  209. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
  210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_animation"), "set_particles_animation", "get_particles_animation");
  211. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
  212. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
  213. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
  214. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  215. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  216. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  217. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  218. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  219. BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
  220. BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
  221. BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
  222. }
  223. CanvasItemMaterial::CanvasItemMaterial() :
  224. element(this) {
  225. blend_mode = BLEND_MODE_MIX;
  226. light_mode = LIGHT_MODE_NORMAL;
  227. particles_animation = false;
  228. set_particles_anim_h_frames(1);
  229. set_particles_anim_v_frames(1);
  230. set_particles_anim_loop(false);
  231. current_key.key = 0;
  232. current_key.invalid_key = 1;
  233. is_initialized = true;
  234. _queue_shader_change();
  235. }
  236. CanvasItemMaterial::~CanvasItemMaterial() {
  237. material_mutex.lock();
  238. if (shader_map.has(current_key)) {
  239. shader_map[current_key].users--;
  240. if (shader_map[current_key].users == 0) {
  241. //deallocate shader, as it's no longer in use
  242. VS::get_singleton()->free(shader_map[current_key].shader);
  243. shader_map.erase(current_key);
  244. }
  245. VS::get_singleton()->material_set_shader(_get_material(), RID());
  246. }
  247. material_mutex.unlock();
  248. }
  249. ///////////////////////////////////////////////////////////////////
  250. #ifdef TOOLS_ENABLED
  251. bool CanvasItem::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  252. if (_edit_use_rect()) {
  253. return _edit_get_rect().has_point(p_point);
  254. } else {
  255. return p_point.length() < p_tolerance;
  256. }
  257. }
  258. Transform2D CanvasItem::_edit_get_transform() const {
  259. return Transform2D(_edit_get_rotation(), _edit_get_position() + _edit_get_pivot());
  260. }
  261. #endif
  262. bool CanvasItem::is_visible_in_tree() const {
  263. if (!is_inside_tree())
  264. return false;
  265. const CanvasItem *p = this;
  266. while (p) {
  267. if (!p->visible)
  268. return false;
  269. p = p->get_parent_item();
  270. }
  271. return true;
  272. }
  273. void CanvasItem::_propagate_visibility_changed(bool p_visible) {
  274. if (p_visible && first_draw) { //avoid propagating it twice
  275. first_draw = false;
  276. }
  277. notification(NOTIFICATION_VISIBILITY_CHANGED);
  278. if (p_visible)
  279. update(); //todo optimize
  280. else
  281. emit_signal(SceneStringNames::get_singleton()->hide);
  282. _block();
  283. for (int i = 0; i < get_child_count(); i++) {
  284. CanvasItem *c = Object::cast_to<CanvasItem>(get_child(i));
  285. if (c && c->visible) //should the toplevels stop propagation? i think so but..
  286. c->_propagate_visibility_changed(p_visible);
  287. }
  288. _unblock();
  289. }
  290. void CanvasItem::show() {
  291. if (visible)
  292. return;
  293. visible = true;
  294. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, true);
  295. if (!is_inside_tree())
  296. return;
  297. _propagate_visibility_changed(true);
  298. _change_notify("visible");
  299. }
  300. void CanvasItem::hide() {
  301. if (!visible)
  302. return;
  303. visible = false;
  304. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item, false);
  305. if (!is_inside_tree())
  306. return;
  307. _propagate_visibility_changed(false);
  308. _change_notify("visible");
  309. }
  310. CanvasItem *CanvasItem::current_item_drawn = NULL;
  311. CanvasItem *CanvasItem::get_current_item_drawn() {
  312. return current_item_drawn;
  313. }
  314. void CanvasItem::_update_callback() {
  315. if (!is_inside_tree()) {
  316. pending_update = false;
  317. return;
  318. }
  319. VisualServer::get_singleton()->canvas_item_clear(get_canvas_item());
  320. //todo updating = true - only allow drawing here
  321. if (is_visible_in_tree()) { //todo optimize this!!
  322. if (first_draw) {
  323. notification(NOTIFICATION_VISIBILITY_CHANGED);
  324. first_draw = false;
  325. }
  326. drawing = true;
  327. current_item_drawn = this;
  328. notification(NOTIFICATION_DRAW);
  329. emit_signal(SceneStringNames::get_singleton()->draw);
  330. if (get_script_instance()) {
  331. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_draw, NULL, 0);
  332. }
  333. current_item_drawn = NULL;
  334. drawing = false;
  335. }
  336. //todo updating = false
  337. pending_update = false; // don't change to false until finished drawing (avoid recursive update)
  338. }
  339. Transform2D CanvasItem::get_global_transform_with_canvas() const {
  340. if (canvas_layer)
  341. return canvas_layer->get_transform() * get_global_transform();
  342. else if (is_inside_tree())
  343. return get_viewport()->get_canvas_transform() * get_global_transform();
  344. else
  345. return get_global_transform();
  346. }
  347. Transform2D CanvasItem::get_global_transform() const {
  348. #ifdef DEBUG_ENABLED
  349. ERR_FAIL_COND_V(!is_inside_tree(), get_transform());
  350. #endif
  351. if (global_invalid) {
  352. const CanvasItem *pi = get_parent_item();
  353. if (pi)
  354. global_transform = pi->get_global_transform() * get_transform();
  355. else
  356. global_transform = get_transform();
  357. global_invalid = false;
  358. }
  359. return global_transform;
  360. }
  361. void CanvasItem::_toplevel_raise_self() {
  362. if (!is_inside_tree())
  363. return;
  364. if (canvas_layer)
  365. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, canvas_layer->get_sort_index());
  366. else
  367. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_viewport()->gui_get_canvas_sort_index());
  368. }
  369. void CanvasItem::_enter_canvas() {
  370. if ((!Object::cast_to<CanvasItem>(get_parent())) || toplevel) {
  371. Node *n = this;
  372. canvas_layer = NULL;
  373. while (n) {
  374. canvas_layer = Object::cast_to<CanvasLayer>(n);
  375. if (canvas_layer) {
  376. break;
  377. }
  378. if (Object::cast_to<Viewport>(n)) {
  379. break;
  380. }
  381. n = n->get_parent();
  382. }
  383. RID canvas;
  384. if (canvas_layer)
  385. canvas = canvas_layer->get_canvas();
  386. else
  387. canvas = get_viewport()->find_world_2d()->get_canvas();
  388. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  389. group = "root_canvas" + itos(canvas.get_id());
  390. add_to_group(group);
  391. if (canvas_layer)
  392. canvas_layer->reset_sort_index();
  393. else
  394. get_viewport()->gui_reset_canvas_sort_index();
  395. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
  396. } else {
  397. CanvasItem *parent = get_parent_item();
  398. canvas_layer = parent->canvas_layer;
  399. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, parent->get_canvas_item());
  400. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  401. }
  402. pending_update = false;
  403. update();
  404. notification(NOTIFICATION_ENTER_CANVAS);
  405. }
  406. void CanvasItem::_exit_canvas() {
  407. notification(NOTIFICATION_EXIT_CANVAS, true); //reverse the notification
  408. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item, RID());
  409. canvas_layer = NULL;
  410. group = "";
  411. }
  412. void CanvasItem::_notification(int p_what) {
  413. switch (p_what) {
  414. case NOTIFICATION_ENTER_TREE: {
  415. first_draw = true;
  416. if (get_parent()) {
  417. CanvasItem *ci = Object::cast_to<CanvasItem>(get_parent());
  418. if (ci)
  419. C = ci->children_items.push_back(this);
  420. }
  421. _enter_canvas();
  422. if (!block_transform_notify && !xform_change.in_list()) {
  423. get_tree()->xform_change_list.add(&xform_change);
  424. }
  425. } break;
  426. case NOTIFICATION_MOVED_IN_PARENT: {
  427. if (!is_inside_tree())
  428. break;
  429. if (group != "") {
  430. get_tree()->call_group_flags(SceneTree::GROUP_CALL_UNIQUE, group, "_toplevel_raise_self");
  431. } else {
  432. CanvasItem *p = get_parent_item();
  433. ERR_FAIL_COND(!p);
  434. VisualServer::get_singleton()->canvas_item_set_draw_index(canvas_item, get_index());
  435. }
  436. } break;
  437. case NOTIFICATION_EXIT_TREE: {
  438. if (xform_change.in_list())
  439. get_tree()->xform_change_list.remove(&xform_change);
  440. _exit_canvas();
  441. if (C) {
  442. Object::cast_to<CanvasItem>(get_parent())->children_items.erase(C);
  443. C = NULL;
  444. }
  445. global_invalid = true;
  446. } break;
  447. case NOTIFICATION_DRAW:
  448. case NOTIFICATION_TRANSFORM_CHANGED: {
  449. } break;
  450. case NOTIFICATION_VISIBILITY_CHANGED: {
  451. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  452. } break;
  453. }
  454. }
  455. void CanvasItem::set_visible(bool p_visible) {
  456. if (p_visible)
  457. show();
  458. else
  459. hide();
  460. }
  461. bool CanvasItem::is_visible() const {
  462. return visible;
  463. }
  464. void CanvasItem::update() {
  465. if (!is_inside_tree())
  466. return;
  467. if (pending_update)
  468. return;
  469. pending_update = true;
  470. MessageQueue::get_singleton()->push_call(this, "_update_callback");
  471. }
  472. void CanvasItem::set_modulate(const Color &p_modulate) {
  473. if (modulate == p_modulate)
  474. return;
  475. modulate = p_modulate;
  476. VisualServer::get_singleton()->canvas_item_set_modulate(canvas_item, modulate);
  477. }
  478. Color CanvasItem::get_modulate() const {
  479. return modulate;
  480. }
  481. void CanvasItem::set_as_toplevel(bool p_toplevel) {
  482. if (toplevel == p_toplevel)
  483. return;
  484. if (!is_inside_tree()) {
  485. toplevel = p_toplevel;
  486. return;
  487. }
  488. _exit_canvas();
  489. toplevel = p_toplevel;
  490. _enter_canvas();
  491. _notify_transform();
  492. }
  493. bool CanvasItem::is_set_as_toplevel() const {
  494. return toplevel;
  495. }
  496. CanvasItem *CanvasItem::get_parent_item() const {
  497. if (toplevel)
  498. return NULL;
  499. return Object::cast_to<CanvasItem>(get_parent());
  500. }
  501. void CanvasItem::set_self_modulate(const Color &p_self_modulate) {
  502. if (self_modulate == p_self_modulate)
  503. return;
  504. self_modulate = p_self_modulate;
  505. VisualServer::get_singleton()->canvas_item_set_self_modulate(canvas_item, self_modulate);
  506. }
  507. Color CanvasItem::get_self_modulate() const {
  508. return self_modulate;
  509. }
  510. void CanvasItem::set_light_mask(int p_light_mask) {
  511. if (light_mask == p_light_mask)
  512. return;
  513. light_mask = p_light_mask;
  514. VS::get_singleton()->canvas_item_set_light_mask(canvas_item, p_light_mask);
  515. }
  516. int CanvasItem::get_light_mask() const {
  517. return light_mask;
  518. }
  519. void CanvasItem::item_rect_changed(bool p_size_changed) {
  520. if (p_size_changed)
  521. update();
  522. emit_signal(SceneStringNames::get_singleton()->item_rect_changed);
  523. }
  524. void CanvasItem::draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
  525. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  526. VisualServer::get_singleton()->canvas_item_add_line(canvas_item, p_from, p_to, p_color, p_width, p_antialiased);
  527. }
  528. void CanvasItem::draw_polyline(const Vector<Point2> &p_points, const Color &p_color, float p_width, bool p_antialiased) {
  529. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  530. Vector<Color> colors;
  531. colors.push_back(p_color);
  532. VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, colors, p_width, p_antialiased);
  533. }
  534. void CanvasItem::draw_polyline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  535. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  536. VisualServer::get_singleton()->canvas_item_add_polyline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  537. }
  538. void CanvasItem::draw_arc(const Vector2 &p_center, float p_radius, float p_start_angle, float p_end_angle, int p_point_count, const Color &p_color, float p_width, bool p_antialiased) {
  539. Vector<Point2> points;
  540. points.resize(p_point_count);
  541. const float delta_angle = p_end_angle - p_start_angle;
  542. for (int i = 0; i < p_point_count; i++) {
  543. float theta = (i / (p_point_count - 1.0f)) * delta_angle + p_start_angle;
  544. points.set(i, p_center + Vector2(Math::cos(theta), Math::sin(theta)) * p_radius);
  545. }
  546. draw_polyline(points, p_color, p_width, p_antialiased);
  547. }
  548. void CanvasItem::draw_multiline(const Vector<Point2> &p_points, const Color &p_color, float p_width, bool p_antialiased) {
  549. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  550. Vector<Color> colors;
  551. colors.push_back(p_color);
  552. VisualServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, colors, p_width, p_antialiased);
  553. }
  554. void CanvasItem::draw_multiline_colors(const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  555. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  556. VisualServer::get_singleton()->canvas_item_add_multiline(canvas_item, p_points, p_colors, p_width, p_antialiased);
  557. }
  558. void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled, float p_width, bool p_antialiased) {
  559. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  560. if (p_filled) {
  561. if (p_width != 1.0) {
  562. WARN_PRINT("The draw_rect() \"width\" argument has no effect when \"filled\" is \"true\".");
  563. }
  564. if (p_antialiased) {
  565. WARN_PRINT("The draw_rect() \"antialiased\" argument has no effect when \"filled\" is \"true\".");
  566. }
  567. VisualServer::get_singleton()->canvas_item_add_rect(canvas_item, p_rect, p_color);
  568. } else {
  569. // Thick lines are offset depending on their width to avoid partial overlapping.
  570. // Thin lines don't require an offset, so don't apply one in this case
  571. float offset;
  572. if (p_width >= 2) {
  573. offset = p_width / 2.0;
  574. } else {
  575. offset = 0.0;
  576. }
  577. VisualServer::get_singleton()->canvas_item_add_line(
  578. canvas_item,
  579. p_rect.position + Size2(-offset, 0),
  580. p_rect.position + Size2(p_rect.size.width + offset, 0),
  581. p_color,
  582. p_width,
  583. p_antialiased);
  584. VisualServer::get_singleton()->canvas_item_add_line(
  585. canvas_item,
  586. p_rect.position + Size2(p_rect.size.width, offset),
  587. p_rect.position + Size2(p_rect.size.width, p_rect.size.height - offset),
  588. p_color,
  589. p_width,
  590. p_antialiased);
  591. VisualServer::get_singleton()->canvas_item_add_line(
  592. canvas_item,
  593. p_rect.position + Size2(p_rect.size.width + offset, p_rect.size.height),
  594. p_rect.position + Size2(-offset, p_rect.size.height),
  595. p_color,
  596. p_width,
  597. p_antialiased);
  598. VisualServer::get_singleton()->canvas_item_add_line(
  599. canvas_item,
  600. p_rect.position + Size2(0, p_rect.size.height - offset),
  601. p_rect.position + Size2(0, offset),
  602. p_color,
  603. p_width,
  604. p_antialiased);
  605. }
  606. }
  607. void CanvasItem::draw_circle(const Point2 &p_pos, float p_radius, const Color &p_color) {
  608. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  609. VisualServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
  610. }
  611. void CanvasItem::draw_texture(const Ref<Texture> &p_texture, const Point2 &p_pos, const Color &p_modulate, const Ref<Texture> &p_normal_map) {
  612. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  613. ERR_FAIL_COND(p_texture.is_null());
  614. p_texture->draw(canvas_item, p_pos, p_modulate, false, p_normal_map);
  615. }
  616. 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) {
  617. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  618. ERR_FAIL_COND(p_texture.is_null());
  619. p_texture->draw_rect(canvas_item, p_rect, p_tile, p_modulate, p_transpose, p_normal_map);
  620. }
  621. 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) {
  622. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  623. ERR_FAIL_COND(p_texture.is_null());
  624. p_texture->draw_rect_region(canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_normal_map, p_clip_uv);
  625. }
  626. void CanvasItem::draw_style_box(const Ref<StyleBox> &p_style_box, const Rect2 &p_rect) {
  627. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  628. ERR_FAIL_COND(p_style_box.is_null());
  629. p_style_box->draw(canvas_item, p_rect);
  630. }
  631. 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) {
  632. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  633. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  634. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  635. VisualServer::get_singleton()->canvas_item_add_primitive(canvas_item, p_points, p_colors, p_uvs, rid, p_width, rid_normal);
  636. }
  637. void CanvasItem::draw_set_transform(const Point2 &p_offset, float p_rot, const Size2 &p_scale) {
  638. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  639. Transform2D xform(p_rot, p_offset);
  640. xform.scale_basis(p_scale);
  641. VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, xform);
  642. }
  643. void CanvasItem::draw_set_transform_matrix(const Transform2D &p_matrix) {
  644. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  645. VisualServer::get_singleton()->canvas_item_add_set_transform(canvas_item, p_matrix);
  646. }
  647. 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) {
  648. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  649. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  650. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  651. VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, p_colors, p_uvs, rid, rid_normal, p_antialiased);
  652. }
  653. 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) {
  654. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  655. Vector<Color> colors;
  656. colors.push_back(p_color);
  657. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  658. RID rid_normal = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  659. VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid, rid_normal, p_antialiased);
  660. }
  661. void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, const Transform2D &p_transform, const Color &p_modulate) {
  662. ERR_FAIL_COND(p_mesh.is_null());
  663. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  664. RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  665. VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), p_transform, p_modulate, texture_rid, normal_map_rid);
  666. }
  667. void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {
  668. ERR_FAIL_COND(p_multimesh.is_null());
  669. RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  670. RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  671. VisualServer::get_singleton()->canvas_item_add_multimesh(canvas_item, p_multimesh->get_rid(), texture_rid, normal_map_rid);
  672. }
  673. 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) {
  674. ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  675. ERR_FAIL_COND(p_font.is_null());
  676. p_font->draw(canvas_item, p_pos, p_text, p_modulate, p_clip_w);
  677. }
  678. 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) {
  679. ERR_FAIL_COND_V_MSG(!drawing, 0, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal.");
  680. ERR_FAIL_COND_V(p_char.length() != 1, 0);
  681. ERR_FAIL_COND_V(p_font.is_null(), 0);
  682. if (p_font->has_outline()) {
  683. p_font->draw_char(canvas_item, p_pos, p_char[0], p_next.c_str()[0], Color(1, 1, 1), true);
  684. }
  685. return p_font->draw_char(canvas_item, p_pos, p_char[0], p_next.c_str()[0], p_modulate);
  686. }
  687. void CanvasItem::_notify_transform(CanvasItem *p_node) {
  688. /* This check exists to avoid re-propagating the transform
  689. * notification down the tree on dirty nodes. It provides
  690. * optimization by avoiding redundancy (nodes are dirty, will get the
  691. * notification anyway).
  692. */
  693. if (/*p_node->xform_change.in_list() &&*/ p_node->global_invalid) {
  694. return; //nothing to do
  695. }
  696. p_node->global_invalid = true;
  697. if (p_node->notify_transform && !p_node->xform_change.in_list()) {
  698. if (!p_node->block_transform_notify) {
  699. if (p_node->is_inside_tree())
  700. get_tree()->xform_change_list.add(&p_node->xform_change);
  701. }
  702. }
  703. for (List<CanvasItem *>::Element *E = p_node->children_items.front(); E; E = E->next()) {
  704. CanvasItem *ci = E->get();
  705. if (ci->toplevel)
  706. continue;
  707. _notify_transform(ci);
  708. }
  709. }
  710. Rect2 CanvasItem::get_viewport_rect() const {
  711. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  712. return get_viewport()->get_visible_rect();
  713. }
  714. RID CanvasItem::get_canvas() const {
  715. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  716. if (canvas_layer)
  717. return canvas_layer->get_canvas();
  718. else
  719. return get_viewport()->find_world_2d()->get_canvas();
  720. }
  721. ObjectID CanvasItem::get_canvas_layer_instance_id() const {
  722. if (canvas_layer) {
  723. return canvas_layer->get_instance_id();
  724. } else {
  725. return 0;
  726. }
  727. }
  728. CanvasItem *CanvasItem::get_toplevel() const {
  729. CanvasItem *ci = const_cast<CanvasItem *>(this);
  730. while (!ci->toplevel && Object::cast_to<CanvasItem>(ci->get_parent())) {
  731. ci = Object::cast_to<CanvasItem>(ci->get_parent());
  732. }
  733. return ci;
  734. }
  735. Ref<World2D> CanvasItem::get_world_2d() const {
  736. ERR_FAIL_COND_V(!is_inside_tree(), Ref<World2D>());
  737. CanvasItem *tl = get_toplevel();
  738. if (tl->get_viewport()) {
  739. return tl->get_viewport()->find_world_2d();
  740. } else {
  741. return Ref<World2D>();
  742. }
  743. }
  744. RID CanvasItem::get_viewport_rid() const {
  745. ERR_FAIL_COND_V(!is_inside_tree(), RID());
  746. return get_viewport()->get_viewport_rid();
  747. }
  748. void CanvasItem::set_block_transform_notify(bool p_enable) {
  749. block_transform_notify = p_enable;
  750. }
  751. bool CanvasItem::is_block_transform_notify_enabled() const {
  752. return block_transform_notify;
  753. }
  754. void CanvasItem::set_draw_behind_parent(bool p_enable) {
  755. if (behind == p_enable)
  756. return;
  757. behind = p_enable;
  758. VisualServer::get_singleton()->canvas_item_set_draw_behind_parent(canvas_item, behind);
  759. }
  760. bool CanvasItem::is_draw_behind_parent_enabled() const {
  761. return behind;
  762. }
  763. void CanvasItem::set_material(const Ref<Material> &p_material) {
  764. material = p_material;
  765. RID rid;
  766. if (material.is_valid())
  767. rid = material->get_rid();
  768. VS::get_singleton()->canvas_item_set_material(canvas_item, rid);
  769. _change_notify(); //properties for material exposed
  770. }
  771. void CanvasItem::set_use_parent_material(bool p_use_parent_material) {
  772. use_parent_material = p_use_parent_material;
  773. VS::get_singleton()->canvas_item_set_use_parent_material(canvas_item, p_use_parent_material);
  774. }
  775. bool CanvasItem::get_use_parent_material() const {
  776. return use_parent_material;
  777. }
  778. Ref<Material> CanvasItem::get_material() const {
  779. return material;
  780. }
  781. Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) const {
  782. ERR_FAIL_COND_V(!is_inside_tree(), screen_point);
  783. Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
  784. return local_matrix.xform(screen_point);
  785. }
  786. Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
  787. ERR_FAIL_COND_V(p_event.is_null(), p_event);
  788. ERR_FAIL_COND_V(!is_inside_tree(), p_event);
  789. return p_event->xformed_by((get_canvas_transform() * get_global_transform()).affine_inverse());
  790. }
  791. Vector2 CanvasItem::get_global_mouse_position() const {
  792. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  793. return get_canvas_transform().affine_inverse().xform(get_viewport()->get_mouse_position());
  794. }
  795. Vector2 CanvasItem::get_local_mouse_position() const {
  796. ERR_FAIL_COND_V(!get_viewport(), Vector2());
  797. return get_global_transform().affine_inverse().xform(get_global_mouse_position());
  798. }
  799. void CanvasItem::force_update_transform() {
  800. ERR_FAIL_COND(!is_inside_tree());
  801. if (!xform_change.in_list()) {
  802. return;
  803. }
  804. get_tree()->xform_change_list.remove(&xform_change);
  805. notification(NOTIFICATION_TRANSFORM_CHANGED);
  806. }
  807. void CanvasItem::_bind_methods() {
  808. ClassDB::bind_method(D_METHOD("_toplevel_raise_self"), &CanvasItem::_toplevel_raise_self);
  809. ClassDB::bind_method(D_METHOD("_update_callback"), &CanvasItem::_update_callback);
  810. #ifdef TOOLS_ENABLED
  811. ClassDB::bind_method(D_METHOD("_edit_set_state", "state"), &CanvasItem::_edit_set_state);
  812. ClassDB::bind_method(D_METHOD("_edit_get_state"), &CanvasItem::_edit_get_state);
  813. ClassDB::bind_method(D_METHOD("_edit_set_position", "position"), &CanvasItem::_edit_set_position);
  814. ClassDB::bind_method(D_METHOD("_edit_get_position"), &CanvasItem::_edit_get_position);
  815. ClassDB::bind_method(D_METHOD("_edit_set_scale", "scale"), &CanvasItem::_edit_set_scale);
  816. ClassDB::bind_method(D_METHOD("_edit_get_scale"), &CanvasItem::_edit_get_scale);
  817. ClassDB::bind_method(D_METHOD("_edit_set_rect", "rect"), &CanvasItem::_edit_set_rect);
  818. ClassDB::bind_method(D_METHOD("_edit_get_rect"), &CanvasItem::_edit_get_rect);
  819. ClassDB::bind_method(D_METHOD("_edit_use_rect"), &CanvasItem::_edit_use_rect);
  820. ClassDB::bind_method(D_METHOD("_edit_set_rotation", "degrees"), &CanvasItem::_edit_set_rotation);
  821. ClassDB::bind_method(D_METHOD("_edit_get_rotation"), &CanvasItem::_edit_get_rotation);
  822. ClassDB::bind_method(D_METHOD("_edit_use_rotation"), &CanvasItem::_edit_use_rotation);
  823. ClassDB::bind_method(D_METHOD("_edit_set_pivot", "pivot"), &CanvasItem::_edit_set_pivot);
  824. ClassDB::bind_method(D_METHOD("_edit_get_pivot"), &CanvasItem::_edit_get_pivot);
  825. ClassDB::bind_method(D_METHOD("_edit_use_pivot"), &CanvasItem::_edit_use_pivot);
  826. ClassDB::bind_method(D_METHOD("_edit_get_transform"), &CanvasItem::_edit_get_transform);
  827. #endif
  828. ClassDB::bind_method(D_METHOD("get_canvas_item"), &CanvasItem::get_canvas_item);
  829. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &CanvasItem::set_visible);
  830. ClassDB::bind_method(D_METHOD("is_visible"), &CanvasItem::is_visible);
  831. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &CanvasItem::is_visible_in_tree);
  832. ClassDB::bind_method(D_METHOD("show"), &CanvasItem::show);
  833. ClassDB::bind_method(D_METHOD("hide"), &CanvasItem::hide);
  834. ClassDB::bind_method(D_METHOD("update"), &CanvasItem::update);
  835. ClassDB::bind_method(D_METHOD("set_as_toplevel", "enable"), &CanvasItem::set_as_toplevel);
  836. ClassDB::bind_method(D_METHOD("is_set_as_toplevel"), &CanvasItem::is_set_as_toplevel);
  837. ClassDB::bind_method(D_METHOD("set_light_mask", "light_mask"), &CanvasItem::set_light_mask);
  838. ClassDB::bind_method(D_METHOD("get_light_mask"), &CanvasItem::get_light_mask);
  839. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &CanvasItem::set_modulate);
  840. ClassDB::bind_method(D_METHOD("get_modulate"), &CanvasItem::get_modulate);
  841. ClassDB::bind_method(D_METHOD("set_self_modulate", "self_modulate"), &CanvasItem::set_self_modulate);
  842. ClassDB::bind_method(D_METHOD("get_self_modulate"), &CanvasItem::get_self_modulate);
  843. ClassDB::bind_method(D_METHOD("set_draw_behind_parent", "enable"), &CanvasItem::set_draw_behind_parent);
  844. ClassDB::bind_method(D_METHOD("is_draw_behind_parent_enabled"), &CanvasItem::is_draw_behind_parent_enabled);
  845. ClassDB::bind_method(D_METHOD("_set_on_top", "on_top"), &CanvasItem::_set_on_top);
  846. ClassDB::bind_method(D_METHOD("_is_on_top"), &CanvasItem::_is_on_top);
  847. //ClassDB::bind_method(D_METHOD("get_transform"),&CanvasItem::get_transform);
  848. ClassDB::bind_method(D_METHOD("draw_line", "from", "to", "color", "width", "antialiased"), &CanvasItem::draw_line, DEFVAL(1.0), DEFVAL(false));
  849. ClassDB::bind_method(D_METHOD("draw_polyline", "points", "color", "width", "antialiased"), &CanvasItem::draw_polyline, DEFVAL(1.0), DEFVAL(false));
  850. ClassDB::bind_method(D_METHOD("draw_polyline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_polyline_colors, DEFVAL(1.0), DEFVAL(false));
  851. ClassDB::bind_method(D_METHOD("draw_arc", "center", "radius", "start_angle", "end_angle", "point_count", "color", "width", "antialiased"), &CanvasItem::draw_arc, DEFVAL(1.0), DEFVAL(false));
  852. ClassDB::bind_method(D_METHOD("draw_multiline", "points", "color", "width", "antialiased"), &CanvasItem::draw_multiline, DEFVAL(1.0), DEFVAL(false));
  853. ClassDB::bind_method(D_METHOD("draw_multiline_colors", "points", "colors", "width", "antialiased"), &CanvasItem::draw_multiline_colors, DEFVAL(1.0), DEFVAL(false));
  854. ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled", "width", "antialiased"), &CanvasItem::draw_rect, DEFVAL(true), DEFVAL(1.0), DEFVAL(false));
  855. ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color"), &CanvasItem::draw_circle);
  856. ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate", "normal_map"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(Variant()));
  857. 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()));
  858. 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));
  859. ClassDB::bind_method(D_METHOD("draw_style_box", "style_box", "rect"), &CanvasItem::draw_style_box);
  860. ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width", "normal_map"), &CanvasItem::draw_primitive, DEFVAL(Variant()), DEFVAL(1.0), DEFVAL(Variant()));
  861. 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));
  862. 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));
  863. ClassDB::bind_method(D_METHOD("draw_string", "font", "position", "text", "modulate", "clip_w"), &CanvasItem::draw_string, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
  864. ClassDB::bind_method(D_METHOD("draw_char", "font", "position", "char", "next", "modulate"), &CanvasItem::draw_char, DEFVAL(Color(1, 1, 1)));
  865. ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map", "transform", "modulate"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()), DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1)));
  866. ClassDB::bind_method(D_METHOD("draw_multimesh", "multimesh", "texture", "normal_map"), &CanvasItem::draw_multimesh, DEFVAL(Ref<Texture>()));
  867. ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform);
  868. ClassDB::bind_method(D_METHOD("draw_set_transform_matrix", "xform"), &CanvasItem::draw_set_transform_matrix);
  869. ClassDB::bind_method(D_METHOD("get_transform"), &CanvasItem::get_transform);
  870. ClassDB::bind_method(D_METHOD("get_global_transform"), &CanvasItem::get_global_transform);
  871. ClassDB::bind_method(D_METHOD("get_global_transform_with_canvas"), &CanvasItem::get_global_transform_with_canvas);
  872. ClassDB::bind_method(D_METHOD("get_viewport_transform"), &CanvasItem::get_viewport_transform);
  873. ClassDB::bind_method(D_METHOD("get_viewport_rect"), &CanvasItem::get_viewport_rect);
  874. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &CanvasItem::get_canvas_transform);
  875. ClassDB::bind_method(D_METHOD("get_local_mouse_position"), &CanvasItem::get_local_mouse_position);
  876. ClassDB::bind_method(D_METHOD("get_global_mouse_position"), &CanvasItem::get_global_mouse_position);
  877. ClassDB::bind_method(D_METHOD("get_canvas"), &CanvasItem::get_canvas);
  878. ClassDB::bind_method(D_METHOD("get_world_2d"), &CanvasItem::get_world_2d);
  879. //ClassDB::bind_method(D_METHOD("get_viewport"),&CanvasItem::get_viewport);
  880. ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
  881. ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);
  882. ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
  883. ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);
  884. ClassDB::bind_method(D_METHOD("set_notify_local_transform", "enable"), &CanvasItem::set_notify_local_transform);
  885. ClassDB::bind_method(D_METHOD("is_local_transform_notification_enabled"), &CanvasItem::is_local_transform_notification_enabled);
  886. ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &CanvasItem::set_notify_transform);
  887. ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &CanvasItem::is_transform_notification_enabled);
  888. ClassDB::bind_method(D_METHOD("force_update_transform"), &CanvasItem::force_update_transform);
  889. ClassDB::bind_method(D_METHOD("make_canvas_position_local", "screen_point"), &CanvasItem::make_canvas_position_local);
  890. ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);
  891. BIND_VMETHOD(MethodInfo("_draw"));
  892. ADD_GROUP("Visibility", "");
  893. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  894. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  895. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "self_modulate"), "set_self_modulate", "get_self_modulate");
  896. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_behind_parent"), "set_draw_behind_parent", "is_draw_behind_parent_enabled");
  897. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_on_top", PROPERTY_HINT_NONE, "", 0), "_set_on_top", "_is_on_top"); //compatibility
  898. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_light_mask", "get_light_mask");
  899. ADD_GROUP("Material", "");
  900. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial"), "set_material", "get_material");
  901. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_parent_material"), "set_use_parent_material", "get_use_parent_material");
  902. //exporting these things doesn't really make much sense i think
  903. // ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toplevel", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_as_toplevel", "is_set_as_toplevel");
  904. // ADD_PROPERTY(PropertyInfo(Variant::BOOL,"transform/notify"),"set_transform_notify","is_transform_notify_enabled");
  905. ADD_SIGNAL(MethodInfo("draw"));
  906. ADD_SIGNAL(MethodInfo("visibility_changed"));
  907. ADD_SIGNAL(MethodInfo("hide"));
  908. ADD_SIGNAL(MethodInfo("item_rect_changed"));
  909. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  910. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  911. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  912. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  913. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  914. BIND_ENUM_CONSTANT(BLEND_MODE_DISABLED);
  915. BIND_CONSTANT(NOTIFICATION_TRANSFORM_CHANGED);
  916. BIND_CONSTANT(NOTIFICATION_DRAW);
  917. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  918. BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
  919. BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
  920. }
  921. Transform2D CanvasItem::get_canvas_transform() const {
  922. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  923. if (canvas_layer)
  924. return canvas_layer->get_transform();
  925. else if (Object::cast_to<CanvasItem>(get_parent()))
  926. return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
  927. else
  928. return get_viewport()->get_canvas_transform();
  929. }
  930. Transform2D CanvasItem::get_viewport_transform() const {
  931. ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());
  932. if (canvas_layer) {
  933. if (get_viewport()) {
  934. return get_viewport()->get_final_transform() * canvas_layer->get_transform();
  935. } else {
  936. return canvas_layer->get_transform();
  937. }
  938. } else {
  939. return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
  940. }
  941. }
  942. void CanvasItem::set_notify_local_transform(bool p_enable) {
  943. notify_local_transform = p_enable;
  944. }
  945. bool CanvasItem::is_local_transform_notification_enabled() const {
  946. return notify_local_transform;
  947. }
  948. void CanvasItem::set_notify_transform(bool p_enable) {
  949. if (notify_transform == p_enable)
  950. return;
  951. notify_transform = p_enable;
  952. if (notify_transform && is_inside_tree()) {
  953. //this ensures that invalid globals get resolved, so notifications can be received
  954. get_global_transform();
  955. }
  956. }
  957. bool CanvasItem::is_transform_notification_enabled() const {
  958. return notify_transform;
  959. }
  960. int CanvasItem::get_canvas_layer() const {
  961. if (canvas_layer)
  962. return canvas_layer->get_layer();
  963. else
  964. return 0;
  965. }
  966. CanvasItem::CanvasItem() :
  967. xform_change(this) {
  968. canvas_item = VisualServer::get_singleton()->canvas_item_create();
  969. visible = true;
  970. pending_update = false;
  971. modulate = Color(1, 1, 1, 1);
  972. self_modulate = Color(1, 1, 1, 1);
  973. toplevel = false;
  974. first_draw = false;
  975. drawing = false;
  976. behind = false;
  977. block_transform_notify = false;
  978. //viewport=NULL;
  979. canvas_layer = NULL;
  980. use_parent_material = false;
  981. global_invalid = true;
  982. notify_local_transform = false;
  983. notify_transform = false;
  984. light_mask = 1;
  985. C = NULL;
  986. }
  987. CanvasItem::~CanvasItem() {
  988. VisualServer::get_singleton()->free(canvas_item);
  989. }