diff --git a/addons/rmsmartshape/actions/action_delete_point.gd b/addons/rmsmartshape/actions/action_delete_point.gd index 2375647..ad63e85 100644 --- a/addons/rmsmartshape/actions/action_delete_point.gd +++ b/addons/rmsmartshape/actions/action_delete_point.gd @@ -4,7 +4,7 @@ extends "res://addons/rmsmartshape/actions/action_delete_points.gd" func _init(shape: SS2D_Shape, key: int, commit_update: bool = true) -> void: - var keys: PackedInt64Array = [key] + var keys: PackedInt32Array = [key] super(shape, keys, commit_update) diff --git a/addons/rmsmartshape/actions/action_delete_points.gd b/addons/rmsmartshape/actions/action_delete_points.gd index 27c5c03..60ec5ba 100644 --- a/addons/rmsmartshape/actions/action_delete_points.gd +++ b/addons/rmsmartshape/actions/action_delete_points.gd @@ -11,8 +11,8 @@ const ActionCloseShape := preload("res://addons/rmsmartshape/actions/action_clos var _close_shape: ActionCloseShape var _shape: SS2D_Shape -var _keys: PackedInt64Array -var _indicies: PackedInt64Array +var _keys: PackedInt32Array +var _indicies: PackedInt32Array var _positions: PackedVector2Array var _points_in: PackedVector2Array var _points_out: PackedVector2Array @@ -22,7 +22,7 @@ var _was_closed: bool var _commit_update: bool -func _init(shape: SS2D_Shape, keys: PackedInt64Array, commit_update: bool = true) -> void: +func _init(shape: SS2D_Shape, keys: PackedInt32Array, commit_update: bool = true) -> void: _shape = shape _invert_orientation = ActionInvertOrientation.new(shape) _close_shape = ActionCloseShape.new(shape) diff --git a/addons/rmsmartshape/actions/action_move_control_points.gd b/addons/rmsmartshape/actions/action_move_control_points.gd index 0b94ce4..5af9e67 100644 --- a/addons/rmsmartshape/actions/action_move_control_points.gd +++ b/addons/rmsmartshape/actions/action_move_control_points.gd @@ -3,14 +3,14 @@ extends SS2D_Action ## ActionMoveControlPoints var _shape: SS2D_Shape -var _keys: PackedInt64Array +var _keys: PackedInt32Array var _old_points_in: PackedVector2Array var _old_points_out: PackedVector2Array var _new_points_in: PackedVector2Array var _new_points_out: PackedVector2Array -func _init(s: SS2D_Shape, keys: PackedInt64Array, +func _init(s: SS2D_Shape, keys: PackedInt32Array, old_points_in: PackedVector2Array, old_points_out: PackedVector2Array) -> void: _shape = s _keys = keys @@ -33,7 +33,7 @@ func undo() -> void: _assign_points_in_out(_keys, _old_points_in, _old_points_out) -func _assign_points_in_out(keys: PackedInt64Array, in_positions: PackedVector2Array, out_positions: PackedVector2Array) -> void: +func _assign_points_in_out(keys: PackedInt32Array, in_positions: PackedVector2Array, out_positions: PackedVector2Array) -> void: _shape.begin_update() for i in keys.size(): if _shape.get_point_in(keys[i]) != in_positions[i]: diff --git a/addons/rmsmartshape/actions/action_move_verticies.gd b/addons/rmsmartshape/actions/action_move_verticies.gd index 9076647..f8d4a47 100644 --- a/addons/rmsmartshape/actions/action_move_verticies.gd +++ b/addons/rmsmartshape/actions/action_move_verticies.gd @@ -6,12 +6,12 @@ const ActionInvertOrientation := preload("res://addons/rmsmartshape/actions/acti var _invert_orientation: ActionInvertOrientation var _shape: SS2D_Shape -var _keys: PackedInt64Array +var _keys: PackedInt32Array var _old_positions: PackedVector2Array var _new_positions: PackedVector2Array -func _init(s: SS2D_Shape, keys: PackedInt64Array, old_positions: PackedVector2Array) -> void: +func _init(s: SS2D_Shape, keys: PackedInt32Array, old_positions: PackedVector2Array) -> void: _shape = s _keys = keys.duplicate() _old_positions = old_positions.duplicate() diff --git a/addons/rmsmartshape/actions/action_split_shape.gd b/addons/rmsmartshape/actions/action_split_shape.gd index bfd7f00..8fb2a28 100644 --- a/addons/rmsmartshape/actions/action_split_shape.gd +++ b/addons/rmsmartshape/actions/action_split_shape.gd @@ -50,7 +50,7 @@ func do() -> void: _splitted.collision_polygon_node_path = _splitted.get_path_to(_splitted_collision) if _delete_points_from_original == null: - var delete_keys := PackedInt64Array() + var delete_keys := PackedInt32Array() for i in range(_shape.get_point_count() - 1, _split_idx, -1): delete_keys.append(_shape.get_point_key_at_index(i)) _delete_points_from_original = ActionDeletePoints.new(_shape, delete_keys) diff --git a/addons/rmsmartshape/lib/tuple.gd b/addons/rmsmartshape/lib/tuple.gd index ca27c3e..92ab7b6 100644 --- a/addons/rmsmartshape/lib/tuple.gd +++ b/addons/rmsmartshape/lib/tuple.gd @@ -46,8 +46,8 @@ static func array_has(tuple_array: Array[Vector2i], t: Vector2i) -> bool: ## Returns a list indices to tuples that contain the given value. -static func array_find_partial(tuple_array: Array[Vector2i], value: int) -> Array[int]: - var out: Array[int] = [] +static func array_find_partial(tuple_array: Array[Vector2i], value: int) -> PackedInt32Array: + var out := PackedInt32Array() for i in tuple_array.size(): if tuple_array[i].x == value or tuple_array[i].y == value: @@ -56,13 +56,22 @@ static func array_find_partial(tuple_array: Array[Vector2i], value: int) -> Arra return out -## Returns a tuple with elements in ascending order. +## Transform the tuple into a normalized representation (elements in ascending order). +## Same as sort_ascending() at the moment. +## Useful in more optimized use-cases where certain assumptions can be made if all tuples share a +## normalized representation. static func normalize_tuple(tuple: Vector2i) -> Vector2i: + return sort_ascending(tuple) + + +## Returns a tuple with elements in ascending order. +static func sort_ascending(tuple: Vector2i) -> Vector2i: if tuple.x <= tuple.y: return tuple return flip_elements(tuple) + ## Returns a tuple with x and y components switched. static func flip_elements(tuple: Vector2i) -> Vector2i: return Vector2i(tuple.y, tuple.x) diff --git a/addons/rmsmartshape/plugin.gd b/addons/rmsmartshape/plugin.gd index b002537..69208e1 100644 --- a/addons/rmsmartshape/plugin.gd +++ b/addons/rmsmartshape/plugin.gd @@ -64,18 +64,18 @@ class ActionDataVert: #Type of Action from the ACTION_VERT enum var type: ACTION_VERT = ACTION_VERT.NONE # The affected Verticies and their initial positions - var keys: Array[int] = [] - var starting_width: Array[float] = [] + var keys: PackedInt32Array + var starting_width: PackedFloat32Array var starting_positions: PackedVector2Array = [] var starting_positions_control_in: PackedVector2Array = [] var starting_positions_control_out: PackedVector2Array = [] func _init( - _keys: Array[int], + _keys: PackedInt32Array, positions: PackedVector2Array, positions_in: PackedVector2Array, positions_out: PackedVector2Array, - width: Array[float], + width: PackedFloat32Array, t: ACTION_VERT ) -> void: type = t @@ -89,13 +89,10 @@ class ActionDataVert: return keys.size() > 0 func _to_string() -> String: - var s := "%s: %s = %s" - return s % [type, keys, starting_positions] + return "%s: %s = %s" % [type, keys, starting_positions] func is_single_vert_selected() -> bool: - if keys.size() == 1: - return true - return false + return keys.size() == 1 func current_point_key() -> int: if not is_single_vert_selected(): @@ -960,11 +957,11 @@ func deselect_verts() -> void: current_action = ActionDataVert.new([], [], [], [], [], ACTION_VERT.NONE) -func select_verticies(keys: Array[int], action: ACTION_VERT) -> ActionDataVert: +func select_verticies(keys: PackedInt32Array, action: ACTION_VERT) -> ActionDataVert: var from_positions := PackedVector2Array() var from_positions_c_in := PackedVector2Array() var from_positions_c_out := PackedVector2Array() - var from_widths: Array[float] = [] + var from_widths := PackedFloat32Array() for key in keys: from_positions.push_back(shape.get_point_position(key)) from_positions_c_in.push_back(shape.get_point_in(key)) @@ -975,19 +972,19 @@ func select_verticies(keys: Array[int], action: ACTION_VERT) -> ActionDataVert: ) -func select_vertices_to_move(keys: Array[int], _mouse_starting_pos_viewport: Vector2) -> void: +func select_vertices_to_move(keys: PackedInt32Array, _mouse_starting_pos_viewport: Vector2) -> void: _mouse_motion_delta_starting_pos = _mouse_starting_pos_viewport current_action = select_verticies(keys, ACTION_VERT.MOVE_VERT) func select_control_points_to_move( - keys: Array[int], _mouse_starting_pos_viewport: Vector2, action: ACTION_VERT = ACTION_VERT.MOVE_CONTROL + keys: PackedInt32Array, _mouse_starting_pos_viewport: Vector2, action: ACTION_VERT = ACTION_VERT.MOVE_CONTROL ) -> void: current_action = select_verticies(keys, action) _mouse_motion_delta_starting_pos = _mouse_starting_pos_viewport -func select_width_handle_to_move(keys: Array[int], _mouse_starting_pos_viewport: Vector2) -> void: +func select_width_handle_to_move(keys: PackedInt32Array, _mouse_starting_pos_viewport: Vector2) -> void: _mouse_motion_delta_starting_pos = _mouse_starting_pos_viewport current_action = select_verticies(keys, ACTION_VERT.MOVE_WIDTH_HANDLE) @@ -1324,10 +1321,10 @@ func _input_split_edge(mb: InputEventMouseButton, vp_m_pos: Vector2, t: Transfor func _input_move_control_points(mb: InputEventMouseButton, vp_m_pos: Vector2, grab_threshold: float) -> bool: - var points_in: Array[int] = FUNC.get_intersecting_control_point_in( + var points_in := SS2D_PluginFunctionality.get_intersecting_control_point_in( shape, get_et(), mb.position, grab_threshold ) - var points_out: Array[int] = FUNC.get_intersecting_control_point_out( + var points_out := SS2D_PluginFunctionality.get_intersecting_control_point_out( shape, get_et(), mb.position, grab_threshold ) if not points_in.is_empty(): diff --git a/addons/rmsmartshape/plugin_functionality.gd b/addons/rmsmartshape/plugin_functionality.gd index 6874b6b..3d12e53 100644 --- a/addons/rmsmartshape/plugin_functionality.gd +++ b/addons/rmsmartshape/plugin_functionality.gd @@ -13,20 +13,20 @@ extends RefCounted static func get_intersecting_control_point_in( s: SS2D_Shape, et: Transform2D, mouse_pos: Vector2, grab_threshold: float -) -> Array[int]: +) -> PackedInt32Array: return _get_intersecting_control_point(s, et, mouse_pos, grab_threshold, true) static func get_intersecting_control_point_out( s: SS2D_Shape, et: Transform2D, mouse_pos: Vector2, grab_threshold: float -) -> Array[int]: +) -> PackedInt32Array: return _get_intersecting_control_point(s, et, mouse_pos, grab_threshold, false) static func _get_intersecting_control_point( s: SS2D_Shape, et: Transform2D, mouse_pos: Vector2, grab_threshold: float, _in: bool -) -> Array[int]: - var points: Array[int] = [] +) -> PackedInt32Array: + var points := PackedInt32Array() var xform: Transform2D = et * s.get_global_transform() for i in s.get_point_count(): var key: int = s.get_point_key_at_index(i) diff --git a/addons/rmsmartshape/shapes/index_map.gd b/addons/rmsmartshape/shapes/index_map.gd index b1a7c3a..31f7eda 100644 --- a/addons/rmsmartshape/shapes/index_map.gd +++ b/addons/rmsmartshape/shapes/index_map.gd @@ -171,7 +171,7 @@ func _split_indicies_into_multiple_mappings(new_indicies: PackedInt32Array) -> A ## ## This may split the IndexMap or make it invalid entirely. ## As a result, the returned array could have 0 or several IndexMaps. -func remove_indicies(to_remove: Array[int]) -> Array[SS2D_IndexMap]: +func remove_indicies(to_remove: PackedInt32Array) -> Array[SS2D_IndexMap]: var out: Array[SS2D_IndexMap] = [] var new_indicies := indicies.duplicate() @@ -202,7 +202,7 @@ func remove_indicies(to_remove: Array[int]) -> Array[SS2D_IndexMap]: ## ## This may split the IndexMap or make it invalid entirely. ## As a result, the returned array could have 0 or several IndexMaps. -func remove_edges(to_remove: Array[int]) -> Array[SS2D_IndexMap]: +func remove_edges(to_remove: PackedInt32Array) -> Array[SS2D_IndexMap]: # Corner case if to_remove.size() == 2: var idx: int = indicies.find(to_remove[0]) @@ -224,7 +224,7 @@ func remove_edges(to_remove: Array[int]) -> Array[SS2D_IndexMap]: for i in range(0, to_remove.size() - 1, 1): var idx1: int = to_remove[i] var idx2: int = to_remove[i + 1] - var edges_to_remove: Array[int] = [] + var edges_to_remove := PackedInt32Array() for ii in new_edges.size(): var edge := new_edges[ii] if (edge[0] == idx1 or edge[0] == idx2) and (edge[1] == idx1 or edge[1] == idx2): diff --git a/addons/rmsmartshape/shapes/point_array.gd b/addons/rmsmartshape/shapes/point_array.gd index 4ab396a..81b32e6 100644 --- a/addons/rmsmartshape/shapes/point_array.gd +++ b/addons/rmsmartshape/shapes/point_array.gd @@ -7,7 +7,7 @@ enum CONSTRAINT { NONE = 0, AXIS_X = 1, AXIS_Y = 2, CONTROL_POINTS = 4, PROPERTI # Maps a key to each point: Dict[int, SS2D_Point] @export var _points: Dictionary = {} : set = _set_points # Contains all keys; the order of the keys determines the order of the points -@export var _point_order: Array[int] = [] : set = set_point_order +@export var _point_order := PackedInt32Array() : set = set_point_order ## Dict[Vector2i, CONSTRAINT] ## Key is tuple of point_keys; Value is the CONSTRAINT enum. @@ -34,7 +34,7 @@ var tessellation_tolerance: float = 4.0 : set = set_tessellation_tolerance var _constraints_enabled: bool = true var _updating_constraints := false -var _keys_to_update_constraints: Array[int] = [] +var _keys_to_update_constraints := PackedInt32Array() var _changed_during_update := false var _updating := false @@ -64,7 +64,6 @@ signal material_override_changed(tuple: Vector2i) func _init() -> void: # Required by Godot to correctly make unique instances of this resource _points = {} - _point_order = [] _constraints = {} _next_key = 0 # Assigning an empty dict to _material_overrides this way @@ -87,7 +86,7 @@ func clone(deep: bool = false) -> SS2D_Point_Array: for k: int in _points: new_point_dict[k] = get_point(k).duplicate(true) copy._points = new_point_dict - copy._point_order = _point_order.duplicate(true) + copy._point_order = _point_order.duplicate() copy._constraints = _constraints.duplicate() copy._material_overrides = _material_overrides.duplicate() else: @@ -107,7 +106,7 @@ func _set_points(ps: Dictionary) -> void: _changed() -func set_point_order(po: Array[int]) -> void: +func set_point_order(po: PackedInt32Array) -> void: _point_order = po _changed() @@ -146,8 +145,8 @@ func is_key_valid(k: int) -> bool: return k >= 0 and not _points.has(k) -## Add a point.[br] -## Returns key of the added point.[br] +## Add a point and insert it at the given index or at the end by default. +## Returns the key of the added point. func add_point(point: Vector2, idx: int = -1, use_key: int = -1) -> int: # print("Add Point :: ", point, " | idx: ", idx, " | key: ", use_key, " |") if use_key == -1 or not is_key_valid(use_key): @@ -265,7 +264,7 @@ func has_point(key: int) -> bool: return _points.has(key) -func get_all_point_keys() -> Array[int]: +func get_all_point_keys() -> PackedInt32Array: # _point_order should contain every single point ONLY ONCE return _point_order @@ -444,7 +443,7 @@ func update_constraints(src: int) -> void: # Subsequent required passes of updating constraints while not _keys_to_update_constraints.is_empty(): var key_set := _keys_to_update_constraints - _keys_to_update_constraints = [] + _keys_to_update_constraints = PackedInt32Array() for k in key_set: _update_constraints(k) @@ -469,11 +468,12 @@ func get_point_constraints_tuples(key1: int) -> Array[Vector2i]: return SS2D_IndexTuple.dict_find_partial(_constraints, key1) -## Returns the constraint for a pair of keys. +## Returns the constraint for a pair of keys or CONSTRAINT.NONE if no constraint exists. func get_point_constraint(key1: int, key2: int) -> CONSTRAINT: return SS2D_IndexTuple.dict_get(_constraints, Vector2i(key1, key2), CONSTRAINT.NONE) +## Set a constraint between two points. If the constraint is NONE, remove_constraint() is called instead. func set_constraint(key1: int, key2: int, constraint: CONSTRAINT) -> void: var t := Vector2i(key1, key2) @@ -569,11 +569,11 @@ func _unhook_mat(mat: SS2D_Material_Edge_Metadata) -> void: mat.changed.disconnect(_on_material_override_changed) -## Returns a dictionary of specific materials to use for specific tuples of points.[br] -## Key is tuple of two point keys, value is material.[br] -## => Dict[Vector2i, SS2D_Material_Edge_Metadata] -func get_material_overrides() -> Dictionary: - return _material_overrides +## Returns a list of index tuples for wich material overrides exist. +func get_material_overrides() -> Array[Vector2i]: + var keys: Array[Vector2i] = [] + keys.assign(_material_overrides.keys()) + return keys func clear_all_material_overrides() -> void: diff --git a/addons/rmsmartshape/shapes/shape.gd b/addons/rmsmartshape/shapes/shape.gd index 7ab44c4..7362716 100644 --- a/addons/rmsmartshape/shapes/shape.gd +++ b/addons/rmsmartshape/shapes/shape.gd @@ -338,9 +338,9 @@ func adjust_add_point_index(index: int) -> int: # FIXME: Only unit tests use this. -func add_points(verts: PackedVector2Array, starting_index: int = -1, key: int = -1) -> Array[int]: +func add_points(verts: PackedVector2Array, starting_index: int = -1, key: int = -1) -> PackedInt32Array: starting_index = adjust_add_point_index(starting_index) - var keys: Array[int] = [] + var keys := PackedInt32Array() _points.begin_update() for i in range(0, verts.size(), 1): var v: Vector2 = verts[i] @@ -511,7 +511,7 @@ func has_point(key: int) -> bool: ## Deprecated. Use respective function in get_point_array() instead. ## @deprecated -func get_all_point_keys() -> Array[int]: +func get_all_point_keys() -> PackedInt32Array: return _points.get_all_point_keys() @@ -1315,12 +1315,10 @@ static func get_meta_material_index_mapping_for_overrides( _s_material: SS2D_Material_Shape, pa: SS2D_Point_Array ) -> Array[SS2D_IndexMap]: var mappings: Array[SS2D_IndexMap] = [] - var overrides := pa.get_material_overrides() - for key_tuple: Vector2i in overrides: - var indicies: Array[int] = [pa.get_point_index(key_tuple.x), pa.get_point_index(key_tuple.y)] - indicies = sort_by_int_ascending(indicies) + for key_tuple in pa.get_material_overrides(): + var indices := SS2D_IndexTuple.sort_ascending(Vector2i(pa.get_point_index(key_tuple.x), pa.get_point_index(key_tuple.y))) var m: SS2D_Material_Edge_Metadata = pa.get_material_override(key_tuple) - var new_mapping := SS2D_IndexMap.new(PackedInt32Array(indicies), m) + var new_mapping := SS2D_IndexMap.new(PackedInt32Array([ indices.x, indices.y ]), m) mappings.push_back(new_mapping) return mappings diff --git a/tests/unit/test_edge.gd b/tests/unit/test_edge.gd index 2e94c6e..814451f 100644 --- a/tests/unit/test_edge.gd +++ b/tests/unit/test_edge.gd @@ -4,7 +4,7 @@ var TEST_TEXTURE: Texture2D = preload("res://tests/unit/test.png") func test_consecutive_quads() -> void: - var big_exclude: Array[int] = [2, 8, 34, 56, 78, 99, 123, 154, 198, 234] + var big_exclude: PackedInt32Array = [2, 8, 34, 56, 78, 99, 123, 154, 198, 234] assert_eq(SS2D_Edge.get_consecutive_quads_for_mesh(generate_quads(256)).size(), 1) assert_eq( SS2D_Edge.get_consecutive_quads_for_mesh(generate_quads(256, null, big_exclude)).size(), @@ -57,7 +57,7 @@ func test_generate_mesh_from_quad_sequence() -> void: func generate_quads( amnt: int, tex: Texture = null, - indicies_to_change: Array = [], + indicies_to_change: PackedInt32Array = [], extents: Vector2 = Vector2(16.0, 16.0) ) -> Array[SS2D_Quad]: var quads: Array[SS2D_Quad] = [] diff --git a/tests/unit/test_index_map.gd b/tests/unit/test_index_map.gd index 212cbd5..0c3c029 100644 --- a/tests/unit/test_index_map.gd +++ b/tests/unit/test_index_map.gd @@ -5,9 +5,9 @@ extends "res://addons/gut/test.gd" ################### func test_remove_indicies_basic() -> void: var object := "OBJECT" - var indicies: Array[int] = [0,1,2,3,4,5,6,7,8] - var to_remove: Array[int] = [2,3,4,5] - var expected := convert_segments_to_typed([[0,1],[6,7,8]]) + var indicies: PackedInt32Array = [0,1,2,3,4,5,6,7,8] + var to_remove: PackedInt32Array = [2,3,4,5] + var expected: Array[PackedInt32Array] = [[0,1],[6,7,8]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_indicies(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -21,9 +21,9 @@ func test_remove_indicies_basic() -> void: func test_remove_indicies_basic_2() -> void: var object := "OBJECT" - var indicies: Array[int] = [1,2,3,4,5,6,7,8] - var to_remove: Array[int] = [2,3,4,5] - var expected := convert_segments_to_typed([[6,7,8]]) + var indicies: PackedInt32Array = [1,2,3,4,5,6,7,8] + var to_remove: PackedInt32Array = [2,3,4,5] + var expected: Array[PackedInt32Array] = [[6,7,8]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_indicies(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -35,8 +35,8 @@ func test_remove_indicies_basic_2() -> void: func test_remove_indicies_none_remaining() -> void: var object := "OBJECT" - var indicies: Array[int] = [1,2,3,4,5,6] - var to_remove: Array[int] = [2,3,4,5] + var indicies: PackedInt32Array = [1,2,3,4,5,6] + var to_remove: PackedInt32Array = [2,3,4,5] var expected := [] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_indicies(to_remove) @@ -46,9 +46,9 @@ func test_remove_indicies_none_remaining() -> void: func test_remove_indicies_unaffected() -> void: var object := "OBJECT" - var indicies: Array[int] = [1,2,3,4,5,6] - var to_remove: Array[int] = [8,9,10] - var expected := convert_segments_to_typed([[1,2,3,4,5,6]]) + var indicies: PackedInt32Array = [1,2,3,4,5,6] + var to_remove: PackedInt32Array = [8,9,10] + var expected: Array[PackedInt32Array] = [[1,2,3,4,5,6]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_indicies(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -64,9 +64,9 @@ func test_remove_indicies_unaffected() -> void: ################ func test_remove_edges_basic() -> void: var object := "OBJECT" - var indicies: Array[int] = [0,1,2,3,4,5,6,7,8] - var to_remove: Array[int] = [2,3,4,5] - var expected := convert_segments_to_typed([[0,1,2],[5,6,7,8]]) + var indicies: PackedInt32Array = [0,1,2,3,4,5,6,7,8] + var to_remove: PackedInt32Array = [2,3,4,5] + var expected: Array[PackedInt32Array] = [[0,1,2],[5,6,7,8]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_edges(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -80,9 +80,9 @@ func test_remove_edges_basic() -> void: func test_remove_edges_basic_2() -> void: var object := "OBJECT" - var indicies: Array[int] = [0,1,2,3,4,5,6,7,8] - var to_remove: Array[int] = [3,4] - var expected := convert_segments_to_typed([[0,1,2,3],[4,5,6,7,8]]) + var indicies: PackedInt32Array = [0,1,2,3,4,5,6,7,8] + var to_remove: PackedInt32Array = [3,4] + var expected: Array[PackedInt32Array] = [[0,1,2,3],[4,5,6,7,8]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_edges(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -97,9 +97,9 @@ func test_remove_edges_basic_2() -> void: func test_remove_edges_basic_3() -> void: var object := "OBJECT" - var indicies: Array[int] = [2,3,4,5,6,7,8] - var to_remove: Array[int] = [2,3] - var expected := convert_segments_to_typed([[3,4,5,6,7,8]]) + var indicies: PackedInt32Array = [2,3,4,5,6,7,8] + var to_remove: PackedInt32Array = [2,3] + var expected: Array[PackedInt32Array] = [[3,4,5,6,7,8]] var imap := SS2D_IndexMap.new(indicies, object) var new_maps := imap.remove_edges(to_remove) assert_eq(new_maps.size(), expected.size()) @@ -131,7 +131,6 @@ func test_remove_edges_basic_3() -> void: func test_join_segments() -> void: # Test contains some points, but not all var segments: Array[PackedInt32Array] = [ [0, 1, 2, 3], [4, 5], [7, 8], [5, 6, 7], [8, 9, 10], [10, 11] ] - convert_segments_to_typed(segments) segments = SS2D_IndexMap.join_segments(segments) gut.p(segments) @@ -140,7 +139,6 @@ func test_join_segments() -> void: assert_eq(segments[1], PackedInt32Array([4,5,6,7,8,9,10,11])) segments = [[0, 1], [1,2], [2,3], [4, 5], [7, 8], [5, 6, 7], [8, 9, 10], [10, 11]] - convert_segments_to_typed(segments) segments = SS2D_IndexMap.join_segments(segments) gut.p(segments) assert_eq(segments.size(), 2) @@ -149,7 +147,6 @@ func test_join_segments() -> void: # Test wrap around segments = [[0, 1, 2,3], [4, 5], [7, 8], [5, 6, 7], [8, 9, 10], [10, 11, 0]] - convert_segments_to_typed(segments) segments = SS2D_IndexMap.join_segments(segments) gut.p(segments) assert_eq(segments.size(), 1) @@ -157,7 +154,6 @@ func test_join_segments() -> void: # Test contains all point pairs segments = [[0, 1, 2,3,4], [4, 5], [7, 8], [5, 6, 7], [8, 9, 10], [10, 11, 0]] - convert_segments_to_typed(segments) segments = SS2D_IndexMap.join_segments(segments) gut.p(segments) assert_eq(segments.size(), 1) @@ -165,7 +161,7 @@ func test_join_segments() -> void: func test_index_order() -> void: - var indicies: Array[int] = [5, 4, 2, 3, 1] + var indicies: PackedInt32Array = [5, 4, 2, 3, 1] var mm2i := new_index_map(indicies) # FIXME: Unused, remove eventually. @@ -179,13 +175,13 @@ func test_index_order() -> void: func test_contiguous() -> void: var mm2i := new_index_map() - mm2i.indicies = to_int_array([1, 2, 3, 4, 5, 6, 7, 8, 9]) + mm2i.indicies = [1, 2, 3, 4, 5, 6, 7, 8, 9] assert_true(mm2i.is_contiguous()) - mm2i.indicies = to_int_array([1, 2, 3, 4, 6, 7, 8, 9]) + mm2i.indicies = [1, 2, 3, 4, 6, 7, 8, 9] assert_false(mm2i.is_contiguous()) - mm2i.indicies = to_int_array([4, 5, 9]) + mm2i.indicies = [4, 5, 9] assert_false(mm2i.is_contiguous()) - mm2i.indicies = to_int_array([4, 8, 9]) + mm2i.indicies = [4, 8, 9] assert_false(mm2i.is_contiguous()) @@ -199,21 +195,7 @@ func test_duplicate() -> void: assert_ne(a.indicies, b.indicies) -func new_index_map(indicies: Array[int] = [1, 2, 3, 4, 5]) -> SS2D_IndexMap: +func new_index_map(indicies: PackedInt32Array = [1, 2, 3, 4, 5]) -> SS2D_IndexMap: var meta_mat := SS2D_Material_Edge_Metadata.new() var mm2i := SS2D_IndexMap.new(indicies, meta_mat) return mm2i - - -# Convert Array[ Array ] -> Array[ Array[p_type] ] -func convert_segments_to_typed(p_arr: Array) -> Array[PackedInt32Array]: - var out: Array[PackedInt32Array] = [] - for i in p_arr.size(): - out.push_back(PackedInt32Array(p_arr[i])) - return out - - -func to_int_array(p_arr: Array) -> Array[int]: - var new_arr: Array[int] = [] - new_arr.assign(p_arr) - return new_arr diff --git a/tests/unit/test_point_array.gd b/tests/unit/test_point_array.gd index 033836f..93d06c6 100644 --- a/tests/unit/test_point_array.gd +++ b/tests/unit/test_point_array.gd @@ -79,7 +79,7 @@ func test_point_constraints() -> void: assert_eq(p_array.get_point_constraints(keys[1]).size(), 1) assert_eq(p_array.get_point_constraints(keys[2]).size(), 2) assert_eq(p_array.get_point_constraints(keys[3]).size(), 2) - p_array.set_constraint(keys[1], keys[2], SS2D_Point_Array.CONSTRAINT.NONE) + p_array.remove_constraint(Vector2i(keys[1], keys[2])) assert_eq(p_array.get_point_constraints(keys[1]).size(), 0) assert_eq(p_array.get_point_constraints(keys[2]).size(), 1) assert_eq(p_array.get_point_constraints(keys[3]).size(), 2) @@ -87,6 +87,12 @@ func test_point_constraints() -> void: assert_eq(p_array.get_point_position(keys[1]), Vector2(777, 888)) assert_eq(p_array.get_point_position(keys[2]), Vector2(123, 321)) + p_array.set_constraint(keys[1], keys[2], SS2D_Point_Array.CONSTRAINT.ALL) + p_array.set_constraint(keys[1], keys[2], SS2D_Point_Array.CONSTRAINT.NONE) + assert_eq(p_array.get_point_constraints(keys[1]).size(), 0) + assert_eq(p_array.get_point_constraints(keys[2]).size(), 1) + assert_eq(p_array.get_point_constraints(keys[3]).size(), 2) + # POINT IN/OUT AND PROPERTIES assert_eq(p_array.get_point_in(keys[3]), Vector2(0, 0)) assert_eq(p_array.get_point_out(keys[3]), Vector2(0, 0)) @@ -113,6 +119,12 @@ func test_point_constraints() -> void: assert_eq(p_array.get_point_out(keys[1]), Vector2(0, 0)) assert_eq(p_array.get_point_properties(keys[1]).flip, false) + # Get constraint + assert_eq(p_array.get_point_constraint(keys[2], keys[3]), SS2D_Point_Array.CONSTRAINT.ALL) + assert_eq(p_array.get_point_constraint(keys[4], keys[5]), SS2D_Point_Array.CONSTRAINT.AXIS_X) + # Should not exist and return NONE + assert_eq(p_array.get_point_constraint(keys[1], keys[1]), SS2D_Point_Array.CONSTRAINT.NONE) + # TODO Test that material overrides are correctly handled when duplicating func test_clone() -> void: @@ -122,42 +134,26 @@ func test_clone() -> void: var other := p_array.clone(true) assert_ne(p_array, other) - p_array._constraints["dummy"] = "" - assert_ne(p_array._constraints, other._constraints) - p_array._constraints.erase("dummy") - p_array._points["dummy"] = "" - assert_ne(p_array._points, other._points) - p_array._points.erase("dummy") - - assert_eq(p_array._point_order, other._point_order) - p_array._point_order.push_back(31337) - assert_ne(p_array._point_order, other._point_order) - p_array._point_order.pop_back() - assert_eq(p_array._point_order, other._point_order) - - assert_eq(p_array._next_key, other._next_key) - assert_eq(p_array._constraints.size(), other._constraints.size()) - assert_eq(p_array._points.size(), other._points.size()) - for i in range(0, p_array._point_order.size(), 1): - var key1 := p_array._point_order[i] - var key2 := other._point_order[i] - assert_eq(key1, key2, "Same Point Order") - - for k: int in p_array._points: - var p1: SS2D_Point = p_array._points[k] + + for k in p_array.get_all_point_keys(): + var p1: SS2D_Point = p_array.get_point(k) print("p1: ", p1.get_instance_id()) - var p2: SS2D_Point = other._points[k] + var p2: SS2D_Point = other.get_point(k) print("p2: ", p2.get_instance_id()) + assert_ne(p1, p2, "Unique Point with key %s" % k) assert_ne(p1.properties, p2.properties) assert_eq(p1.get_signal_connection_list("changed").size(), 1, "SIGNALS CONNECTED") print(p1.get_signal_connection_list("changed")[0]) + + @warning_ignore("unsafe_method_access") assert_eq( p1.get_signal_connection_list("changed")[0].callable.get_object(), p_array, "SIGNALS CONNECTED to Parent" ) + @warning_ignore("unsafe_method_access") assert_eq( p2.get_signal_connection_list("changed")[0].callable.get_object(), other, @@ -175,6 +171,26 @@ func test_clone() -> void: assert_eq(p1.properties.flip, p2.properties.flip, "flip Same Values") assert_eq(p1.properties.width, p2.properties.width, "width Same Values") + # Run these tests after checking for point uniqueness because they modify the point array + assert_eq(p_array.get_next_key(), other.get_next_key()) + p_array.reserve_key() + assert_ne(p_array.get_next_key(), other.get_next_key()) + + assert_eq(p_array.get_point_constraints_tuples(0), other.get_point_constraints_tuples(0)) + p_array.set_constraint(0, 1, SS2D_Point_Array.CONSTRAINT.ALL) + assert_eq(p_array.get_point_constraints_tuples(0).size(), 1) + assert_eq(other.get_point_constraints_tuples(0).size(), 0) + + assert_eq(p_array.get_point_count(), other.get_point_count()) + var added_key := p_array.add_point(Vector2.ONE) + assert_ne(p_array.get_point_count(), other.get_point_count()) + p_array.remove_point(added_key) + + assert_eq(p_array.get_all_point_keys(), other.get_all_point_keys()) + p_array.set_point_order([2, 1, 0]) + assert_ne(p_array.get_all_point_keys(), other.get_all_point_keys()) + + func test_material_override_add_delete() -> void: var mmat1 := SS2D_Material_Edge_Metadata.new() var mmat2 := SS2D_Material_Edge_Metadata.new() @@ -252,7 +268,7 @@ func test_helpers() -> void: assert_eq(p_array.get_edge_keys_for_indices(Vector2i(1, 2)), Vector2i(key2, key3)) -func generate_points() -> Array[Vector2]: +func generate_points() -> PackedVector2Array: return [ Vector2(0, 0), Vector2(10, 10),