tile_atlas_view.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /**************************************************************************/
  2. /* tile_atlas_view.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tile_atlas_view.h"
  31. #include "editor/editor_settings.h"
  32. #include "editor/themes/editor_scale.h"
  33. #include "scene/2d/tile_map_layer.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/panel.h"
  37. #include "scene/gui/view_panner.h"
  38. void TileAtlasView::gui_input(const Ref<InputEvent> &p_event) {
  39. if (panner->gui_input(p_event, get_global_rect())) {
  40. accept_event();
  41. }
  42. }
  43. void TileAtlasView::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  44. panning += p_scroll_vec;
  45. _update_zoom_and_panning(true);
  46. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  47. }
  48. void TileAtlasView::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  49. zoom_widget->set_zoom(zoom_widget->get_zoom() * p_zoom_factor);
  50. _update_zoom_and_panning(true, p_origin);
  51. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  52. }
  53. Size2i TileAtlasView::_compute_base_tiles_control_size() {
  54. if (tile_set_atlas_source.is_null()) {
  55. return Size2i();
  56. }
  57. // Update the texture.
  58. Vector2i size;
  59. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  60. if (texture.is_valid()) {
  61. size = texture->get_size();
  62. }
  63. return size;
  64. }
  65. Size2i TileAtlasView::_compute_alternative_tiles_control_size() {
  66. if (tile_set_atlas_source.is_null()) {
  67. return Size2i();
  68. }
  69. Vector2i size;
  70. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  71. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  72. int alternatives_count = tile_set_atlas_source->get_alternative_tiles_count(tile_id);
  73. Vector2i line_size;
  74. Size2i texture_region_size = tile_set_atlas_source->get_tile_texture_region(tile_id).size;
  75. for (int j = 1; j < alternatives_count; j++) {
  76. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(tile_id, j);
  77. bool transposed = tile_set_atlas_source->get_tile_data(tile_id, alternative_id)->get_transpose();
  78. line_size.x += transposed ? texture_region_size.y : texture_region_size.x;
  79. line_size.y = MAX(line_size.y, transposed ? texture_region_size.x : texture_region_size.y);
  80. }
  81. size.x = MAX(size.x, line_size.x);
  82. size.y += line_size.y;
  83. }
  84. return size;
  85. }
  86. void TileAtlasView::_update_zoom_and_panning(bool p_zoom_on_mouse_pos, const Vector2 &p_mouse_pos) {
  87. if (tile_set_atlas_source.is_null()) {
  88. return;
  89. }
  90. float zoom = zoom_widget->get_zoom();
  91. // Compute the minimum sizes.
  92. Size2i base_tiles_control_size = _compute_base_tiles_control_size();
  93. base_tiles_root_control->set_custom_minimum_size(Vector2(base_tiles_control_size) * zoom);
  94. Size2i alternative_tiles_control_size = _compute_alternative_tiles_control_size();
  95. alternative_tiles_root_control->set_custom_minimum_size(Vector2(alternative_tiles_control_size) * zoom);
  96. // Set the texture for the base tiles.
  97. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  98. // Set the scales.
  99. if (base_tiles_control_size.x > 0 && base_tiles_control_size.y > 0) {
  100. base_tiles_drawing_root->set_scale(Vector2(zoom, zoom));
  101. } else {
  102. base_tiles_drawing_root->set_scale(Vector2(1, 1));
  103. }
  104. if (alternative_tiles_control_size.x > 0 && alternative_tiles_control_size.y > 0) {
  105. alternative_tiles_drawing_root->set_scale(Vector2(zoom, zoom));
  106. } else {
  107. alternative_tiles_drawing_root->set_scale(Vector2(1, 1));
  108. }
  109. // Update the margin container's margins.
  110. const char *constants[] = { "margin_left", "margin_top", "margin_right", "margin_bottom" };
  111. for (int i = 0; i < 4; i++) {
  112. margin_container->add_theme_constant_override(constants[i], margin_container_paddings[i] * zoom);
  113. }
  114. // Update the backgrounds.
  115. background_left->set_size(base_tiles_root_control->get_custom_minimum_size());
  116. background_right->set_size(alternative_tiles_root_control->get_custom_minimum_size());
  117. // Zoom on the position.
  118. if (p_zoom_on_mouse_pos) {
  119. Vector2 relative_mpos = p_mouse_pos - get_size() / 2;
  120. panning = (panning - relative_mpos) * zoom / previous_zoom + relative_mpos;
  121. } else {
  122. // Center of panel.
  123. panning = panning * zoom / previous_zoom;
  124. }
  125. button_center_view->set_disabled(panning.is_zero_approx());
  126. previous_zoom = zoom;
  127. center_container->set_begin(panning - center_container->get_minimum_size() / 2);
  128. center_container->set_size(center_container->get_minimum_size());
  129. }
  130. void TileAtlasView::_zoom_widget_changed() {
  131. _update_zoom_and_panning();
  132. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  133. }
  134. void TileAtlasView::_center_view() {
  135. panning = Vector2();
  136. button_center_view->set_disabled(true);
  137. _update_zoom_and_panning();
  138. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  139. }
  140. void TileAtlasView::_base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event) {
  141. if (tile_set_atlas_source.is_null()) {
  142. return;
  143. }
  144. base_tiles_root_control->set_tooltip_text("");
  145. Ref<InputEventMouseMotion> mm = p_event;
  146. if (mm.is_valid()) {
  147. Transform2D xform = base_tiles_drawing_root->get_transform().affine_inverse();
  148. Vector2i coords = get_atlas_tile_coords_at_pos(xform.xform(mm->get_position()));
  149. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  150. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  151. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  152. base_tiles_root_control->set_tooltip_text(vformat(TTR("Source: %d\nAtlas coordinates: %s\nAlternative: 0"), source_id, coords));
  153. }
  154. }
  155. }
  156. }
  157. void TileAtlasView::_draw_base_tiles() {
  158. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  159. return;
  160. }
  161. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  162. if (texture.is_valid()) {
  163. Vector2i margins = tile_set_atlas_source->get_margins();
  164. Vector2i separation = tile_set_atlas_source->get_separation();
  165. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  166. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  167. // Draw the texture where there is no tile.
  168. for (int x = 0; x < grid_size.x; x++) {
  169. for (int y = 0; y < grid_size.y; y++) {
  170. Vector2i coords = Vector2i(x, y);
  171. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  172. Rect2i rect = Rect2i((texture_region_size + separation) * coords + margins, texture_region_size + separation);
  173. rect = rect.intersection(Rect2i(Vector2(), texture->get_size()));
  174. if (rect.size.x > 0 && rect.size.y > 0) {
  175. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  176. }
  177. }
  178. }
  179. }
  180. // Draw dark overlay after for performance reasons.
  181. for (int x = 0; x < grid_size.x; x++) {
  182. for (int y = 0; y < grid_size.y; y++) {
  183. Vector2i coords = Vector2i(x, y);
  184. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  185. Rect2i rect = Rect2i((texture_region_size + separation) * coords + margins, texture_region_size + separation);
  186. rect = rect.intersection(Rect2i(Vector2(), texture->get_size()));
  187. if (rect.size.x > 0 && rect.size.y > 0) {
  188. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  189. }
  190. }
  191. }
  192. }
  193. // Draw the texture around the grid.
  194. Rect2i rect;
  195. // Top.
  196. rect.position = Vector2i();
  197. rect.set_end(Vector2i(texture->get_size().x, margins.y));
  198. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  199. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  200. // Bottom
  201. int bottom_border = margins.y + (grid_size.y * (texture_region_size.y + separation.y));
  202. if (bottom_border < texture->get_size().y) {
  203. rect.position = Vector2i(0, bottom_border);
  204. rect.set_end(texture->get_size());
  205. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  206. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  207. }
  208. // Left
  209. rect.position = Vector2i(0, margins.y);
  210. rect.set_end(Vector2i(margins.x, margins.y + (grid_size.y * (texture_region_size.y + separation.y))));
  211. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  212. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  213. // Right.
  214. int right_border = margins.x + (grid_size.x * (texture_region_size.x + separation.x));
  215. if (right_border < texture->get_size().x) {
  216. rect.position = Vector2i(right_border, margins.y);
  217. rect.set_end(Vector2i(texture->get_size().x, margins.y + (grid_size.y * (texture_region_size.y + separation.y))));
  218. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  219. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  220. }
  221. // Draw actual tiles, using their properties (modulation, etc...)
  222. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  223. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  224. // Different materials need to be drawn with different CanvasItems.
  225. RID ci_rid = _get_canvas_item_to_draw(tile_set_atlas_source->get_tile_data(atlas_coords, 0), base_tiles_draw, material_tiles_draw);
  226. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(atlas_coords); frame++) {
  227. // Update the y to max value.
  228. Rect2i base_frame_rect = tile_set_atlas_source->get_tile_texture_region(atlas_coords, frame);
  229. Vector2 offset_pos = Rect2(base_frame_rect).get_center() + Vector2(tile_set_atlas_source->get_tile_data(atlas_coords, 0)->get_texture_origin());
  230. // Draw the tile.
  231. TileMapLayer::draw_tile(ci_rid, offset_pos, tile_set, source_id, atlas_coords, 0, frame);
  232. }
  233. }
  234. // Draw Dark overlay on separation in its own pass.
  235. if (separation.x > 0 || separation.y > 0) {
  236. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  237. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  238. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(atlas_coords); frame++) {
  239. // Update the y to max value.
  240. Rect2i base_frame_rect = tile_set_atlas_source->get_tile_texture_region(atlas_coords, frame);
  241. if (separation.x > 0) {
  242. Rect2i right_sep_rect = Rect2i(base_frame_rect.get_position() + Vector2i(base_frame_rect.size.x, 0), Vector2i(separation.x, base_frame_rect.size.y));
  243. right_sep_rect = right_sep_rect.intersection(Rect2i(Vector2(), texture->get_size()));
  244. if (right_sep_rect.size.x > 0 && right_sep_rect.size.y > 0) {
  245. //base_tiles_draw->draw_texture_rect_region(texture, right_sep_rect, right_sep_rect);
  246. base_tiles_draw->draw_rect(right_sep_rect, Color(0.0, 0.0, 0.0, 0.5));
  247. }
  248. }
  249. if (separation.y > 0) {
  250. Rect2i bottom_sep_rect = Rect2i(base_frame_rect.get_position() + Vector2i(0, base_frame_rect.size.y), Vector2i(base_frame_rect.size.x + separation.x, separation.y));
  251. bottom_sep_rect = bottom_sep_rect.intersection(Rect2i(Vector2(), texture->get_size()));
  252. if (bottom_sep_rect.size.x > 0 && bottom_sep_rect.size.y > 0) {
  253. //base_tiles_draw->draw_texture_rect_region(texture, bottom_sep_rect, bottom_sep_rect);
  254. base_tiles_draw->draw_rect(bottom_sep_rect, Color(0.0, 0.0, 0.0, 0.5));
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. RID TileAtlasView::_get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map) {
  263. Ref<Material> mat = p_for_data->get_material();
  264. if (mat.is_null()) {
  265. return p_base_item->get_canvas_item();
  266. } else if (p_material_map.has(mat)) {
  267. return p_material_map[mat];
  268. } else {
  269. RID ci_rid = RS::get_singleton()->canvas_item_create();
  270. RS::get_singleton()->canvas_item_set_parent(ci_rid, p_base_item->get_canvas_item());
  271. RS::get_singleton()->canvas_item_set_material(ci_rid, mat->get_rid());
  272. RS::get_singleton()->canvas_item_set_default_texture_filter(ci_rid, RS::CanvasItemTextureFilter(p_base_item->get_texture_filter_in_tree()));
  273. p_material_map[mat] = ci_rid;
  274. return ci_rid;
  275. }
  276. }
  277. void TileAtlasView::_clear_material_canvas_items() {
  278. for (KeyValue<Ref<Material>, RID> kv : material_tiles_draw) {
  279. RS::get_singleton()->free(kv.value);
  280. }
  281. material_tiles_draw.clear();
  282. for (KeyValue<Ref<Material>, RID> kv : material_alternatives_draw) {
  283. RS::get_singleton()->free(kv.value);
  284. }
  285. material_alternatives_draw.clear();
  286. }
  287. void TileAtlasView::_draw_base_tiles_texture_grid() {
  288. if (tile_set_atlas_source.is_null()) {
  289. return;
  290. }
  291. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  292. if (texture.is_valid()) {
  293. Vector2i margins = tile_set_atlas_source->get_margins();
  294. Vector2i separation = tile_set_atlas_source->get_separation();
  295. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  296. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  297. // Draw each tile texture region.
  298. for (int x = 0; x < grid_size.x; x++) {
  299. for (int y = 0; y < grid_size.y; y++) {
  300. Vector2i origin = margins + (Vector2i(x, y) * (texture_region_size + separation));
  301. Vector2i base_tile_coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  302. if (base_tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  303. if (base_tile_coords == Vector2i(x, y)) {
  304. // Draw existing tile.
  305. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(base_tile_coords);
  306. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  307. base_tiles_texture_grid->draw_rect(Rect2i(origin, region_size), Color(1.0, 1.0, 1.0, 0.8), false);
  308. }
  309. } else {
  310. // Draw the grid.
  311. base_tiles_texture_grid->draw_rect(Rect2i(origin, texture_region_size), Color(0.7, 0.7, 0.7, 0.1), false);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. void TileAtlasView::_draw_base_tiles_shape_grid() {
  318. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  319. return;
  320. }
  321. // Draw the shapes.
  322. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  323. Vector2i tile_shape_size = tile_set->get_tile_size();
  324. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  325. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  326. Vector2 in_tile_base_offset = tile_set_atlas_source->get_tile_data(tile_id, 0)->get_texture_origin();
  327. if (tile_set_atlas_source->is_rect_in_tile_texture_region(tile_id, 0, Rect2(Vector2(-tile_shape_size) / 2, tile_shape_size))) {
  328. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(tile_id); frame++) {
  329. Color color = grid_color;
  330. if (frame > 0) {
  331. color.a *= 0.3;
  332. }
  333. Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(tile_id, frame);
  334. Transform2D tile_xform;
  335. tile_xform.set_origin(Rect2(texture_region).get_center() + in_tile_base_offset);
  336. tile_xform.set_scale(tile_shape_size);
  337. tile_set->draw_tile_shape(base_tiles_shape_grid, tile_xform, color);
  338. }
  339. }
  340. }
  341. }
  342. void TileAtlasView::_alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event) {
  343. alternative_tiles_root_control->set_tooltip_text("");
  344. Ref<InputEventMouseMotion> mm = p_event;
  345. if (mm.is_valid()) {
  346. Transform2D xform = alternative_tiles_drawing_root->get_transform().affine_inverse();
  347. Vector3i coords3 = get_alternative_tile_at_pos(xform.xform(mm->get_position()));
  348. Vector2i coords = Vector2i(coords3.x, coords3.y);
  349. int alternative_id = coords3.z;
  350. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  351. alternative_tiles_root_control->set_tooltip_text(vformat(TTR("Source: %d\nAtlas coordinates: %s\nAlternative: %d"), source_id, coords, alternative_id));
  352. }
  353. }
  354. }
  355. void TileAtlasView::_draw_alternatives() {
  356. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  357. return;
  358. }
  359. // Draw the alternative tiles.
  360. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  361. if (texture.is_valid()) {
  362. Vector2 current_pos;
  363. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  364. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  365. current_pos.x = 0;
  366. int y_increment = 0;
  367. Size2i texture_region_size = tile_set_atlas_source->get_tile_texture_region(atlas_coords).size;
  368. int alternatives_count = tile_set_atlas_source->get_alternative_tiles_count(atlas_coords);
  369. for (int j = 1; j < alternatives_count; j++) {
  370. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(atlas_coords, j);
  371. TileData *tile_data = tile_set_atlas_source->get_tile_data(atlas_coords, alternative_id);
  372. bool transposed = tile_data->get_transpose();
  373. // Different materials need to be drawn with different CanvasItems.
  374. RID ci_rid = _get_canvas_item_to_draw(tile_data, alternatives_draw, material_alternatives_draw);
  375. // Update the y to max value.
  376. Vector2i offset_pos;
  377. if (transposed) {
  378. offset_pos = (current_pos + Vector2(texture_region_size.y, texture_region_size.x) / 2 + tile_data->get_texture_origin());
  379. y_increment = MAX(y_increment, texture_region_size.x);
  380. } else {
  381. offset_pos = (current_pos + texture_region_size / 2 + tile_data->get_texture_origin());
  382. y_increment = MAX(y_increment, texture_region_size.y);
  383. }
  384. // Draw the tile.
  385. TileMapLayer::draw_tile(ci_rid, offset_pos, tile_set, source_id, atlas_coords, alternative_id);
  386. // Increment the x position.
  387. current_pos.x += transposed ? texture_region_size.y : texture_region_size.x;
  388. }
  389. if (alternatives_count > 1) {
  390. current_pos.y += y_increment;
  391. }
  392. }
  393. }
  394. }
  395. void TileAtlasView::_draw_background_left() {
  396. background_left->draw_texture_rect(theme_cache.checkerboard, Rect2(Vector2(), background_left->get_size()), true);
  397. }
  398. void TileAtlasView::_draw_background_right() {
  399. background_right->draw_texture_rect(theme_cache.checkerboard, Rect2(Vector2(), background_right->get_size()), true);
  400. }
  401. void TileAtlasView::set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  402. tile_set = Ref<TileSet>(p_tile_set);
  403. tile_set_atlas_source = Ref<TileSetAtlasSource>(p_tile_set_atlas_source);
  404. _clear_material_canvas_items();
  405. if (tile_set.is_null()) {
  406. return;
  407. }
  408. ERR_FAIL_COND(p_source_id < 0);
  409. ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  410. source_id = p_source_id;
  411. // Show or hide the view.
  412. bool valid = tile_set_atlas_source->get_texture().is_valid();
  413. hbox->set_visible(valid);
  414. missing_source_label->set_visible(!valid);
  415. // Update the rect cache.
  416. _update_alternative_tiles_rect_cache();
  417. // Update everything.
  418. _update_zoom_and_panning();
  419. base_tiles_drawing_root->set_size(_compute_base_tiles_control_size());
  420. alternative_tiles_drawing_root->set_size(_compute_alternative_tiles_control_size());
  421. // Update.
  422. base_tiles_draw->queue_redraw();
  423. base_tiles_texture_grid->queue_redraw();
  424. base_tiles_shape_grid->queue_redraw();
  425. alternatives_draw->queue_redraw();
  426. background_left->queue_redraw();
  427. background_right->queue_redraw();
  428. }
  429. float TileAtlasView::get_zoom() const {
  430. return zoom_widget->get_zoom();
  431. }
  432. void TileAtlasView::set_transform(float p_zoom, Vector2i p_panning) {
  433. zoom_widget->set_zoom(p_zoom);
  434. panning = p_panning;
  435. _update_zoom_and_panning();
  436. }
  437. void TileAtlasView::set_padding(Side p_side, int p_padding) {
  438. ERR_FAIL_COND(p_padding < 0);
  439. margin_container_paddings[p_side] = p_padding;
  440. }
  441. Vector2i TileAtlasView::get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp) const {
  442. if (tile_set_atlas_source.is_null()) {
  443. return Vector2i();
  444. }
  445. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  446. if (texture.is_null()) {
  447. return TileSetSource::INVALID_ATLAS_COORDS;
  448. }
  449. Vector2i margins = tile_set_atlas_source->get_margins();
  450. Vector2i separation = tile_set_atlas_source->get_separation();
  451. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  452. // Compute index in atlas.
  453. Vector2 pos = p_pos - margins;
  454. Vector2i ret = (pos / (texture_region_size + separation)).floor();
  455. // Clamp.
  456. if (p_clamp) {
  457. Vector2i size = tile_set_atlas_source->get_atlas_grid_size();
  458. ret = ret.clamp(Vector2i(), size - Vector2i(1, 1));
  459. }
  460. return ret;
  461. }
  462. void TileAtlasView::_update_alternative_tiles_rect_cache() {
  463. if (tile_set_atlas_source.is_null()) {
  464. return;
  465. }
  466. alternative_tiles_rect_cache.clear();
  467. Rect2i current;
  468. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  469. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  470. int alternatives_count = tile_set_atlas_source->get_alternative_tiles_count(tile_id);
  471. Size2i texture_region_size = tile_set_atlas_source->get_tile_texture_region(tile_id).size;
  472. int line_height = 0;
  473. for (int j = 1; j < alternatives_count; j++) {
  474. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(tile_id, j);
  475. TileData *tile_data = tile_set_atlas_source->get_tile_data(tile_id, alternative_id);
  476. bool transposed = tile_data->get_transpose();
  477. current.size = transposed ? Vector2i(texture_region_size.y, texture_region_size.x) : texture_region_size;
  478. // Update the rect.
  479. if (!alternative_tiles_rect_cache.has(tile_id)) {
  480. alternative_tiles_rect_cache[tile_id] = HashMap<int, Rect2i>();
  481. }
  482. alternative_tiles_rect_cache[tile_id][alternative_id] = current;
  483. current.position.x += transposed ? texture_region_size.y : texture_region_size.x;
  484. line_height = MAX(line_height, transposed ? texture_region_size.x : texture_region_size.y);
  485. }
  486. current.position.x = 0;
  487. current.position.y += line_height;
  488. }
  489. }
  490. Vector3i TileAtlasView::get_alternative_tile_at_pos(const Vector2 p_pos) const {
  491. for (const KeyValue<Vector2, HashMap<int, Rect2i>> &E_coords : alternative_tiles_rect_cache) {
  492. for (const KeyValue<int, Rect2i> &E_alternative : E_coords.value) {
  493. if (E_alternative.value.has_point(p_pos)) {
  494. return Vector3i(E_coords.key.x, E_coords.key.y, E_alternative.key);
  495. }
  496. }
  497. }
  498. return Vector3i(TileSetSource::INVALID_ATLAS_COORDS.x, TileSetSource::INVALID_ATLAS_COORDS.y, TileSetSource::INVALID_TILE_ALTERNATIVE);
  499. }
  500. Rect2i TileAtlasView::get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile) {
  501. ERR_FAIL_COND_V_MSG(!alternative_tiles_rect_cache.has(p_coords), Rect2i(), vformat("No cached rect for tile coords:%s", p_coords));
  502. ERR_FAIL_COND_V_MSG(!alternative_tiles_rect_cache[p_coords].has(p_alternative_tile), Rect2i(), vformat("No cached rect for tile coords:%s alternative_id:%d", p_coords, p_alternative_tile));
  503. return alternative_tiles_rect_cache[p_coords][p_alternative_tile];
  504. }
  505. void TileAtlasView::queue_redraw() {
  506. base_tiles_draw->queue_redraw();
  507. base_tiles_texture_grid->queue_redraw();
  508. base_tiles_shape_grid->queue_redraw();
  509. alternatives_draw->queue_redraw();
  510. background_left->queue_redraw();
  511. background_right->queue_redraw();
  512. }
  513. void TileAtlasView::_update_theme_item_cache() {
  514. Control::_update_theme_item_cache();
  515. theme_cache.center_view_icon = get_editor_theme_icon(SNAME("CenterView"));
  516. theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  517. }
  518. void TileAtlasView::_notification(int p_what) {
  519. switch (p_what) {
  520. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  521. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  522. break;
  523. }
  524. [[fallthrough]];
  525. }
  526. case NOTIFICATION_ENTER_TREE: {
  527. panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
  528. panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
  529. } break;
  530. case NOTIFICATION_THEME_CHANGED: {
  531. button_center_view->set_button_icon(theme_cache.center_view_icon);
  532. } break;
  533. }
  534. }
  535. void TileAtlasView::_bind_methods() {
  536. ADD_SIGNAL(MethodInfo("transform_changed", PropertyInfo(Variant::FLOAT, "zoom"), PropertyInfo(Variant::VECTOR2, "scroll")));
  537. }
  538. TileAtlasView::TileAtlasView() {
  539. set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  540. Panel *panel = memnew(Panel);
  541. panel->set_clip_contents(true);
  542. panel->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  543. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  544. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  545. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  546. add_child(panel);
  547. zoom_widget = memnew(EditorZoomWidget);
  548. add_child(zoom_widget);
  549. zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
  550. zoom_widget->connect("zoom_changed", callable_mp(this, &TileAtlasView::_zoom_widget_changed).unbind(1));
  551. zoom_widget->set_shortcut_context(this);
  552. button_center_view = memnew(Button);
  553. button_center_view->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT, Control::PRESET_MODE_MINSIZE, 5);
  554. button_center_view->set_grow_direction_preset(Control::PRESET_TOP_RIGHT);
  555. button_center_view->connect(SceneStringName(pressed), callable_mp(this, &TileAtlasView::_center_view));
  556. button_center_view->set_flat(true);
  557. button_center_view->set_disabled(true);
  558. button_center_view->set_tooltip_text(TTR("Center View"));
  559. button_center_view->set_accessibility_name(TTRC("Center View"));
  560. add_child(button_center_view);
  561. panner.instantiate();
  562. panner->set_callbacks(callable_mp(this, &TileAtlasView::_pan_callback), callable_mp(this, &TileAtlasView::_zoom_callback));
  563. panner->set_enable_rmb(true);
  564. center_container = memnew(CenterContainer);
  565. center_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  566. center_container->set_anchors_preset(Control::PRESET_CENTER);
  567. center_container->connect(SceneStringName(gui_input), callable_mp(this, &TileAtlasView::gui_input));
  568. center_container->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  569. center_container->set_focus_mode(FOCUS_CLICK);
  570. panel->add_child(center_container);
  571. missing_source_label = memnew(Label);
  572. missing_source_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  573. missing_source_label->set_text(TTR("The selected atlas source has no valid texture. Assign a texture in the TileSet bottom tab."));
  574. center_container->add_child(missing_source_label);
  575. margin_container = memnew(MarginContainer);
  576. margin_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  577. center_container->add_child(margin_container);
  578. hbox = memnew(HBoxContainer);
  579. hbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  580. hbox->add_theme_constant_override("separation", 10);
  581. hbox->hide();
  582. margin_container->add_child(hbox);
  583. VBoxContainer *left_vbox = memnew(VBoxContainer);
  584. left_vbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  585. hbox->add_child(left_vbox);
  586. VBoxContainer *right_vbox = memnew(VBoxContainer);
  587. right_vbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  588. hbox->add_child(right_vbox);
  589. // Base tiles.
  590. Label *base_tile_label = memnew(Label);
  591. base_tile_label->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  592. base_tile_label->set_text(TTR("Base Tiles"));
  593. base_tile_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  594. left_vbox->add_child(base_tile_label);
  595. base_tiles_root_control = memnew(Control);
  596. base_tiles_root_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  597. base_tiles_root_control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  598. base_tiles_root_control->connect(SceneStringName(gui_input), callable_mp(this, &TileAtlasView::_base_tiles_root_control_gui_input));
  599. left_vbox->add_child(base_tiles_root_control);
  600. background_left = memnew(Control);
  601. background_left->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  602. background_left->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  603. background_left->set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  604. background_left->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_background_left));
  605. base_tiles_root_control->add_child(background_left);
  606. base_tiles_drawing_root = memnew(Control);
  607. base_tiles_drawing_root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  608. base_tiles_drawing_root->set_texture_filter(TEXTURE_FILTER_NEAREST);
  609. base_tiles_root_control->add_child(base_tiles_drawing_root);
  610. base_tiles_draw = memnew(Control);
  611. base_tiles_draw->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  612. base_tiles_draw->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  613. base_tiles_draw->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles));
  614. base_tiles_drawing_root->add_child(base_tiles_draw);
  615. base_tiles_texture_grid = memnew(Control);
  616. base_tiles_texture_grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  617. base_tiles_texture_grid->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  618. base_tiles_texture_grid->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles_texture_grid));
  619. base_tiles_drawing_root->add_child(base_tiles_texture_grid);
  620. base_tiles_shape_grid = memnew(Control);
  621. base_tiles_shape_grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  622. base_tiles_shape_grid->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  623. base_tiles_shape_grid->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles_shape_grid));
  624. base_tiles_drawing_root->add_child(base_tiles_shape_grid);
  625. // Alternative tiles.
  626. Label *alternative_tiles_label = memnew(Label);
  627. alternative_tiles_label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  628. alternative_tiles_label->set_text(TTR("Alternative Tiles"));
  629. alternative_tiles_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  630. right_vbox->add_child(alternative_tiles_label);
  631. alternative_tiles_root_control = memnew(Control);
  632. alternative_tiles_root_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  633. alternative_tiles_root_control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  634. alternative_tiles_root_control->connect(SceneStringName(gui_input), callable_mp(this, &TileAtlasView::_alternative_tiles_root_control_gui_input));
  635. right_vbox->add_child(alternative_tiles_root_control);
  636. background_right = memnew(Control);
  637. background_right->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  638. background_right->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  639. background_right->set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  640. background_right->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_background_right));
  641. alternative_tiles_root_control->add_child(background_right);
  642. alternative_tiles_drawing_root = memnew(Control);
  643. alternative_tiles_drawing_root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  644. alternative_tiles_drawing_root->set_texture_filter(TEXTURE_FILTER_NEAREST);
  645. alternative_tiles_root_control->add_child(alternative_tiles_drawing_root);
  646. alternatives_draw = memnew(Control);
  647. alternatives_draw->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  648. alternatives_draw->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  649. alternatives_draw->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_alternatives));
  650. alternative_tiles_drawing_root->add_child(alternatives_draw);
  651. }
  652. TileAtlasView::~TileAtlasView() {
  653. _clear_material_canvas_items();
  654. }