Merge pull request #93047 from groud/fix_polygon_editor_grid

Fix tile polygon editor grid and missing update
This commit is contained in:
Rémi Verschelde 2024-06-29 13:03:56 +02:00
commit 3fbb6044d0
No known key found for this signature in database
GPG key ID: C3336907360768E1
3 changed files with 62 additions and 43 deletions

View file

@ -138,6 +138,11 @@ 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.
TileData *tile_data = background_atlas_source->get_tile_data(background_atlas_coords, background_alternative_id);
ERR_FAIL_NULL(tile_data);
Rect2 background_region = background_atlas_source->get_tile_texture_region(background_atlas_coords);
// Draw the focus rectangle.
if (base_control->has_focus()) {
base_control->draw_style_box(focus_stylebox, Rect2(Vector2(), base_control->get_size()));
@ -145,42 +150,49 @@ 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 - tile_data->get_texture_origin(), background_region.size);
base_control->draw_rect(texture_rect, Color(1, 1, 1, 0.3));
// Draw the background.
if (background_texture.is_valid()) {
if (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);
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());
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));
}
}
@ -263,6 +275,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 +735,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 +770,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 +1478,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 +1487,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 +1634,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 +1653,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 +2894,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 +2903,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) {

View file

@ -142,13 +142,9 @@ private:
Vector2 panning;
bool initializing = true;
Ref<Texture2D> background_texture;
Rect2 background_region;
Vector2 background_offset;
bool background_h_flip = false;
bool background_v_flip = false;
bool background_transpose = false;
Color background_modulate;
Ref<TileSetAtlasSource> background_atlas_source;
Vector2i background_atlas_coords;
int background_alternative_id;
Color polygon_color = Color(1.0, 0.0, 0.0);
@ -183,7 +179,7 @@ public:
void set_use_undo_redo(bool p_use_undo_redo);
void set_tile_set(Ref<TileSet> p_tile_set);
void set_background(Ref<Texture2D> p_texture, Rect2 p_region = Rect2(), Vector2 p_offset = Vector2(), bool p_flip_h = false, bool p_flip_v = false, bool p_transpose = false, Color p_modulate = Color(1.0, 1.0, 1.0, 0.0));
void set_background_tile(const TileSetAtlasSource *p_atlas_source, const Vector2 &p_atlas_coords, int p_alternative_id);
int get_polygon_count();
int add_polygon(const Vector<Point2> &p_polygon, int p_index = -1);

View file

@ -2822,13 +2822,15 @@ void EditorPropertyTilePolygon::update_property() {
ERR_FAIL_COND(atlas_tile_proxy_object->get_edited_tiles().is_empty());
Ref<TileSetAtlasSource> tile_set_atlas_source = atlas_tile_proxy_object->get_edited_tile_set_atlas_source();
generic_tile_polygon_editor->set_tile_set(Ref<TileSet>(tile_set_atlas_source->get_tile_set()));
Ref<TileSet> tile_set(tile_set_atlas_source->get_tile_set());
// Update the polyugon editor tile_set.
generic_tile_polygon_editor->set_tile_set(tile_set);
// Set the background
Vector2i coords = atlas_tile_proxy_object->get_edited_tiles().front()->get().tile;
int alternative = atlas_tile_proxy_object->get_edited_tiles().front()->get().alternative;
TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
generic_tile_polygon_editor->set_background(tile_set_atlas_source->get_texture(), tile_set_atlas_source->get_tile_texture_region(coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
generic_tile_polygon_editor->set_background_tile(*tile_set_atlas_source, coords, alternative);
// Reset the polygons.
generic_tile_polygon_editor->clear_polygons();