Sorry it's me again......
Now I'm learning about timers, and wondering that, within window.SetInterval(function() {
HERE
},interval)
, what is the behaviour of keyword "this"?
this.refresh = function () {
if (this.refresh_frame_count != 0) {
window.ClearInterval(this.refresh_timer);
this.refresh_frame_count = 0;
this.refresh_timer = false;
}
window.RepaintRect(this.x, this.y, this.w, this.h);
this.refresh_timer = window.SetInterval(this.refresh_nested, this.refresh_interval);
}
this.refresh_nested = function () {
if (this.refresh_frame_count >= 1000 / this.refresh_interval) {
window.ClearInterval(this.refresh_timer);
this.refresh_frame_count = 0;
this.refresh_timer = false;
} else {
this.refresh_frame_count ++;
window.RepaintRect(this.x, this.y, this.w, this.h);
}
}
This part of code belong to example_object.
In this example, if I call "example_object.refresh", it only refreshes once, and the timer doesn't seem to work.
But once I changed all "this" in the code of "this.refresh_nested" to "example_object", the code worked properly, to refresh 25 times in a second.
I am confused by now. the keyword "this" is too convenient for me to be adapted. And given that the exact name of the used reference is uncertain:
var panel = new Array();
panel.push(new example_object);
panel.push(new example_object2);
(I used this kind of code because it's easy to form something like below)
function on_mouse_lbtn_down(x, y) {
for (i = 0; i < panel.length; i++) {
if (x >= panel[i].x && x < panel[i].x + panel[i].w && y >= panel[i].y && y < panel[i].y + panel[i].h) {
panel[i].lbtn_down(x, y);
}
}
}
I even don't know how to reference the correct variable name in "this.refresh_nested", if "this" is not an option.
Wait. This is hilarious. there actually is a "this" in the workable version of code.
this.refresh_timer = window.SetInterval(this.refresh_nested, this.refresh_interval);
"this.refresh_nested". How can that "this" work while the "this"'s within itself cannot?
Desperate.
Appendix: the full code
// ==PREPROCESSOR==
// @name "Seekbar"
// ==/PREPROCESSOR==
//--------
// General Functions
function RGB(r,g,b){ return (0xff000000|(r<<16)|(g<<8)|(b)); }
function RGBA(r,g,b,a){ return ((a<<24)|(r<<16)|(g<<8)|(b)); }
function TimeFmt(t){
var zpad = function(n){
var str = n.toString();
return (str.length<2) ? "0"+str : str;
}
var m = Math.floor(t/60); t-=m*60;
var s = Math.floor(t);
return m.toString()+":"+zpad(s);
}
// Global Variables
var theme_font = fb.TitleFormat("%theme_font%").Eval(true);
var panelh, panelw;
// for time display
/*
var
*/
// Seekbar
seekbar = function () {
this.init = function (x, y, w, h) {
this.name = seekbar;
this.font = gdi.Font(theme_font, 14, 2);
this.timewidth = window.GetProperty("seekbar_time_width",70);
this.iscd = window.GetProperty("seekbar_is_countdown",true);
this.handle_size = window.GetProperty("seekbar_handle_size__even_only",12);
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.drag = false;
this.seek = 0;
this.refresh_interval = window.GetProperty("seekbar_refresh_interval",40)
this.refresh_frame_count = 0;
this.refresh_timer = false;
this.bar = {
x: this.timewidth,
y: this.x + Math.floor(this.h / 2) - this.handle_size / 2,
w: this.w - 2 * this.timewidth
};
}
this.draw = function (gr) {
var newt = 0;
var pos = 0;
var t1, t2;
var length = fb.PlaybackLength;
if (length > 0) {
if (this.drag) {
newt = length * this.seek;
pos = this.bar.w * this.seek;
} else {
var pbkt = fb.PlaybackTime;
newt = pbkt;
pos = this.bar.w * (pbkt / length);
}
newt = (length < newt) ? length: (newt < 0) ? 0: newt;
t1 = TimeFmt (newt);
t2 = (this.iscd) ? " -" + TimeFmt(length - newt): TimeFmt(length);
} else {
t1 = t2 = "-:--";
}
gr.SetSmoothingMode(2);
gr.FillSolidRect(this.bar.x, this.bar.y + this.handle_size / 2 + 1, this.bar.w, 1, RGB(67,67,67));
gr.FillSolidRect(this.bar.x, this.bar.y + this.handle_size / 2 - 1, this.bar.w, 2, RGB(80,80,80));
gr.FillSolidRect(this.bar.x, this.bar.y + this.handle_size / 2 - 1, pos + 5, 2, RGB(223,232,4));
gr.FillEllipse(this.bar.x + pos - this.handle_size / 2 - 1, this.bar.y, this.handle_size, this.handle_size, RGB(180,180,180));
gr.FillEllipse(this.bar.x + pos - this.handle_size / 2, this.bar.y + 1, this.handle_size, this.handle_size, RGB(255,255,255));
var tformat = 0x00000001 | 0x00000004 | 0x00000400 | 0x00000800 | 0x00008000;
gr.GdiDrawText(t2, this.font, RGB(255,255,255), this.w - this.timewidth, this.y, this.timewidth, this.h, tformat);
gr.GdiDrawText(t1, this.font, RGB(255,255,255), this.x, this.y, this.timewidth, this.h, tformat);
}
this.key_down = function (key) {
switch (key) {
}
}
this.lbtn_down = function (x, y) {
if (x >= this.bar.x && x < this.bar.x + this.bar.w && y >= this.bar.y && y < this.bar.y + this.handle_size) this.drag = 1;
}
this.lbtn_up = function (x, y) {
if (this.drag) {
this.drag = 0;
this.seek = (x - this.bar.x) / this.bar.w;
this.seek = (this.seek < 0) ? 0 : (this.seek < 1) ? this.seek : 1;
fb.PlaybackTime = fb.PlaybackLength * this.seek;
} else if (x > this.bar.x + this.bar.w) {
var val;
val = this.iscd? false: true;
window.SetProperty("use_countdown", val)
this.iscd = val;
}
}
this.move = function (x, y) {
if (this.drag) {
this.seek = (x - this.bar.x) / this.bar.w;
this.seek = (this.seek < 0) ? 0 : (this.seek < 1) ? this.seek : 1;
window.RepaintRect(this.x, this.y, this.w, this.h);
}
}
this.refresh = function () {
if (this.refresh_frame_count != 0) {
window.ClearInterval(this.refresh_timer);
this.refresh_frame_count = 0;
this.refresh_timer = false;
}
window.RepaintRect(this.x, this.y, this.w, this.h);
this.refresh_timer = window.SetInterval(this.refresh_nested, this.refresh_interval);
}
this.refresh_nested = function () {
if (this.refresh_frame_count >= 1000 / this.refresh_interval) {
window.ClearInterval(this.refresh_timer);
this.refresh_frame_count = 0;
this.refresh_timer = false;
} else {
this.refresh_frame_count ++;
window.RepaintRect(this.x, this.y, this.w, this.h);
}
}
}
// Panel global variables
var panel = new Array();
panel.push(new seekbar);
function on_paint(gr) {
for (i = 0; i < panel.length; i++) {
panel[i].draw(gr);
}
}
function on_size() {
panelw = window.Width;
panelh = window.Height;
panel[0].init(0, 0, panelw, panelh);
}
function on_key_down(key) {
}
function on_mouse_lbtn_down(x, y) {
for (i = 0; i < panel.length; i++) {
if (x >= panel[i].x && x < panel[i].x + panel[i].w && y >= panel[i].y && y < panel[i].y + panel[i].h) {
panel[i].lbtn_down(x, y);
}
}
}
function on_mouse_lbtn_up(x, y) {
for (i = 0; i < panel.length; i++) {
if (x >= panel[i].x && x < panel[i].x + panel[i].w && y >= panel[i].y && y < panel[i].y + panel[i].h) {
panel[i].lbtn_up(x, y);
}
}
}
function on_mouse_move(x, y) {
for (i = 0; i < panel.length; i++) {
panel[i].move(x, y);
}
}
function on_mouse_wheel(delta) {
}
function on_playback_starting(cmd, paused){
}
function on_playback_new_track(info){
panel[0].refresh();
}
function on_playback_stop(){
window.Repaint();
}
function on_playback_seek(time){
panel[0].refresh();
}
function on_playback_pause(state){
panel[0].refresh();
}
function on_playback_edited(){
}
function on_playback_dynamic_info(){
}
function on_playback_dynamic_info_track(){
}
function on_playback_time(time){
panel[0].refresh();
}
function on_volume_change(val){
}