godot_osc_demo/ui.gd

67 lines
2.0 KiB
GDScript3
Raw Normal View History

extends Control
const OSCReceiver = preload("res://osc_receiver.gd")
var osc_server: OSCReceiver
const OSCSender = preload("res://osc_sender.gd")
var osc_client: OSCSender
@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
func _ready():
osc_client = OSCSender.new()
osc_client.debug = debug
osc_client.set_destination([osc_dest_address, osc_dest_port])
osc_server = OSCReceiver.new()
osc_server.debug = debug
add_child(osc_server)
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)
var osc_addr = "/slider/%d" % i
var slider_node = find_child(nodename)
osc_server.register_callback(osc_addr, "f", slider_node.recv_osc)
slider_node.value_changed.connect(slider_callback.bind(i))
for i in range(4):
var nodename = "Button%d" % (i + 1)
var osc_addr = "/button/%d" % i
var button_node = find_child(nodename)
osc_server.register_callback(osc_addr, "i", button_node.recv_osc)
button_node.pressed.connect(button_callback.bind(button_node, i))
# start listening for osc messages
osc_server.start_server(osc_server_port, osc_server_address)
func _button_pressed(btn: Button, idx: int):
_debug("Button %s pressed: state = %s" % [idx, btn.button_pressed])
osc_client.send_osc("/button/%d" % idx, "i", [1 if btn.button_pressed else 0])
func _slider_moved(value: float, idx: int):
_debug("Slider %d moved: value = %f" % [idx, value])
osc_client.send_osc("/slider/%d" % idx, "f", [value])
func recv_osc(msg_info, values):
_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)
func _debug(msg):
if debug:
print(msg)