2023-11-04 16:06:33 +01:00
|
|
|
extends Control
|
|
|
|
|
2023-11-04 23:45:43 +01:00
|
|
|
|
2023-11-04 16:06:33 +01:00
|
|
|
const OSCReceiver = preload("res://osc_receiver.gd")
|
|
|
|
var osc_server: OSCReceiver
|
|
|
|
const OSCSender = preload("res://osc_sender.gd")
|
|
|
|
var osc_client: OSCSender
|
|
|
|
|
2023-11-04 23:45:43 +01:00
|
|
|
@export var osc_server_address:String = "*"
|
|
|
|
@export var osc_server_port: int = 9001
|
|
|
|
@export var osc_dest_address:String = "127.0.0.1"
|
|
|
|
@export var osc_dest_port: int = 9000
|
|
|
|
@export var debug: bool = false
|
|
|
|
|
|
|
|
|
2023-11-04 16:06:33 +01:00
|
|
|
func _ready():
|
|
|
|
osc_client = OSCSender.new()
|
2023-11-04 23:45:43 +01:00
|
|
|
osc_client.debug = debug
|
|
|
|
osc_client.set_destination([osc_dest_address, osc_dest_port])
|
2023-11-04 16:06:33 +01:00
|
|
|
osc_server = OSCReceiver.new()
|
2023-11-04 23:45:43 +01:00
|
|
|
osc_server.debug = debug
|
2023-11-04 16:06:33 +01:00
|
|
|
add_child(osc_server)
|
2023-11-04 23:45:43 +01:00
|
|
|
|
2023-11-04 16:06:33 +01:00
|
|
|
var button_callback = Callable(self, "_button_pressed")
|
|
|
|
var slider_callback = Callable(self, "_slider_moved")
|
|
|
|
|
|
|
|
# configure all sensors
|
|
|
|
for i in range(4):
|
|
|
|
var nodename = "VSlider%d" % (i + 1)
|
2023-11-06 17:22:18 +01:00
|
|
|
var osc_address = "/slider/%d" % i
|
2023-11-04 16:06:33 +01:00
|
|
|
var slider_node = find_child(nodename)
|
2023-11-06 17:22:18 +01:00
|
|
|
osc_server.register_callback(osc_address, "f", slider_node.recv_osc)
|
2023-11-04 16:06:33 +01:00
|
|
|
slider_node.value_changed.connect(slider_callback.bind(i))
|
2023-11-06 17:22:18 +01:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
osc_server.register_callback(osc_address, "f", print_osc_message)
|
2023-11-04 23:45:43 +01:00
|
|
|
|
2023-11-04 16:06:33 +01:00
|
|
|
for i in range(4):
|
|
|
|
var nodename = "Button%d" % (i + 1)
|
2023-11-06 17:22:18 +01:00
|
|
|
var osc_address = "/button/%d" % i
|
2023-11-04 16:06:33 +01:00
|
|
|
var button_node = find_child(nodename)
|
|
|
|
button_node.pressed.connect(button_callback.bind(button_node, i))
|
2023-11-06 17:22:18 +01:00
|
|
|
osc_server.register_callback(osc_address, "i", button_node.recv_osc)
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
osc_server.register_callback(osc_address, "i", print_osc_message)
|
2023-11-04 16:06:33 +01:00
|
|
|
|
|
|
|
# start listening for osc messages
|
2023-11-04 23:45:43 +01:00
|
|
|
osc_server.start_server(osc_server_port, osc_server_address)
|
2023-11-04 16:06:33 +01:00
|
|
|
|
|
|
|
|
2023-11-06 17:22:18 +01:00
|
|
|
func _button_pressed(btn: Button, idx: int) -> void:
|
|
|
|
var value = 1 if btn.button_pressed else 0
|
|
|
|
_debug("Button %s pressed: value = %s" % [idx, value])
|
|
|
|
osc_client.send_osc("/button/%d" % idx, "i", [value])
|
2023-11-04 16:06:33 +01:00
|
|
|
|
|
|
|
|
2023-11-06 17:22:18 +01:00
|
|
|
func _slider_moved(value: float, idx: int) -> void:
|
2023-11-04 23:45:43 +01:00
|
|
|
_debug("Slider %d moved: value = %f" % [idx, value])
|
|
|
|
osc_client.send_osc("/slider/%d" % idx, "f", [value])
|
2023-11-04 16:06:33 +01:00
|
|
|
|
|
|
|
|
2023-11-06 17:22:18 +01:00
|
|
|
func print_osc_message(msg_info, values) -> void:
|
2023-11-04 23:45:43 +01:00
|
|
|
_debug("Sender IP: %s" % msg_info["ip"])
|
|
|
|
_debug("Sender Port: %d" % msg_info["port"])
|
|
|
|
_debug("Address: %s" % msg_info["address"])
|
|
|
|
_debug("Types: %s" % msg_info["types"])
|
|
|
|
_debug("Values: %s" % values)
|
|
|
|
|
|
|
|
|
2023-11-06 17:22:18 +01:00
|
|
|
func _debug(msg) -> void:
|
2023-11-04 23:45:43 +01:00
|
|
|
if debug:
|
|
|
|
print(msg)
|