load custom artwork

This commit is contained in:
Fabien Freling 2020-06-10 17:15:33 +02:00
parent 8cc55513b7
commit 5e1e27b2cb
10 changed files with 225 additions and 47 deletions

View file

@ -15,6 +15,9 @@ 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
@ -23,6 +26,8 @@ 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
@ -34,6 +39,8 @@ onready var normal_button = $Panel/VBoxContainer/Difficulty/Normal
onready var hard_button = $Panel/VBoxContainer/Difficulty/Hard
onready var custom_button = $Panel/VBoxContainer/HBoxContainer/Custom
onready var preview = $Panel/VBoxContainer/ArtworkSource/Preview
onready var columns_spinbox = $EditPanel/VBoxContainer/Columns/SpinBox
onready var rows_spinbox = $EditPanel/VBoxContainer/Rows/SpinBox
onready var iterations_spinbox = $EditPanel/VBoxContainer/Iterations/SpinBox
@ -173,6 +180,8 @@ 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)
@ -191,6 +200,10 @@ func _on_NewGamePanel_about_to_show():
iterations_spinbox.value = preferences.get_value("game", "custom_shuffle_iterations", normal_iterations)
_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)
# $Panel/Start.grab_focus()
fade_in()
@ -223,3 +236,23 @@ func _on_Hard_pressed():
func _on_Custom_pressed():
_update_description()
flip_over()
func _on_LoadImage_pressed():
$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