If this is a bug, then it goes all the way back to the 7.91 release. I've tested these scripts on that, 8.0r2 and 10.5.1721.1140.
eSignal Development Staff, please take a look at this.
First, let's start off with something that works. I create a simple object and initialize a variable in that object, flag, to false. When I invoked the sub1() function, the flag property is set to true. Then, when I call sub2(), true for the flag property is correctly printed out because the object retains the value of flag between function calls.
This next piece of code shows what I think is a bug. What it's supposed to do is draw a "hello" button onto the chart and then every time the user clicks and drags the button with the left mouse button and releases it, the button will be moved to the new location on the chart.
This code is not working properly because, as you can see from the debugPrintln() statements when you test it on a chart, the bFlag variable within the object is not retaining its value between successive object method calls.
The only thing I can figure out is that somehow the usage of callbacks is stepping on the bDrag variable in a way to where it isn't updating between function calls as it should.
One more note about this code. You can see at the top that I'm pointing a global variable to a function internal to the button object so that the drawTextAbsolute() function inside it can call it. Currently, these callbacks can only be bound to global variable names and this hack is a workaround (see detailed posts by me in another thread about this issue).
The workaround for the above code so that the button moving functions as expected is to make the bDrag variable global and have the object's methods modify it as the mouse is pressed down and released. I would be delighted if I am wrong and I'm missing something that is dead simple and doesn't require some gawd awful hacking in the global namespace.
Here is the global flag hack to the button moving code shown below to make it work as it should (from a user's perspective).
eSignal Development Staff, please take a look at this.
First, let's start off with something that works. I create a simple object and initialize a variable in that object, flag, to false. When I invoked the sub1() function, the flag property is set to true. Then, when I call sub2(), true for the flag property is correctly printed out because the object retains the value of flag between function calls.
PHP Code:
var bInit = false;
function preMain()
{
setPriceStudy(true);
}
function main()
{
if (bInit == false) {
var obj = new myObject();
obj.sub1();
obj.sub2();
bInit = true;
}
}
function myObject()
{
this.flag = false;
this.sub1 = function() { this.flag = true; }
this.sub2 = function() { debugPrintln("in sub2(), this.flag = " + this.flag); }
}
This code is not working properly because, as you can see from the debugPrintln() statements when you test it on a chart, the bFlag variable within the object is not retaining its value between successive object method calls.
The only thing I can figure out is that somehow the usage of callbacks is stepping on the bDrag variable in a way to where it isn't updating between function calls as it should.
One more note about this code. You can see at the top that I'm pointing a global variable to a function internal to the button object so that the drawTextAbsolute() function inside it can call it. Currently, these callbacks can only be bound to global variable names and this hack is a workaround (see detailed posts by me in another thread about this issue).
PHP Code:
var bInit = false;
var first = true;
var button = new HelloButton();
var moveButton = button.moveButton;
function preMain()
{
setPriceStudy(true);
debugClear();
}
function main()
{
if (bInit == false)
{
bInit = true;
}
else if (first && isLastBarOnChart())
{
button.drawButton();
first = false;
}
}
function onLButtonUp(barIndex, yValue)
{
button.onLButtonUp(barIndex, yValue);
}
function HelloButton()
{
this.bDrag = false;
this.drawButton = function()
{
debugPrintln("drawButton called");
drawTextAbsolute(3, close(0), " hello @URL=EFS:moveButton", null, null,
Text.RELATIVEBOTTOM | Text.VCENTER | Text.FRAME, "Arial", 11, "T1");
}
this.moveButton = function(nButtonPressed)
{
debugPrintln("moveButton called");
this.bDrag = true;
debugPrintln("this.bDrag = " + this.bDrag);
}
this.onLButtonUp = function(barIndex, yValue)
{
debugPrintln("onLButtonUp called with this.bDrag = " + this.bDrag);
if (this.bDrag == true)
{
drawTextAbsolute(barIndex-1, yValue, " hello @URL=EFS:moveButton", null, null,
Text.RELATIVEBOTTOM | Text.VCENTER | Text.FRAME, "Arial", 11, "T1");
this.bDrag = false;
}
}
}
Here is the global flag hack to the button moving code shown below to make it work as it should (from a user's perspective).
PHP Code:
var bInit = false;
var first = true;
var button = new HelloButton();
var moveButton = button.moveButton;
var bDrag = false;
function preMain()
{
setPriceStudy(true);
debugClear();
}
function main()
{
if (bInit == false)
{
bInit = true;
}
else if (first && isLastBarOnChart())
{
button.drawButton();
first = false;
}
}
function onLButtonUp(barIndex, yValue)
{
button.onLButtonUp(barIndex, yValue);
}
function HelloButton()
{
this.drawButton = function()
{
debugPrintln("drawButton called");
drawTextAbsolute(3, close(0), " hello @URL=EFS:moveButton", null, null,
Text.RELATIVEBOTTOM | Text.VCENTER | Text.FRAME, "Arial", 11, "T1");
}
this.moveButton = function(nButtonPressed)
{
debugPrintln("moveButton called");
bDrag = true;
debugPrintln("bDrag = " + bDrag);
}
this.onLButtonUp = function(barIndex, yValue)
{
debugPrintln("onLButtonUp called with bDrag = " + bDrag);
if (bDrag == true)
{
drawTextAbsolute(barIndex-1, yValue, " hello @URL=EFS:moveButton", null, null,
Text.RELATIVEBOTTOM | Text.VCENTER | Text.FRAME, "Arial", 11, "T1");
bDrag = false;
}
}
}
Comment