diff --git a/caching.gd b/caching.gd index edbe2fc..bc6e26e 100644 --- a/caching.gd +++ b/caching.gd @@ -31,7 +31,7 @@ func setup() -> Error: if !_all_downloads_done(): push_error("Not done downloading Bulk Data.") return FAILED - + _fetch_mana_symbols() _setup_cache_in_mem() @@ -45,7 +45,6 @@ func _init() -> void: fetch_done.connect(_on_end_emit) fetch_start.connect(_on_start_emit) - func _on_start_emit() -> void: @@ -132,6 +131,7 @@ func _get_card_data_from_bulk(dict_entry: Dictionary) -> Dictionary: return dict_entry + func _get_mana_img(symbol: String, img_url: String) -> Error: fetch_start.emit() if FileAccess.file_exists("res://symbol_cache/" + symbol + ".svg"): @@ -151,12 +151,14 @@ func _get_mana_img(symbol: String, img_url: String) -> Error: if err != OK: push_error(_cache_error("IMG_LOADING") + "Couldn't load the image.") return FAILED - - img.save_png("res://symbol_cache/" + symbol.replace("/", "-").replace("{", "").replace("}", "") + ".png") + + img.save_png( + "res://symbol_cache/" + symbol.replace("/", "-").replace("{", "").replace("}", "") + ".png" + ) img = null fetch_done.emit() - + return OK @@ -166,32 +168,37 @@ func _fetch_mana_symbols() -> Error: return OK else: DirAccess.make_dir_absolute("res://symbol_cache") - + var httpr = HTTPRequest.new() add_child(httpr) - + var err = httpr.request("https://api.scryfall.com/symbology", _req_headers) if err != OK: push_error(_cache_error("GET_REQUEST") + "An error occured in the Scryfall request.") return FAILED var resp = await httpr.request_completed - + var unprocessed_body = resp[3].get_string_from_utf8() var json_body = JSON.parse_string(unprocessed_body) for icon in json_body["data"]: err = await _get_mana_img(icon["symbol"], icon["svg_uri"]) if err != OK: push_error("Couldn't fetch mana symbol " + icon["symbol"]) - mana_symbols[icon["symbol"]] = "res://symbol_cache/" + icon["symbol"].replace("/", "-").replace("{", "").replace("}", "") + ".png" + mana_symbols[icon["symbol"]] = ( + "res://symbol_cache/" + + icon["symbol"].replace("/", "-").replace("{", "").replace("}", "") + + ".png" + ) print(icon["symbol"] + " image cached.") - + var file = FileAccess.open("res://symbol_cache/symbols.json", FileAccess.WRITE) file.store_line(JSON.stringify(mana_symbols)) file.close() - + print("Done caching mana symbols.") return OK + func _fetch_card_img(data: Dictionary) -> Error: fetch_start.emit() if FileAccess.file_exists("user://card_cache/" + data["id"] + "card.png"): diff --git a/event_bus.gd b/event_bus.gd index 92351eb..ab85b8e 100644 --- a/event_bus.gd +++ b/event_bus.gd @@ -3,4 +3,4 @@ extends Node @warning_ignore("unused_signal") signal card_on_hover(card_info, card_image) @warning_ignore("unused_signal") -signal card_on_unhover() +signal card_on_unhover diff --git a/scenes/card/card.gd b/scenes/card/card.gd index 7ed99de..07ad091 100644 --- a/scenes/card/card.gd +++ b/scenes/card/card.gd @@ -68,5 +68,5 @@ func _on_mouse_exited() -> void: Input.set_default_cursor_shape(Input.CURSOR_ARROW) $TweenController.scale(1.0) - + EventBus.emit_signal("card_on_unhover") diff --git a/scenes/card/load.gd b/scenes/card/load.gd index bc802d4..914f16c 100644 --- a/scenes/card/load.gd +++ b/scenes/card/load.gd @@ -37,7 +37,7 @@ func _load_data() -> Error: if card_json == null: push_error("%s\nCard json data is could not be parsed as valid json" % card.error("DATA")) return FAILED - + card.card_info["name"] = card_json["name"] card.card_info["type"] = card_json["type_line"] card.card_info["desc"] = card_json["oracle_text"] @@ -46,7 +46,9 @@ func _load_data() -> Error: func _load_image() -> Error: - var cached_img = FileAccess.get_file_as_bytes("user://card_cache/" + card.card_info["id"] + "/card.png") + var cached_img = FileAccess.get_file_as_bytes( + "user://card_cache/" + card.card_info["id"] + "/card.png" + ) if cached_img.is_empty(): push_error("%sCard on-board image was not found in cache" % card.error("CACHE")) @@ -58,9 +60,9 @@ func _load_image() -> Error: if image_status != OK: push_error("%sCard on-board image failed to load correctly" % card.error("IMAGE")) return FAILED - + card.cached_image = cache_image - + var image = Image.new() image_status = image.load_png_from_buffer(cached_img) @@ -77,5 +79,4 @@ func _load_image() -> Error: var card_sprite = card.get_node("Sprite2D") card_sprite.texture = image_texture - return OK diff --git a/scenes/tooltip/card_image.gd b/scenes/tooltip/card_image.gd index c052431..1b5f65f 100644 --- a/scenes/tooltip/card_image.gd +++ b/scenes/tooltip/card_image.gd @@ -1,14 +1,17 @@ extends TextureRect + func _set_tip_image(_card_info: Dictionary, card_image: Image) -> void: card_image.resize(int(size.x / 1.75), int(size.y), Image.INTERPOLATE_LANCZOS) var tex = ImageTexture.new() tex.set_image(card_image) texture = tex + func _clear_tip_image() -> void: texture = null + func _ready() -> void: EventBus.connect("card_on_hover", _set_tip_image) EventBus.connect("card_on_unhover", _clear_tip_image) diff --git a/scenes/tooltip/card_text.gd b/scenes/tooltip/card_text.gd index f6083b4..5f44859 100644 --- a/scenes/tooltip/card_text.gd +++ b/scenes/tooltip/card_text.gd @@ -2,6 +2,7 @@ extends RichTextLabel var mana_symbols: Dictionary + func _set_tip_text(card_info: Dictionary, _card_image: Image) -> void: # TODO: add more card formatting, check all of the logos, very niche icons will be affected i believe since they're # different sizes @@ -10,19 +11,22 @@ func _set_tip_text(card_info: Dictionary, _card_image: Image) -> void: text += "[i]" + card_info["type"] + "[/i]\n" var oracle_text = card_info["desc"] var last_idx = 0 - + for symbol in mana_symbols: last_idx = 0 while oracle_text.find(symbol, last_idx) != -1: - oracle_text = oracle_text.replace(symbol, "[img width=\"16\" height=\"16\"]" + mana_symbols[symbol] + "[/img]") - last_idx = oracle_text.find(symbol, last_idx) + symbol.length() + oracle_text = oracle_text.replace( + symbol, '[img width="16" height="16"]' + mana_symbols[symbol] + "[/img]" + ) + last_idx = oracle_text.find(symbol, last_idx) + symbol.length() text += oracle_text - + func _clear_tip_text() -> void: text = "" + func _ready() -> void: if !FileAccess.file_exists("res://symbol_cache/symbols.json"): push_error("Symbols haven't been cached yet!") @@ -30,8 +34,8 @@ func _ready() -> void: var file = FileAccess.open("res://symbol_cache/symbols.json", FileAccess.READ) mana_symbols = JSON.parse_string(file.get_as_text()) file.close() - + set_use_bbcode(true) - + EventBus.connect("card_on_hover", _set_tip_text) EventBus.connect("card_on_unhover", _clear_tip_text)