canvas_item.cpp 48 KB

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