2
0

tile_atlas_view.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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.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);
  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) {
  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. // Offset the panning relative to the center of panel.
  120. Vector2 relative_mpos = get_local_mouse_position() - get_size() / 2;
  121. panning = (panning - relative_mpos) * zoom / previous_zoom + relative_mpos;
  122. } else {
  123. // Center of panel.
  124. panning = panning * zoom / previous_zoom;
  125. }
  126. button_center_view->set_disabled(panning.is_zero_approx());
  127. previous_zoom = zoom;
  128. center_container->set_begin(panning - center_container->get_minimum_size() / 2);
  129. center_container->set_size(center_container->get_minimum_size());
  130. }
  131. void TileAtlasView::_zoom_widget_changed() {
  132. _update_zoom_and_panning();
  133. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  134. }
  135. void TileAtlasView::_center_view() {
  136. panning = Vector2();
  137. button_center_view->set_disabled(true);
  138. _update_zoom_and_panning();
  139. emit_signal(SNAME("transform_changed"), zoom_widget->get_zoom(), panning);
  140. }
  141. void TileAtlasView::_base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event) {
  142. if (tile_set_atlas_source.is_null()) {
  143. return;
  144. }
  145. base_tiles_root_control->set_tooltip_text("");
  146. Ref<InputEventMouseMotion> mm = p_event;
  147. if (mm.is_valid()) {
  148. Transform2D xform = base_tiles_drawing_root->get_transform().affine_inverse();
  149. Vector2i coords = get_atlas_tile_coords_at_pos(xform.xform(mm->get_position()));
  150. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  151. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  152. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  153. base_tiles_root_control->set_tooltip_text(vformat(TTR("Source: %d\nAtlas coordinates: %s\nAlternative: 0"), source_id, coords));
  154. }
  155. }
  156. }
  157. }
  158. void TileAtlasView::_draw_base_tiles() {
  159. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  160. return;
  161. }
  162. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  163. if (texture.is_valid()) {
  164. Vector2i margins = tile_set_atlas_source->get_margins();
  165. Vector2i separation = tile_set_atlas_source->get_separation();
  166. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  167. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  168. // Draw the texture where there is no tile.
  169. for (int x = 0; x < grid_size.x; x++) {
  170. for (int y = 0; y < grid_size.y; y++) {
  171. Vector2i coords = Vector2i(x, y);
  172. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  173. Rect2i rect = Rect2i((texture_region_size + separation) * coords + margins, texture_region_size + separation);
  174. rect = rect.intersection(Rect2i(Vector2(), texture->get_size()));
  175. if (rect.size.x > 0 && rect.size.y > 0) {
  176. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  177. }
  178. }
  179. }
  180. }
  181. // Draw dark overlay after for performance reasons.
  182. for (int x = 0; x < grid_size.x; x++) {
  183. for (int y = 0; y < grid_size.y; y++) {
  184. Vector2i coords = Vector2i(x, y);
  185. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  186. Rect2i rect = Rect2i((texture_region_size + separation) * coords + margins, texture_region_size + separation);
  187. rect = rect.intersection(Rect2i(Vector2(), texture->get_size()));
  188. if (rect.size.x > 0 && rect.size.y > 0) {
  189. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  190. }
  191. }
  192. }
  193. }
  194. // Draw the texture around the grid.
  195. Rect2i rect;
  196. // Top.
  197. rect.position = Vector2i();
  198. rect.set_end(Vector2i(texture->get_size().x, margins.y));
  199. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  200. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  201. // Bottom
  202. int bottom_border = margins.y + (grid_size.y * (texture_region_size.y + separation.y));
  203. if (bottom_border < texture->get_size().y) {
  204. rect.position = Vector2i(0, bottom_border);
  205. rect.set_end(texture->get_size());
  206. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  207. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  208. }
  209. // Left
  210. rect.position = Vector2i(0, margins.y);
  211. rect.set_end(Vector2i(margins.x, margins.y + (grid_size.y * (texture_region_size.y + separation.y))));
  212. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  213. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  214. // Right.
  215. int right_border = margins.x + (grid_size.x * (texture_region_size.x + separation.x));
  216. if (right_border < texture->get_size().x) {
  217. rect.position = Vector2i(right_border, margins.y);
  218. rect.set_end(Vector2i(texture->get_size().x, margins.y + (grid_size.y * (texture_region_size.y + separation.y))));
  219. base_tiles_draw->draw_texture_rect_region(texture, rect, rect);
  220. base_tiles_draw->draw_rect(rect, Color(0.0, 0.0, 0.0, 0.5));
  221. }
  222. // Draw actual tiles, using their properties (modulation, etc...)
  223. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  224. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  225. // Different materials need to be drawn with different CanvasItems.
  226. RID ci_rid = _get_canvas_item_to_draw(tile_set_atlas_source->get_tile_data(atlas_coords, 0), base_tiles_draw, material_tiles_draw);
  227. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(atlas_coords); frame++) {
  228. // Update the y to max value.
  229. Rect2i base_frame_rect = tile_set_atlas_source->get_tile_texture_region(atlas_coords, frame);
  230. Vector2 offset_pos = Rect2(base_frame_rect).get_center() + Vector2(tile_set_atlas_source->get_tile_data(atlas_coords, 0)->get_texture_origin());
  231. // Draw the tile.
  232. TileMapLayer::draw_tile(ci_rid, offset_pos, tile_set, source_id, atlas_coords, 0, frame);
  233. }
  234. }
  235. // Draw Dark overlay on separation in its own pass.
  236. if (separation.x > 0 || separation.y > 0) {
  237. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  238. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  239. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(atlas_coords); frame++) {
  240. // Update the y to max value.
  241. Rect2i base_frame_rect = tile_set_atlas_source->get_tile_texture_region(atlas_coords, frame);
  242. if (separation.x > 0) {
  243. 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));
  244. right_sep_rect = right_sep_rect.intersection(Rect2i(Vector2(), texture->get_size()));
  245. if (right_sep_rect.size.x > 0 && right_sep_rect.size.y > 0) {
  246. //base_tiles_draw->draw_texture_rect_region(texture, right_sep_rect, right_sep_rect);
  247. base_tiles_draw->draw_rect(right_sep_rect, Color(0.0, 0.0, 0.0, 0.5));
  248. }
  249. }
  250. if (separation.y > 0) {
  251. 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));
  252. bottom_sep_rect = bottom_sep_rect.intersection(Rect2i(Vector2(), texture->get_size()));
  253. if (bottom_sep_rect.size.x > 0 && bottom_sep_rect.size.y > 0) {
  254. //base_tiles_draw->draw_texture_rect_region(texture, bottom_sep_rect, bottom_sep_rect);
  255. base_tiles_draw->draw_rect(bottom_sep_rect, Color(0.0, 0.0, 0.0, 0.5));
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. RID TileAtlasView::_get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map) {
  264. Ref<Material> mat = p_for_data->get_material();
  265. if (mat.is_null()) {
  266. return p_base_item->get_canvas_item();
  267. } else if (p_material_map.has(mat)) {
  268. return p_material_map[mat];
  269. } else {
  270. RID ci_rid = RS::get_singleton()->canvas_item_create();
  271. RS::get_singleton()->canvas_item_set_parent(ci_rid, p_base_item->get_canvas_item());
  272. RS::get_singleton()->canvas_item_set_material(ci_rid, mat->get_rid());
  273. RS::get_singleton()->canvas_item_set_default_texture_filter(ci_rid, RS::CanvasItemTextureFilter(p_base_item->get_texture_filter_in_tree()));
  274. p_material_map[mat] = ci_rid;
  275. return ci_rid;
  276. }
  277. }
  278. void TileAtlasView::_clear_material_canvas_items() {
  279. for (KeyValue<Ref<Material>, RID> kv : material_tiles_draw) {
  280. RS::get_singleton()->free(kv.value);
  281. }
  282. material_tiles_draw.clear();
  283. for (KeyValue<Ref<Material>, RID> kv : material_alternatives_draw) {
  284. RS::get_singleton()->free(kv.value);
  285. }
  286. material_alternatives_draw.clear();
  287. }
  288. void TileAtlasView::_draw_base_tiles_texture_grid() {
  289. if (tile_set_atlas_source.is_null()) {
  290. return;
  291. }
  292. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  293. if (texture.is_valid()) {
  294. Vector2i margins = tile_set_atlas_source->get_margins();
  295. Vector2i separation = tile_set_atlas_source->get_separation();
  296. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  297. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  298. // Draw each tile texture region.
  299. for (int x = 0; x < grid_size.x; x++) {
  300. for (int y = 0; y < grid_size.y; y++) {
  301. Vector2i origin = margins + (Vector2i(x, y) * (texture_region_size + separation));
  302. Vector2i base_tile_coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  303. if (base_tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  304. if (base_tile_coords == Vector2i(x, y)) {
  305. // Draw existing tile.
  306. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(base_tile_coords);
  307. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  308. base_tiles_texture_grid->draw_rect(Rect2i(origin, region_size), Color(1.0, 1.0, 1.0, 0.8), false);
  309. }
  310. } else {
  311. // Draw the grid.
  312. base_tiles_texture_grid->draw_rect(Rect2i(origin, texture_region_size), Color(0.7, 0.7, 0.7, 0.1), false);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. void TileAtlasView::_draw_base_tiles_shape_grid() {
  319. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  320. return;
  321. }
  322. // Draw the shapes.
  323. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  324. Vector2i tile_shape_size = tile_set->get_tile_size();
  325. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  326. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  327. Vector2 in_tile_base_offset = tile_set_atlas_source->get_tile_data(tile_id, 0)->get_texture_origin();
  328. if (tile_set_atlas_source->is_rect_in_tile_texture_region(tile_id, 0, Rect2(Vector2(-tile_shape_size) / 2, tile_shape_size))) {
  329. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(tile_id); frame++) {
  330. Color color = grid_color;
  331. if (frame > 0) {
  332. color.a *= 0.3;
  333. }
  334. Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(tile_id, frame);
  335. Transform2D tile_xform;
  336. tile_xform.set_origin(Rect2(texture_region).get_center() + in_tile_base_offset);
  337. tile_xform.set_scale(tile_shape_size);
  338. tile_set->draw_tile_shape(base_tiles_shape_grid, tile_xform, color);
  339. }
  340. }
  341. }
  342. }
  343. void TileAtlasView::_alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event) {
  344. alternative_tiles_root_control->set_tooltip_text("");
  345. Ref<InputEventMouseMotion> mm = p_event;
  346. if (mm.is_valid()) {
  347. Transform2D xform = alternative_tiles_drawing_root->get_transform().affine_inverse();
  348. Vector3i coords3 = get_alternative_tile_at_pos(xform.xform(mm->get_position()));
  349. Vector2i coords = Vector2i(coords3.x, coords3.y);
  350. int alternative_id = coords3.z;
  351. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  352. alternative_tiles_root_control->set_tooltip_text(vformat(TTR("Source: %d\nAtlas coordinates: %s\nAlternative: %d"), source_id, coords, alternative_id));
  353. }
  354. }
  355. }
  356. void TileAtlasView::_draw_alternatives() {
  357. if (tile_set.is_null() || tile_set_atlas_source.is_null()) {
  358. return;
  359. }
  360. // Draw the alternative tiles.
  361. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  362. if (texture.is_valid()) {
  363. Vector2 current_pos;
  364. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  365. Vector2i atlas_coords = tile_set_atlas_source->get_tile_id(i);
  366. current_pos.x = 0;
  367. int y_increment = 0;
  368. Size2i texture_region_size = tile_set_atlas_source->get_tile_texture_region(atlas_coords).size;
  369. int alternatives_count = tile_set_atlas_source->get_alternative_tiles_count(atlas_coords);
  370. for (int j = 1; j < alternatives_count; j++) {
  371. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(atlas_coords, j);
  372. TileData *tile_data = tile_set_atlas_source->get_tile_data(atlas_coords, alternative_id);
  373. bool transposed = tile_data->get_transpose();
  374. // Different materials need to be drawn with different CanvasItems.
  375. RID ci_rid = _get_canvas_item_to_draw(tile_data, alternatives_draw, material_alternatives_draw);
  376. // Update the y to max value.
  377. Vector2i offset_pos;
  378. if (transposed) {
  379. offset_pos = (current_pos + Vector2(texture_region_size.y, texture_region_size.x) / 2 + tile_data->get_texture_origin());
  380. y_increment = MAX(y_increment, texture_region_size.x);
  381. } else {
  382. offset_pos = (current_pos + texture_region_size / 2 + tile_data->get_texture_origin());
  383. y_increment = MAX(y_increment, texture_region_size.y);
  384. }
  385. // Draw the tile.
  386. TileMapLayer::draw_tile(ci_rid, offset_pos, tile_set, source_id, atlas_coords, alternative_id);
  387. // Increment the x position.
  388. current_pos.x += transposed ? texture_region_size.y : texture_region_size.x;
  389. }
  390. if (alternatives_count > 1) {
  391. current_pos.y += y_increment;
  392. }
  393. }
  394. }
  395. }
  396. void TileAtlasView::_draw_background_left() {
  397. background_left->draw_texture_rect(theme_cache.checkerboard, Rect2(Vector2(), background_left->get_size()), true);
  398. }
  399. void TileAtlasView::_draw_background_right() {
  400. background_right->draw_texture_rect(theme_cache.checkerboard, Rect2(Vector2(), background_right->get_size()), true);
  401. }
  402. void TileAtlasView::set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  403. tile_set = Ref<TileSet>(p_tile_set);
  404. tile_set_atlas_source = Ref<TileSetAtlasSource>(p_tile_set_atlas_source);
  405. _clear_material_canvas_items();
  406. if (tile_set.is_null()) {
  407. return;
  408. }
  409. ERR_FAIL_COND(p_source_id < 0);
  410. ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  411. source_id = p_source_id;
  412. // Show or hide the view.
  413. bool valid = tile_set_atlas_source->get_texture().is_valid();
  414. hbox->set_visible(valid);
  415. missing_source_label->set_visible(!valid);
  416. // Update the rect cache.
  417. _update_alternative_tiles_rect_cache();
  418. // Update everything.
  419. _update_zoom_and_panning();
  420. base_tiles_drawing_root->set_size(_compute_base_tiles_control_size());
  421. alternative_tiles_drawing_root->set_size(_compute_alternative_tiles_control_size());
  422. // Update.
  423. base_tiles_draw->queue_redraw();
  424. base_tiles_texture_grid->queue_redraw();
  425. base_tiles_shape_grid->queue_redraw();
  426. alternatives_draw->queue_redraw();
  427. background_left->queue_redraw();
  428. background_right->queue_redraw();
  429. }
  430. float TileAtlasView::get_zoom() const {
  431. return zoom_widget->get_zoom();
  432. }
  433. void TileAtlasView::set_transform(float p_zoom, Vector2i p_panning) {
  434. zoom_widget->set_zoom(p_zoom);
  435. panning = p_panning;
  436. _update_zoom_and_panning();
  437. }
  438. void TileAtlasView::set_padding(Side p_side, int p_padding) {
  439. ERR_FAIL_COND(p_padding < 0);
  440. margin_container_paddings[p_side] = p_padding;
  441. }
  442. Vector2i TileAtlasView::get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp) const {
  443. if (tile_set_atlas_source.is_null()) {
  444. return Vector2i();
  445. }
  446. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  447. if (texture.is_null()) {
  448. return TileSetSource::INVALID_ATLAS_COORDS;
  449. }
  450. Vector2i margins = tile_set_atlas_source->get_margins();
  451. Vector2i separation = tile_set_atlas_source->get_separation();
  452. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  453. // Compute index in atlas.
  454. Vector2 pos = p_pos - margins;
  455. Vector2i ret = (pos / (texture_region_size + separation)).floor();
  456. // Clamp.
  457. if (p_clamp) {
  458. Vector2i size = tile_set_atlas_source->get_atlas_grid_size();
  459. ret = ret.clamp(Vector2i(), size - Vector2i(1, 1));
  460. }
  461. return ret;
  462. }
  463. void TileAtlasView::_update_alternative_tiles_rect_cache() {
  464. if (tile_set_atlas_source.is_null()) {
  465. return;
  466. }
  467. alternative_tiles_rect_cache.clear();
  468. Rect2i current;
  469. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  470. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  471. int alternatives_count = tile_set_atlas_source->get_alternative_tiles_count(tile_id);
  472. Size2i texture_region_size = tile_set_atlas_source->get_tile_texture_region(tile_id).size;
  473. int line_height = 0;
  474. for (int j = 1; j < alternatives_count; j++) {
  475. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(tile_id, j);
  476. TileData *tile_data = tile_set_atlas_source->get_tile_data(tile_id, alternative_id);
  477. bool transposed = tile_data->get_transpose();
  478. current.size = transposed ? Vector2i(texture_region_size.y, texture_region_size.x) : texture_region_size;
  479. // Update the rect.
  480. if (!alternative_tiles_rect_cache.has(tile_id)) {
  481. alternative_tiles_rect_cache[tile_id] = HashMap<int, Rect2i>();
  482. }
  483. alternative_tiles_rect_cache[tile_id][alternative_id] = current;
  484. current.position.x += transposed ? texture_region_size.y : texture_region_size.x;
  485. line_height = MAX(line_height, transposed ? texture_region_size.x : texture_region_size.y);
  486. }
  487. current.position.x = 0;
  488. current.position.y += line_height;
  489. }
  490. }
  491. Vector3i TileAtlasView::get_alternative_tile_at_pos(const Vector2 p_pos) const {
  492. for (const KeyValue<Vector2, HashMap<int, Rect2i>> &E_coords : alternative_tiles_rect_cache) {
  493. for (const KeyValue<int, Rect2i> &E_alternative : E_coords.value) {
  494. if (E_alternative.value.has_point(p_pos)) {
  495. return Vector3i(E_coords.key.x, E_coords.key.y, E_alternative.key);
  496. }
  497. }
  498. }
  499. return Vector3i(TileSetSource::INVALID_ATLAS_COORDS.x, TileSetSource::INVALID_ATLAS_COORDS.y, TileSetSource::INVALID_TILE_ALTERNATIVE);
  500. }
  501. Rect2i TileAtlasView::get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile) {
  502. ERR_FAIL_COND_V_MSG(!alternative_tiles_rect_cache.has(p_coords), Rect2i(), vformat("No cached rect for tile coords:%s", p_coords));
  503. 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));
  504. return alternative_tiles_rect_cache[p_coords][p_alternative_tile];
  505. }
  506. void TileAtlasView::queue_redraw() {
  507. base_tiles_draw->queue_redraw();
  508. base_tiles_texture_grid->queue_redraw();
  509. base_tiles_shape_grid->queue_redraw();
  510. alternatives_draw->queue_redraw();
  511. background_left->queue_redraw();
  512. background_right->queue_redraw();
  513. }
  514. void TileAtlasView::_update_theme_item_cache() {
  515. Control::_update_theme_item_cache();
  516. theme_cache.center_view_icon = get_editor_theme_icon(SNAME("CenterView"));
  517. theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
  518. }
  519. void TileAtlasView::_notification(int p_what) {
  520. switch (p_what) {
  521. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  522. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  523. break;
  524. }
  525. [[fallthrough]];
  526. }
  527. case NOTIFICATION_ENTER_TREE: {
  528. 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")));
  529. panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
  530. } break;
  531. case NOTIFICATION_THEME_CHANGED: {
  532. button_center_view->set_button_icon(theme_cache.center_view_icon);
  533. } break;
  534. }
  535. }
  536. void TileAtlasView::_bind_methods() {
  537. ADD_SIGNAL(MethodInfo("transform_changed", PropertyInfo(Variant::FLOAT, "zoom"), PropertyInfo(Variant::VECTOR2, "scroll")));
  538. }
  539. TileAtlasView::TileAtlasView() {
  540. set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  541. Panel *panel = memnew(Panel);
  542. panel->set_clip_contents(true);
  543. panel->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  544. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  545. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  546. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  547. add_child(panel);
  548. zoom_widget = memnew(EditorZoomWidget);
  549. add_child(zoom_widget);
  550. zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
  551. zoom_widget->connect("zoom_changed", callable_mp(this, &TileAtlasView::_zoom_widget_changed).unbind(1));
  552. zoom_widget->set_shortcut_context(this);
  553. button_center_view = memnew(Button);
  554. button_center_view->set_anchors_and_offsets_preset(Control::PRESET_TOP_RIGHT, Control::PRESET_MODE_MINSIZE, 5);
  555. button_center_view->set_grow_direction_preset(Control::PRESET_TOP_RIGHT);
  556. button_center_view->connect(SceneStringName(pressed), callable_mp(this, &TileAtlasView::_center_view));
  557. button_center_view->set_flat(true);
  558. button_center_view->set_disabled(true);
  559. button_center_view->set_tooltip_text(TTR("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_text(TTR("The selected atlas source has no valid texture. Assign a texture in the TileSet bottom tab."));
  573. center_container->add_child(missing_source_label);
  574. margin_container = memnew(MarginContainer);
  575. margin_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  576. center_container->add_child(margin_container);
  577. hbox = memnew(HBoxContainer);
  578. hbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  579. hbox->add_theme_constant_override("separation", 10);
  580. hbox->hide();
  581. margin_container->add_child(hbox);
  582. VBoxContainer *left_vbox = memnew(VBoxContainer);
  583. left_vbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  584. hbox->add_child(left_vbox);
  585. VBoxContainer *right_vbox = memnew(VBoxContainer);
  586. right_vbox->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  587. hbox->add_child(right_vbox);
  588. // Base tiles.
  589. Label *base_tile_label = memnew(Label);
  590. base_tile_label->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  591. base_tile_label->set_text(TTR("Base Tiles"));
  592. base_tile_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  593. left_vbox->add_child(base_tile_label);
  594. base_tiles_root_control = memnew(Control);
  595. base_tiles_root_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  596. base_tiles_root_control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  597. base_tiles_root_control->connect(SceneStringName(gui_input), callable_mp(this, &TileAtlasView::_base_tiles_root_control_gui_input));
  598. left_vbox->add_child(base_tiles_root_control);
  599. background_left = memnew(Control);
  600. background_left->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  601. background_left->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  602. background_left->set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  603. background_left->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_background_left));
  604. base_tiles_root_control->add_child(background_left);
  605. base_tiles_drawing_root = memnew(Control);
  606. base_tiles_drawing_root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  607. base_tiles_drawing_root->set_texture_filter(TEXTURE_FILTER_NEAREST);
  608. base_tiles_root_control->add_child(base_tiles_drawing_root);
  609. base_tiles_draw = memnew(Control);
  610. base_tiles_draw->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  611. base_tiles_draw->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  612. base_tiles_draw->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles));
  613. base_tiles_drawing_root->add_child(base_tiles_draw);
  614. base_tiles_texture_grid = memnew(Control);
  615. base_tiles_texture_grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  616. base_tiles_texture_grid->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  617. base_tiles_texture_grid->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles_texture_grid));
  618. base_tiles_drawing_root->add_child(base_tiles_texture_grid);
  619. base_tiles_shape_grid = memnew(Control);
  620. base_tiles_shape_grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  621. base_tiles_shape_grid->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  622. base_tiles_shape_grid->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_base_tiles_shape_grid));
  623. base_tiles_drawing_root->add_child(base_tiles_shape_grid);
  624. // Alternative tiles.
  625. Label *alternative_tiles_label = memnew(Label);
  626. alternative_tiles_label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  627. alternative_tiles_label->set_text(TTR("Alternative Tiles"));
  628. alternative_tiles_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  629. right_vbox->add_child(alternative_tiles_label);
  630. alternative_tiles_root_control = memnew(Control);
  631. alternative_tiles_root_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  632. alternative_tiles_root_control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  633. alternative_tiles_root_control->connect(SceneStringName(gui_input), callable_mp(this, &TileAtlasView::_alternative_tiles_root_control_gui_input));
  634. right_vbox->add_child(alternative_tiles_root_control);
  635. background_right = memnew(Control);
  636. background_right->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  637. background_right->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT);
  638. background_right->set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
  639. background_right->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_background_right));
  640. alternative_tiles_root_control->add_child(background_right);
  641. alternative_tiles_drawing_root = memnew(Control);
  642. alternative_tiles_drawing_root->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  643. alternative_tiles_drawing_root->set_texture_filter(TEXTURE_FILTER_NEAREST);
  644. alternative_tiles_root_control->add_child(alternative_tiles_drawing_root);
  645. alternatives_draw = memnew(Control);
  646. alternatives_draw->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  647. alternatives_draw->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  648. alternatives_draw->connect(SceneStringName(draw), callable_mp(this, &TileAtlasView::_draw_alternatives));
  649. alternative_tiles_drawing_root->add_child(alternatives_draw);
  650. }
  651. TileAtlasView::~TileAtlasView() {
  652. _clear_material_canvas_items();
  653. }