Advertisement
r0k

Constructors

r0k
Jun 3rd, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* **************************************** Constructors ********************************************
  2.  * Author : r0k
  3.  * Those functions are used by my WSH scripts to create custom objects. Feel free to use them in your
  4.  * scripts but don't change them nor rename this file or my scripts won't work.
  5.  * Please note that those functions might be changed in the future to adress bugs or add features.
  6.  * There is no guarantee updates will be backward compatibles. If if you use one of those functions
  7.  * for one of your script it might be a good idea to keep a copy of this file somewhere.
  8.  */
  9.  
  10. function ButtonObject(label, x, y, w, h, action, args, style) {
  11.     this.label = label;
  12.     this.x = x;
  13.     this.y = y;
  14.     this.w = w;
  15.     this.h = h;
  16.     this.state = $buttonStates.normal;
  17.     this.action = action;
  18.     this.args = args ? args : null;
  19.     this.style = style || "text";
  20.     this.onScreen = true;
  21.     this.img = null;
  22.    
  23.     this.getImage = function(_imgPath) {
  24.         __img = _imgPath+this.label+".jpg";
  25.         doPrint("GetImage : "+__img,this.label);
  26.         switch (true) {
  27.         case utils.FileTest(__img, "e") :
  28.             doPrint("true");
  29.             this.img = gdi.Image(__img);
  30.             break;
  31.         default :
  32.             this.style = "text";
  33.             doPrint("false");
  34.         }
  35.     }
  36.     this.getActiveStatus = function (x, y) {
  37.         return (this.x <= x) && (x <= this.x + this.w) && (this.y <= y) && (y <= this.y + this.h);
  38.     }  
  39.     this.setState = function (state) {
  40.         __old = this.state;
  41.         this.state = state;
  42.         return __old;
  43.     }
  44.     this.draw = function(gr) {
  45.         switch (this.style) {
  46.         case "text":
  47.             $system.themeButton.SetPartAndStateId(1, this.state);
  48.             $system.themeButton.DrawThemeBackground(gr, this.x, this.y, this.w, this.h);
  49.             gr.GdiDrawText(this.label, $options.font, RGB(0,0,0), this.x, this.y, this.w, this.h, 0x00000C05);
  50.             break;
  51.         case "img":
  52. //          this.img.Resize(this.w, this.h, 0);
  53.             gr.DrawImage(this.img, this.x, this.y, this.w, this.h, 0, 0, this.img.Width, this.img.Height);
  54. //          gr.DrawImage(this.img, this.x, this.y, this.w, this.h, 0, 0, this.w, this.h);
  55.             break;
  56.         case "mixed":
  57.             break;
  58.         }
  59.     }
  60.     this.onClick = function () {
  61.         this.action();
  62.     }
  63. }
  64.  
  65. function PlayListObject(id, name, sort, auto, query) {
  66.     this.idx = id;
  67.     this.name = name;
  68.     this.auto = (auto == true) ? true : false;
  69.     this.query = (auto != true) ? false : (query || $default.query);
  70.     this.sort = (sort) ? ((sort.charAt(0) == "%" || "$") ? sort : ((sort = "random") ? "" : $default.sort))
  71.                        : $default.sort;
  72.     this.getIdx = function() {
  73.         var idx = this.idx;
  74.         if (fb.GetPlaylistName(idx) != this.name) {
  75.             idx = -1;
  76.             for (var i=0; i<fb.PlaylistCount; i++) {
  77.                 if (fb.GetPlaylistName(i) == this.name) {
  78.                     idx = i;
  79.                     break;
  80.                 }
  81.             }
  82.             this.idx = (idx > -1) ? idx : this.idx;
  83.         }
  84.         return (idx > -1) ? idx : false;
  85.     }
  86.     this.doCreate = function() {
  87.         if (this.getIdx()>=0) this.doRemove();
  88.         if (this.auto) {
  89.             fb.CreateAutoPlaylist(this.idx, this.name, this.query, this.sort);
  90.         } else {
  91.             fb.CreatePlaylist(this.idx, this.name);
  92.         }
  93.     }
  94.     this.doRemove = function() {
  95.         if (this.getIdx()>=0) plman.RemovePlaylist(this.idx);
  96.     }
  97.     this.getMeta = function(metaName) {
  98.         var metaHandles = plman.GetPlaylistItems(this.idx);
  99.         var orderBy = fb.TitleFormat("%" + metaName + "%");
  100.         metaHandles.OrderByFormat(orderBy, 1);
  101.         orderBy.Dispose();
  102.         var metaList=[];
  103.         var lastMeta="";
  104.         var meta="";
  105.         for (var i=0; i < metaHandles.Count; i++) {
  106.             meta = metaHandles.Item(i).GetFileInfo().
  107.                    MetaValue(metaHandles.Item(i).GetFileInfo().MetaFind(metaName), 0);
  108.             if (lastMeta != meta) {
  109.                 lastMeta = meta;
  110.                 metaList.push(meta);
  111.             }
  112.         }
  113.         metaHandles.Dispose();
  114.         return metaList;
  115.     }
  116.     this.setQuery = function(query) {
  117.         this.query = (this.queryBase = ".") ? query : this.queryBase + " AND " + query;
  118.         this.create();
  119.     }
  120.     this.doClear = function() {
  121.         if (this.getIdx()>=0) {
  122.             plman.SetPlaylistSelectionSingle(this.idx, 0, true);
  123.             plman.RemovePlaylistSelection(this.idx, true);
  124.             plman.RemovePlaylistSelection(this.idx, false);
  125.         }
  126.     }
  127.     this.setContent = function(hndList) {
  128.     if (this.getIdx()>=0) {
  129. //      __active = plman.ActivePlaylist;
  130.         this.doClear();
  131.         plman.InsertPlaylistItems(this.idx, 0, hndList);
  132.         plman.ActivePlaylist = this.idx;
  133. //      plman.ActivePlaylist = __active;
  134.         }
  135.     }
  136.     this.doPlay = function(_mode) {
  137.     _mode = _mode || "now"
  138.     if (this.getIdx()>=0) {
  139.         plman.SetPlaylistFocusItem(this.idx, 0);
  140.         fb.PlayingPlaylist = this.idx;
  141.         if (!(fb.IsPlaying)) fb.Play();
  142.         }
  143.     }
  144. }
  145.  
  146. function ToolbarObject(id,_style,y,h,w,btnH,btnW) {
  147.     this.ownId = id;
  148.     this.align = (_style) ? _style.split("|")[0] : "hLeft";
  149.     this.style = (_style) ? _style.split("|")[1] : "text";
  150.     this.buttonsHeight = btnH || 25;
  151.     this.buttonsWidth = btnW || 80;
  152.     this.buttonsList = [];
  153. //  this.buttons = {height : (btnH || 26), width : (btnW || 80), list : null};
  154.     this.top = y || 0;
  155.     this.height = h || 30;
  156.     this.bottom = this.top + this.height;
  157.     this.width = w || 250;
  158.     this.visible = {row1 : 0, rowN : 0, col1 : 0, colN : 0}; // Columns are not used currently
  159.  
  160.     this.updateHeight = function(h) {
  161.         this.height = h;
  162.         this.bottom = this.top + this.height;
  163.     }
  164.     this.addButton = function(label, action, args) {
  165.         this.visible.rowN = Math.floor(this.height/(5+this.buttonsHeight));
  166.         __w = this.buttonsWidth;
  167.         __h = this.buttonsHeight;
  168.         __buttonID = this.buttonsList.length;
  169.         doPrint("Adding button : "+__buttonID+label+" - "+__w+"x"+__h,this.ownId);
  170.         switch (this.align) {
  171.         case "hLeft" :
  172.             __x = 5 + ((__w+5) * __buttonID);
  173.             __y = 5 + this.top;
  174.             __row = false;
  175.         break;
  176.         case "vLeft" :
  177.             __x = 5;
  178.             __y = (5 + this.top) + ((5+__h) * __buttonID);
  179.             __row = false;
  180.         break;
  181.         case "grid" :
  182.             __rowSize = Math.floor((this.width-10)/(__w+5));
  183.             __row = Math.floor(__buttonID/__rowSize);
  184.             __rowPos = __buttonID-(__row*__rowSize);
  185.             __x = 5 + ((__w+5) * __rowPos);
  186.             __y = (5 + this.top) + ((5+__h) * __row);
  187.             doPrint("Grid. Row : "+__row+" - Visi : "+this.visible.row1+"-"+this.visible.rowN,this.ownId);
  188.         break;
  189.         }
  190.         this.buttonsList[__buttonID] = new ButtonObject(label,__x,__y,__w,__h,action,args,this.style);
  191.         if (__row >= this.visible.rowN) this.buttonsList[__buttonID].onScreen = false;
  192.         if (this.style == "img") this.buttonsList[__buttonID].getImage($system.imgPath);
  193.     }
  194.     this.doRecalculateLayout = function(_newW,_newH) {
  195.         this.visible.rowN = Math.floor(this.height/(5+this.buttonsHeight));
  196.         this.height = _newH || this.height;
  197.         this.bottom = this.top + this.height;
  198.         this.width = _newW || this.width;
  199.         __w = this.buttonsWidth; __h = this.buttonsHeight;
  200.         doPrint("Rcalculate layout : "+this.width+"x"+this.height+
  201.                 " -Rows : "+this.visible.row1+"-"+this.visible.rowN,this.ownId);
  202.         for (var i in this.buttonsList) {
  203.             this.buttonsList[i].onScreen = true;
  204.             switch (this.align) {
  205.             case "hLeft" :
  206.                 __x = 5 + ((__w+5) * i);
  207.                 __y = 5 + this.top;
  208.                 __row = false;
  209.             break;
  210.             case "vLeft" :
  211.                 __x = 5;
  212.                 __y = (5 + this.top) + ((5+__h) * i);
  213.                 __row = false;
  214.             break;
  215.             case "grid" :
  216.                 __rowSize = Math.floor((this.width-10)/(__w+5));
  217.                 __row = Math.floor(i/__rowSize);
  218.                 __rowPos = i-(__row*__rowSize);
  219.                 __x = 5 + ((__w+5) * __rowPos);
  220.                 __y = (5 + this.top) + ((5+__h) * (__row-this.visible.row1));
  221.             break;
  222.             }
  223.             this.buttonsList[i].x = __x; this.buttonsList[i].y = __y;
  224.             if ((__row < this.visible.row1) || (__row >= this.visible.rowN + this.visible.row1))
  225.                 this.buttonsList[i].onScreen = false;
  226.         }
  227.     }
  228.     this.doClear = function() {
  229.         this.buttonsList = [];
  230.         CollectGarbage();
  231.     }  
  232.     this.drawButtons = function(gr) {
  233.         for (var i in this.buttonsList) {
  234.             if (this.buttonsList[i].onScreen) this.buttonsList[i].draw(gr);
  235.         }
  236.     }
  237.     this.getActiveStatus = function (y) {
  238.         return (this.top <= y) && (y <= this.bottom);
  239.     }  
  240.     this.getActiveButton = function(x, y) {
  241.         for (var i in this.buttonsList) {
  242.             if (this.buttonsList[i].onScreen && this.buttonsList[i].getActiveStatus(x, y))
  243.                 return this.buttonsList[i];
  244.         }
  245.         return null;
  246.     }
  247. }
  248.  
  249. function MetaDBObject(id, query, sortBy, groupBy) {
  250.     this.ownId = id;
  251.     this.query = (query) ? query : $default.query;
  252.     this.sortBy = (sortBy) ? sortBy : $default.sort;
  253.     this.groupBy = (groupBy) ? groupBy : $default.group;
  254.     this.groups = {};
  255.    
  256.     this.doListHandles = function(_query,_sort) {
  257.         __query = _query || this.query
  258.         __sort = _sort || this.sortBy
  259.         var __idx = fb.PlaylistCount;
  260.         fb.CreateAutoPlaylist(__idx, "temp", __query, __sort);
  261.         this.hndListAll = plman.GetPlaylistItems(__idx);
  262.         this.hndList = this.hndListAll.Clone();
  263.         plman.RemovePlaylist(__idx);
  264.     }
  265.     this.doResetHandles = function() {
  266.         this.hndList.RemoveAll();
  267.         this.hndList = this.hndListAll.Clone();
  268.     }
  269.     this.doFilterHandles = function(__filter) {
  270.         this.hndList.RemoveAll();
  271.         this.hndList = this.groups[__filter].Clone();
  272.     }
  273.     this.doGroups = function(_key,_sort) {
  274.         __key = _key || this.groupBy;
  275.         __sort = (_sort) ? fb.TitleFormat(__key + _sort) : fb.TitleFormat(__key + this.sortBy);
  276.         __group = fb.TitleFormat(__key);
  277.         this.hndList.OrderByFormat(__sort, 1);
  278.         this.groups = {};
  279.         for (var i=0; i < this.hndList.Count; i++) {
  280.             __hnd = this.hndList.Item(i);
  281.             __groupName = (__group.EvalWithMetadb(__hnd)) || "MISSING";
  282.             if (!(__groupName in this.groups)) {
  283.                 this.groups[__groupName] = fb.getSelections(1);
  284.                 this.groups[__groupName].RemoveAll();
  285.                 doPrint("Group found : "+__groupName,this.ownId);
  286.             }
  287.             this.groups[__groupName].Add(__hnd);
  288.         }
  289.     }
  290.     this.getGroups = function() {
  291.         var __groups = [];
  292.         for(i in this.groups) {
  293.             if (this.groups.hasOwnProperty(i)) __groups.push(i);
  294.         }
  295.         return __groups;
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement