Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==PREPROCESSOR==
- // @name "Damask Buttons Panel"
- // @version "1.0"
- // @author "so_kindly"
- // ==/PREPROCESSOR==
- // Variables {{
- // Flags, used with GdiDrawText
- // For more information, see: http://msdn.microsoft.com/en-us/library/dd162498(VS.85).aspx
- var DT_TOP = 0x00000000;
- var DT_LEFT = 0x00000000;
- var DT_CENTER = 0x00000001;
- var DT_RIGHT = 0x00000002;
- var DT_VCENTER = 0x00000004;
- var DT_BOTTOM = 0x00000008;
- var DT_WORDBREAK = 0x00000010;
- var DT_SINGLELINE = 0x00000020;
- var DT_EXPANDTABS = 0x00000040;
- var DT_TABSTOP = 0x00000080;
- var DT_NOCLIP = 0x00000100;
- var DT_EXTERNALLEADING = 0x00000200;
- var DT_CALCRECT = 0x00000400;
- var DT_NOPREFIX = 0x00000800;
- var DT_INTERNAL = 0x00001000;
- var DT_EDITCONTROL = 0x00002000;
- var DT_PATH_ELLIPSIS = 0x00004000;
- var DT_END_ELLIPSIS = 0x00008000;
- var DT_MODIFYSTRING = 0x00010000;
- var DT_RTLREADING = 0x00020000;
- var DT_WORD_ELLIPSIS = 0x00040000;
- var DT_NOFULLWIDTHCHARBREAK = 0x00080000;
- var DT_HIDEPREFIX = 0x00100000;
- var DT_PREFIXONLY = 0x00200000;
- // menu flags
- var MF_SEPARATOR = 0x00000800;
- var MF_ENABLED = 0x00000000;
- var MF_GRAYED = 0x00000001;
- var MF_DISABLED = 0x00000002;
- var MF_UNCHECKED = 0x00000000;
- var MF_CHECKED = 0x00000008;
- var MF_STRING = 0x00000000;
- var MF_POPUP = 0x00000010;
- var MF_RIGHTJUSTIFY = 0x00004000;
- var MK_SHIFT = 0x0004;
- var CursorFollowMode = {
- OnlyStopped: 1,
- Always: 2
- }
- // globals
- var ww = 0;
- var wh = 0;
- var Properties = new GetProperties();
- var MetaDB;
- var MetaDB_type;
- // margins (left,right,top,bottom)
- var lm = 5;
- var rm = 5;
- var tm = 5;
- var bm = 5;
- var border_size = 10;
- // buttons vars
- var Buttons;
- var MaxRange;
- var Tooltip = window.CreateTooltip();
- var EnableTooltip = true;
- var hbtn; // hover button
- var dbtn; // pressed down button
- var dbtnBackup;
- var rbtnDown;
- // rating vars
- var tf_rating = fb.TitleFormat("$if2(%rating%,0)");
- var rating;
- var h_rating;
- rating_rect = {
- x: 0,
- y: 0,
- width: 110,
- height: 22
- }
- // images {{
- var imgPath = fb.FoobarPath + "skins\\Damask\\images\\";
- var ButtonsPath = fb.FoobarPath + "skins\\Damask\\images\\buttons\\";
- // buttons
- var img_stop = gdi.Image(ButtonsPath + "stop20.png");
- var img_play = gdi.Image(ButtonsPath + "play20.png");
- var img_pause = gdi.Image(ButtonsPath + "pause20.png");
- var img_prev = gdi.Image(ButtonsPath + "prev20.png");
- var img_next = gdi.Image(ButtonsPath + "next20.png");
- var img_random = gdi.Image(ButtonsPath + "playrandom20.png");
- var img_folder = gdi.Image(ButtonsPath + "folder20.png");
- var img_library = gdi.Image(ButtonsPath + "library20.png");
- // rating
- var star_normal = gdi.Image(ButtonsPath + "star_normal.png");
- var star_empty = gdi.Image(ButtonsPath + "star_empty.png");
- var star_hover = gdi.Image(ButtonsPath + "star_hover.png");
- var star_x = gdi.Image(ButtonsPath + "star_x.png");
- // bg images
- var img_bg_bmp;
- var img_main_wallpaper = gdi.Image(imgPath + "DamaskBotBGSmall.png").CreateRawBitmap();
- borders_img = {
- left_top: gdi.Image(imgPath + "borders\\top-bot\\left_top.png"),
- top_line: gdi.Image(imgPath + "borders\\top-bot\\top_line.png"),
- right_top: gdi.Image(imgPath + "borders\\top-bot\\right_top.png"),
- left_line: gdi.Image(imgPath + "borders\\top-bot\\left_line.png"),
- right_line: gdi.Image(imgPath + "borders\\top-bot\\right_line.png"),
- left_bot: gdi.Image(imgPath + "borders\\top-bot\\left_bot.png"),
- bot_line: gdi.Image(imgPath + "borders\\top-bot\\bot_line.png"),
- right_bot: gdi.Image(imgPath + "borders\\top-bot\\right_bot.png")
- };
- var img_rating_container = gdi.Image(ButtonsPath + "rating_container.png");
- // }}
- // others
- var ButtonAnimationTimerID;
- var ButtonWaitTimerID;
- var ButtonAnimationTimer;
- var ButtonWaitTimer;
- // Variables}}
- //===================================================================================================================================================
- function RGB(r, g, b) {
- return (0xff000000 | (r << 16) | (g << 8) | (b));
- }
- function TrackType(trkpath) {
- var taggable;
- var type;
- switch (trkpath) {
- case "file":
- taggable = 1;
- type = 0;
- break;
- case "cdda":
- taggable = 1;
- type = 1;
- break;
- case "FOO_":
- taggable = 0;
- type = 2;
- break;
- case "http":
- taggable = 0;
- type = 3;
- break;
- case "mms:":
- taggable = 0;
- type = 3;
- break;
- case "unpa":
- taggable = 0;
- type = 4;
- break;
- default:
- taggable = 0;
- type = 5;
- }
- return type;
- }
- // Generate background image.
- function GenerateBG() {
- var img_bg = gdi.CreateImage(ww, wh);
- var g = img_bg.GetGraphics();
- // wallpaper
- g.GdiDrawBitmap(img_main_wallpaper, border_size, border_size, img_bg.width - (border_size * 2), img_bg.height - (border_size * 2), 0, 0, img_bg.width - (border_size * 2), img_bg.height - (border_size * 2));
- // borders
- g.DrawImage(borders_img.left_top, 0, 0, border_size, border_size, 0, 0, 10, 10);
- g.DrawImage(borders_img.top_line, border_size, 0, img_bg.width - (border_size * 2), border_size, 0, 0, img_bg.width - (border_size * 2), 10);
- g.DrawImage(borders_img.right_top, img_bg.width - border_size, 0, border_size, border_size, 0, 0, 10, 10);
- g.DrawImage(borders_img.left_line, 0, border_size, border_size, img_bg.height - (border_size * 2), 0, 0, border_size, img_bg.height - (border_size * 2));
- g.DrawImage(borders_img.right_line, img_bg.width - border_size, border_size, border_size, img_bg.height - (border_size * 2), 0, 0, border_size, img_bg.height - (border_size * 2));
- g.DrawImage(borders_img.left_bot, 0, img_bg.height - border_size, border_size, border_size, 0, 0, 10, 10);
- g.DrawImage(borders_img.bot_line, border_size, img_bg.height - border_size, img_bg.width - (border_size * 2), border_size, 0, 0, img_bg.width - (border_size * 2), 10);
- g.DrawImage(borders_img.right_bot, img_bg.width - border_size, img_bg.height - border_size, border_size, border_size, 0, 0, 10, 10);
- //rating container
- g.DrawImage(img_rating_container, img_bg.width - border_size - rm - 118 - 4, Math.round((img_bg.height - 28) / 2), 119, 29, 0, 0, 119, 29);
- img_bg_bmp = img_bg.CreateRawBitmap();
- img_bg.ReleaseGraphics(g);
- }
- //Properties
- function GetProperties() {
- this.Panel = {
- CursorFollowMode: window.GetProperty("A01.Panel.CursorFollowMode", CursorFollowMode.OnlyStopped),
- }
- this.Panel.CursorFollowMode = Math.max(this.Panel.CursorFollowMode, CursorFollowMode.OnlyStopped);
- this.Panel.CursorFollowMode = Math.min(this.Panel.CursorFollowMode, CursorFollowMode.Always);
- window.SetProperty("A01.Panel.CursorFollowMode", this.Panel.CursorFollowMode);
- }
- // context menu
- function ShowContextMenu(x, y) {
- var menu = window.CreatePopupMenu();
- var FollowModeChild = window.CreatePopupMenu();
- var ret;
- FollowModeChild.AppendMenuItem(MF_STRING, 1, "Only Stopped");
- FollowModeChild.AppendMenuItem(MF_STRING, 2, "Always");
- FollowModeChild.CheckMenuRadioItem(1, 2, Properties.Panel.CursorFollowMode);
- FollowModeChild.AppendTo(menu, MF_STRING, "Cursor follow mode");
- menu.AppendMenuItem(MF_STRING, 3, "Properties");
- ret = menu.TrackPopupMenu(x, y);
- switch (ret) {
- case 1:
- SetCursorFollowMode(CursorFollowMode.OnlyStopped);
- break;
- case 2:
- SetCursorFollowMode(CursorFollowMode.Always);
- break;
- case 3:
- window.ShowProperties();
- break;
- }
- }
- // Cursor follow tools {{
- function SetCursorFollowMode(FollowMode) {
- if (FollowMode != Properties.Panel.CursorFollowMode) {
- Properties.Panel.CursorFollowMode = FollowMode;
- window.SetProperty("A01.Panel.CursorFollowMode", FollowMode);
- CursorFollow(FollowMode);
- }
- }
- function CursorFollow(FollowMode) {
- switch (FollowMode) {
- case CursorFollowMode.OnlyStopped:
- if (fb.IsPlaying) on_playback_new_track();
- else on_item_focus_change();
- break;
- case CursorFollowMode.Always:
- on_item_focus_change();
- break;
- }
- }
- // Cursor follow tools }}
- //===================================================================================================================================================
- // Buttons {{
- // button class
- function Button(x, y, width, height, c_width, c_height, img, fn_onclick, tiptext) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- this.c_width = c_width;
- this.c_height = c_height;
- this.tiptext = tiptext;
- this.NeedRefresh = 0;
- this.img = img;
- this.oldstate = 0; // 0=normal, 1=hover, 2=down.
- this.state = 0;
- this.opacity = 0; // Opacity of new state image.
- this.step = 0;
- this.OnClick = fn_onclick;
- this.ChangeState = function (s) {
- this.oldstate = this.state;
- this.state = s;
- this.opacity = 0;
- this.NeedRefresh = 1;
- switch (s) {
- case 0:
- this.step = 40; // Step of changing to normal state.
- Tooltip.Deactivate();
- Tooltip.Text = "";
- break;
- case 1:
- this.step = 75; // hover state.
- if (EnableTooltip) {
- Tooltip.Text = this.tiptext;
- Tooltip.Activate();
- }
- break;
- case 2:
- this.step = 85; // down state.
- Tooltip.Deactivate();
- Tooltip.Text = "";
- break;
- }
- if (!ButtonAnimationTimerID) // start animation
- ButtonAnimationTimer.Start();
- }
- this.isXYok = function (x2, y2) {
- return (x2 >= this.x && y2 >= this.y && x2 <= this.x + width && y2 <= this.y + height) ? true : false;
- }
- this.Draw = function (x2, y2, gr) {
- x2 = this.x;
- y2 = this.y;
- if (this.opacity < 255) this.img && gr.DrawImage(this.img, x2, y2, width, height, this.oldstate * c_width, 0, c_width, c_height, 0, 255);
- if (this.opacity) this.img && gr.DrawImage(this.img, x2, y2, width, height, this.state * c_width, 0, c_width, c_height, 0, this.opacity);
- }
- this.RefreshOpacity = function (fn) {
- if (this.opacity < 255) this.opacity = Math.min(this.opacity + this.step, 255);
- else {
- this.oldstate = this.state;
- this.opacity = 0;
- this.NeedRefresh = 0;
- }
- }
- }
- function GetCoordinates() {
- for (var i = 0; i < Buttons.length; i++) {
- Buttons[i].y = Math.round((wh - Buttons[i].height) / 2);
- }
- rating_rect.x = ww - border_size - rm - 118;
- rating_rect.y = Math.round((wh - 28) / 2) + 3;
- // Calculate the max range of buttons, for increasing refresh speed.
- MaxRange = {
- x: Buttons[0].x,
- y: Buttons[0].y,
- width: Buttons[0].width,
- height: Buttons[0].height
- }
- for (var i = 1; i < Buttons.length; i++) {
- MaxRange.x = Math.min(Buttons[i].x, MaxRange.x);
- MaxRange.y = Math.min(Buttons[i].y, MaxRange.y);
- MaxRange.width = Math.max(Buttons[i].x + Buttons[i].width - MaxRange.x, MaxRange.width);
- MaxRange.height = Math.max(Buttons[i].y + Buttons[i].height - MaxRange.y, MaxRange.height);
- }
- }
- // Buttons init }}
- //===================================================================================================================================================
- // Timers {{
- function c_ButtonAnimationTimer() {
- this.Stop = function () {
- if (ButtonAnimationTimerID) {
- window.ClearInterval(ButtonAnimationTimerID);
- ButtonAnimationTimerID = null;
- }
- }
- this.Start = function () {
- ButtonAnimationTimerID = window.SetInterval(function () {
- var NeedRepaint = 0;
- for (var i = 0; i < Buttons.length; i++) {
- if (Buttons[i].NeedRefresh) {
- Buttons[i].RefreshOpacity();
- NeedRepaint += 1;
- Onlybtn = Buttons[i]; // If only one button need to be repainted.
- }
- }
- if (NeedRepaint) {
- if (NeedRepaint == 1) window.RepaintRect(Onlybtn.x, Onlybtn.y, Onlybtn.width, Onlybtn.height);
- else window.RepaintRect(MaxRange.x, MaxRange.y, MaxRange.width, MaxRange.height); // This is range of buttons.
- } else {
- ButtonAnimationTimer.Stop();
- }
- }, 50)
- }
- }
- function c_ButtonWaitTimer() {
- this.Stop = function () {
- if (ButtonWaitTimerID) {
- window.ClearTimeout(ButtonWaitTimerID);
- ButtonWaitTimerID = null;
- }
- }
- this.Start = function () {
- ButtonWaitTimerID = window.SetTimeout(function () {
- ButtonWaitTimerID = null;
- dbtnBackup.ChangeState(0);
- dbtnBackup = null;
- }, 200)
- }
- }
- // Timers }}
- //===================================================================================================================================================
- //===================================================================================================================================================
- function on_init() {
- // creating buttons array
- Buttons = new Array(
- b1 = new Button(lm + border_size, 0, 58, 58, 58, 58, img_stop, function () {
- fb.Stop()
- }, "Stop"),
- b2 = new Button(5 + lm + border_size + b1.width, 0, 58, 58, 58, 58, fb.IsPaused || !fb.IsPlaying ? img_play : img_pause, function () {
- fb.PlayOrPause();
- }, "Play or pause"),
- b3 = new Button(10 + lm + border_size + b1.width + b2.width, 0, 58, 58, 58, 58, img_prev, function () {
- fb.Prev()
- }, "Previous"),
- b4 = new Button(15 + lm + border_size + b1.width + b2.width + b3.width, 0, 58, 58, 58, 58, img_next, function () {
- fb.Next()
- }, "Next"),
- b5 = new Button(20 + lm + border_size + b1.width + b2.width + b3.width + b4.width, 0, 58, 58, 58, 58, img_random, function () {
- fb.RunMainMenuCommand("Playback/Random")
- }, "Play random track"),
- b6 = new Button(25 + lm + border_size + b1.width + b2.width + b3.width + b4.width + b5.width, 0, 58, 58, 58, 58, img_folder, function () {
- if (MetaDB) fb.RunContextCommandWithMetadb("Open containing folder", MetaDB)
- }, "Open folder"),
- b7 = new Button(30 + lm + border_size + b1.width + b2.width + b3.width + b4.width + b5.width + b6.width, 0, 58, 58, 58, 58, img_library, function () {
- fb.RunMainMenuCommand("Library/Facets")
- }, "Library browser"));
- // init timers
- ButtonAnimationTimer = new c_ButtonAnimationTimer();
- ButtonWaitTimer = new c_ButtonWaitTimer();
- }
- on_init();
- function on_size() {
- if (!window.Width || !window.Height) return;
- ww = window.Width;
- wh = window.Height;
- GetCoordinates();
- CursorFollow(Properties.Panel.CursorFollowMode);
- GenerateBG(); // Generate background image
- }
- function on_paint(gr) {
- // Draw background image.
- if (img_bg_bmp) gr.GdiDrawBitmap(img_bg_bmp, 0, 0, ww, wh, 0, 0, img_bg_bmp.width, img_bg_bmp.height);
- // Draw buttons
- for (var i = 0; i < Buttons.length; i++)
- Buttons[i].Draw(Buttons[i].x, Buttons[i].y, gr);
- // Rating
- if (h_rating) {
- for (var i = 0; i < 5; i++) {
- gr.DrawImage(i + 1 <= h_rating ? (h_rating == rating ? star_x : star_hover) : star_empty, rating_rect.x + i * 22, rating_rect.y, 22, 22, 0, 0, 22, 22);
- }
- } else {
- for (var i = 0; i < 5; i++) {
- gr.DrawImage(i + 1 <= rating ? star_normal : star_empty, rating_rect.x + i * 22, rating_rect.y, 22, 22, 0, 0, 22, 22);
- }
- }
- }
- //===================================================================================================================================================
- // Mouse callbacks {{
- function on_mouse_move(x, y) {
- if (dbtn) {
- if (dbtn.isXYok(x, y)) {
- if (dbtn.state != 2) dbtn.ChangeState(2);
- } else if (dbtn.state != 1) dbtn.ChangeState(1);
- } else {
- for (var i = 0; i < Buttons.length; i++)
- if (Buttons[i].isXYok(x, y)) {
- if (hbtn != Buttons[i] && Buttons[i] != dbtnBackup) {
- if (hbtn) hbtn.ChangeState(0);
- hbtn = Buttons[i];
- hbtn.ChangeState(1);
- };
- break;
- };
- if (i == Buttons.length) {
- if (hbtn) {
- if (hbtn && hbtn != dbtnBackup) hbtn.ChangeState(0);
- hbtn = undefined;
- };
- };
- };
- // rating
- if (MetaDB) {
- if (MetaDB_type < 2) {
- if (x > ww - border_size - rm - 122 && y > Math.round((wh - 29) / 2) && x < 110 + ww - border_size - rm - 122 && y < Math.round((wh - 29) / 2) + 29) {
- h_rating = Math.ceil((x - rating_rect.x) / 22);
- window.RepaintRect(ww - border_size - rm - 131, Math.round((wh - 29) / 2), 131, 29); //repaint rating
- } else {
- h_rating = 0;
- window.RepaintRect(ww - border_size - rm - 131, Math.round((wh - 29) / 2), 131, 29); //repaint rating
- }
- }
- }
- }
- function on_mouse_lbtn_down(x, y) {
- if (hbtn && !ButtonWaitTimerID && !dbtn) {
- dbtn = hbtn;
- dbtn.ChangeState(2);
- };
- }
- function on_mouse_lbtn_up(x, y) {
- if (dbtn) {
- if (dbtn.state == 2 && dbtn != dbtnBackup) {
- dbtn.OnClick(x, y);
- if (!ButtonWaitTimerID) {
- dbtnBackup = dbtn;
- ButtonWaitTimer.Start();
- }
- };
- dbtn = undefined;
- on_mouse_move(x, y);
- };
- if (MetaDB_type < 2 && h_rating) {
- if (h_rating != rating) {
- if (MetaDB) {
- fb.RunContextCommandWithMetadb("Rating/" + ((h_rating == 0) ? "<not set>" : h_rating), MetaDB);
- }
- } else {
- fb.RunContextCommandWithMetadb("Rating/<not set>", MetaDB);
- }
- }
- }
- function on_mouse_rbtn_down(x, y, vkey) {
- rbtnDown = true;
- }
- function on_mouse_rbtn_up(x, y, vkey) {
- if (rbtnDown) {
- rbtnDown = false;
- if (vkey == MK_SHIFT) {
- return false; // if shift button pressed, show default context menu.
- } else {
- ShowContextMenu(x, y);
- return true;
- }
- }
- }
- function on_mouse_leave() {
- if (hbtn && !ButtonWaitTimerID) {
- hbtn.ChangeState(0);
- hbtn = undefined;
- }
- Tooltip.Text = "";
- if (h_rating) {
- h_rating = 0;
- window.RepaintRect(ww - border_size - rm - 131, Math.round((wh - 29) / 2), 131, 29); //repaint rating
- }
- }
- // Mouse callbacks }}
- //===================================================================================================================================================
- // Playback callbacks {{
- function on_playback_new_track() {
- Buttons[1].img = fb.IsPaused || !fb.IsPlaying ? img_play : img_pause;
- window.RepaintRect(Buttons[1].x,Buttons[1].y,Buttons[1].width,Buttons[1].height);
- if(Properties.Panel.CursorFollowMode == CursorFollowMode.OnlyStopped){
- if (MetaDB) {
- window.UnwatchMetadb();
- }
- MetaDB = fb.GetNowPlaying();
- if (MetaDB) {
- on_metadb_changed();
- window.WatchMetadb(MetaDB);
- }
- }
- }
- function on_playback_stop(reason) {
- if (!reason || reason == 1) {
- Buttons[1].img = fb.IsPaused || !fb.IsPlaying ? img_play : img_pause;
- window.RepaintRect(Buttons[1].x,Buttons[1].y,Buttons[1].width,Buttons[1].height);
- if(Properties.Panel.CursorFollowMode == CursorFollowMode.OnlyStopped){
- if (MetaDB) {
- window.UnwatchMetadb();
- }
- MetaDB = fb.GetFocusItem();
- if (MetaDB) {
- on_metadb_changed();
- window.WatchMetadb(MetaDB);
- }
- }
- }
- }
- // Playback callbacks }}
- //===================================================================================================================================================
- // Other events
- function on_playback_pause(state) {
- Buttons[1].img = state ? img_play : img_pause;
- window.RepaintRect(Buttons[1].x,Buttons[1].y,Buttons[1].width,Buttons[1].height);
- }
- function on_item_focus_change() {
- if (Properties.Panel.CursorFollowMode == CursorFollowMode.Always || (Properties.Panel.CursorFollowMode == CursorFollowMode.OnlyStopped && !fb.IsPlaying && !fb.IsPaused)) {
- if (MetaDB) {
- window.UnwatchMetadb();
- }
- MetaDB = fb.GetFocusItem();
- if (MetaDB) {
- on_metadb_changed();
- window.WatchMetadb(MetaDB);
- }
- }
- }
- function on_metadb_changed() {
- MetaDB_type = TrackType(MetaDB.RawPath.substring(0, 4));
- if (MetaDB_type < 2) rating = tf_rating.EvalWithMetadb(MetaDB);
- window.RepaintRect(ww - border_size - rm - 131, Math.round((wh - 29) / 2), 131, 29); //repaint rating
- window.RepaintRect(Buttons[1].x, Buttons[1].y, Buttons[1].width, Buttons[1].height); // repaint play/pause btn
- }
- function on_playlist_switch() {
- on_playlists_changed();
- }
- function on_playlist_items_removed(playlist, new_count) {
- on_playlists_changed();
- }
- function on_playlists_changed() {
- on_item_focus_change();
- if (!fb.IsPlaying && fb.PlaylistItemCount(fb.ActivePlaylist) == 0) {
- MetaDB = false;
- }
- }
- //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement