godot_osc_demo/ui.gd

74 lines
2.2 KiB
GDScript

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_address = "/slider/%d" % i
var slider_node = find_child(nodename)
osc_server.register_callback(osc_address, "f", slider_node.recv_osc)
slider_node.value_changed.connect(slider_callback.bind(i))
if debug:
osc_server.register_callback(osc_address, "f", print_osc_message)
for i in range(4):
var nodename = "Button%d" % (i + 1)
var osc_address = "/button/%d" % i
var button_node = find_child(nodename)
button_node.pressed.connect(button_callback.bind(button_node, i))
osc_server.register_callback(osc_address, "i", button_node.recv_osc)
if debug:
osc_server.register_callback(osc_address, "i", print_osc_message)
# start listening for osc messages
osc_server.start_server(osc_server_port, osc_server_address)
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])
func _slider_moved(value: float, idx: int) -> void:
_debug("Slider %d moved: value = %f" % [idx, value])
osc_client.send_osc("/slider/%d" % idx, "f", [value])
func print_osc_message(msg_info, values) -> void:
_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) -> void:
if debug:
print(msg)