Преглед на файлове

Merge pull request #3204 from neikeq/fix_3153

Consider texture offset when rotating tiles
Rémi Verschelde преди 9 години
родител
ревизия
3c6dd5749d
променени са 2 файла, в които са добавени 56 реда и са изтрити 12 реда
  1. 25 2
      scene/2d/tile_map.cpp
  2. 31 10
      tools/editor/plugins/tile_map_editor_plugin.cpp

+ 25 - 2
scene/2d/tile_map.cpp

@@ -223,6 +223,14 @@ void TileMap::_fix_cell_transform(Matrix32& xform,const Cell& p_cell, const Vect
 	Size2 s=p_sc;
 	Vector2 offset = p_offset;
 
+	if (s.y > s.x) {
+		if ((p_cell.flip_h && (p_cell.flip_v || p_cell.transpose)) || (p_cell.flip_v && !p_cell.transpose))
+			offset.y += s.y - s.x;
+	} else if (s.y < s.x) {
+		if ((p_cell.flip_v && (p_cell.flip_h || p_cell.transpose)) || (p_cell.flip_h && !p_cell.transpose))
+			offset.x += s.x - s.y;
+	}
+
 	if (p_cell.transpose) {
 		SWAP(xform.elements[0].x, xform.elements[0].y);
 		SWAP(xform.elements[1].x, xform.elements[1].y);
@@ -376,13 +384,28 @@ void TileMap::_update_dirty_quadrants() {
 			rect.pos=offset.floor();
 			rect.size=s;
 
+			if (rect.size.y > rect.size.x) {
+				if ((c.flip_h && (c.flip_v || c.transpose)) || (c.flip_v && !c.transpose))
+					tile_ofs.y += rect.size.y - rect.size.x;
+			} else if (rect.size.y < rect.size.x) {
+				if ((c.flip_v && (c.flip_h || c.transpose)) || (c.flip_h && !c.transpose))
+					tile_ofs.x += rect.size.x - rect.size.y;
+			}
+
 		/*	rect.size.x+=fp_adjust;
 			rect.size.y+=fp_adjust;*/
 
-			if (c.flip_h)
+			if (c.transpose)
+				SWAP(tile_ofs.x, tile_ofs.y);
+
+			if (c.flip_h) {
 				rect.size.x=-rect.size.x;
-			if (c.flip_v)
+				tile_ofs.x=-tile_ofs.x;
+			}
+			if (c.flip_v) {
 				rect.size.y=-rect.size.y;
+				tile_ofs.y=-tile_ofs.y;
+			}
 
 			Vector2 center_ofs;
 

+ 31 - 10
tools/editor/plugins/tile_map_editor_plugin.cpp

@@ -676,12 +676,9 @@ void TileMapEditor::_canvas_draw() {
 					Ref<Texture> t = ts->tile_get_texture(st);
 					if (t.is_valid()) {
 						Vector2 from = node->map_to_world(over_tile)+node->get_cell_draw_offset();
+						Vector2 tile_ofs = ts->tile_get_texture_offset(st);
 						Rect2 r = ts->tile_get_region(st);
 						Size2 sc = xform.get_scale();
-						if (mirror_x->is_pressed())
-							sc.x*=-1.0;
-						if (mirror_y->is_pressed())
-							sc.y*=-1.0;
 
 						Rect2 rect;
 						if (r==Rect2()) {
@@ -691,23 +688,47 @@ void TileMapEditor::_canvas_draw() {
 							rect=Rect2(from,r.get_size());
 						}
 
+						bool transp = transpose->is_pressed();
+						bool flip_h = mirror_x->is_pressed();
+						bool flip_v = mirror_y->is_pressed();
+
+						if (rect.size.y > rect.size.x) {
+							if ((flip_h && (flip_v || transp)) || (flip_v && !transp))
+								tile_ofs.y += rect.size.y - rect.size.x;
+						} else if (rect.size.y < rect.size.x) {
+							if ((flip_v && (flip_h || transp)) || (flip_h && !transp))
+								tile_ofs.x += rect.size.x - rect.size.y;
+						}
+
+						if (transp) {
+							SWAP(tile_ofs.x, tile_ofs.y);
+						}
+
+						if (flip_h) {
+							sc.x*=-1.0;
+							tile_ofs.x*=-1.0;
+						}
+						if (flip_v) {
+							sc.y*=-1.0;
+							tile_ofs.y*=-1.0;
+						}
 
 						if (node->get_tile_origin()==TileMap::TILE_ORIGIN_TOP_LEFT) {
-							rect.pos+=ts->tile_get_texture_offset(st);
+							rect.pos+=tile_ofs;
 
 						} else if (node->get_tile_origin()==TileMap::TILE_ORIGIN_CENTER) {
 							rect.pos+=node->get_cell_size()/2;
 							Vector2 s = r.size;
 
-							Vector2 center = (s/2) - ts->tile_get_texture_offset(st);
+							Vector2 center = (s/2) - tile_ofs;
 
 
-							if (mirror_x->is_pressed())
+							if (flip_h)
 								rect.pos.x-=s.x-center.x;
 							else
 								rect.pos.x-=center.x;
 
-							if (mirror_y->is_pressed())
+							if (flip_v)
 								rect.pos.y-=s.y-center.y;
 							else
 								rect.pos.y-=center.y;
@@ -718,10 +739,10 @@ void TileMapEditor::_canvas_draw() {
 
 						if (r==Rect2()) {
 
-							canvas_item_editor->draw_texture_rect(t,rect,false,Color(1,1,1,0.5),transpose->is_pressed());
+							canvas_item_editor->draw_texture_rect(t,rect,false,Color(1,1,1,0.5),transp);
 						} else {
 
-							canvas_item_editor->draw_texture_rect_region(t,rect,r,Color(1,1,1,0.5),transpose->is_pressed());
+							canvas_item_editor->draw_texture_rect_region(t,rect,r,Color(1,1,1,0.5),transp);
 						}
 					}
 				}