Skip to content

Commit 753c637

Browse files
committed
fix: improve truncation of decimal digits in downloaded size
...and fix a missing space in the text
1 parent b697373 commit 753c637

1 file changed

Lines changed: 45 additions & 25 deletions

File tree

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,72 @@
1-
function draw_downloadprogress(argument0, argument1, argument2, argument3) {
2-
// draw_downloadprogress(caption, desc, downloaded_size, total_size)
3-
var caption, desc, done, total, done_mb, total_mb, done_text, total_text, text, percent, x1, y1;
4-
caption = argument0
5-
desc = argument1
6-
done = argument2
7-
total = argument3
8-
percent = done/total
9-
done_mb = done/power(1024, 2)
10-
total_mb = total/power(1024, 2)
11-
x1 = floor(rw / 2 - 150)
12-
y1 = floor(rh / 2 - 50)
1+
function draw_downloadprogress(caption, desc, downloaded_bytes, total_bytes) {
2+
// Draw window
3+
var x1 = floor(rw / 2 - 150)
4+
var y1 = floor(rh / 2 - 50)
135
draw_theme_color()
146
draw_window(x1, y1, x1 + 300, y1 + 100)
157
draw_theme_font(font_main_bold)
168
draw_text_dynamic(x1 + 16, y1 + 16, caption)
179
draw_theme_font(font_main)
1810
draw_set_halign(fa_center)
1911
draw_text_dynamic(floor(rw / 2), y1 + 40, desc)
12+
13+
// Draw progress bar
14+
var percent_downloaded = downloaded_bytes / total_bytes
15+
var percent_downloaded_str = string(round(percent_downloaded * 100)) + "%"
16+
2017
if (theme = 3) {
2118
draw_theme_color()
2219
draw_line(x1 + 30, y1 + 77, x1 + 270, y1 + 77)
2320
draw_set_color(accent[5])
2421
draw_set_alpha(0.5)
25-
draw_rectangle(x1 + 30, y1 + 77 - 1, x1 + 30 + percent * 240, y1 + 77 + 1, 0)
22+
draw_rectangle(x1 + 30, y1 + 77 - 1, x1 + 30 + percent_downloaded * 240, y1 + 77 + 1, 0)
2623
draw_set_alpha(1)
27-
draw_rectangle(x1 + 30 + 1, y1 + 77 - 1, x1 + 30 + clamp(percent * 240, 1, 239), y1 + 77 + 1, 0)
28-
draw_rectangle(x1 + 30, y1 + 77, x1 + 30 + percent * 240, y1 + 77, 0)
24+
draw_rectangle(x1 + 30 + 1, y1 + 77 - 1, x1 + 30 + clamp(percent_downloaded * 240, 1, 239), y1 + 77 + 1, 0)
25+
draw_rectangle(x1 + 30, y1 + 77, x1 + 30 + percent_downloaded * 240, y1 + 77, 0)
2926
draw_theme_color()
3027
} else {
3128
draw_set_color(10512464)
32-
draw_rectangle(x1 + 30, y1 + 60, x1 + 30 + percent * 240, y1 + 80, 0)
29+
draw_rectangle(x1 + 30, y1 + 60, x1 + 30 + percent_downloaded * 240, y1 + 80, 0)
3330
draw_theme_color()
3431
draw_rectangle(x1 + 30, y1 + 60, x1 + 270, y1 + 80, 1)
3532
}
36-
if (percent > 0.5) draw_set_color(c_white)
37-
if (total <= 0) {
38-
done_text = "-.--"
39-
total_text = "-.--"
33+
if (percent_downloaded > 0.5) draw_set_color(c_white)
34+
35+
// Draw text
36+
var downloaded_size = downloaded_bytes / 1024
37+
var total_size = total_bytes / 1024
38+
var decimal_digits = 0;
39+
var size_unit = "KB";
40+
41+
// If size > 1 MB, show in MB
42+
if (total_size >= 1024) {
43+
downloaded_size = downloaded_size / 1024
44+
total_size = total_size / 1024
45+
decimal_digits = 2;
46+
size_unit = "MB";
47+
}
48+
49+
var downloaded_size_str, total_size_str;
50+
if (total_bytes <= 0) {
51+
downloaded_size_str = "-.--"
52+
total_size_str = "-.--"
4053
} else {
41-
done_text = string_format(done_mb, 0, 2)
42-
total_text = string_format(total_mb, 0, 2)
54+
downloaded_size_str = format_size(downloaded_size)
55+
total_size_str = format_size(total_size)
4356
}
44-
text = done_text + "/" + total_text + " MB (" + string(round(percent * 100)) + "%)"
57+
var text = downloaded_size_str + "/" + total_size_str + " " + size_unit + " (" + percent_downloaded_str + ")"
58+
4559
if (theme = 3 && !fdark) draw_set_color(0)
4660
draw_text_dynamic(floor(rw / 2), y1 + 65 - 5 * (theme = 3), text)
4761
draw_theme_color()
4862
draw_set_halign(fa_left)
63+
}
4964

5065

51-
52-
}
66+
function format_size(size) {
67+
var decimal_digits = 2;
68+
if (size >= 10) decimal_digits = 1;
69+
if (size >= 100) decimal_digits = 0;
70+
71+
return string_format(size, 0, decimal_digits);
72+
}

0 commit comments

Comments
 (0)