summaryrefslogtreecommitdiffstats
path: root/editor/plugins/tiles/tile_data_editors.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/plugins/tiles/tile_data_editors.cpp')
-rw-r--r--editor/plugins/tiles/tile_data_editors.cpp105
1 files changed, 71 insertions, 34 deletions
diff --git a/editor/plugins/tiles/tile_data_editors.cpp b/editor/plugins/tiles/tile_data_editors.cpp
index 62090a5d17..f985bbc629 100644
--- a/editor/plugins/tiles/tile_data_editors.cpp
+++ b/editor/plugins/tiles/tile_data_editors.cpp
@@ -138,6 +138,19 @@ void GenericTilePolygonEditor::_base_control_draw() {
const Ref<Texture2D> add_handle = get_editor_theme_icon(SNAME("EditorHandleAdd"));
const Ref<StyleBox> focus_stylebox = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
+ // Get the background data.
+ Rect2 background_region;
+ TileData *tile_data = nullptr;
+
+ if (background_atlas_source.is_valid()) {
+ tile_data = background_atlas_source->get_tile_data(background_atlas_coords, background_alternative_id);
+ ERR_FAIL_NULL(tile_data);
+ background_region = background_atlas_source->get_tile_texture_region(background_atlas_coords);
+ } else {
+ // If no tile was selected yet, use default size.
+ background_region.size = tile_set->get_tile_size();
+ }
+
// Draw the focus rectangle.
if (base_control->has_focus()) {
base_control->draw_style_box(focus_stylebox, Rect2(Vector2(), base_control->get_size()));
@@ -145,42 +158,57 @@ void GenericTilePolygonEditor::_base_control_draw() {
// Draw tile-related things.
const Size2 base_tile_size = tile_set->get_tile_size();
- const Size2 tile_size = background_region.size;
Transform2D xform;
xform.set_origin(base_control->get_size() / 2 + panning);
xform.set_scale(Vector2(editor_zoom_widget->get_zoom(), editor_zoom_widget->get_zoom()));
base_control->draw_set_transform_matrix(xform);
- // Draw the tile shape filled.
- Transform2D tile_xform;
- tile_xform.set_scale(tile_size);
- tile_set->draw_tile_shape(base_control, tile_xform, Color(1.0, 1.0, 1.0, 0.3), true);
+ // Draw fill rect under texture region.
+ Rect2 texture_rect(-background_region.size / 2, background_region.size);
+ if (tile_data) {
+ texture_rect.position -= tile_data->get_texture_origin();
+ }
+ base_control->draw_rect(texture_rect, Color(1, 1, 1, 0.3));
// Draw the background.
- if (background_texture.is_valid()) {
+ if (tile_data && background_atlas_source->get_texture().is_valid()) {
Size2 region_size = background_region.size;
- if (background_h_flip) {
+ if (tile_data->get_flip_h()) {
region_size.x = -region_size.x;
}
- if (background_v_flip) {
+ if (tile_data->get_flip_v()) {
region_size.y = -region_size.y;
}
- base_control->draw_texture_rect_region(background_texture, Rect2(-background_region.size / 2 - background_offset, region_size), background_region, background_modulate, background_transpose);
+ base_control->draw_texture_rect_region(background_atlas_source->get_texture(), Rect2(-background_region.size / 2 - tile_data->get_texture_origin(), region_size), background_region, tile_data->get_modulate(), tile_data->get_transpose());
}
+ // Compute and draw the grid area.
+ Rect2 grid_area = Rect2(-base_tile_size / 2, base_tile_size);
+ if (tile_data) {
+ grid_area.expand_to(-background_region.get_size() / 2 - tile_data->get_texture_origin());
+ grid_area.expand_to(background_region.get_size() / 2 - tile_data->get_texture_origin());
+ } else {
+ grid_area.expand_to(-background_region.get_size() / 2);
+ grid_area.expand_to(background_region.get_size() / 2);
+ }
+ base_control->draw_rect(grid_area, Color(1, 1, 1, 0.3), false);
+
// Draw grid.
if (current_snap_option == SNAP_GRID) {
Vector2 spacing = base_tile_size / snap_subdivision->get_value();
- Vector2 offset = -tile_size / 2;
- int w = snap_subdivision->get_value() * (tile_size / base_tile_size).x;
- int h = snap_subdivision->get_value() * (tile_size / base_tile_size).y;
-
- for (int y = 1; y < h; y++) {
- for (int x = 1; x < w; x++) {
- base_control->draw_line(Vector2(spacing.x * x, 0) + offset, Vector2(spacing.x * x, tile_size.y) + offset, Color(1, 1, 1, 0.33));
- base_control->draw_line(Vector2(0, spacing.y * y) + offset, Vector2(tile_size.x, spacing.y * y) + offset, Color(1, 1, 1, 0.33));
- }
+ Vector2 origin = -base_tile_size / 2;
+ for (real_t y = origin.y; y < grid_area.get_end().y; y += spacing.y) {
+ base_control->draw_line(Vector2(grid_area.get_position().x, y), Vector2(grid_area.get_end().x, y), Color(1, 1, 1, 0.33));
+ }
+ for (real_t y = origin.y - spacing.y; y > grid_area.get_position().y; y -= spacing.y) {
+ base_control->draw_line(Vector2(grid_area.get_position().x, y), Vector2(grid_area.get_end().x, y), Color(1, 1, 1, 0.33));
+ }
+ for (real_t x = origin.x; x < grid_area.get_end().x; x += spacing.x) {
+ base_control->draw_line(Vector2(x, grid_area.get_position().y), Vector2(x, grid_area.get_end().y), Color(1, 1, 1, 0.33));
+ }
+ for (real_t x = origin.x - spacing.x; x > grid_area.get_position().x; x -= spacing.x) {
+ base_control->draw_line(Vector2(x, grid_area.get_position().y), Vector2(x, grid_area.get_end().y), Color(1, 1, 1, 0.33));
}
}
@@ -237,7 +265,7 @@ void GenericTilePolygonEditor::_base_control_draw() {
for (int i = 0; i < (int)polygons.size(); i++) {
const Vector<Vector2> &polygon = polygons[i];
for (int j = 0; j < polygon.size(); j++) {
- const Color poly_modulate = (tinted_polygon_index == i && tinted_point_index == j) ? Color(0.5, 1, 2) : Color(1, 1, 1);
+ const Color poly_modulate = (tinted_polygon_index == i && tinted_point_index == j) ? Color(0.4, 1, 1) : Color(1, 1, 1);
base_control->draw_texture(handle, xform.xform(polygon[j]) - handle->get_size() / 2, poly_modulate);
}
}
@@ -253,7 +281,7 @@ void GenericTilePolygonEditor::_base_control_draw() {
}
if (drag_type == DRAG_TYPE_CREATE_POINT) {
- base_control->draw_texture(handle, xform.xform(in_creation_point) - handle->get_size() / 2, Color(0.5, 1, 2));
+ base_control->draw_texture(handle, xform.xform(in_creation_point) - handle->get_size() / 2, Color(0.4, 1, 1));
}
// Draw the point creation preview in edit mode.
@@ -263,6 +291,8 @@ void GenericTilePolygonEditor::_base_control_draw() {
// Draw the tile shape line.
base_control->draw_set_transform_matrix(xform);
+ Transform2D tile_xform;
+ tile_xform.set_scale(base_tile_size);
tile_set->draw_tile_shape(base_control, tile_xform, grid_color, false);
base_control->draw_set_transform_matrix(Transform2D());
}
@@ -721,8 +751,18 @@ void GenericTilePolygonEditor::set_tile_set(Ref<TileSet> p_tile_set) {
add_polygon(polygon);
}
+ // Trigger a redraw on tile_set change.
+ Callable callable = callable_mp((CanvasItem *)base_control, &CanvasItem::queue_redraw);
+ if (tile_set.is_valid()) {
+ tile_set->disconnect_changed(callable);
+ }
+
tile_set = p_tile_set;
+ if (tile_set.is_valid()) {
+ tile_set->connect_changed(callable);
+ }
+
// Set the default zoom value.
int default_control_y_size = 200 * EDSCALE;
Vector2 zoomed_tile = editor_zoom_widget->get_zoom() * tile_set->get_tile_size();
@@ -746,14 +786,11 @@ void GenericTilePolygonEditor::set_tile_set(Ref<TileSet> p_tile_set) {
_zoom_changed();
}
-void GenericTilePolygonEditor::set_background(Ref<Texture2D> p_texture, Rect2 p_region, Vector2 p_offset, bool p_flip_h, bool p_flip_v, bool p_transpose, Color p_modulate) {
- background_texture = p_texture;
- background_region = p_region;
- background_offset = p_offset;
- background_h_flip = p_flip_h;
- background_v_flip = p_flip_v;
- background_transpose = p_transpose;
- background_modulate = p_modulate;
+void GenericTilePolygonEditor::set_background_tile(const TileSetAtlasSource *p_atlas_source, const Vector2 &p_atlas_coords, int p_alternative_id) {
+ ERR_FAIL_NULL(p_atlas_source);
+ background_atlas_source = p_atlas_source;
+ background_atlas_coords = p_atlas_coords;
+ background_alternative_id = p_alternative_id;
base_control->queue_redraw();
}
@@ -1457,7 +1494,7 @@ void TileDataOcclusionShapeEditor::_set_painted_value(TileSetAtlasSource *p_tile
if (occluder_polygon.is_valid()) {
polygon_editor->add_polygon(occluder_polygon->get_polygon());
}
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
void TileDataOcclusionShapeEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) {
@@ -1466,7 +1503,7 @@ void TileDataOcclusionShapeEditor::_set_value(TileSetAtlasSource *p_tile_set_atl
Ref<OccluderPolygon2D> occluder_polygon = p_value;
tile_data->set_occluder(occlusion_layer, occluder_polygon);
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
Variant TileDataOcclusionShapeEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
@@ -1613,7 +1650,7 @@ void TileDataCollisionEditor::_set_painted_value(TileSetAtlasSource *p_tile_set_
E.value->update_property();
}
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
void TileDataCollisionEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) {
@@ -1632,7 +1669,7 @@ void TileDataCollisionEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_so
tile_data->set_collision_polygon_one_way_margin(physics_layer, i, polygon_dict["one_way_margin"]);
}
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
Variant TileDataCollisionEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {
@@ -2873,7 +2910,7 @@ void TileDataNavigationEditor::_set_painted_value(TileSetAtlasSource *p_tile_set
polygon_editor->add_polygon(nav_polygon->get_outline(i));
}
}
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
void TileDataNavigationEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile, const Variant &p_value) {
@@ -2882,7 +2919,7 @@ void TileDataNavigationEditor::_set_value(TileSetAtlasSource *p_tile_set_atlas_s
Ref<NavigationPolygon> nav_polygon = p_value;
tile_data->set_navigation_polygon(navigation_layer, nav_polygon);
- polygon_editor->set_background(p_tile_set_atlas_source->get_texture(), p_tile_set_atlas_source->get_tile_texture_region(p_coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
+ polygon_editor->set_background_tile(p_tile_set_atlas_source, p_coords, p_alternative_tile);
}
Variant TileDataNavigationEditor::_get_value(TileSetAtlasSource *p_tile_set_atlas_source, Vector2 p_coords, int p_alternative_tile) {