add custom image picker

This commit is contained in:
Fabien Freling 2020-06-16 19:28:50 +02:00
parent 42fd81b914
commit e90eb40630
12 changed files with 310 additions and 43 deletions

BIN
assets/icon_folder.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon_folder.png-b3a2a9bdd4c609fb3eeaba30c71ceb5a.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/icon_folder.png"
dest_files=[ "res://.import/icon_folder.png-b3a2a9bdd4c609fb3eeaba30c71ceb5a.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon_parent_folder.png-28fe9c21193d111a87cc30b6d49f5418.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/icon_parent_folder.png"
dest_files=[ "res://.import/icon_parent_folder.png-28fe9c21193d111a87cc30b6d49f5418.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View file

@ -136,7 +136,7 @@ permissions/process_outgoing_calls=false
permissions/read_calendar=false permissions/read_calendar=false
permissions/read_call_log=false permissions/read_call_log=false
permissions/read_contacts=false permissions/read_contacts=false
permissions/read_external_storage=false permissions/read_external_storage=true
permissions/read_frame_buffer=false permissions/read_frame_buffer=false
permissions/read_history_bookmarks=false permissions/read_history_bookmarks=false
permissions/read_input_state=false permissions/read_input_state=false

View file

@ -14,6 +14,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://src/DepthButton.gd" "path": "res://src/DepthButton.gd"
}, { }, {
"base": "Panel",
"class": "ImagePicker",
"language": "GDScript",
"path": "res://src/ImagePicker.gd"
}, {
"base": "PopupPanel", "base": "PopupPanel",
"class": "NewGamePanel", "class": "NewGamePanel",
"language": "GDScript", "language": "GDScript",
@ -31,6 +36,7 @@ _global_script_classes=[ {
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"DepthButton": "", "DepthButton": "",
"ImagePicker": "",
"NewGamePanel": "", "NewGamePanel": "",
"Piece": "", "Piece": "",
"Taquin": "" "Taquin": ""
@ -56,8 +62,8 @@ window/energy_saving/keep_screen_on=false
window/handheld/orientation="portrait" window/handheld/orientation="portrait"
window/size/width.mobile=600 window/size/width.mobile=600
window/size/height.mobile=1200 window/size/height.mobile=1200
window/stretch/mode.mobile="2d"
window/stretch/aspect.mobile="expand" window/stretch/aspect.mobile="expand"
window/stretch/mode.mobile="2d"
[filesystem] [filesystem]

60
src/FileList.gd Normal file
View file

@ -0,0 +1,60 @@
extends ItemList
signal file_selected(path)
var _parent_display_name := "<parent directory>"
var _sep := "/"
var _dir := Directory.new()
export var root_dir: String = OS.get_system_dir(OS.SYSTEM_DIR_PICTURES)
export var walkable := false
export var folder_texture: Texture = null
export var parent_folder_texture: Texture = null
# Called when the node enters the scene tree for the first time.
func _ready():
if root_dir != null:
populate(root_dir)
func populate(dir: String) -> void:
print_debug(dir)
clear()
if _dir.change_dir(dir) != OK:
print_debug("Cannot open path ", dir)
assert(false)
return
_dir.list_dir_begin()
var file_name := _dir.get_next()
while file_name != "":
if _dir.current_is_dir():
if not walkable:
pass
elif file_name == "..":
add_item(_parent_display_name, parent_folder_texture)
elif file_name.begins_with("."):
pass
else:
add_item(file_name, folder_texture)
else:
var ext := file_name.get_extension()
if ext == "jpeg" or ext == "jpg" or ext == "png":
var file_path := _dir.get_current_dir() + _sep + file_name
add_item(file_name, load(file_path))
file_name = _dir.get_next()
_dir.list_dir_end()
sort_items_by_text()
func _on_FileList_item_selected(index):
var item_text := get_item_text(index)
if item_text == _parent_display_name:
item_text = ".."
if _dir.dir_exists(item_text):
populate(item_text)
else:
var full_path = root_dir + _sep + item_text
print_debug(full_path)
emit_signal("file_selected", full_path)

19
src/FileList.tscn Normal file
View file

@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=2]
[ext_resource path="res://src/FileList.gd" type="Script" id=1]
[ext_resource path="res://assets/icon_parent_folder.png" type="Texture" id=2]
[ext_resource path="res://assets/icon_folder.png" type="Texture" id=3]
[node name="FileList" type="ItemList"]
anchor_right = 1.0
anchor_bottom = 1.0
max_columns = 4
fixed_icon_size = Vector2( 160, 160 )
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
root_dir = "res://assets/artworks"
folder_texture = ExtResource( 3 )
parent_folder_texture = ExtResource( 2 )
[connection signal="item_selected" from="." to="." method="_on_FileList_item_selected"]

13
src/ImagePicker.gd Normal file
View file

@ -0,0 +1,13 @@
tool
class_name ImagePicker
extends Panel
signal file_selected(path)
onready var fs_list := $VBoxContainer/TabContainer/Filesystem/FileList
func _ready():
fs_list.populate(OS.get_system_dir(OS.SYSTEM_DIR_PICTURES))
func _on_FileList_file_selected(path):
emit_signal("file_selected", path)

69
src/ImagePicker.tscn Normal file
View file

@ -0,0 +1,69 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://src/ImagePicker.gd" type="Script" id=1]
[ext_resource path="res://src/FileList.tscn" type="PackedScene" id=2]
[node name="ImagePicker" type="Panel"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="VBoxContainer" type="VBoxContainer" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
rect_clip_content = true
alignment = 1
__meta__ = {
"_edit_use_anchors_": false
}
[node name="TabContainer" type="TabContainer" parent="VBoxContainer"]
margin_right = 1024.0
margin_bottom = 512.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Taqin" type="Tabs" parent="VBoxContainer/TabContainer"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 8.0
margin_top = 61.0
margin_right = -8.0
margin_bottom = -8.0
rect_clip_content = true
[node name="FileList" parent="VBoxContainer/TabContainer/Taqin" instance=ExtResource( 2 )]
[node name="Filesystem" type="Tabs" parent="VBoxContainer/TabContainer"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 8.0
margin_top = 61.0
margin_right = -8.0
margin_bottom = -8.0
[node name="FileList" parent="VBoxContainer/TabContainer/Filesystem" instance=ExtResource( 2 )]
root_dir = "/home/fabs/Pictures"
walkable = true
[node name="Button" type="Button" parent="VBoxContainer"]
margin_top = 520.0
margin_right = 1024.0
margin_bottom = 600.0
rect_min_size = Vector2( 0, 80 )
text = "Cancel"
flat = true
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="item_rect_changed" from="." to="." method="_on_ImagePicker_item_rect_changed"]
[connection signal="resized" from="." to="." method="_on_ImagePicker_resized"]
[connection signal="file_selected" from="VBoxContainer/TabContainer/Taqin/FileList" to="." method="_on_FileList_file_selected"]
[connection signal="file_selected" from="VBoxContainer/TabContainer/Filesystem/FileList" to="." method="_on_FileList_file_selected"]

View file

@ -88,22 +88,22 @@ func fade_out():
tween.interpolate_callback(self, fade_duration, "hide") tween.interpolate_callback(self, fade_duration, "hide")
tween.start() tween.start()
func flip_over(): func flip_over(new_panel: Panel) -> void:
tween.remove_all() tween.remove_all()
var mid_duration = flip_duration / 2.0 var mid_duration = flip_duration / 2.0
tween.interpolate_property(self, "rect_scale:x", 1.0, 0.0, mid_duration) tween.interpolate_property(self, "rect_scale:x", 1.0, 0.0, mid_duration)
tween.interpolate_callback($Panel, mid_duration, "hide") tween.interpolate_callback($Panel, mid_duration, "hide")
tween.interpolate_callback($EditPanel, mid_duration, "show") tween.interpolate_callback(new_panel, mid_duration, "show")
tween.interpolate_property(self, "rect_scale:x", 0.0, 1.0, mid_duration, Tween.TRANS_LINEAR, Tween.EASE_IN, mid_duration) tween.interpolate_property(self, "rect_scale:x", 0.0, 1.0, mid_duration, Tween.TRANS_LINEAR, Tween.EASE_IN, mid_duration)
tween.start() tween.start()
func flip_back(): func flip_back(previous_panel: Panel) -> void:
_update_description() _update_description()
tween.remove_all() tween.remove_all()
var mid_duration = flip_duration / 2.0 var mid_duration = flip_duration / 2.0
tween.interpolate_property(self, "rect_scale:x", 1.0, 0.0, mid_duration) tween.interpolate_property(self, "rect_scale:x", 1.0, 0.0, mid_duration)
tween.interpolate_callback($Panel, mid_duration, "show") tween.interpolate_callback($Panel, mid_duration, "show")
tween.interpolate_callback($EditPanel, mid_duration, "hide") tween.interpolate_callback(previous_panel, mid_duration, "hide")
tween.interpolate_property(self, "rect_scale:x", 0.0, 1.0, mid_duration, Tween.TRANS_LINEAR, Tween.EASE_IN, mid_duration) tween.interpolate_property(self, "rect_scale:x", 0.0, 1.0, mid_duration, Tween.TRANS_LINEAR, Tween.EASE_IN, mid_duration)
tween.start() tween.start()
@ -208,21 +208,18 @@ func _on_NewGamePanel_about_to_show():
# $Panel/Start.grab_focus() # $Panel/Start.grab_focus()
fade_in() fade_in()
func _on_Edit_pressed():
flip_over()
func _on_Edit_Cancel_pressed(): func _on_Edit_Cancel_pressed():
columns_spinbox.value = preferences.get_value("game", "custom_columns", normal_columns) columns_spinbox.value = preferences.get_value("game", "custom_columns", normal_columns)
rows_spinbox.value = preferences.get_value("game", "custom_rows", normal_rows) rows_spinbox.value = preferences.get_value("game", "custom_rows", normal_rows)
iterations_spinbox.value = preferences.get_value("game", "custom_shuffle_iterations", normal_iterations) iterations_spinbox.value = preferences.get_value("game", "custom_shuffle_iterations", normal_iterations)
flip_back() flip_back($EditPanel)
func _on_Edit_Save_pressed(): func _on_Edit_Save_pressed():
print_debug(columns_spinbox.value) print_debug(columns_spinbox.value)
preferences.set_value("game", "custom_columns", columns_spinbox.value as int) preferences.set_value("game", "custom_columns", columns_spinbox.value as int)
preferences.set_value("game", "custom_rows", rows_spinbox.value as int) preferences.set_value("game", "custom_rows", rows_spinbox.value as int)
preferences.set_value("game", "custom_shuffle_iterations", iterations_spinbox.value as int) preferences.set_value("game", "custom_shuffle_iterations", iterations_spinbox.value as int)
flip_back() flip_back($EditPanel)
func _on_Easy_pressed(): func _on_Easy_pressed():
_update_description() _update_description()
@ -235,10 +232,13 @@ func _on_Hard_pressed():
func _on_Custom_pressed(): func _on_Custom_pressed():
_update_description() _update_description()
flip_over() flip_over($EditPanel)
func _on_LoadImage_pressed(): func _on_LoadImage_pressed():
$FileDialog.popup_centered(rect_size) 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): func _on_FileDialog_file_selected(path: String):
var texture := load(path) var texture := load(path)
@ -256,3 +256,25 @@ func _on_FileDialog_file_selected(path: String):
_artwork_path = path _artwork_path = path
else: else:
_artwork_path = cached_artwork_path _artwork_path = cached_artwork_path
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
flip_back($ImagePicker)

File diff suppressed because one or more lines are too long