HydrogenAudio

Hosted Forums => foobar2000 => General - (fb2k) => Topic started by: 62a on 2017-08-11 19:15:35

Title: $rand() always returns zero?
Post by: 62a on 2017-08-11 19:15:35
I'm trying to use the $rand() function in a query to randomly switch between sets of files that share a certain tag value. But it seems like the function just returns zero every time I use it. I did notice the wiki says $rand() is "[a]vailable only in sort-related contexts," so is that why it's not working here?

Example: I have a bunch of tracks with a tag called "V", which is set to either 0 or 1. The query
Code: [Select]
v EQUAL "$rand()"
always just finds the tracks where V is 0.

Related question: Is there a quick way to test title formatting expressions i.e. see what they evaluate to?
Title: Re: $rand() always returns zero?
Post by: marc2003 on 2017-08-12 00:45:51
I can make a button generate a random query for you each time it's clicked. It requires that you add foo_jscript_panel to your layout...

https://github.com/19379/foo-jscript-panel/wiki/Requirements-&-Installation

The script...

Code: [Select]
// ==PREPROCESSOR==
// @name "SimpleThemedButton"
// @author "T.P Wang"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

function SimpleButton(x, y, w, h, text, fonClick, state) {
this.state = state ? state : ButtonStates.normal;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = text;
this.fonClick = fonClick;

this.containXY = function (x, y) {
return (this.x <= x) && (x <= this.x + this.w) && (this.y <= y) && (y <= this.y + this.h);
}

this.changeState = function (state) {
var old = this.state;
this.state = state;
return old;
}

this.draw = function (gr) {
if (this.state == ButtonStates.hide) return;

switch (this.state)
{
case ButtonStates.normal:
g_theme.SetPartAndStateId(1, 1);
break;

case ButtonStates.hover:
g_theme.SetPartAndStateId(1, 2);
break;

case ButtonStates.down:
g_theme.SetPartAndStateId(1, 3);
break;

case ButtonStates.hide:
return;
}

g_theme.DrawThemeBackground(gr, this.x, this.y, this.w, this.h);
// RGB function is defined in docs\helpers.txt
// DT_* are defined in docs\flags.txt
gr.GdiDrawText(this.text, g_font, RGB(0,0,0), this.x, this.y, this.w, this.h, DT_CENTER| DT_VCENTER | DT_CALCRECT | DT_NOPREFIX);
}

this.onClick = function () {
this.fonClick && this.fonClick();
}
}

function drawAllButtons(gr) {
for (var i in buttons) {
buttons[i].draw(gr);
}
}

function chooseButton(x, y) {
for (var i in buttons) {
if (buttons[i].containXY(x, y) && buttons[i].state != ButtonStates.hide) return buttons[i];
}

return null;
}

function on_paint(gr) {
gr.FillSolidRect(0, 0, window.Width, window.Height, utils.GetSysColor(15));
drawAllButtons(gr);
}

function on_mouse_move(x, y) {
var old = cur_btn;
cur_btn = chooseButton(x, y);

if (old == cur_btn) {
if (g_down) return;
} else if (g_down && cur_btn && cur_btn.state != ButtonStates.down) {
cur_btn.changeState(ButtonStates.down);
window.Repaint();
return;
}

old && old.changeState(ButtonStates.normal);
cur_btn && cur_btn.changeState(ButtonStates.hover);
window.Repaint();
}

function on_mouse_leave() {
g_down = false;

if (cur_btn) {
cur_btn.changeState(ButtonStates.normal);
window.Repaint();
}
}

function on_mouse_lbtn_down(x, y) {
g_down = true;

if (cur_btn) {
cur_btn.changeState(ButtonStates.down);
window.Repaint();
}
}

function on_mouse_lbtn_up(x, y) {
g_down = false;

if (cur_btn) {
cur_btn.onClick();
cur_btn.changeState(ButtonStates.hover);
window.Repaint();
}
}

var cur_btn = null;
var g_theme = window.CreateThemeManager("Button");
var g_font = gdi.Font("Segoe UI", 12);
var g_down = false;

var ButtonStates = {
normal: 0,
hover: 1,
down: 2,
hide: 3
}

var buttons = {
random: new SimpleButton(5, 5, 100, 26, "Randomise V", function () {
fb.ShowLibrarySearchUI("v EQUAL " + Math.round(Math.random()));
})
};

It uses javascript rather than title formatting for the random number generation. The relevant bit of code is right at the bottom of the script.

For testing title formatting, I generally use the status bar preview in the main preferences or you can mess around with this..

http://www.foobar2000.org/components/view/foo_tfsandbox
Title: Re: $rand() always returns zero?
Post by: 62a on 2017-08-12 01:46:20
Nice. This looks promising. It also looks easily modifiable for more complex queries. I will give it a try. Thanks!