Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Helper function for DrawString() and MeasureString()
- // args: h_align, v_align, trimming, flags
- function StringFormat() {
- var h_align = 0, v_align = 0, trimming = 0, flags = 0;
- switch (arguments.length)
- {
- // fall-thru
- case 4:
- flags = arguments[3];
- case 3:
- trimming = arguments[2];
- case 2:
- v_align = arguments[1];
- case 1:
- h_align = arguments[0];
- break;
- default:
- return 0;
- }
- return ((h_align << 28) | (v_align << 24) | (trimming << 20) | flags);
- };
- StringFormatFlags = {
- DirectionRightToLeft: 0x00000001,
- DirectionVertical: 0x00000002,
- NoFitBlackBox: 0x00000004,
- DisplayFormatControl: 0x00000020,
- NoFontFallback: 0x00000400,
- MeasureTrailingSpaces: 0x00000800,
- NoWrap: 0x00001000,
- LineLimit: 0x00002000,
- NoClip: 0x00004000
- };
- // Used in gdi.Font(), can be combined
- // For more information, see: http://msdn.microsoft.com/en-us/library/ms534124(VS.85).aspx
- FontStyle = {
- Regular: 0,
- Bold: 1,
- Italic: 2,
- BoldItalic: 3,
- Underline: 4,
- Strikeout: 8
- };
- // h_align/v_align:
- // http://msdn.microsoft.com/en-us/library/ms534177(VS.85).aspx
- StringAlignment = {
- Near: 0,
- Center: 1,
- Far: 2
- };
- SmoothingMode = {
- Invalid: -1,
- Default: 0,
- HighSpeed: 1,
- HighQuality: 2,
- None: 3,
- AntiAlias: 4
- };
- // Used in window.GetColorCUI()
- ColorTypeCUI = {
- text: 0,
- selection_text: 1,
- inactive_selection_text: 2,
- background: 3,
- selection_background: 4,
- inactive_selection_background: 5,
- active_item_frame: 6
- };
- // Used in window.GetFontCUI()
- FontTypeCUI = {
- items: 0,
- labels: 1
- };
- // Used in window.GetColorDUI()
- ColorTypeDUI = {
- text: 0,
- background: 1,
- highlight: 2,
- selection: 3
- };
- // Used in window.GetFontDUI()
- FontTypeDUI = {
- defaults: 0,
- tabs: 1,
- lists: 2,
- playlists: 3,
- statusbar: 4,
- console: 5
- };
- String.prototype.repeat = function( num ) {
- return new Array( num + 1 ).join( this );
- }
- function RGB(r, g, b) {
- return (0xff000000 | (r << 16) | (g << 8) | (b));
- }
- function setAlpha(color, a) {
- return ((color & 0x00ffffff) | (a << 24));
- }
- function colorBlend(colors, weights)
- {
- var r = 0, g = 0, b = 0;
- var totalWeight = 0.0;
- for(var i = 0; i < Math.min(colors.length, weights.length); i++) {
- weight = weights[i];
- color = colors[i];
- totalWeight += weight;
- r += ((color >> 16) & 0xff) * weight;
- g += ((color >> 8) & 0xff) * weight;
- b += ((color) & 0xff) * weight;
- }
- return RGB(r / totalWeight, g / totalWeight, b / totalWeight);
- }
- // ===================================================================== //
- RatingChar = {
- Filled: window.GetProperty("rating.filledChar", '★') || '★',
- Unfilled: window.GetProperty("rating.unfilledChar", '☆') || '☆'
- };
- // ===================================================================== //
- g_ratingTF = fb.TitleFormat("%rating%");
- function getCurRating()
- {
- return parseInt(g_ratingTF.Eval()) || 0;
- };
- g_updateTextPending = false;
- function updateText(repaint)
- {
- g_updateTextPending = true;
- if(repaint)
- window.Repaint();
- };
- function rect(x, y, w, h)
- {
- function pick(arg, def) {
- return (typeof arg == 'undefined' ? def : arg);
- }
- this.x = pick(x, 0);
- this.y = pick(y, 0);
- this.w = pick(w, 0);
- this.h = pick(h, 0);
- this.check = function(x, y)
- {
- return x >= this.x && x < this.x + this.w
- && y >= this.y && y < this.y + this.h;
- }
- };
- function textPartsCreate()
- {
- var rating = getCurRating();
- var hoverCount = g_curRegion + 1;
- var filledCount = Math.max(0, rating - hoverCount);
- var UnfilledCount = Math.max(0, 5 - hoverCount - filledCount);
- var textParts = [];
- for(i = 0; i < hoverCount; i++)
- textParts.push({text: RatingChar.Filled, color: g_textHighlightColor});
- for(i = 0; i < filledCount; i++)
- textParts.push({text: RatingChar.Filled, color: g_textColor});
- for(i = 0; i < UnfilledCount; i++)
- textParts.push({text: RatingChar.Unfilled, color: g_textColor});
- return textParts;
- };
- function textPartsMeasureWidth(textParts, gr, width, height, textFlags)
- {
- var totalWidth = 0;
- for(var i = 0; i < textParts.length; i++)
- {
- var textPart = textParts[i];
- var measure = gr.MeasureString(textPart.text, g_font,
- 0, 0, width, height,
- textFlags);
- totalWidth += measure.Width;
- }
- return totalWidth;
- };
- function textPartsDraw(textParts, gr, x, y, width, height, textFlags)
- {
- var regions = [];
- var rating = getCurRating();
- for(var i = 0; i < textParts.length; i++)
- {
- var textPart = textParts[i];
- var glowAlpha = (rating == 0 || i < rating) ? g_textGlowAlpha : 0;
- var textStyle = gdi.CreateStyleTextRender();
- textStyle.GlowText(textPart.color, setAlpha(g_textHighlightColor, glowAlpha), 4);
- textStyle.renderStringRect(gr, textPart.text, g_font,
- x, y, width, height,
- textFlags);
- var measure = gr.MeasureString(textPart.text, g_font,
- x, y, width, height,
- textFlags);
- var region = new rect(measure.x, measure.y, measure.Width, measure.Height);
- regions.push(region);
- x += measure.Width;
- }
- return regions;
- };
- // ===================================================================== //
- g_textGlowAlpha = 0;
- g_textGlowTimer = 0;
- function applyTextGlow()
- {
- g_textGlowAlpha = 200;
- if(g_textGlowTimer)
- window.ClearInterval(g_textGlowTimer);
- g_textGlowTimer = window.SetInterval(function() {
- g_textGlowAlpha -= 20;
- if(g_textGlowAlpha <= 0) {
- g_textGlowAlpha = 0;
- window.ClearInterval(g_textGlowTimer);
- g_textGlowTimer = 0;
- }
- window.Repaint();
- }, 50.0);
- window.Repaint();
- };
- g_textParts = null;
- g_textPartsWidth = null;
- g_ratingRegions = [];
- g_curRegion = -1;
- g_textAlignmentX = (function() {
- switch(window.GetProperty("text.align.x", "center")) {
- case "left":
- return StringAlignment.Near;
- case "right":
- return StringAlignment.Far;
- }
- return StringAlignment.Center;
- })();
- g_textAlignmentY = (function() {
- switch(window.GetProperty("text.align.y", "center")) {
- case "top":
- return StringAlignment.Near;
- case "bottom":
- return StringAlignment.Far;
- }
- return StringAlignment.Center;
- })();
- function on_paint(gr)
- {
- if(g_updateTextPending) {
- g_textParts = textPartsCreate();
- g_textPartsWidth = textPartsMeasureWidth(g_textParts, gr,
- window.Width, window.Height,
- StringFormat(StringAlignment.Near, StringAlignment.Near,
- 0, StringFormatFlags.NoWrap));
- g_updateTextPending = false;
- }
- gr.SetSmoothingMode(SmoothingMode.HighQuality);
- var startX;
- switch(g_textAlignmentX) {
- case StringAlignment.Near:
- startX = 0;
- break;
- case StringAlignment.Center:
- startX = (window.Width - g_textPartsWidth) / 2;
- break;
- case StringAlignment.Far:
- startX = window.Width - g_textPartsWidth;
- }
- g_ratingRegions = textPartsDraw(g_textParts, gr,
- startX, 0, g_textPartsWidth, window.Height,
- StringFormat(StringAlignment.Near, g_textAlignmentY,
- 0, StringFormatFlags.NoWrap));
- };
- // ===================================================================== //
- function on_mouse_move(x, y)
- {
- var newRegion = -1;
- for(var i = 0; i < g_ratingRegions.length; i++)
- {
- region = g_ratingRegions[i];
- if(region.check(x, y)) {
- newRegion = i;
- break;
- }
- }
- if(newRegion != g_curRegion)
- {
- updateText(true);
- }
- g_curRegion = newRegion;
- };
- function on_mouse_leave()
- {
- if(g_curRegion != -1) {
- g_curRegion = -1;
- updateText(true);
- }
- };
- function on_mouse_lbtn_up(x, y, mask)
- {
- var newRegion = -1;
- for(var i = 0; i < g_ratingRegions.length; i++)
- {
- region = g_ratingRegions[i];
- if(region.check(x, y)) {
- newRegion = i;
- break;
- }
- }
- if(newRegion == -1)
- return;
- var newRating = newRegion + 1;
- var oldRating = getCurRating();
- if(newRating != oldRating) {
- fb.RunContextCommand("Playback Statistics/Rating/" + newRating);
- applyTextGlow();
- }
- };
- // ===================================================================== //
- g_cachedRating = -1;
- function on_playback_new_track(metadb)
- {
- updateText(false);
- applyTextGlow();
- g_cachedRating = getCurRating();
- };
- function on_playback_stop(reason)
- {
- if(reason != 2) { // starting another
- updateText(false);
- applyTextGlow();
- }
- g_cachedRating = -1;
- };
- function on_playback_edited(metadb)
- {
- var newRating = getCurRating();
- if(g_cachedRating == -1 || g_cachedRating != newRating) {
- updateText(false);
- applyTextGlow();
- g_cachedRating = newRating;
- }
- };
- // ===================================================================== //
- function on_font_changed()
- {
- var origFont = window.InstanceType ?
- window.GetFontDUI(FontTypeDUI.defaults) :
- window.GetFontCUI(FontTypeCUI.labels);
- var origSize = 10, origName = "Segoe UI";
- if(origFont) {
- origSize = origFont.size;
- origName = origFont.Name;
- }
- var size = window.GetProperty("font.size", "") || origSize;
- var name = window.GetProperty("font.face", "") || origName;
- var dpi = window.GetProperty("font.dpi", "") || 96;
- g_font = gdi.Font(name, size * dpi / 72.0, FontStyle.regular);
- updateText(true);
- };
- function on_colors_changed()
- {
- if(window.instanceType) {
- g_textColor = window.getColorDUI(ColorTypeDUI.text);
- g_backGroundColor = window.GetColorDUI(ColorTypeDUI.background);
- g_textHighlightColor = window.GetColorDUI(ColorTypeDUI.highlight);
- } else {
- g_textColor = window.GetColorCUI(ColorTypeCUI.text);
- g_backGroundColor = window.GetColorCUI(ColorTypeCUI.background);
- g_textHighlightColor = colorBlend([g_textColor, g_backGroundColor], [60, 40]);
- }
- window.Repaint();
- };
- (function init()
- {
- on_font_changed();
- on_colors_changed();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement