/
common.js
449 lines (394 loc) · 12.3 KB
/
common.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
String.prototype.calc_width = function (font_str) {
var font = JSON.parse(font_str);
return utils.CalcTextWidth(this, font.Name, font.Size, font.Weight || 400);
}
String.prototype.repeat = function (num) {
if (num >= 0 && num <= 5) {
var g = Math.round(num);
} else {
return "";
}
return new Array(g + 1).join(this);
}
function on_colours_changed() {
get_colours();
brw.scrollbar.setNewColours();
brw.repaint();
}
function on_font_changed() {
get_font();
get_metrics();
brw.repaint();
}
function on_get_album_art_done(metadb, art_id, image) {
if (!image) return;
var max_size = 300;
for (var i = 0; i < brw.groups.length; i++) {
if (brw.groups[i].metadb && brw.groups[i].metadb.Compare(metadb)) {
if (image.Width > max_size || image.Height > max_size) {
var s = Math.min(max_size / image.Width, max_size / image.Height);
var w = Math.floor(image.Width * s);
var h = Math.floor(image.Height * s);
image.Resize(w, h);
}
var cached_filename = generate_filename(brw.groups[i].cachekey, art_id);
image.SaveAs(cached_filename);
images.cache[cached_filename] = image;
brw.groups[i].cover_image = image;
brw.repaint();
break;
}
}
}
function on_size() {
ww = window.Width;
wh = window.Height;
brw.setSize();
}
function clamp(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
function update_extra_font_size(step) {
var tmp = clamp(ppt.extra_font_size + step, 0, 10);
if (ppt.extra_font_size != tmp) {
ppt.extra_font_size = tmp;
window.SetProperty("SMOOTH.EXTRA.FONT.SIZE", ppt.extra_font_size);
get_font();
get_metrics();
get_images();
brw.repaint();
}
}
function get_images() {
var gb;
var cover_size = 200;
var button_size = scale(24);
var background = setAlpha(g_colour_text, 10);
var text_colour = setAlpha(g_colour_text, 150);
var image_font = JSON.stringify({"Name":"FontAwesome","Size":40,"Weight":700})
images.noart = utils.CreateImage(cover_size, cover_size);
gb = images.noart.GetGraphics();
gb.FillRectangle(0, 0, cover_size, cover_size, background);
gb.WriteText("NO\nCOVER", image_font, text_colour, 1, 1, cover_size, cover_size, 2, 2);
images.noart.ReleaseGraphics();
images.stream = utils.CreateImage(cover_size, cover_size);
gb = images.stream.GetGraphics();
gb.FillRectangle(0, 0, cover_size, cover_size, background);
gb.WriteText("STREAM", image_font, text_colour, 1, 1, cover_size, cover_size, 2, 2);
images.stream.ReleaseGraphics();
images.all = utils.CreateImage(cover_size, cover_size);
gb = images.all.GetGraphics();
gb.FillRectangle(0, 0, cover_size, cover_size, background);
gb.WriteText("ALL\nITEMS", image_font, text_colour, 1, 1, cover_size, cover_size, 2, 2);
images.all.ReleaseGraphics();
images.reset = utils.CreateImage(button_size, button_size);
gb = images.reset.GetGraphics();
gb.WriteText(chars.close, g_font_awesome, g_colour_text, 0, 0, button_size, button_size, 2, 2);
images.reset.ReleaseGraphics();
images.reset_hover = utils.CreateImage(button_size, button_size);
gb = images.reset_hover.GetGraphics();
gb.WriteText(chars.close, g_font_awesome, RGB(255, 50, 50), 0, 0, button_size, button_size, 2, 2);
images.reset_hover.ReleaseGraphics();
}
function validate_indexes(playlist, item) {
return playlist >=0 && playlist < plman.PlaylistCount && item >= 0 && item < plman.GetPlaylistItemCount(playlist);
}
function play(playlist, item) {
if (validate_indexes(playlist, item)) {
plman.ExecutePlaylistDefaultAction(playlist, item);
}
}
function generate_filename(cachekey, art_id) {
var prefix = art_id == 4 ? "artist" : "front";
return CACHE_FOLDER + prefix + cachekey + ".jpg";
}
0
function get_art(metadb, filename, art_id) {
var img = images.cache[filename];
if (img) return img;
img = utils.LoadImage(filename);
if (img) {
images.cache[filename] = img;
return img;
}
window.SetTimeout(function () {
metadb.GetAlbumArtAsync(window.ID, art_id, false);
}, 10);
return img;
}
function drawImage(gr, img, dst_x, dst_y, dst_w, dst_h, auto_fill, border, opacity) {
if (!img || !dst_w || !dst_h) {
return;
}
if (auto_fill) {
if (img.Width / img.Height < dst_w / dst_h) {
var src_x = 0;
var src_w = img.Width;
var src_h = Math.round(dst_h * img.Width / dst_w);
var src_y = Math.round((img.Height - src_h) / 2);
} else {
var src_y = 0;
var src_w = Math.round(dst_w * img.Height / dst_h);
var src_h = img.Height;
var src_x = Math.round((img.Width - src_w) / 2);
}
gr.DrawImage(img, dst_x, dst_y, dst_w, dst_h, src_x + 3, src_y + 3, src_w - 6, src_h - 6, opacity || 1);
} else {
var s = Math.min(dst_w / img.Width, dst_h / img.Height);
var w = Math.floor(img.Width * s);
var h = Math.floor(img.Height * s);
dst_x += Math.round((dst_w - w) / 2);
dst_y += Math.round((dst_h - h) / 2);
dst_w = w;
dst_h = h;
gr.DrawImage(img, dst_x, dst_y, dst_w, dst_h, 0, 0, img.Width, img.Height, opacity || 1);
}
if (border) {
gr.DrawRectangle(dst_x, dst_y, dst_w - 1, dst_h - 1, 1, border);
}
}
function drawSelectedRectangle(gr, x, y, w, h) {
if (g_themed) {
g_theme.DrawThemeBackground(gr, x, y, w, h);
} else {
gr.FillRectangle(x, y, w, h, g_colour_selection);
}
}
function GetKeyboardMask() {
if (utils.IsKeyPressed(VK_CONTROL))
return KMask.ctrl;
if (utils.IsKeyPressed(VK_SHIFT))
return KMask.shift;
return KMask.none;
}
function button(normal, hover, down) {
this.x = 0;
this.y = 0;
this.w = normal.Width;
this.h = normal.Height;
this.img = [normal, hover, down];
this.state = ButtonStates.normal;
this.update = function (normal, hover, down) {
this.w = normal.Width;
this.h = normal.Height;
this.img = [normal, hover, down];
}
this.draw = function (gr, x, y, alpha) {
this.x = x;
this.y = y;
if (this.img[this.state]) {
gr.DrawImage(this.img[this.state], this.x, this.y, this.w, this.h, 0, 0, this.w, this.h);
}
}
this.repaint = function () {
window.RepaintRect(this.x, this.y, this.w, this.h);
}
this.checkstate = function (event, x, y) {
this.ishover = (x > this.x && x < this.x + this.w - 1 && y > this.y && y < this.y + this.h - 1);
this.old = this.state;
switch (event) {
case "lbtn_down":
switch (this.state) {
case ButtonStates.normal:
case ButtonStates.hover:
this.state = this.ishover ? ButtonStates.down : ButtonStates.normal;
this.isdown = true;
break;
}
break;
case "lbtn_up":
this.state = this.ishover ? ButtonStates.hover : ButtonStates.normal;
this.isdown = false;
break;
case "move":
switch (this.state) {
case ButtonStates.normal:
case ButtonStates.hover:
this.state = this.ishover ? ButtonStates.hover : ButtonStates.normal;
break;
}
break;
}
if (this.state != this.old)
this.repaint();
return this.state;
}
}
function setWallpaperImg() {
if (g_wallpaperImg) g_wallpaperImg.Dispose();
g_wallpaperImg = null;
switch (ppt.wallpapermode) {
case 0:
return;
case 1:
var metadb = fb.GetNowPlaying();
if (metadb) g_wallpaperImg = metadb.GetAlbumArt();
break;
case 2:
if (utils.IsFile(ppt.wallpaperpath)) {
g_wallpaperImg = utils.LoadImage(ppt.wallpaperpath);
} else {
g_wallpaperImg = utils.LoadImage(fb.ProfilePath + ppt.wallpaperpath);
}
break;
}
if (g_wallpaperImg && ppt.wallpaperblurred) {
g_wallpaperImg.StackBlur(ppt.wallpaperblur);
}
}
function height(font) {
return JSON.parse(font).Size + 4;
}
function scale(size) {
return Math.round(size * g_fsize / 12);
}
function _font(name, size, bold) {
var font = {
Name : name,
Size : scale(size),
Weight : bold == 1 ? 700 : 400,
};
return JSON.stringify(font);
}
function get_font() {
var default_font;
if (window.IsDefaultUI) {
default_font = JSON.parse(window.GetFontDUI(FontTypeDUI.playlists));
} else {
default_font = JSON.parse(window.GetFontCUI(FontTypeCUI.items));
}
var name = default_font.Name;
g_fsize = default_font.Size + Math.min(ppt.extra_font_size, 10);
g_font = _font(name, 12, 0);
g_font_bold = _font(name, 14, 1);
g_font_box = _font(name, 10, 1);
g_font_group1 = _font(name, 20, 1);
g_font_group2 = _font(name, 16);
g_font_awesome = _font("FontAwesome", 18);
if (ppt.ratingGUIFX && utils.CheckFont("Guifx v2 Transports")) {
g_font_rating = _font("Guifx v2 Transports", 16);
chars.rating_on = 'b';
chars.rating_off = 'b';
} else {
g_font_rating = _font("FontAwesome", 18);
chars.rating_on = '\uF005';
chars.rating_off = '\uF006';
}
g_rating_width = chars.rating_off.repeat(5).calc_width(g_font_rating) + 4;
g_time_width = "00:00:00".calc_width(g_font) + 20;
g_font_height = height(g_font);
g_font_bold_height = height(g_font_bold);
g_font_group2_height = height(g_font_group2);
}
function get_colours() {
if (ppt.enableDynamicColours) {
var arr = GetNowPlayingColours();
if (arr.length) {
g_colour_background = arr[0];
g_colour_text = arr[1];
g_colour_selection = arr[2];
g_colour_selected_text = arr[3];
g_colour_highlight = g_colour_text;
g_themed = false;
get_images();
return;
}
}
if (ppt.enableCustomColours) {
g_colour_background = window.GetProperty("SMOOTH.COLOUR.BACKGROUND.NORMAL", RGB(25, 25, 35));
g_colour_selection = window.GetProperty("SMOOTH.COLOUR.BACKGROUND.SELECTED", RGB(15, 177, 255));
g_colour_text = window.GetProperty("SMOOTH.COLOUR.TEXT", RGB(255, 255, 255));
g_colour_selected_text = DetermineTextColour(g_colour_selection);
g_colour_highlight = g_colour_text;
g_themed = false;
} else {
if (window.IsDefaultUI) {
g_colour_background = window.GetColourDUI(ColourTypeDUI.background);
g_colour_selection = window.GetColourDUI(ColourTypeDUI.selection);
g_colour_text = window.GetColourDUI(ColourTypeDUI.text);
g_colour_selected_text = DetermineTextColour(g_colour_selection);
g_colour_highlight = window.GetColourDUI(ColourTypeDUI.highlight);
} else {
g_colour_background = window.GetColourCUI(ColourTypeCUI.background);
g_colour_selection = window.GetColourCUI(ColourTypeCUI.selection_background);
g_colour_text = window.GetColourCUI(ColourTypeCUI.text);
g_colour_selected_text = window.GetColourCUI(ColourTypeCUI.selection_text);
g_colour_highlight = g_colour_text;
}
// check g_theme to make sure window.CreateThemeManager didn't return null
// window.IsThemed is a new boolean property for 3.2.11 and later, undefined for previous versions
g_themed = g_theme && window.IsThemed;
if (g_themed) {
g_theme.SetPartAndStateID(6, 12);
g_colour_selected_text = utils.GetSysColour(COLOR_WINDOWTEXT);
}
}
get_images();
}
var ButtonStates = {
normal: 0,
hover: 1,
down: 2
};
var KMask = {
none: 0,
ctrl: 1,
shift: 2,
};
var images = {
cache : {},
clear : function () {
for (var i in this.cache) {
this.cache[i].Dispose();
}
this.cache = {};
}
}
var ppt = {
enableDynamicColours : window.GetProperty("SMOOTH.DYNAMIC.COLOURS.ENABLED", false),
enableCustomColours: window.GetProperty("SMOOTH.CUSTOM.COLOURS.ENABLED", false),
extra_font_size: clamp(window.GetProperty("SMOOTH.EXTRA.FONT.SIZE", 0), 0, 10),
showHeaderBar: window.GetProperty("SMOOTH.SHOW.TOP.BAR", true),
autoFill: window.GetProperty("SMOOTH.AUTO.FILL", true),
wallpapermode: window.GetProperty("SMOOTH.WALLPAPER.MODE2", 0), // 0 none, 1 front cover 2 custom image
wallpaperblurred: window.GetProperty("SMOOTH.WALLPAPER.BLURRED", false),
wallpaperblur: window.GetProperty("SMOOTH.WALLPAPER.BLUR", 50),
wallpaperpath: window.GetProperty("SMOOTH.WALLPAPER.PATH", ""),
wallpaperopacity: window.GetProperty("SMOOTH.WALLPAPER.OPACITY", 0.2),
ratingGUIFX : window.GetProperty("SMOOTH.RATING.USE.GUIFX", false),
refreshRate: 40,
defaultHeaderBarHeight: 25,
headerBarHeight: 25,
scrollSmoothness: 2.5,
rowScrollStep: 3,
};
var CACHE_FOLDER = fb.ProfilePath + "js_smooth_cache\\";
utils.CreateFolder(CACHE_FOLDER);
var g_font = "";
var g_font_bold = "";
var g_font_box = "";
var g_font_group1 = "";
var g_font_group2 = "";
var g_font_awesome = "";
var g_fsize = 16;
var g_colour_text = 0;
var g_colour_selected_text = 0;
var g_colour_background = 0;
var g_colour_selection = 0;
var g_colour_highlight = 0;
var g_themed = false;
var g_theme = window.CreateThemeManager("LISTVIEW");
var COLOR_WINDOWTEXT = 8;
var g_time_width = 0;
var g_rating_width = 0;
var g_wallpaperImg = null;
var isScrolling = false;
var need_repaint = false;
var g_start_ = 0, g_end_ = 0;
var m_x = 0, m_y = 0;
var scroll_ = 0, scroll = 0, scroll_prev = 0;
var ww = 0, wh = 0;
get_font();
get_colours();