forked from dingmaotu/mql4-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionMarker.mqh
101 lines (99 loc) · 4.2 KB
/
ActionMarker.mqh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//+------------------------------------------------------------------+
//| UI/ActionMarker.mqh |
//| Copyright 2017, Bear Two Technologies Co., Ltd. |
//+------------------------------------------------------------------+
#property strict
#include "Mouse.mqh"
#include "../Collection/Set.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
interface ActionEventHandler
{
void onAction(string id,string label);
};
//+------------------------------------------------------------------+
//| A label on chart and signal an action when moved |
//| This approach to trigger an action can be used in testing, where |
//| events are not processed |
//+------------------------------------------------------------------+
class ActionMarker
{
private:
const long m_chart;
const string m_id;
const string m_label;
const int m_ox,m_oy;
bool m_actived;
protected:
void ensureCreated();
Set<ActionEventHandler*>ActionEvent;
public:
ActionMarker(string id,string label,int x,int y,long chart=0):m_chart(chart==0?ChartID():chart),m_id(id),m_label(label),m_ox(x),m_oy(y),m_actived(false)
{
ensureCreated();
}
~ActionMarker() {ObjectDelete(m_chart,m_id);}
void check();
void operator+=(ActionEventHandler *handler) {ActionEvent.add(handler);}
void operator-=(ActionEventHandler *handler) {ActionEvent.remove(handler);}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ActionMarker::ensureCreated(void)
{
if(ObjectFind(m_chart,m_id)>=0) return;
ObjectCreate(m_chart,m_id,OBJ_LABEL,0,0,0);
ObjectSetString(m_chart,m_id,OBJPROP_TEXT,m_label);
ObjectSetString(m_chart,m_id,OBJPROP_TOOLTIP,"拖出当前位置之外一定距离,变色后松开");
ObjectSetInteger(m_chart,m_id,OBJPROP_CORNER,CORNER_RIGHT_LOWER);
ObjectSetInteger(m_chart,m_id,OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
ObjectSetInteger(m_chart,m_id,OBJPROP_SELECTABLE,1);
ObjectSetInteger(m_chart,m_id,OBJPROP_SELECTED,0);
ObjectSetInteger(m_chart,m_id,OBJPROP_XDISTANCE,m_ox);
ObjectSetInteger(m_chart,m_id,OBJPROP_YDISTANCE,m_oy);
ObjectSetInteger(m_chart,m_id,OBJPROP_COLOR,clrYellow);
ObjectSetInteger(m_chart,m_id,OBJPROP_FONTSIZE,12);
ObjectSetString(m_chart,m_id,OBJPROP_FONT,"Arial");
ObjectSetInteger(m_chart,m_id,OBJPROP_HIDDEN,1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ActionMarker::check(void)
{
ensureCreated();
if(!m_actived)
{
int x = (int)ObjectGetInteger(m_chart, m_id, OBJPROP_XDISTANCE);
int y = (int)ObjectGetInteger(m_chart, m_id, OBJPROP_YDISTANCE);
double xd = x-m_ox;
double yd = y-m_oy;
double distance=MathSqrt(xd*xd+yd*yd);
if(distance>50.0)
{
ObjectSetInteger(m_chart,m_id,OBJPROP_COLOR,clrRed);
m_actived=true;
}
}
else
{
if(Mouse::isLeftDown()) return;
ObjectSetInteger(m_chart,m_id,OBJPROP_XDISTANCE,m_ox);
ObjectSetInteger(m_chart,m_id,OBJPROP_YDISTANCE,m_oy);
ObjectSetInteger(m_chart,m_id,OBJPROP_COLOR,clrYellow);
ObjectSetInteger(m_chart,m_id,OBJPROP_SELECTED,0);
ChartRedraw(m_chart);
if(ActionEvent.size()>0)
{
foreach(ActionEventHandler*,ActionEvent)
{
ActionEventHandler *handler=it.current();
handler.onAction(m_id,m_label);
}
}
m_actived=false;
}
}
//+------------------------------------------------------------------+