canvas_item.cpp 45 KB

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