diff --git a/caching.gd b/caching.gd index 9d17745..35db0f1 100644 --- a/caching.gd +++ b/caching.gd @@ -31,6 +31,8 @@ func setup() -> Error: if !_all_downloads_done(): push_error("Not done downloading Bulk Data.") return FAILED + + _fetch_mana_symbols() _setup_cache_in_mem() return OK @@ -130,6 +132,58 @@ 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("user://card_cache/" + symbol + ".svg"): + return OK + + var httpr = HTTPRequest.new() + add_child(httpr) + + var err = httpr.request(img_url, _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 img = Image.new() + err = img.load_svg_from_buffer(resp[3]) + if err != OK: + push_error(_cache_error("IMG_LOADING") + "Couldn't load the image.") + return FAILED + + img.save_png("user://symbol_cache/" + symbol.replace("/", "-") + ".png") + img = null + + fetch_done.emit() + + return OK + + +func _fetch_mana_symbols() -> Error: + if DirAccess.dir_exists_absolute("user://symbol_cache"): + return OK + else: + DirAccess.make_dir_absolute("user://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: + print(icon["symbol"] + " image cached.") + + print("Done caching mana symbols.") + return OK func _fetch_card_img(data: Dictionary) -> Error: fetch_start.emit() diff --git a/event_bus.gd b/event_bus.gd index 7ccd010..92351eb 100644 --- a/event_bus.gd +++ b/event_bus.gd @@ -1,6 +1,6 @@ extends Node @warning_ignore("unused_signal") -signal card_on_hover(val) +signal card_on_hover(card_info, card_image) @warning_ignore("unused_signal") signal card_on_unhover() diff --git a/scenes/card/card.gd b/scenes/card/card.gd index d021665..7ed99de 100644 --- a/scenes/card/card.gd +++ b/scenes/card/card.gd @@ -6,6 +6,7 @@ extends Node2D # Card information. var card_info: Dictionary +var cached_image: Image # Card properties. var tapped: bool @@ -55,7 +56,7 @@ func _on_mouse_entered() -> void: Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) $TweenController.scale(1.05) - EventBus.emit_signal("card_on_hover", card_info) + EventBus.emit_signal("card_on_hover", card_info, cached_image) func _on_mouse_exited() -> void: diff --git a/scenes/card/load.gd b/scenes/card/load.gd index b3ea812..bc802d4 100644 --- a/scenes/card/load.gd +++ b/scenes/card/load.gd @@ -46,15 +46,23 @@ func _load_data() -> Error: func _load_image() -> Error: - # NOTE: Assuming we're going with using the .png cards on board. 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")) return FAILED + var cache_image = Image.new() + var image_status: Error = cache_image.load_png_from_buffer(cached_img) + + 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() - var image_status: Error = image.load_png_from_buffer(cached_img) + image_status = image.load_png_from_buffer(cached_img) if image_status != OK: push_error("%sCard on-board image failed to load correctly" % card.error("IMAGE")) @@ -69,4 +77,5 @@ 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 new file mode 100644 index 0000000..c052431 --- /dev/null +++ b/scenes/tooltip/card_image.gd @@ -0,0 +1,14 @@ +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_image.gd.uid b/scenes/tooltip/card_image.gd.uid new file mode 100644 index 0000000..59ea636 --- /dev/null +++ b/scenes/tooltip/card_image.gd.uid @@ -0,0 +1 @@ +uid://cpvbftm0swoa6 diff --git a/scenes/tooltip/card_text.gd b/scenes/tooltip/card_text.gd new file mode 100644 index 0000000..1ac97c5 --- /dev/null +++ b/scenes/tooltip/card_text.gd @@ -0,0 +1,12 @@ +extends RichTextLabel + +func _set_tip_text(card_info: Dictionary, _card_image: Image) -> void: + text = card_info["name"] + " | " + card_info["type"] + "\n" + text += card_info["desc"] + +func _clear_tip_text() -> void: + text = "" + +func _ready() -> void: + EventBus.connect("card_on_hover", _set_tip_text) + EventBus.connect("card_on_unhover", _clear_tip_text) diff --git a/scenes/tooltip/card_text.gd.uid b/scenes/tooltip/card_text.gd.uid new file mode 100644 index 0000000..1d4059c --- /dev/null +++ b/scenes/tooltip/card_text.gd.uid @@ -0,0 +1 @@ +uid://b8tioen4n1rip diff --git a/tabletop.tscn b/tabletop.tscn index 73967b0..0b76c3a 100644 --- a/tabletop.tscn +++ b/tabletop.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=3 uid="uid://b4ldtb3gw0jlu"] +[gd_scene load_steps=7 format=3 uid="uid://b4ldtb3gw0jlu"] [ext_resource type="Script" uid="uid://cfkew150yl1y3" path="res://tabletop.gd" id="1_3we3x"] -[ext_resource type="Script" uid="uid://brto6yniu3yx1" path="res://tooltip.gd" id="2_d43bn"] +[ext_resource type="Script" uid="uid://b8tioen4n1rip" path="res://scenes/tooltip/card_text.gd" id="2_d43bn"] +[ext_resource type="Script" uid="uid://cpvbftm0swoa6" path="res://scenes/tooltip/card_image.gd" id="2_pqag1"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_3we3x"] bg_color = Color(0, 0, 0, 1) @@ -35,6 +36,20 @@ theme_override_constants/separation = 0 [node name="MenuArea" type="PanelContainer" parent="UI/BigBar/Items"] custom_minimum_size = Vector2(0, 540) layout_mode = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="UI/BigBar/Items/MenuArea"] +layout_mode = 2 + +[node name="TextureRect" type="TextureRect" parent="UI/BigBar/Items/MenuArea/VBoxContainer"] +custom_minimum_size = Vector2(400, 300) +layout_direction = 2 +layout_mode = 2 +stretch_mode = 5 +script = ExtResource("2_pqag1") + +[node name="RichTextLabel" type="RichTextLabel" parent="UI/BigBar/Items/MenuArea/VBoxContainer"] +custom_minimum_size = Vector2(0, 230) +layout_mode = 2 script = ExtResource("2_d43bn") [node name="ChatArea" type="PanelContainer" parent="UI/BigBar/Items"] diff --git a/tooltip.gd b/tooltip.gd deleted file mode 100644 index 14ec9ab..0000000 --- a/tooltip.gd +++ /dev/null @@ -1,20 +0,0 @@ -extends PanelContainer - -var back: ColorRect - -func _clear_card_info(): - tooltip_text = "" - back.color = Color(1,1,1,1) - -func _display_card_info(card_info: Dictionary): - tooltip_text = "TEST" - back.color = Color(0,0,0,1) - -func _ready(): - back = ColorRect.new() - add_child(back) - - back.color = Color(1, 1, 1, 1) - - EventBus.connect("card_on_hover", _display_card_info) - EventBus.connect("card_on_unhover", _clear_card_info) diff --git a/tooltip.gd.uid b/tooltip.gd.uid deleted file mode 100644 index 326b94d..0000000 --- a/tooltip.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://brto6yniu3yx1