Browse Source

added negative X and negative Y offset to TileMap

clang-format

added negative X and negative Y offset to TileMap
Aaron Winter 6 years ago
parent
commit
63e0fd7675
4 changed files with 37 additions and 11 deletions
  1. 6 0
      doc/classes/TileMap.xml
  2. 10 6
      editor/plugins/tile_map_editor_plugin.cpp
  3. 19 5
      scene/2d/tile_map.cpp
  4. 2 0
      scene/2d/tile_map.h

+ 6 - 0
doc/classes/TileMap.xml

@@ -323,6 +323,12 @@
 		<constant name="HALF_OFFSET_DISABLED" value="2" enum="HalfOffset">
 			Half offset disabled.
 		</constant>
+		<constant name="HALF_OFFSET_NEGATIVE_X" value="3" enum="HalfOffset">
+			Half offset on the X coordinate (negative).
+		</constant>
+		<constant name="HALF_OFFSET_NEGATIVE_Y" value="4" enum="HalfOffset">
+			Half offset on the Y coordinate (negative).
+		</constant>
 		<constant name="TILE_ORIGIN_TOP_LEFT" value="0" enum="TileOrigin">
 			Tile origin at its top-left corner.
 		</constant>

+ 10 - 6
editor/plugins/tile_map_editor_plugin.cpp

@@ -1434,9 +1434,9 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 		aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size)));
 		Rect2i si = aabb.grow(1.0);
 
