Some function signature cleanup
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
parent
bf9a21b76d
commit
4e5e0f7345
1
UI.tscn
1
UI.tscn
|
@ -24,7 +24,6 @@ anchor_bottom = 1.0
|
|||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_28t32")
|
||||
osc_dest_port = 9001
|
||||
debug = true
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="."]
|
||||
|
|
|
@ -26,14 +26,14 @@ func _exit_tree():
|
|||
stop_server()
|
||||
|
||||
|
||||
func register_callback(oscaddress, argtypes, callback):
|
||||
if not _observers.has([oscaddress, argtypes]):
|
||||
_observers[[oscaddress, argtypes]] = []
|
||||
func register_callback(osc_address, arg_types, callback) -> void:
|
||||
if not _observers.has([osc_address, arg_types]):
|
||||
_observers[[osc_address, arg_types]] = []
|
||||
|
||||
_observers[[oscaddress, argtypes]].append(callback)
|
||||
_observers[[osc_address, arg_types]].append(callback)
|
||||
|
||||
|
||||
func start_server(port:int = default_port, bind_address:String = default_address):
|
||||
func start_server(port:int = default_port, bind_address:String = default_address) -> void:
|
||||
_server = UDPServer.new()
|
||||
|
||||
if _server.listen(port, bind_address) != OK:
|
||||
|
@ -44,7 +44,7 @@ func start_server(port:int = default_port, bind_address:String = default_address
|
|||
_timer.start(poll_interval)
|
||||
|
||||
|
||||
func stop_server():
|
||||
func stop_server() -> void:
|
||||
_timer.stop()
|
||||
remove_child(_timer)
|
||||
_timer.free()
|
||||
|
@ -53,12 +53,12 @@ func stop_server():
|
|||
_server.stop()
|
||||
|
||||
|
||||
func _debug(msg):
|
||||
func _debug(msg) -> void:
|
||||
if debug:
|
||||
print(msg)
|
||||
|
||||
|
||||
func _poll():
|
||||
func _poll() -> void:
|
||||
if not _server.is_listening():
|
||||
return
|
||||
|
||||
|
@ -81,11 +81,11 @@ func _poll():
|
|||
_debug("OSC arg types: %s" % types)
|
||||
|
||||
if _observers.has([address, types]):
|
||||
var values = _parse_osc_values(packet.slice(offset), types)
|
||||
result = _parse_osc_values(packet.slice(offset), types)
|
||||
|
||||
if values == null:
|
||||
if result[0] != OK:
|
||||
_debug("Invalid/Unsupported OSC message.")
|
||||
elif values.size() != types.length():
|
||||
elif result[1].size() != types.length():
|
||||
_debug("Mismatch between expected / received number of OSC arguments.")
|
||||
else:
|
||||
var msg_info = {
|
||||
|
@ -96,9 +96,9 @@ func _poll():
|
|||
}
|
||||
|
||||
for callback in _observers[[address, types]]:
|
||||
callback.call(msg_info, values)
|
||||
callback.call(msg_info, result[1])
|
||||
|
||||
func _parse_osc_addr_and_types(packet: PackedByteArray):
|
||||
func _parse_osc_addr_and_types(packet: PackedByteArray) -> Array:
|
||||
var asep = packet.find(0)
|
||||
var address = packet.slice(0, asep).get_string_from_ascii()
|
||||
|
||||
|
@ -110,7 +110,7 @@ func _parse_osc_addr_and_types(packet: PackedByteArray):
|
|||
return [address, types, tsep + (4 - tsep % 4)]
|
||||
|
||||
|
||||
func _parse_osc_values(packet: PackedByteArray, types: String):
|
||||
func _parse_osc_values(packet: PackedByteArray, types: String) -> Array:
|
||||
var result
|
||||
var values = []
|
||||
var stream = StreamPeerBuffer.new()
|
||||
|
@ -153,7 +153,7 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
|
|||
stream.seek(stream.get_position() + (4 - count % 4))
|
||||
else:
|
||||
_debug("Could not read OSC blob argument.")
|
||||
return
|
||||
return [ERR_PARSE_ERROR]
|
||||
"t":
|
||||
result = stream.get_data(8)
|
||||
|
||||
|
@ -161,7 +161,7 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
|
|||
values.append(result[1])
|
||||
else:
|
||||
_debug("Could not read OSC timetag argument.")
|
||||
return
|
||||
return [ERR_PARSE_ERROR]
|
||||
"m", "r":
|
||||
values.append([
|
||||
stream.get_u8(),
|
||||
|
@ -177,6 +177,6 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
|
|||
values.append(null)
|
||||
_:
|
||||
_debug("Argument type '%s' not supported." % type_id)
|
||||
return
|
||||
return [ERR_INVALID_DATA]
|
||||
|
||||
return values
|
||||
return [OK, values]
|
||||
|
|
|
@ -17,12 +17,12 @@ func _init():
|
|||
_socket.set_dest_address(_address, _port)
|
||||
|
||||
|
||||
func _debug(msg):
|
||||
func _debug(msg) -> void:
|
||||
if debug:
|
||||
print(msg)
|
||||
|
||||
|
||||
func set_destination(dest):
|
||||
func set_destination(dest) -> void:
|
||||
if dest is int:
|
||||
_socket.set_dest_address(_address, dest)
|
||||
_port = dest
|
||||
|
@ -35,16 +35,18 @@ func set_destination(dest):
|
|||
_port = dest[1]
|
||||
|
||||
|
||||
func send_osc(oscaddress: String, argtypes: String, values: Array, dest = null):
|
||||
assert(oscaddress.begins_with("/"))
|
||||
func create_message(osc_address: String, arg_types: String, values: Array) -> Array:
|
||||
if not osc_address.begins_with("/"):
|
||||
return [ERR_INVALID_DATA]
|
||||
|
||||
var buf = StreamPeerBuffer.new()
|
||||
buf.set_big_endian(true)
|
||||
_pack_string(buf, oscaddress)
|
||||
_pack_string(buf, "," + argtypes)
|
||||
_pack_string(buf, osc_address)
|
||||
_pack_string(buf, "," + arg_types)
|
||||
var vidx: int = 0
|
||||
|
||||
for i in argtypes.length():
|
||||
var typetag = argtypes[i]
|
||||
for i in arg_types.length():
|
||||
var typetag = arg_types[i]
|
||||
var inc_vidx = true
|
||||
|
||||
match typetag:
|
||||
|
@ -72,19 +74,28 @@ func send_osc(oscaddress: String, argtypes: String, values: Array, dest = null):
|
|||
inc_vidx = false # no argument value sent
|
||||
_:
|
||||
_debug("Argument type '%s' not supported." % typetag)
|
||||
return FAILED
|
||||
return [ERR_INVALID_PARAMETER]
|
||||
|
||||
if inc_vidx:
|
||||
vidx += 1
|
||||
|
||||
return [OK, buf.get_data_array()]
|
||||
|
||||
|
||||
func send_osc(osc_address: String, arg_types: String, values: Array, dest = null) -> Error:
|
||||
var res = create_message(osc_address, arg_types, values)
|
||||
|
||||
if res[0] != OK:
|
||||
return res[0]
|
||||
|
||||
if dest != null:
|
||||
set_destination(dest)
|
||||
|
||||
_socket.put_packet(buf.get_data_array())
|
||||
_socket.put_packet(res[1])
|
||||
return OK
|
||||
|
||||
|
||||
func _pack_string(buf: StreamPeerBuffer, s: String):
|
||||
func _pack_string(buf: StreamPeerBuffer, s: String) -> void:
|
||||
## Pack a string into a binary OSC buffer
|
||||
buf.put_data(s.to_ascii_buffer())
|
||||
|
||||
|
@ -93,7 +104,7 @@ func _pack_string(buf: StreamPeerBuffer, s: String):
|
|||
buf.put_u8(0)
|
||||
|
||||
|
||||
func _pack_blob(buf, blob):
|
||||
func _pack_blob(buf, blob) -> void:
|
||||
## Pack a PackedByteArray, Array or String into a binary OSC buffer
|
||||
if blob is String:
|
||||
blob = blob.to_utf8_buffer()
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
|
@ -0,0 +1,34 @@
|
|||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://bc0u3ivd6q1jg"
|
||||
path="res://.godot/imported/screenshot.png-024a21af5d37bf0f0dd0e2bccdd149d0.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://screenshot.png"
|
||||
dest_files=["res://.godot/imported/screenshot.png-024a21af5d37bf0f0dd0e2bccdd149d0.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
|
@ -1,9 +1,5 @@
|
|||
extends VSlider
|
||||
|
||||
|
||||
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"])
|
||||
func recv_osc(msg_info, values) -> void:
|
||||
set_value_no_signal(clampf(values[0], 0.0, 1.0))
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
extends Button
|
||||
|
||||
|
||||
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"])
|
||||
func recv_osc(msg_info, values) -> void:
|
||||
set_pressed_no_signal(bool(values[0]))
|
||||
|
|
27
ui.gd
27
ui.gd
|
@ -27,33 +27,40 @@ func _ready():
|
|||
# configure all sensors
|
||||
for i in range(4):
|
||||
var nodename = "VSlider%d" % (i + 1)
|
||||
var osc_addr = "/slider/%d" % i
|
||||
var osc_address = "/slider/%d" % i
|
||||
var slider_node = find_child(nodename)
|
||||
osc_server.register_callback(osc_addr, "f", slider_node.recv_osc)
|
||||
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_addr = "/button/%d" % i
|
||||
var osc_address = "/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))
|
||||
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):
|
||||
_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 _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):
|
||||
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 recv_osc(msg_info, values):
|
||||
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"])
|
||||
|
@ -61,6 +68,6 @@ func recv_osc(msg_info, values):
|
|||
_debug("Values: %s" % values)
|
||||
|
||||
|
||||
func _debug(msg):
|
||||
func _debug(msg) -> void:
|
||||
if debug:
|
||||
print(msg)
|
||||
|
|
Loading…
Reference in New Issue