forked from tslab-hub/handlers
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInteractiveConstGen.cs
More file actions
157 lines (140 loc) · 7.42 KB
/
InteractiveConstGen.cs
File metadata and controls
157 lines (140 loc) · 7.42 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.ComponentModel;
using TSLab.DataSource;
using TSLab.Script.GraphPane;
using TSLab.Script.Handlers.Options;
using TSLab.Script.Optimization;
namespace TSLab.Script.Handlers
{
[HandlerCategory(BlockCategories.GraphPaneHandler)]
[HelperName("Interactive constant", Language = Constants.En)]
[HelperName("Интерактивная константа", Language = Constants.Ru)]
[InputsCount(1)]
[Input(0, TemplateTypes.GRAPHPANE, Name = "TemplateLibrary.Pane")]
[OutputsCount(1)]
[OutputType(TemplateTypes.DOUBLE)]
[Description("Создает интерактивную константу на панели графика (горизонтальная линия).")]
[HelperDescription("Creates an interactive constant on a chart pane (a horizontal line).", Constants.En)]
public sealed class InteractiveConstGen : ConstGenBase<double>, IInteractiveConstGen
{
private IInteractiveSimpleLine m_interactiveSimpleLine;
public IContext Context { get; set; }
/// <summary>
/// \~english Value of a constant
/// \~russian Значение константы
/// </summary>
[HelperName("Value", Constants.En)]
[HelperName("Значение", Constants.Ru)]
[Description("Значение константы")]
[HelperDescription("Value of a constant", Constants.En)]
[HandlerParameter(Name = "Value", Default = "100", IsShown = false)]
public OptimProperty Value { get; set; }
/// <summary>
/// \~english Pane side (vertical axis)
/// \~russian Сторона графика (вертикальная ось)
/// </summary>
[HelperName("Pane side", Constants.En)]
[HelperName("Сторона графика", Constants.Ru)]
[Description("Сторона графика (вертикальная ось)")]
[HelperDescription("Pane side (vertical axis)", Constants.En)]
[HandlerParameter(Default = "RIGHT", IsShown = false, NotOptimized = true)]
public PaneSides PaneSide { get; set; }
/// <summary>
/// \~english Color in hexadecimal RGB format (i.e. #ff0000 - red, #00ff00 - green, #0000ff - blue)
/// \~russian Цвет в шестнадцатеричном формате RGB (например, #ff0000 - красный, #00ff00 - зеленый, #0000ff - синий)
/// </summary>
[HelperName("Color", Constants.En)]
[HelperName("Цвет", Constants.Ru)]
[Description("Цвет в шестнадцатеричном формате RGB (например, #ff0000 - красный, #00ff00 - зеленый, #0000ff - синий)")]
[HelperDescription("Color in hexadecimal RGB format (i.e. #ff0000 - red, #00ff00 - green, #0000ff - blue)", Constants.En)]
[HandlerParameter(Default = "#ff0000", IsShown = false, NotOptimized = true, Editor = "StringToColorTemplate")]
public string Color { get; set; }
/// <summary>
/// \~english Thickness of a line
/// \~russian Толщина линии
/// </summary>
[HelperName("Thickness", Constants.En)]
[HelperName("Толщина", Constants.Ru)]
[Description("Толщина линии")]
[HelperDescription("Thickness of a line", Constants.En)]
[HandlerParameter(true, "1", Min = "1", Max = "10", Step = "1", EditorMin = "1", EditorMax = "10")]
public double Thickness { get; set; }
/// <summary>
/// \~english Recalculate agent when line changes its parameters
/// \~russian Пересчет агента, если изменяются параметры линии
/// </summary>
[HelperName("Recalculate agent?", Constants.En)]
[HelperName("Пересчитать агент?", Constants.Ru)]
[Description("Пересчет агента, если изменяются параметры линии")]
[HelperDescription("Recalculate agent when line changes its parameters", Constants.En)]
[HandlerParameter(Default = "true", IsShown = false, NotOptimized = true)]
public bool IsNeedRecalculate { get; set; }
public IList<double> Execute(IGraphPane pane)
{
if (pane == null)
throw new ArgumentNullException(nameof(pane));
var id = typeof(IInteractiveConstGen).Name + "." + Value.Data.GetId();
var container = (NotClearableContainer<InteractiveConstGen>)Context.LoadObject(id);
container?.Content.Unsubscribe();
InitInteractiveSimpleLine(pane);
Subscribe();
MakeList(Context.BarsCount, Value.Value);
Context.StoreObject(id, new NotClearableContainer<InteractiveConstGen>(this));
return this;
}
private void InitInteractiveSimpleLine(IGraphPane pane)
{
var id = Value.Data.GetId();
m_interactiveSimpleLine = (IInteractiveSimpleLine)pane.GetInteractiveObject(id);
var intColor = ColorParser.Parse(Color);
MarketPoint marketPosition;
if (m_interactiveSimpleLine != null)
{
marketPosition = new MarketPoint(m_interactiveSimpleLine.MarketPosition.X, Value.Value);
if (m_interactiveSimpleLine.PaneSides == PaneSide && m_interactiveSimpleLine.Color == intColor)
{
m_interactiveSimpleLine.Thickness = Thickness;
m_interactiveSimpleLine.MarketPosition = marketPosition;
pane.AddUnremovableInteractiveObjectId(id);
return;
}
pane.RemoveInteractiveObject(id);
}
else
marketPosition = new MarketPoint(DateTime.UtcNow, Value.Value);
m_interactiveSimpleLine = pane.AddInteractiveSimpleLine(id, PaneSide, false, intColor, InteractiveSimpleLineMode.Horizontal, marketPosition);
m_interactiveSimpleLine.Thickness = Thickness;
}
private void Unsubscribe()
{
Value.PropertyChanged -= OnValuePropertyChanged;
m_interactiveSimpleLine.PropertyChanged -= OnInteractiveSimpleLinePropertyChanged;
}
private void Subscribe()
{
Value.PropertyChanged += OnValuePropertyChanged;
m_interactiveSimpleLine.PropertyChanged += OnInteractiveSimpleLinePropertyChanged;
}
private void OnValuePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IOptimPropertyBase.Value))
{
Unsubscribe();
m_interactiveSimpleLine.MarketPosition = new MarketPoint(m_interactiveSimpleLine.MarketPosition.X, Value.Value);
Subscribe();
}
}
private void OnInteractiveSimpleLinePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IInteractivePoint.MarketPosition))
{
Unsubscribe();
((OptimDataBase)Value.Data).Value = Value.Value = m_interactiveSimpleLine.MarketPosition.Y;
Subscribe();
}
if (IsNeedRecalculate && !m_interactiveSimpleLine.IsMoving && (e.PropertyName == nameof(IInteractivePoint.MarketPosition) || e.PropertyName == nameof(IInteractivePoint.IsMoving)))
Context.Recalc();
}
}
}