Skip to content

Commit

Permalink
Use Packed*Arrays where possible
Browse files Browse the repository at this point in the history
- Mostly replaces Array[int] with PackedInt32Array
- Replace PackedInt64Array with PackedInt32Array because int32 should
  suffice.
- Update unit tests
  • Loading branch information
mphe committed Jun 8, 2024
1 parent c1c41b7 commit ad344b9
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 131 deletions.
2 changes: 1 addition & 1 deletion addons/rmsmartshape/actions/action_delete_point.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions addons/rmsmartshape/actions/action_delete_points.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions addons/rmsmartshape/actions/action_move_control_points.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions addons/rmsmartshape/actions/action_move_verticies.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion addons/rmsmartshape/actions/action_split_shape.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions addons/rmsmartshape/lib/tuple.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
29 changes: 13 additions & 16 deletions addons/rmsmartshape/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -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))
Expand All @@ -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)

Expand Down Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions addons/rmsmartshape/plugin_functionality.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions addons/rmsmartshape/shapes/index_map.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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])
Expand All @@ -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):
Expand Down
30 changes: 15 additions & 15 deletions addons/rmsmartshape/shapes/point_array.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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()

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions addons/rmsmartshape/shapes/shape.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()


Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit ad344b9

Please sign in to comment.