summaryrefslogtreecommitdiff
path: root/scene/2d
diff options
context:
space:
mode:
Diffstat (limited to 'scene/2d')
-rw-r--r--scene/2d/collision_polygon_2d.cpp3
-rw-r--r--scene/2d/line_2d.cpp9
-rw-r--r--scene/2d/line_builder.cpp12
-rw-r--r--scene/2d/line_builder.h1
-rw-r--r--scene/2d/polygon_2d.cpp91
-rw-r--r--scene/2d/polygon_2d.h5
-rw-r--r--scene/2d/sprite.cpp56
-rw-r--r--scene/2d/sprite.h3
-rw-r--r--scene/2d/tile_map.cpp30
-rw-r--r--scene/2d/tile_map.h3
10 files changed, 197 insertions, 16 deletions
diff --git a/scene/2d/collision_polygon_2d.cpp b/scene/2d/collision_polygon_2d.cpp
index 978fb379ac..7e2026d225 100644
--- a/scene/2d/collision_polygon_2d.cpp
+++ b/scene/2d/collision_polygon_2d.cpp
@@ -175,7 +175,8 @@ void CollisionPolygon2D::_notification(int p_what) {
Vector2 p = polygon[i];
Vector2 n = polygon[(i + 1) % polygon.size()];
- draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 3);
+ // draw line with width <= 1, so it does not scale with zoom and break pixel exact editing
+ draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 1);
}
#define DEBUG_DECOMPOSE
#if defined(TOOLS_ENABLED) && defined(DEBUG_DECOMPOSE)
diff --git a/scene/2d/line_2d.cpp b/scene/2d/line_2d.cpp
index f0ab9652b4..ba4a5c5571 100644
--- a/scene/2d/line_2d.cpp
+++ b/scene/2d/line_2d.cpp
@@ -252,12 +252,15 @@ void Line2D::_draw() {
lb.sharp_limit = _sharp_limit;
lb.width = _width;
- lb.build();
-
RID texture_rid;
- if (_texture.is_valid())
+ if (_texture.is_valid()) {
texture_rid = (**_texture).get_rid();
+ lb.tile_aspect = _texture->get_size().aspect();
+ }
+
+ lb.build();
+
VS::get_singleton()->canvas_item_add_triangle_array(
get_canvas_item(),
lb.indices,
diff --git a/scene/2d/line_builder.cpp b/scene/2d/line_builder.cpp
index b1a072729f..845788bada 100644
--- a/scene/2d/line_builder.cpp
+++ b/scene/2d/line_builder.cpp
@@ -101,6 +101,7 @@ LineBuilder::LineBuilder() {
round_precision = 8;
begin_cap_mode = Line2D::LINE_CAP_NONE;
end_cap_mode = Line2D::LINE_CAP_NONE;
+ tile_aspect = 1.f;
_interpolate_color = false;
_last_index[0] = 0;
@@ -111,6 +112,7 @@ void LineBuilder::clear_output() {
vertices.clear();
colors.clear();
indices.clear();
+ uvs.clear();
}
void LineBuilder::build() {
@@ -121,6 +123,8 @@ void LineBuilder::build() {
return;
}
+ ERR_FAIL_COND(tile_aspect <= 0.f);
+
const float hw = width / 2.f;
const float hw_sq = hw * hw;
const float sharp_limit_sq = sharp_limit * sharp_limit;
@@ -164,7 +168,7 @@ void LineBuilder::build() {
current_distance1 = current_distance0;
} else if (begin_cap_mode == Line2D::LINE_CAP_ROUND) {
if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
- uvx0 = 0.5f;
+ uvx0 = 0.5f / tile_aspect;
}
new_arc(pos0, pos_up0 - pos0, -Math_PI, color0, Rect2(0.f, 0.f, 1.f, 1.f));
total_distance += width;
@@ -286,8 +290,8 @@ void LineBuilder::build() {
color1 = gradient->get_color_at_offset(current_distance1 / total_distance);
}
if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
- uvx0 = current_distance0 / width;
- uvx1 = current_distance1 / width;
+ uvx0 = current_distance0 / (width * tile_aspect);
+ uvx1 = current_distance1 / (width * tile_aspect);
}
strip_add_quad(pos_up1, pos_down1, color1, uvx1);
@@ -373,7 +377,7 @@ void LineBuilder::build() {
color1 = gradient->get_color(gradient->get_points_count() - 1);
}
if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
- uvx1 = current_distance1 / width;
+ uvx1 = current_distance1 / (width * tile_aspect);
}
strip_add_quad(pos_up1, pos_down1, color1, uvx1);
diff --git a/scene/2d/line_builder.h b/scene/2d/line_builder.h
index daa2ed7c24..b1c62f84e2 100644
--- a/scene/2d/line_builder.h
+++ b/scene/2d/line_builder.h
@@ -50,6 +50,7 @@ public:
Line2D::LineTextureMode texture_mode;
float sharp_limit;
int round_precision;
+ float tile_aspect; // w/h
// TODO offset_joints option (offers alternative implementation of round joints)
// TODO Move in a struct and reference it
diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp
index e63dc4e515..2cb1e86f51 100644
--- a/scene/2d/polygon_2d.cpp
+++ b/scene/2d/polygon_2d.cpp
@@ -192,7 +192,80 @@ void Polygon2D::_notification(int p_what) {
// Vector<int> indices = Geometry::triangulate_polygon(points);
// VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID());
- VS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID(), RID(), antialiased);
+ if (invert || splits.size() == 0) {
+ VS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID(), RID(), antialiased);
+ } else {
+ //use splits
+ Vector<int> loop;
+ int sc = splits.size();
+ PoolVector<int>::Read r = splits.read();
+ int last = points.size();
+
+ Vector<Vector<int> > loops;
+
+ for (int i = 0; i < last; i++) {
+
+ int split;
+ int min_end = -1;
+
+ do {
+
+ loop.push_back(i);
+
+ split = -1;
+ int end = -1;
+
+ for (int j = 0; j < sc; j += 2) {
+ if (r[j + 1] >= last)
+ continue; //no longer valid
+ if (min_end != -1 && r[j + 1] >= min_end)
+ continue;
+ if (r[j] == i) {
+ if (split == -1 || r[j + 1] > end) {
+ split = r[j];
+ end = r[j + 1];
+ }
+ }
+ }
+
+ if (split != -1) {
+ for (int j = end; j < last; j++) {
+ loop.push_back(j);
+ }
+ loops.push_back(loop);
+ last = end + 1;
+ loop.clear();
+ min_end = end; //avoid this split from repeating
+ }
+
+ } while (split != -1);
+ }
+
+ if (loop.size()) {
+ loops.push_back(loop);
+ }
+
+ Vector<int> indices;
+
+ for (int i = 0; i < loops.size(); i++) {
+ Vector<int> loop = loops[i];
+ Vector<Vector2> vertices;
+ vertices.resize(loop.size());
+ for (int j = 0; j < vertices.size(); j++) {
+ vertices[j] = points[loop[j]];
+ }
+ Vector<int> sub_indices = Geometry::triangulate_polygon(vertices);
+ int from = indices.size();
+ indices.resize(from + sub_indices.size());
+ for (int j = 0; j < sub_indices.size(); j++) {
+ indices[from + j] = loop[sub_indices[j]];
+ }
+ }
+
+ //print_line("loops: " + itos(loops.size()) + " indices: " + itos(indices.size()));
+
+ VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID());
+ }
} break;
}
@@ -220,6 +293,18 @@ PoolVector<Vector2> Polygon2D::get_uv() const {
return uv;
}
+void Polygon2D::set_splits(const PoolVector<int> &p_splits) {
+
+ ERR_FAIL_COND(p_splits.size() & 1); //splits should be multiple of 2
+ splits = p_splits;
+ update();
+}
+
+PoolVector<int> Polygon2D::get_splits() const {
+
+ return splits;
+}
+
void Polygon2D::set_color(const Color &p_color) {
color = p_color;
@@ -352,6 +437,9 @@ void Polygon2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
+ ClassDB::bind_method(D_METHOD("set_splits", "splits"), &Polygon2D::set_splits);
+ ClassDB::bind_method(D_METHOD("get_splits"), &Polygon2D::get_splits);
+
ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
@@ -384,6 +472,7 @@ void Polygon2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
+ ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "splits"), "set_splits", "get_splits");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
diff --git a/scene/2d/polygon_2d.h b/scene/2d/polygon_2d.h
index 66f44cb3f1..3a24177548 100644
--- a/scene/2d/polygon_2d.h
+++ b/scene/2d/polygon_2d.h
@@ -40,6 +40,8 @@ class Polygon2D : public Node2D {
PoolVector<Vector2> polygon;
PoolVector<Vector2> uv;
PoolVector<Color> vertex_colors;
+ PoolVector<int> splits;
+
Color color;
Ref<Texture> texture;
Size2 tex_scale;
@@ -75,6 +77,9 @@ public:
void set_uv(const PoolVector<Vector2> &p_uv);
PoolVector<Vector2> get_uv() const;
+ void set_splits(const PoolVector<int> &p_uv);
+ PoolVector<int> get_splits() const;
+
void set_color(const Color &p_color);
Color get_color() const;
diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp
index 796969be1e..67f016ae79 100644
--- a/scene/2d/sprite.cpp
+++ b/scene/2d/sprite.cpp
@@ -73,8 +73,8 @@ void Sprite::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_cli
s = s / Size2(hframes, vframes);
r_src_rect.size = s;
- r_src_rect.position.x += float(frame % hframes) * s.x;
- r_src_rect.position.y += float(frame / hframes) * s.y;
+ r_src_rect.position.x = float(frame % hframes) * s.x;
+ r_src_rect.position.y = float(frame / hframes) * s.y;
}
Point2 ofs = offset;
@@ -121,7 +121,15 @@ void Sprite::set_texture(const Ref<Texture> &p_texture) {
if (p_texture == texture)
return;
+
+ if (texture.is_valid())
+ texture->remove_change_receptor(this);
+
texture = p_texture;
+
+ if (texture.is_valid())
+ texture->add_change_receptor(this);
+
update();
emit_signal("texture_changed");
item_rect_changed();
@@ -281,13 +289,39 @@ bool Sprite::_edit_is_selected_on_click(const Point2 &p_point, double p_toleranc
Rect2 src_rect, dst_rect;
bool filter_clip;
_get_rects(src_rect, dst_rect, filter_clip);
+ dst_rect.size = dst_rect.size.abs();
if (!dst_rect.has_point(p_point))
return false;
- Vector2 q = ((p_point - dst_rect.position) / dst_rect.size) * src_rect.size + src_rect.position;
+ Vector2 q = (p_point - dst_rect.position) / dst_rect.size;
+ if (hflip)
+ q.x = 1.0f - q.x;
+ if (vflip)
+ q.y = 1.0f - q.y;
+ q = q * src_rect.size + src_rect.position;
+
+ Ref<Image> image;
+ Ref<AtlasTexture> atlasTexture = texture;
+ if (atlasTexture.is_null()) {
+ image = texture->get_data();
+ } else {
+ ERR_FAIL_COND_V(atlasTexture->get_atlas().is_null(), false);
+
+ image = atlasTexture->get_atlas()->get_data();
+
+ Rect2 region = atlasTexture->get_region();
+ Rect2 margin = atlasTexture->get_margin();
+
+ q -= margin.position;
+
+ if ((q.x > region.size.width) || (q.y > region.size.height)) {
+ return false;
+ }
+
+ q += region.position;
+ }
- Ref<Image> image = texture->get_data();
ERR_FAIL_COND_V(image.is_null(), false);
image->lock();
@@ -336,6 +370,15 @@ void Sprite::_validate_property(PropertyInfo &property) const {
}
}
+void Sprite::_changed_callback(Object *p_changed, const char *p_prop) {
+
+ // Changes to the texture need to trigger an update to make
+ // the editor redraw the sprite with the updated texture.
+ if (texture.is_valid() && texture.ptr() == p_changed) {
+ update();
+ }
+}
+
void Sprite::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite::set_texture);
@@ -410,3 +453,8 @@ Sprite::Sprite() {
vframes = 1;
hframes = 1;
}
+
+Sprite::~Sprite() {
+ if (texture.is_valid())
+ texture->remove_change_receptor(this);
+}
diff --git a/scene/2d/sprite.h b/scene/2d/sprite.h
index dd3719099f..abd04515ec 100644
--- a/scene/2d/sprite.h
+++ b/scene/2d/sprite.h
@@ -64,6 +64,8 @@ protected:
virtual void _validate_property(PropertyInfo &property) const;
+ virtual void _changed_callback(Object *p_changed, const char *p_prop);
+
public:
virtual Dictionary _edit_get_state() const;
virtual void _edit_set_state(const Dictionary &p_state);
@@ -113,6 +115,7 @@ public:
Rect2 get_rect() const;
Sprite();
+ ~Sprite();
};
#endif // SPRITE_H
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp
index 2aa55e2825..b602839b99 100644
--- a/scene/2d/tile_map.cpp
+++ b/scene/2d/tile_map.cpp
@@ -142,16 +142,20 @@ void TileMap::_update_quadrant_transform() {
void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
- if (tile_set.is_valid())
+ if (tile_set.is_valid()) {
tile_set->disconnect("changed", this, "_recreate_quadrants");
+ tile_set->remove_change_receptor(this);
+ }
_clear_quadrants();
tile_set = p_tileset;
- if (tile_set.is_valid())
+ if (tile_set.is_valid()) {
tile_set->connect("changed", this, "_recreate_quadrants");
- else
+ tile_set->add_change_receptor(this);
+ } else {
clear();
+ }
_recreate_quadrants();
emit_signal("settings_changed");
@@ -830,6 +834,16 @@ void TileMap::update_dirty_bitmask() {
}
}
+void TileMap::fix_invalid_tiles() {
+
+ for (Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) {
+
+ if (!tile_set->has_tile(get_cell(E->key().x, E->key().y))) {
+ set_cell(E->key().x, E->key().y, INVALID_CELL);
+ }
+ }
+}
+
int TileMap::get_cell(int p_x, int p_y) const {
PosKey pk(p_x, p_y);
@@ -1515,6 +1529,7 @@ void TileMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped);
ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed);
+ ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells);
@@ -1573,6 +1588,12 @@ void TileMap::_bind_methods() {
BIND_ENUM_CONSTANT(TILE_ORIGIN_BOTTOM_LEFT);
}
+void TileMap::_changed_callback(Object *p_changed, const char *p_prop) {
+ if (tile_set.is_valid() && tile_set.ptr() == p_changed) {
+ emit_signal("settings_changed");
+ }
+}
+
TileMap::TileMap() {
rect_cache_dirty = true;
@@ -1601,5 +1622,8 @@ TileMap::TileMap() {
TileMap::~TileMap() {
+ if (tile_set.is_valid())
+ tile_set->remove_change_receptor(this);
+
clear();
}
diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h
index 973e527b42..587bd3b684 100644
--- a/scene/2d/tile_map.h
+++ b/scene/2d/tile_map.h
@@ -217,6 +217,8 @@ protected:
void _notification(int p_what);
static void _bind_methods();
+ virtual void _changed_callback(Object *p_changed, const char *p_prop);
+
public:
enum {
INVALID_CELL = -1
@@ -308,6 +310,7 @@ public:
void set_clip_uv(bool p_enable);
bool get_clip_uv() const;
+ void fix_invalid_tiles();
void clear();
TileMap();