Skip to content

Commit

Permalink
Refactor code base
Browse files Browse the repository at this point in the history
- Add type hints across code base to improve consistency, performance,
readability and compiler ability to detect errors.
- Use PackedVector2Array for consistency and performance.
- Convert floating string literals into documentation comments.
  - See: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_documentation_comments.html

Also:
- Remove some missed normal texture code from test functions
- Fix normal range property editor not used in inspector
- Remove deprecated property "end" in normal_range.gd
- Remove __new() workarounds
- Mark suspicious code with FIXME
  • Loading branch information
limbonaut committed Mar 12, 2023
1 parent a996892 commit 194f934
Show file tree
Hide file tree
Showing 29 changed files with 1,375 additions and 1,442 deletions.
9 changes: 7 additions & 2 deletions addons/rmsmartshape/common_functions.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
extends Node
class_name SS2D_Common_Functions


static func sort_z(a, b) -> bool:
if a.z_index < b.z_index:
return true
return false


static func sort_int_ascending(a: int, b: int) -> bool:
if a < b:
return true
return false


static func sort_int_descending(a: int, b: int) -> bool:
if a < b:
return false
return true

static func to_vector3(vector: Vector2):

static func to_vector3(vector: Vector2) -> Vector3:
return Vector3(vector.x, vector.y, 0)


static func merge_arrays(arrays: Array) -> Array:
var new_array = []
var new_array := []
for array in arrays:
for v in array:
new_array.push_back(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ class_name SS2D_NormalRangeEditorProperty

var control = preload("res://addons/rmsmartshape/editors/NormalRangeEditor/NormalRangeEditor.tscn").instantiate()

func _init():
func _init() -> void:
add_child(control)
add_focusable(control)

func _enter_tree():
control.connect("value_changed",Callable(self,"_value_changed"))

func _enter_tree() -> void:
control.connect("value_changed", self._value_changed)
_value_changed()

func _exit_tree():
control.disconnect("value_changed",Callable(self,"_value_changed"))

func _value_changed():
var obj = get_edited_object()
control.end = obj.end - obj.begin
func _exit_tree() -> void:
control.disconnect("value_changed", self._value_changed)


func _value_changed() -> void:
var obj: SS2D_NormalRange = get_edited_object()
control.end = obj.distance
control.start = obj.begin
41 changes: 21 additions & 20 deletions addons/rmsmartshape/inpsector_plugin.gd
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
extends EditorInspectorPlugin

var properties = []
var control = null
var control: Control = null

func can_handle(object):

func _can_handle(object: Object) -> bool:
#if object is SS2D_NormalRange:
# return true
if object is SS2D_NormalRange:
#Connect
var parms = [object]
if object.is_connected("changed",Callable(self,"_changed"))==false:
object.connect("changed",Callable(self,"_changed").bind(parms))
if not object.is_connected("changed", self._changed):
object.connect("changed", self._changed.bind(parms))
return true
else:
#Disconnect
if control != null:
control = null

if object.has_signal("changed"):
if object.is_connected("changed",Callable(self,"_changed")):
object.disconnect("changed",Callable(self,"_changed"))
if object.is_connected("changed", self._changed):
object.disconnect("changed", self._changed)
pass

return false

# Called when the node enters the scene tree for the first time.
func _ready():
print("loaded...")
pass # Replace with function body.

func _changed(object):

func _changed(_object) -> void:
control._value_changed()
pass

func parse_property(object, type, path, hint, hint_text, usage):
if path=="edgeRendering":

func _parse_property(
_object: Object,
_type: Variant.Type,
name: String,
_hint_type: PropertyHint,
_hint_string: String,
_usage_flags: PropertyUsageFlags,
_wide: bool
) -> bool:
if name == "edgeRendering":
control = SS2D_NormalRangeEditorProperty.new()
add_property_editor(" ", control)
return true
return false

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
13 changes: 6 additions & 7 deletions addons/rmsmartshape/lib/meta_mat_to_idx_array.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
@tool
extends RefCounted

"""
Everything in this script should be static
This script contains code that may referenced in multiple locations in the plugin
## Everything in this script should be static.
## This script contains code that may referenced in multiple locations in the plugin
##
## This is a simple script to work with arrays of SS2D_IndexMap

This is a simple script to work with arrays of SS2D_IndexMap
Some notes:
"""

# Will merge two arrays of MetaMatToIdxs; a overriting on top of b; returning a new array of MetaMatToIDxs
## FIXME: Method unused within project. Remove file?
## Will merge two arrays of MetaMatToIdxs; a overriting on top of b; returning a new array of MetaMatToIDxs.
static func overwrite_array_a_into_array_b(a: Array, b: Array) -> Array:
var ret = []
# Make equal to b; b serves as the baseline
Expand Down
39 changes: 22 additions & 17 deletions addons/rmsmartshape/lib/tuple.gd
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
@tool
extends RefCounted

"""
Everything in this script should be static
This script contains code that may referenced in multiple locations in the plugin
This is a simple script for a *very* simple tuple class
Some notes:
- "tuples" are just an array with two elements
- Only intger Tuples are fully supported
- Tuple(X,Y) is equal to Tuple(Y,X)
"""

static func create_tuple(a: int, b: int) -> Array:
## Everything in this script should be static.
##
## This script contains code that may referenced in multiple locations in the plugin.
##
## This is a simple script for a *very* simple tuple class
## Some notes:
## - "tuples" are just an array with two elements
## - Only integer Tuples are fully supported
## - Tuple(X,Y) is equal to Tuple(Y,X)


static func create_tuple(a: int, b: int) -> Array[int]:
return [a, b]

static func get_other_value_from_tuple(t: Array, value: int) -> int:

static func get_other_value_from_tuple(t: Array[int], value: int) -> int:
if t[0] == value:
return t[1]
elif t[1] == value:
return t[0]
return -1

static func tuples_are_equal(t1: Array, t2: Array) -> bool:

static func tuples_are_equal(t1: Array[int], t2: Array[int]) -> bool:
return (t1[0] == t2[0] and t1[1] == t2[1]) or (t1[0] == t2[1] and t1[1] == t2[0])

static func find_tuple_in_array_of_tuples(tuple_array: Array, t: Array) -> int:

static func find_tuple_in_array_of_tuples(tuple_array: Array, t: Array[int]) -> int:
for i in range(tuple_array.size()):
var other = tuple_array[i]
var other: Array[int] = tuple_array[i]
if tuples_are_equal(t, other):
return i
return -1

static func is_tuple_in_array_of_tuples(tuple_array: Array, t: Array) -> bool:

static func is_tuple_in_array_of_tuples(tuple_array: Array, t: Array[int]) -> bool:
return find_tuple_in_array_of_tuples(tuple_array, t) != -1


static func is_tuple(thing) -> bool:
if thing is Array:
if thing.size() == 2:
Expand Down
66 changes: 32 additions & 34 deletions addons/rmsmartshape/materials/edge_material.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
extends Resource
class_name SS2D_Material_Edge

"""
This material represents the set of textures used for a single edge
This consists of:
- textures
- corner_textures
- taper_textures
- normals for each texture
"""
# All variations of the main edge texture
# _textures[0] is considered the "main" texture for the EdgeMaterial
#### Will be used to generate an icon representing an edge texture
## This material represents the set of textures used for a single edge.
##
## This consists of: [br]
## - textures [br]
## - corner_textures [br]
## - taper_textures [br]

## All variations of the main edge texture.[br]
## _textures[0] is considered the "main" texture for the EdgeMaterial.[br][br]
## [b]Note:[/b] Will be used to generate an icon representing an edge texture.[br]
@export var textures: Array[Texture2D] = [] : set = _set_textures

# Textures for the final left and right quad of the edge when the angle is steep
Expand All @@ -24,13 +23,13 @@ This consists of:
@export var textures_taper_left: Array[Texture2D] = [] : set = _set_textures_taper_left
@export var textures_taper_right: Array[Texture2D] = [] : set = _set_textures_taper_right

# If the texture choice should be randomized instead of the choice by point setup
## If the texture choice should be randomized instead of the choice by point setup
@export var randomize_texture: bool = false : set = _set_randomize_texture
# If corner textures should be used
## If corner textures should be used
@export var use_corner_texture: bool = true : set = _set_use_corner
# If taper textures should be used
## If taper textures should be used
@export var use_taper_texture: bool = true : set = _set_use_taper
# if set to true, then squishing can occur when texture doesn't fit nicely into total length.
## Whether squishing can occur when texture doesn't fit nicely into total length.
enum FITMODE {SQUISH_AND_STRETCH, CROP}
@export var fit_mode: FITMODE = FITMODE.SQUISH_AND_STRETCH : set = _set_fit_texture

Expand All @@ -40,86 +39,85 @@ enum FITMODE {SQUISH_AND_STRETCH, CROP}
###########
# SETTERS #
###########
func _set_textures(ta: Array):
func _set_textures(ta: Array[Texture2D]) -> void:
textures = ta
emit_signal("changed")


func _set_textures_corner_outer(a: Array):
func _set_textures_corner_outer(a: Array[Texture2D]) -> void:
textures_corner_outer = a
emit_signal("changed")


func _set_textures_corner_inner(a: Array):
func _set_textures_corner_inner(a: Array[Texture2D]) -> void:
textures_corner_inner = a
emit_signal("changed")


func _set_textures_taper_left(a: Array):
func _set_textures_taper_left(a: Array[Texture2D]) -> void:
textures_taper_left = a
emit_signal("changed")


func _set_textures_taper_right(a: Array):
func _set_textures_taper_right(a: Array[Texture2D]) -> void:
textures_taper_right = a
emit_signal("changed")


func _set_randomize_texture(b: bool):
func _set_randomize_texture(b: bool) -> void:
randomize_texture = b
emit_signal("changed")


func _set_use_corner(b: bool):
func _set_use_corner(b: bool) -> void:
use_corner_texture = b
emit_signal("changed")


func _set_use_taper(b: bool):
func _set_use_taper(b: bool) -> void:
use_taper_texture = b
emit_signal("changed")


func _set_fit_texture(fitmode):
func _set_fit_texture(fitmode: FITMODE) -> void:
fit_mode = fitmode
emit_signal("changed")


func _set_material(m:Material):
func _set_material(m: Material) -> void:
material = m
emit_signal("changed")


###########
# GETTERS #
###########
func get_texture(idx: int):
func get_texture(idx: int) -> Texture2D:
return _get_element(idx, textures)


func get_texture_corner_inner(idx: int):
func get_texture_corner_inner(idx: int) -> Texture2D:
return _get_element(idx, textures_corner_inner)


func get_texture_corner_outer(idx: int):
func get_texture_corner_outer(idx: int) -> Texture2D:
return _get_element(idx, textures_corner_outer)


func get_texture_taper_left(idx: int):
func get_texture_taper_left(idx: int) -> Texture2D:
return _get_element(idx, textures_taper_left)


func get_texture_taper_right(idx: int):
func get_texture_taper_right(idx: int) -> Texture2D:
return _get_element(idx, textures_taper_right)


#########
# USAGE #
#########

## Returns main texture used to visually identify this edge material
func get_icon_texture() -> Texture2D:
"""
Returns main texture used to visually identify this edge material
"""
if not textures.is_empty():
return textures[0]
return null
Expand All @@ -128,7 +126,7 @@ func get_icon_texture() -> Texture2D:
############
# INTERNAL #
############
func _get_element(idx: int, a: Array):
func _get_element(idx: int, a: Array) -> Variant:
if a.is_empty():
return null
return a[_adjust_idx(idx, a)]
Expand Down
Loading

0 comments on commit 194f934

Please sign in to comment.