extends BaseWidgetNode
class_name AccelerometerWidgetNode

var accel_vector: Line2D
var x_vector: Line2D
var y_vector: Line2D
var label: Label
var origin: Vector2 = Vector2(0., 0.)


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	super._ready()
	Signals.linear_acceleration_updated.connect(_on_linear_acceleration_updated)
	label = get_node('Label')
	accel_vector = get_node("AccelVector")
	accel_vector.points = [origin, origin]
	x_vector = get_node("XVector")
	x_vector.points = [origin, origin]
	y_vector = get_node("YVector")
	y_vector.points = [origin, origin]

func _on_linear_acceleration_updated(sensor_ip: String, linear_acceleration: Array):
	var accel_vec = Vector2(-linear_acceleration[1], linear_acceleration[0])
	var vec_norm: Vector2 = accel_vec.normalized()
	var vec_length: float = accel_vec.length()
	var accel_g = vec_length / Constants.g
	var x_vec = Vector2(vec_norm.x*vec_length, 0)
	accel_vector.points = [origin, accel_vec * 10]
	x_vector.points = [origin, x_vec * 10]
	y_vector.points = [x_vec * 10, accel_vec * 10]
	label.text = str(round(accel_g*100)/100) + ' g'
