-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemblance.rb
204 lines (170 loc) · 5.79 KB
/
Semblance.rb
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
Shoes.setup do
gem 'inifile'
end
require 'inifile'
# Setup of INI for accessing default values
if (File.file?('./slides.ini')) then
ini_file = IniFile.load('./slides.ini')
else
# Create new IniFile
ini_file = IniFile.new
ini_file["Default"] = {
"Length" => '3',
"Shuffle" => '0'
}
ini_file.filename = "./slides.ini"
ini_file.write()
end
# Get default values from a config file
data = ini_file["DEFAULT"]
configured_slide_length = data["Length"].to_i
if data["Shuffle"] == '0' then
configured_shuffle = false
else
configured_shuffle = true
end
Shoes.app(title: "Semblance", width: 1200, height: 800) do
background "#7A1D1F"
title("Semblance",
top: 60,
align: "center",
font: "Century Gothic",
stroke: white)
stack() {
flow(margin_left:'38%', margin_top:'40%') {
splash = image("./RUby23.png")
}
flow(margin_left:'38%', margin_top:'15%'){
selected_folder = ''
button("Select Folder") {
selected_folder = ask_open_folder
}
button("Start Semblance") {
Shoes.app(fullscreen: true) do
background "#000000"
@original_images_list = nil
@shuffled_images_list = nil
@current_images_list = nil
@images_length = 0
@shuffle = false
@slide_length = 3
@current_index = 0
@folder = ''
if Gem.win_platform? then
@screen_width = `powershell Get-WmiObject -Class Win32_DesktopMonitor`.scan(/ScreenWidth \s+ : (\d+)/).flatten
@screen_width = @screen_width.first.to_i
@screen_height = `powershell Get-WmiObject -Class Win32_DesktopMonitor`.scan(/ScreenHeight \s+ : (\d+)/).flatten
@screen_height = @screen_height.first.to_i
else
@screen_width, @screen_height = `xrandr`.scan(/current (\d+) x (\d+)/).flatten # Not sure what is actually happening here
@screen_width = @screen_width.to_i
@screen_height = @screen_height.to_i
end
stack() {
slideshow_start(selected_folder, configured_shuffle,
configured_slide_length)
x1 = 0
y1 = 0
slide = image
new_slide(slide, true)
every(@slide_length) do
new_slide(slide, true)
end
keypress do |k|
if (k.inspect == ":right") then
new_slide(slide, true)
end
if (k.inspect == ":left") then
new_slide(slide, false)
end
if (k.inspect == ":escape") then
close()
end
end
}
end
}
}
}
end
# Methods
def new_slide(slide, forward)
if (forward) then
slide.path = next_image
else
slide.path = prev_image
end
x1, y1 = scale_image(slide.full_width, slide.full_height, @screen_width, @screen_height)
w, h = center_image(x1, y1, @screen_width, @screen_height)
slide.style(:width => x1, :height => y1, :displace_left => w, :displace_top => h)
end
def slideshow_start(folder, shuffle_set, slide_length)
@original_images_list = Dir.entries(folder).select {|f| !File.directory? f}
accepted_exts = [".jpg", ".bmp", ".png", ".gif"]
# Remove from the slideshow files that are not images (ie, webms)
@original_images_list = @original_images_list.select {|elem|
accepted_exts.include? File.extname(elem)
}
@images_length = @original_images_list.length # makes code cleaner
if (shuffle_set) then
@shuffle = shuffle_set
@shuffled_images_list = @original_images_list.shuffle
@current_images_list = @shuffled_images_list
else
@current_images_list = @original_images_list
end
@folder = folder
@slide_length = slide_length
end
def next_image
end_of_list = (@current_index + 1) > (@images_length - 1)
if (end_of_list) then
@current_index = 0
else
@current_index += 1
end
image_index = @current_index
img_path = @current_images_list[image_index]
return @folder + '/' + img_path
end
def prev_image
begin_of_list = (@current_index - 1) < 0
if (begin_of_list) then
@current_index = @images_length - 1
else
@current_index = @current_index - 1
end
image_index = @current_index
img_path = @current_images_list[image_index]
return @folder + '/' + img_path
end
def scale_image(x1, y1, x2, y2)
aspect_ratio = x1.to_f / y1.to_f
screen_ratio = x2.to_f / y2.to_f
if x1 > x2 or y1 > y2 then
if aspect_ratio <= 1 then # vertical image
height = y2.to_f
width = height * aspect_ratio
else # horizontal image
width = x2.to_f
height = width / aspect_ratio
end
if width.to_i > x2 or height.to_i > y2 then
width.to_i
end
while width.to_i > x2 or height.to_i > y2 do
width *= 0.98
height *= 0.98
end
return width.to_i, height.to_i
else
return x1, y1
end
end
def center_image(w, h, x2, y2)
q = x2 - w
q = q / 2
r = y2 - h
r = r / 2
return q, r
end