-		if (node->get_half_offset() != TileMap::HALF_OFFSET_X) {
+		if (node->get_half_offset() != TileMap::HALF_OFFSET_X && node->get_half_offset() != TileMap::HALF_OFFSET_NEGATIVE_X) {
 
-			int max_lines = 2000; //avoid crash if size too smal
+			int max_lines = 2000; //avoid crash if size too small
 
 			for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
 
@@ -1450,7 +1450,7 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 			}
 		} else {
 
-			int max_lines = 10000; //avoid crash if size too smal
+			int max_lines = 10000; //avoid crash if size too small
 
 			for (int i = (si.position.x) - 1; i <= (si.position.x + si.size.x); i++) {
 
@@ -1458,7 +1458,7 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 
 					Vector2 ofs;
 					if (ABS(j) & 1) {
-						ofs = cell_xf[0] * 0.5;
+						ofs = cell_xf[0] * (node->get_half_offset() == TileMap::HALF_OFFSET_X ? 0.5 : -0.5);
 					}
 
 					Vector2 from = xform.xform(node->map_to_world(Vector2(i, j), true) + ofs);
@@ -1474,7 +1474,7 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 
 		int max_lines = 10000; //avoid crash if size too smal
 
-		if (node->get_half_offset() != TileMap::HALF_OFFSET_Y) {
+		if (node->get_half_offset() != TileMap::HALF_OFFSET_Y && node->get_half_offset() != TileMap::HALF_OFFSET_NEGATIVE_Y) {
 
 			for (int i = (si.position.y) - 1; i <= (si.position.y + si.size.y); i++) {
 
@@ -1495,7 +1495,7 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 
 					Vector2 ofs;
 					if (ABS(j) & 1) {
-						ofs = cell_xf[1] * 0.5;
+						ofs = cell_xf[1] * (node->get_half_offset() == TileMap::HALF_OFFSET_Y ? 0.5 : -0.5);
 					}
 
 					Vector2 from = xform.xform(node->map_to_world(Vector2(j, i), true) + ofs);
@@ -1533,8 +1533,12 @@ void TileMapEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
 		for (int i = 0; i < 4; i++) {
 			if (node->get_half_offset() == TileMap::HALF_OFFSET_X && ABS(over_tile.y) & 1)
 				endpoints[i] += cell_xf[0] * 0.5;
+			if (node->get_half_offset() == TileMap::HALF_OFFSET_NEGATIVE_X && ABS(over_tile.y) & 1)
+				endpoints[i] += cell_xf[0] * -0.5;
 			if (node->get_half_offset() == TileMap::HALF_OFFSET_Y && ABS(over_tile.x) & 1)
 				endpoints[i] += cell_xf[1] * 0.5;
+			if (node->get_half_offset() == TileMap::HALF_OFFSET_NEGATIVE_Y && ABS(over_tile.x) & 1)
+				endpoints[i] += cell_xf[1] * -0.5;
 			endpoints[i] = xform.xform(endpoints[i]);
 		}
 		Color col;

+ 19 - 5
scene/2d/tile_map.cpp

@@ -1391,15 +1391,17 @@ Vector2 TileMap::_map_to_world(int p_x, int p_y, bool p_ignore_ofs) const {
 	if (!p_ignore_ofs) {
 		switch (half_offset) {
 
-			case HALF_OFFSET_X: {
+			case HALF_OFFSET_X:
+			case HALF_OFFSET_NEGATIVE_X: {
 				if (ABS(p_y) & 1) {
 
-					ret += get_cell_transform()[0] * 0.5;
+					ret += get_cell_transform()[0] * (half_offset == HALF_OFFSET_X ? 0.5 : -0.5);
 				}
 			} break;
-			case HALF_OFFSET_Y: {
+			case HALF_OFFSET_Y:
+			case HALF_OFFSET_NEGATIVE_Y: {
 				if (ABS(p_x) & 1) {
-					ret += get_cell_transform()[1] * 0.5;
+					ret += get_cell_transform()[1] * (half_offset == HALF_OFFSET_Y ? 0.5 : -0.5);
 				}
 			} break;
 			default: {}
@@ -1462,11 +1464,21 @@ Vector2 TileMap::world_to_map(const Vector2 &p_pos) const {
 				ret.x -= 0.5;
 			}
 		} break;
+		case HALF_OFFSET_NEGATIVE_X: {
+			if (ret.y > 0 ? int(ret.y) & 1 : (int(ret.y) - 1) & 1) {
+				ret.x += 0.5;
+			}
+		} break;
 		case HALF_OFFSET_Y: {
 			if (ret.x > 0 ? int(ret.x) & 1 : (int(ret.x) - 1) & 1) {
 				ret.y -= 0.5;
 			}
 		} break;
+		case HALF_OFFSET_NEGATIVE_Y: {
+			if (ret.x > 0 ? int(ret.x) & 1 : (int(ret.x) - 1) & 1) {
+				ret.y += 0.5;
+			}
+		} break;
 		default: {}
 	}
 
@@ -1677,7 +1689,7 @@ void TileMap::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size", PROPERTY_HINT_RANGE, "1,8192,1"), "set_cell_size", "get_cell_size");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
 	ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "cell_custom_transform"), "set_custom_transform", "get_custom_transform");
-	ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled"), "set_half_offset", "get_half_offset");
+	ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_half_offset", PROPERTY_HINT_ENUM, "Offset X,Offset Y,Disabled,Offset Negative X,Offset Negative Y"), "set_half_offset", "get_half_offset");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_tile_origin", PROPERTY_HINT_ENUM, "Top Left,Center,Bottom Left"), "set_tile_origin", "get_tile_origin");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_y_sort"), "set_y_sort_mode", "is_y_sort_mode_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_clip_uv"), "set_clip_uv", "get_clip_uv");
@@ -1703,6 +1715,8 @@ void TileMap::_bind_methods() {
 	BIND_ENUM_CONSTANT(HALF_OFFSET_X);
 	BIND_ENUM_CONSTANT(HALF_OFFSET_Y);
 	BIND_ENUM_CONSTANT(HALF_OFFSET_DISABLED);
+	BIND_ENUM_CONSTANT(HALF_OFFSET_NEGATIVE_X);
+	BIND_ENUM_CONSTANT(HALF_OFFSET_NEGATIVE_Y);
 
 	BIND_ENUM_CONSTANT(TILE_ORIGIN_TOP_LEFT);
 	BIND_ENUM_CONSTANT(TILE_ORIGIN_CENTER);

+ 2 - 0
scene/2d/tile_map.h

@@ -52,6 +52,8 @@ public:
 		HALF_OFFSET_X,
 		HALF_OFFSET_Y,
 		HALF_OFFSET_DISABLED,
+		HALF_OFFSET_NEGATIVE_X,
+		HALF_OFFSET_NEGATIVE_Y,
 	};
 
 	enum TileOrigin {