godot_osc_demo/ui.gd

50 lines
1.6 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
func _ready():
osc_client = OSCSender.new()
osc_server = OSCReceiver.new()
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/set" % 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/set" % 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()
func _button_pressed(btn: Button, idx: int):
print("Button %s pressed: state = %s" % [idx, btn.button_pressed])
osc_client.send_osc(9000, "/button/%d" % idx, "i", [1 if btn.button_pressed else 0])
func _slider_moved(value: float, idx: int):
print("Slider %d moved: value = %f" % [idx, value])
osc_client.send_osc(9000, "/slider/%d" % idx, "f", [value])
func recv_osc(msg_info, values):
print("Sender IP: %s" % msg_info["ip"])
print("Sender Port: %d" % msg_info["port"])
print("Address: %s" % msg_info["address"])
print("Types: %s" % msg_info["types"])
print("Values:", values)