// ==PREPROCESSOR==
// @name "Volbar with GdiDrawText"
// @author "T.P Wang"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

var g_font = gdi.Font('Segoe UI', 12, 0);
var g_drag = 0;
var ww = 0, wh = 0;

function on_size() {
	ww = window.Width;
	wh = window.Height;
}

function on_paint(gr) {
	var volume = fb.Volume;
	var pos = ww * vol2pos(volume);
	var txt = volume.toFixed(2) + 'dB';
	gr.FillGradRect(0, 0, pos, wh, 90, RGB(240, 240, 240), RGB(100, 230, 100));
	gr.FillGradRect(pos, 0, ww - pos, wh, 90, RGB(240, 240, 240), RGB(190, 190, 190));
	gr.GdiDrawText(txt, g_font, RGB(64, 64, 128), 0, 0, ww, wh, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	gr.DrawRect(0, 0, ww - 1, wh - 1, 1.0, RGB(150, 150, 150));
}

function on_mouse_lbtn_down(x, y) {
	g_drag = 1;
}

function on_mouse_lbtn_up(x, y) {
	on_mouse_move(x, y);
	g_drag = 0;
}

function on_mouse_move(x, y) {
	if (g_drag) {
		var pos = x < 0 ? 0 : x > ww ? 1 : x / ww;
		fb.Volume = pos2vol(pos);
	}
}

function on_mouse_wheel(delta) {
	if (delta > 0)
		fb.VolumeUp();
	else
		fb.VolumeDown();
}

function on_volume_change(val) {
	window.Repaint();
}
