list all artworks from res://

I cannot simply explore the res:// filesystem on Android.
As a workaround, I create a static list of embedded artworks.
This commit is contained in:
Fabien Freling 2020-06-17 19:11:20 +02:00
parent e90eb40630
commit cd49ba8afc
8 changed files with 112 additions and 107 deletions

View file

@ -2,9 +2,10 @@ tool
class_name NewGamePanel
extends PopupPanel
signal start_triggered(config)
signal start_triggered(config, texture)
const pref_path := "user://preferences.cfg"
const cached_artwork_path := "user://artwork.dat"
const easy_columns := 3
const easy_rows := 3
@ -15,19 +16,17 @@ const normal_iterations := 10
const hard_columns := 5
const hard_rows := 5
const hard_iterations := 30
const default_artwork_path := "res://assets/hokusai.jpg"
const Utils = preload("res://src/Utils.gd")
export var window_scale_factor = 0.9 # how big the popup will be compared to screen
export var default_artwork_texture: Texture
var preferences = ConfigFile.new()
var fade_duration = 0.2
var fade_scale_factor = 0.9
var flip_duration = 0.4
var _artwork_path: String = default_artwork_path
onready var popup = $"."
onready var panel = $Panel
onready var edit_panel = $EditPanel
@ -153,6 +152,15 @@ func _update_description():
desc.add_text("%d" % [iterations])
desc.pop()
func _set_artwork(tex: Texture) -> void:
preview.texture = tex
var file = File.new()
if file.open(cached_artwork_path, File.WRITE) != OK:
assert(false)
file.store_var(tex.get_data(), true)
file.close()
#
# Signals
#
@ -180,12 +188,10 @@ func _on_Start_pressed():
preferences.set_value("game", "columns", columns_spinbox.value)
preferences.set_value("game", "rows", rows_spinbox.value)
preferences.set_value("game", "shuffle_iterations", iterations_spinbox.value)
preferences.set_value("game", "artwork_path", _artwork_path)
preferences.save(pref_path)
emit_signal("start_triggered", preferences)
emit_signal("start_triggered", preferences, preview.texture)
fade_out()
func _on_NewGamePanel_about_to_show():
@ -201,9 +207,9 @@ func _on_NewGamePanel_about_to_show():
_update_description()
_artwork_path = preferences.get_value("game", "artwork_path", default_artwork_path)
print_debug("artwork path: ", _artwork_path)
preview.texture = Utils.load_texture_from_path(_artwork_path)
preview.texture = Utils.deserialize_texture(cached_artwork_path)
if preview.texture == null:
preview.texture = default_artwork_texture
# $Panel/Start.grab_focus()
fade_in()
@ -236,45 +242,32 @@ func _on_Custom_pressed():
func _on_LoadImage_pressed():
flip_over($ImagePicker)
# if OS.get_name() == "Android":
# $FileDialog.current_dir = "/storage/emulated/0/"
# $FileDialog.popup_centered(rect_size)
func _on_FileDialog_file_selected(path: String):
var texture := load(path)
if texture == null:
print_debug("Cannot load image from path: ", path)
return
preview.texture = texture
var directory = Directory.new()
# TODO: remove previous artwork.png, artwork.jpg files
var cached_artwork_path := "user://artwork.%s" % [path.get_extension()]
var error = directory.copy(path, cached_artwork_path)
if error != OK:
print_debug("Cannot cache image")
_artwork_path = path
else:
_artwork_path = cached_artwork_path
#func _on_ImagePicker_file_selected(path):
# print_debug(path)
# var texture := load(path)
# if texture == null:
# print_debug("Cannot load image from path: ", path)
# return
# preview.texture = texture
#
# var file = File.new()
# file.store_var(texture)
#
# var directory = Directory.new()
# # TODO: remove previous artwork.png, artwork.jpg files
# var cached_artwork_path := "user://artwork.%s" % [path.get_extension()]
# var error = directory.copy(path, cached_artwork_path)
# if error != OK:
# print_debug("Cannot cache image")
# _artwork_path = path
# else:
# _artwork_path = cached_artwork_path
# flip_back($ImagePicker)
func _on_FileDialog_dir_selected(dir):
print_debug("dir selected")
func _on_ImagePicker_file_selected(path):
print_debug(path)
var texture := load(path)
if texture == null:
print_debug("Cannot load image from path: ", path)
return
preview.texture = texture
var directory = Directory.new()
# TODO: remove previous artwork.png, artwork.jpg files
var cached_artwork_path := "user://artwork.%s" % [path.get_extension()]
var error = directory.copy(path, cached_artwork_path)
if error != OK:
print_debug("Cannot cache image")
_artwork_path = path
else:
_artwork_path = cached_artwork_path
func _on_ImagePicker_canceled():
flip_back($ImagePicker)
func _on_ImagePicker_texture_selected(texture):
_set_artwork(texture)
flip_back($ImagePicker)