canvas_item.cpp 45 KB

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