-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.gd
265 lines (191 loc) · 5.96 KB
/
main.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
extends Control
@onready var app_name = "MyCollection"
@onready var version = "1.0.2"
@onready var screen_size = DisplayServer.screen_get_size()
@onready var window_size = DisplayServer.window_get_size()
@onready var movie_list = []
@onready var search_wait = true
@onready var window_mode = 0
@onready var grid_columns = 5
@onready var grid_rows = 2
@onready var grid = $VBoxContainer/ScrollContainer/MarginContainer/GridContainer
@onready var searchbar = $VBoxContainer/MarginContainer/HBoxContainer/LineEditSearch
@onready var items_amount = $VBoxContainer/MarginContainer/HBoxContainer/LabelAmount
@onready var searching_wait = $ColorRect
@onready var path = "/media/sda7/Programming/Godot4/movies"
func _ready():
# set app name
DisplayServer.window_set_title(app_name + " " + version)
# hide loading screen
searching_wait.visible = false
# get path
path = get_app_path()
# read config
read_config(path)
# get movie list folders
movie_list = get_files(path, "folders")
print(movie_list)
# show error if nothing found
if movie_list == []:
searching_wait.get_node("Label").text = "No folder found"
searching_wait.visible = true
#show movies
load_movies(movie_list)
# resize signal
get_tree().get_root().size_changed.connect(set_sizes)
# grab focus on search bar
searchbar.grab_focus()
# set window mode button sizes
DisplayServer.window_set_mode(window_mode)
set_sizes()
func get_app_path():
#var path = ""
if OS.has_feature("editor"):
#path = ProjectSettings.globalize_path("res://") # use this to set it to res://
pass
else:
path = OS.get_executable_path().get_base_dir()
path = path.path_join("") # + "/"
print(path)
return path
func read_config(config_path):
var config_file = FileAccess.open(config_path + "MyCollection.cfg", FileAccess.READ)
if config_file:
var config = JSON.parse_string(config_file.get_as_text())
print(config)
grid_columns = int(config["grid_columns"])
grid_rows = int(config["grid_rows"])
window_mode = int(config["window_mode"])
var new_path = config["collection_path"]
if new_path != "":
path = new_path
func set_sizes():
grid.columns = grid_columns
window_size = DisplayServer.window_get_size()
for child in grid.get_children():
child.custom_minimum_size.x = window_size.x/grid_columns - 30
child.custom_minimum_size.y = window_size.y/grid_rows - 44#45
func get_files(path, what):
var folders = []
var files = []
var dir = DirAccess.open(path)
if dir:
dir.list_dir_begin()
var item = dir.get_next()
while item != "":
if dir.current_is_dir():
folders.append(item)
else:
files.append(item)
item = dir.get_next()
folders.sort()
files.sort()
if what == "folders":
return folders
elif what == "files":
return files
else:
return folders + files
func create_movie_title(movie_title):
## old method split at 24 char
var factor = len(movie_title)/24.0
if factor > 1:
for i in range(int(factor)):
movie_title = movie_title.insert(24*(i+1), "\n")
return movie_title
func load_movies(movie_list):
# clear grid straight away
for child in grid.get_children():
child.free()
# show movies
for movie in movie_list:
var button = Button.new()
button.name = movie
# set movie title
button.text = create_movie_title(movie)
button.size_flags_horizontal = SIZE_EXPAND_FILL
#button.size_flags_vertical = SIZE_EXPAND_FILL
# get first image file
var movie_files = get_files(path.path_join(movie), "all")
var image_file = ""
for file in movie_files:
if file.ends_with(".jpg") or file.ends_with(".png"):
image_file = file
break
# load image from disk
var image = Image.new()
image.load(path.path_join(movie).path_join(image_file))
var image_texture = ImageTexture.new()
image_texture.set_image(image)
# set button image
button.icon = image_texture
button.icon_alignment = HORIZONTAL_ALIGNMENT_CENTER
button.vertical_icon_alignment = VERTICAL_ALIGNMENT_TOP
button.expand_icon = true
# set tooltip
var tooltip_text = ""
for i in movie_files:
tooltip_text += i + "\n"
button.tooltip_text = tooltip_text
# connect signal
button.pressed.connect(open_folder.bind(path + movie))
# create button
grid.add_child(button)
# set button size
set_sizes()
#show amount of visible items
count_visible_items()
func open_folder(path):
print("Open: " + path)
OS.shell_open(path)
func _on_search_text_changed(new_text):
if search_wait == true:
# show loading screen
searching_wait.visible = true
# wait and search
search_wait = false
await get_tree().create_timer(0.75).timeout
on_search(searchbar.text)
search_wait = true
func on_search(new_text):
print("Search: " + new_text)
# if searchbar is empty, show all
if new_text == "":
for child in grid.get_children():
child.visible = true
# filter buttons
else:
for child in grid.get_children():
if new_text.to_lower() in child.name.to_lower():
child.visible = true
else:
child.visible = false
# hide loading screen
searching_wait.visible = false
#show amount of visible items
count_visible_items()
func count_visible_items():
var items_visible = 0
for i in grid.get_children():
if i.visible == true:
items_visible += 1
items_amount.text = str(items_visible)
# shortcuts
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("toggle_fullscreen"):
if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_MAXIMIZED:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
else:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MAXIMIZED)
if event.is_action_pressed("escape"):
searchbar.clear()
searchbar.grab_focus()
$VBoxContainer/ScrollContainer.scroll_vertical = 0
# press ui_down from search bar focuses first item
func _input(event: InputEvent) -> void:
if event.is_action_released("ui_down"):
if searchbar.has_focus():
for i in grid.get_children():
if i.visible == true:
i.grab_focus()
break