Consult this code maybe it will give you an idea of how your structure should be.
#property version"1.00"string system_tag="test_";
//variables , names are elongated on purpose//draft control variables , can be placed inside structures ...you know ,normallyint button_from_x=0,button_to_x=0,button_from_y=0,button_to_y=0;
bool is_button_on_a_click=false,is_button_hovered=false,button_mouse_scroll_memory=false;
//mouse control ,also could be in a structure ... normallyint mouse_x=0,mouse_y=0,previous_mouse_x=0,previous_mouse_y=0;
string mouse_click_string=NULL,previous_mouse_click_string=NULL;
intOnInit()
{
//resetObjectsDeleteAll(ChartID(),system_tag);
is_button_hovered=false;is_button_on_a_click=false;button_mouse_scroll_memory=false;
mouse_x=0;mouse_y=0;mouse_click_string=NULL;
previous_mouse_x=0;previous_mouse_y=0;previous_mouse_click_string=NULL;
//create a button ,ill use rectangle,what it is is irrelevantObjectCreate(ChartID(),system_tag+"_btn",OBJ_RECTANGLE_LABEL,0,0,0);
ObjectSetInteger(ChartID(),system_tag+"_btn",OBJPROP_XSIZE,200);
ObjectSetInteger(ChartID(),system_tag+"_btn",OBJPROP_YSIZE,40);
ObjectSetInteger(ChartID(),system_tag+"_btn",OBJPROP_XDISTANCE,20);
ObjectSetInteger(ChartID(),system_tag+"_btn",OBJPROP_YDISTANCE,20);
button_from_x=20;button_to_x=button_from_x+200;
button_from_y=20;button_to_y=button_from_y+40;
ChartRedraw();
//first ,activate mousemove ChartSetInteger(ChartID(),CHART_EVENT_MOUSE_MOVE,true);
return(INIT_SUCCEEDED);
}
//EVENTS voidOnChartEvent(constint id,
constlong &lparam,
constdouble &dparam,
conststring &sparam)
{
//---//mousemoveif(id==CHARTEVENT_MOUSE_MOVE)
{
//get mouse coordinates
mouse_x=(int)lparam;
mouse_y=(int)dparam;
mouse_click_string=sparam;
//hover checkbool was_button_hovered=is_button_hovered;
is_button_hovered=false;
if(mouse_x>=button_from_x&&mouse_x<=button_to_x&&mouse_y>=button_from_y&&mouse_y<=button_to_y){is_button_hovered=true;}
//if button is not clickingif(is_button_on_a_click==false)
{
//if a left click arrives if(mouse_click_string=="1"&&previous_mouse_click_string=="0")
{
//and we was hovering the button - so you know , the click starts on the buttonif(was_button_hovered==true)
{
//and the mouse is still over the buttonif(is_button_hovered==true)
{
//get the mouse scroll memory if(ChartGetInteger(ChartID(),CHART_MOUSE_SCROLL)){button_mouse_scroll_memory=true;}
//disable the mouse scroll if need be (and if you want to)if(button_mouse_scroll_memory){ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,false);}
//enable clicking
is_button_on_a_click=true;
}
//and the mouse is still over the button ends here
}
//and we was hovering the button - so you know , the click starts on the button ends here
}
//if a left click arrives ends here
}
elseif(is_button_on_a_click==true)//if button is on a click but dont access the first time
{
//and user lifts their finger from the mouse buttonif(mouse_click_string=="0")
{
//restore mouse scrollif(button_mouse_scroll_memory==true){ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);button_mouse_scroll_memory=false;}
//cancel clicking event
is_button_on_a_click=false;
//dont cancel hover //alert the end of the eventAlert("THE CLICK IS NOW COMPLETED");
}
}
//if button is not clicking ends here
previous_mouse_x=mouse_x;
previous_mouse_y=mouse_y;
previous_mouse_click_string=mouse_click_string;
}
//mousemove ends here
}
//EVENTSvoidOnTick(){}
voidOnDeinit(constint reason)
{
//resetObjectsDeleteAll(ChartID(),system_tag);
is_button_hovered=false;is_button_on_a_click=false;button_mouse_scroll_memory=false;
mouse_x=0;mouse_y=0;mouse_click_string=NULL;
previous_mouse_x=0;previous_mouse_y=0;previous_mouse_click_string=NULL;
}
Narek Kamalyan: @Lorentzos Roussos Thank you. It does what I need. How I can modify the code to use with multiple objects.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates and run through them on each mousemove event execution.
The difference is that if any one of the buttons "is clicking" the rest are not interactable.
Pair that with the ability to change the bitmap label and you have a system where the buttons "light up" and stay "lit" ( xD ) when being clicked.
Beware though as in huge deployments you will need to enforce delays and cancel outs of the mouse click to avoid misfires or double execution.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates and run through them on each mousemove event execution.
The difference is that if any one of the buttons "is clicking" the rest are not interactable.
Pair that with the ability to change the bitmap label and you have a system where the buttons "light up" and stay "lit" ( xD ) when being clicked.
Beware though as in huge deployments you will need to enforce delays and cancel outs of the mouse click to avoid misfires or double execution.
@Lorentzos Roussos I played arround and was able to get what I want, i.e.
So what I am trying to figure out is to cancel the action if the mouse release was outside of the button. So, where should put the execution code to make it work only if the mouse button was released on the button.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates and run through them on each mousemove event execution.
Can you please show a code sample for two different objects.
Can you please show a code sample for two different objects.
No
So what I am trying to figure out is to cancel the action if the mouse release was outside of the button. So, where should put the execution code to make it work only if the mouse button was released on the button.
Then you will go to the section that controls execution here :
elseif(is_button_on_a_click==true)//if button is on a click but dont access the first time
{
//and user lifts their finger from the mouse buttonif(mouse_click_string=="0")
{
//restore mouse scrollif(button_mouse_scroll_memory==true){ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);button_mouse_scroll_memory=false;}
//cancel clicking event
is_button_on_a_click=false;
//dont cancel hover //alert the end of the eventAlert("THE CLICK IS NOW COMPLETED");
}
}
and specify if the button is no longer hovered on release then dont do whats supposed to be done
elseif(is_button_on_a_click==true)//if button is on a click but dont access the first time
{
//and user lifts their finger from the mouse buttonif(mouse_click_string=="0")
{
//restore mouse scrollif(button_mouse_scroll_memory==true){ChartSetInteger(ChartID(),CHART_MOUSE_SCROLL,true);button_mouse_scroll_memory=false;}
//cancel clicking event
is_button_on_a_click=false;
//dont cancel hover //alert the end of the eventif(is_button_hovered){Alert("THE CLICK IS NOW COMPLETED");}
if(!is_button_hovered){Alert("THE USER CHANGED THEIR MIND");}
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I found this code for checking mouse position on Mouse Left DOWN and Mouse Left UP
Lets say I have a button named "My Button"
How I can set action to this button using the code above.
Consult this code maybe it will give you an idea of how your structure should be.
@Lorentzos Roussos Thank you. It does what I need. How I can modify the code to use with multiple objects.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates
and run through them on each mousemove event execution.
The difference is that if any one of the buttons "is clicking" the rest are not interactable.
Pair that with the ability to change the bitmap label and you have a system where the buttons "light up" and stay "lit" ( xD ) when being clicked.
Beware though as in huge deployments you will need to enforce delays and cancel outs of the mouse click to avoid misfires or double execution.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates
and run through them on each mousemove event execution.
The difference is that if any one of the buttons "is clicking" the rest are not interactable.
Pair that with the ability to change the bitmap label and you have a system where the buttons "light up" and stay "lit" ( xD ) when being clicked.
Beware though as in huge deployments you will need to enforce delays and cancel outs of the mouse click to avoid misfires or double execution.
@Lorentzos Roussos I played arround and was able to get what I want, i.e.
Changed button state by mouse click
And reset on mouse release
So what I am trying to figure out is to cancel the action if the mouse release was outside of the button. So, where should put the execution code to make it work only if the mouse button was released on the button.
You will need to keep a log of all your objects (mostly buttons) with an is_hovered switch , is_clicking switch ,from_x,from_y,to_x,to_y coordinates
and run through them on each mousemove event execution.
Can you please show a code sample for two different objects.
Can you please show a code sample for two different objects.
No
Then you will go to the section that controls execution here :
and specify if the button is no longer hovered on release then dont do whats supposed to be done
@Lorentzos Roussos you are genious.
Is it possible to put execution code on hovering (without click)?
@Lorentzos Roussos you are genious.
Is it possible to put execution code on hovering (without click)?
For effects ? yes