EmoteWall/src/emotes.gd
2024-10-21 22:14:11 -04:00

103 lines
2.8 KiB
GDScript

extends Control
@onready var texture_rect = $Emote
@onready var added_channels_1: VBoxContainer = $AddToWhitelist/AddedChannels/AddedChannels1
@onready var added_channels_2: VBoxContainer = $AddToWhitelist/AddedChannels/AddedChannels2
@onready var added_channels_3: VBoxContainer = $AddToWhitelist/AddedChannels/AddedChannels3
const ADDED_CHANNEL = preload("res://src/AddedChannel.tscn")
const EMOTE = preload("res://src/Emote.tscn")
var whitelist = []
var emote_cache = {}
func _ready() -> void:
_load_from_whitelist()
func _save_to_whitelist():
var file = FileAccess.open("user://whitelist.txt", FileAccess.WRITE)
for channel in whitelist:
file.store_line("%s#%s" % [channel.id, channel.name])
func _update_labels():
for child in added_channels_1.get_children():
child.queue_free()
for child in added_channels_2.get_children():
child.queue_free()
for child in added_channels_3.get_children():
child.queue_free()
for i in range(0, whitelist.size()):
if i < 21:
var new_label = ADDED_CHANNEL.instantiate()
new_label.remove_channel.connect(_remove_from_whitelist.bind(whitelist[i].id))
if i < 7:
added_channels_1.add_child(new_label)
elif i < 14:
added_channels_2.add_child(new_label)
elif i < 21:
added_channels_3.add_child(new_label)
new_label.label.text = whitelist[i].name
func _load_from_whitelist():
if FileAccess.file_exists("user://whitelist.txt"):
var file = FileAccess.open("user://whitelist.txt", FileAccess.READ)
var content = file.get_as_text()
var filecontent = content.split("\n")
for i in range(0, filecontent.size()):
var idsplit = filecontent[i].split("#")
if idsplit.size() > 1:
whitelist.push_back({
"id": idsplit[0],
"name": idsplit[1]
})
else:
whitelist = []
_update_labels()
func _on_twitch_event_listener_received(data):
for fragment in data.message.fragments:
if fragment.type != "emote":
continue
if not whitelist.has(fragment.emote.owner_id):
continue
if not emote_cache.has(fragment.emote.id):
var image_buffer = await TwitchGod.http_client.request(HttpClient.API_TYPE.EMOTE, { "id": fragment.emote.id })
var image = Image.new()
image.load_png_from_buffer(image_buffer)
var texture = ImageTexture.create_from_image(image)
emote_cache[fragment.emote.id] = texture
var new_emote = EMOTE.instantiate()
new_emote.position.x = randi_range(50, 900)
new_emote.position.y = 580
new_emote.texture = emote_cache[fragment.emote.id]
add_child(new_emote)
func _remove_from_whitelist(id: String):
whitelist = whitelist.filter(func (x): return x.id != id)
_update_labels()
_save_to_whitelist()
func _on_add_to_whitelist_new_whitelist_id(id: String, name: String) -> void:
if whitelist.filter(func (x): return x.id == id).size():
return
whitelist.push_back({
"id": id,
"name": name
})
_update_labels()
_save_to_whitelist()