Skip to content

Commit 22ed6c1

Browse files
committed
Add support for obtaining box texture regions
1 parent bcba2a1 commit 22ed6c1

File tree

5 files changed

+524
-1
lines changed

5 files changed

+524
-1
lines changed

source/Config.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ func fetch_game_data(path: String, game: RetroHubGameData) -> bool:
391391
game.has_media = data["has_media"]
392392
if data.has("emulator"):
393393
game.emulator = data["emulator"]
394+
if data.has("box_texture_regions"):
395+
for key in data["box_texture_regions"]:
396+
var region_data : PackedFloat64Array = data["box_texture_regions"][key].split_floats(";")
397+
if region_data.size() < 4: continue
398+
var key_idx : int = RetroHubGameData.BoxTextureRegions.keys().find(key.to_upper())
399+
if key_idx == -1: key_idx = int(key)
400+
game.box_texture_regions[key_idx] = Rect2(region_data[0], region_data[1], region_data[2], region_data[3])
401+
else:
402+
# Update older game data with ScreenScraper box texture regions
403+
RetroHubScreenScraperScraper._process_box_texture_regions(game)
404+
save_game_data(game)
394405

395406
return true
396407

@@ -417,6 +428,18 @@ func save_game_data(game_data: RetroHubGameData) -> bool:
417428
"emulator": game_data.emulator
418429
}
419430

431+
var box_texture_regions : Dictionary = {}
432+
for key in game_data.box_texture_regions.keys():
433+
var region_data := "%f;%f;%f;%f" % [
434+
game_data.box_texture_regions[key].position.x,
435+
game_data.box_texture_regions[key].position.y,
436+
game_data.box_texture_regions[key].size.x,
437+
game_data.box_texture_regions[key].size.y
438+
]
439+
var key_name : String = RetroHubGameData.BoxTextureRegions.keys()[key]
440+
box_texture_regions[key_name.to_lower()] = region_data
441+
game_data_raw["box_texture_regions"] = box_texture_regions
442+
420443
var file := FileAccess.open(metadata_path, FileAccess.WRITE)
421444
if not file:
422445
push_error("Error when opening file %s!" % metadata_path)

source/Media.gd

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,51 @@ func compute_blurhash(game_data: RetroHubGameData) -> void:
372372
FileUtils.ensure_path(file_path)
373373
JSONUtils.save_json_file(json_data, file_path)
374374

375+
func get_box_texture_region(data: RetroHubGameData, media: RetroHubGameMediaData, type: RetroHubGameData.BoxTextureRegions, rotate: bool = true) -> Texture2D:
376+
if not data.box_texture_regions.has(type) or not media.box_texture:
377+
return null
378+
379+
var coords_raw : Rect2 = data.box_texture_regions[type]
380+
var _offset_1 := coords_raw.position
381+
var _offset_2 := coords_raw.size
382+
383+
var offset : Vector2
384+
var size : Vector2
385+
var rotation : int = 0
386+
387+
# Coords embed text direction. We infer it by the xy ordering of both coords.
388+
# 90 degrees (text up-to-down)
389+
if _offset_1.x >= _offset_2.x and _offset_1.y < _offset_2.y:
390+
offset = Vector2(_offset_2.x, _offset_1.y)
391+
size = Vector2(_offset_1.x, _offset_2.y) - offset
392+
rotation = 90
393+
# 180 degrees (text right-to-left)
394+
elif _offset_1.x >= _offset_2.x and _offset_1.y >= _offset_2.y:
395+
offset = Vector2(_offset_2.x, _offset_2.y)
396+
size = Vector2(_offset_1.x, _offset_1.y) - offset
397+
rotation = 180
398+
# -90 degrees (text down-to-up)
399+
elif _offset_1.x < _offset_2.x and _offset_1.y >= _offset_2.y:
400+
offset = Vector2(_offset_1.x, _offset_2.y)
401+
size = Vector2(_offset_2.x, _offset_1.y) - offset
402+
rotation = -90
403+
else:
404+
offset = Vector2(_offset_1.x, _offset_1.y)
405+
size = Vector2(_offset_2.x, _offset_2.y) - offset
406+
407+
var image := media.box_texture.get_image()
408+
var image_size := Vector2(image.get_width(), image.get_height())
409+
var offset_i := Vector2i((offset * image_size).round())
410+
var size_i := Vector2i((size * image_size).round())
411+
var blit_image := Image.create(size_i.x, size_i.y, false, image.get_format())
412+
blit_image.blit_rect(image, Rect2i(offset_i, size_i), Vector2i.ZERO)
413+
if rotate:
414+
match rotation:
415+
-90:
416+
blit_image.rotate_90(CLOCKWISE)
417+
90:
418+
blit_image.rotate_90(COUNTERCLOCKWISE)
419+
180:
420+
blit_image.rotate_180()
421+
422+
return ImageTexture.create_from_image(blit_image)

source/data/GameData.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
extends Resource
22
class_name RetroHubGameData
33

4+
enum BoxTextureRegions {
5+
BACK,
6+
SPINE,
7+
FRONT,
8+
}
9+
410
# Sorter function
511
static func sort(a: RetroHubGameData, b: RetroHubGameData):
612
return a.name.naturalnocasecmp_to(b.name) == -1
@@ -24,6 +30,7 @@ func copy_from(other: RetroHubGameData) -> void:
2430
play_count = other.play_count
2531
last_played = other.last_played
2632
emulator = other.emulator
33+
box_texture_regions = other.box_texture_regions.duplicate()
2734

2835
## Whether this game already has metadata; if it doesn't, you should
2936
## present a much simpler view of it
@@ -88,3 +95,8 @@ var last_played : String
8895
## Custom emulator to run this game on, overriding default values.
8996
## If non-empty, specifies a valid emulator name
9097
var emulator : String
98+
99+
## Information about box texture regions. This is a dictionary of
100+
## Rect2 dimensions, with range [0.0 , 1.0] delimiting the regions of the box texture.
101+
## (use `RetroHubMedia.get_box_texture_region` to fetch the region's sub-Texture)
102+
var box_texture_regions : Dictionary = {}

0 commit comments

Comments
 (0)