-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbestgraph.go
More file actions
195 lines (168 loc) · 4.88 KB
/
bestgraph.go
File metadata and controls
195 lines (168 loc) · 4.88 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package bestgraph
import (
"fmt"
"image/color"
"github.com/civiledcode/bestgraph/chart"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
const (
HUD_SIZE = 128
FOOTER_SIZE = 48
)
type ChartRender struct {
Ctx *chart.Chart
Candles []chart.Candle
PressedKeys []ebiten.Key
ScreenWidth int
ScreenHeight int
KeyTick uint64
ShouldClose bool
CandleIntervals []float32
CurrentInterval int
TickRate uint64
CandlesPerMove int
DefaultFont *text.GoTextFaceSource
}
func (g *ChartRender) StartChart() error {
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
ebiten.SetWindowFloating(false)
ebiten.SetWindowTitle("BestGraph - " + g.Ctx.SymbolName)
if err := ebiten.RunGame(g); err != nil {
return err
}
return nil
}
func (g *ChartRender) Update() error {
if g.ShouldClose {
return ebiten.Termination
}
g.PressedKeys = inpututil.AppendPressedKeys(g.PressedKeys[:0])
return nil
}
func (g *ChartRender) Draw(screen *ebiten.Image) {
// uint64 max
if g.KeyTick == 18446744073709551615 {
g.KeyTick = 0
} else {
g.KeyTick++
}
for _, key := range g.PressedKeys {
if key == ebiten.KeyH {
g.drawHelp(screen)
return
}
if key == ebiten.KeyQ {
g.ShouldClose = true
return
}
}
if g.KeyTick%g.TickRate == 0 {
for _, key := range g.PressedKeys {
if g.KeyTick%(g.TickRate*5) == 0 {
if key == ebiten.KeyO {
if g.CurrentInterval < len(g.CandleIntervals)-1 {
fmt.Println("Zoom Out")
g.CurrentInterval++
g.Ctx.SetWidthCandles(g.CandleIntervals[g.CurrentInterval])
}
} else if key == ebiten.KeyI {
if g.CurrentInterval > 0 {
fmt.Println("Zoom In")
g.CurrentInterval--
g.Ctx.SetWidthCandles(g.CandleIntervals[g.CurrentInterval])
}
}
if key == ebiten.KeyJ {
fmt.Println("Pip Price Increase")
g.Ctx.PricePerPip += g.Ctx.PipPriceIncrement
} else if key == ebiten.KeyK {
if (g.Ctx.PricePerPip - g.Ctx.PipPriceIncrement) >= g.Ctx.PipPriceMinimum {
fmt.Println("Pip Price Decrease")
g.Ctx.PricePerPip -= g.Ctx.PipPriceIncrement
} else {
g.Ctx.PricePerPip = g.Ctx.PipPriceMinimum
}
}
if key == ebiten.KeyN {
fmt.Println("Candles Per Move Increased")
g.CandlesPerMove++
} else if key == ebiten.KeyM {
if g.CandlesPerMove > 1 {
fmt.Println("Candles Per Move Decreased")
g.CandlesPerMove--
}
}
}
if key == ebiten.KeyW || key == ebiten.KeyArrowUp {
g.Ctx.CurrentPip++
} else if key == ebiten.KeyS || key == ebiten.KeyArrowDown {
g.Ctx.CurrentPip--
} else if key == ebiten.KeyA || key == ebiten.KeyArrowLeft {
if g.Ctx.CurrentCandle-g.CandlesPerMove >= 0 {
g.Ctx.CurrentCandle -= g.CandlesPerMove
}
} else if key == ebiten.KeyD || key == ebiten.KeyArrowRight {
g.Ctx.CurrentCandle += g.CandlesPerMove
}
}
}
//g.Ctx.DrawCandle(screen, g.Ctx.Candles[0], g.Ctx.GetCandleXPixel(g.Ctx.CurrentCandle))
g.Ctx.DrawPipPoints(screen, float32(g.ScreenWidth), float32(g.ScreenHeight))
g.Ctx.DrawCandles(screen)
g.Ctx.DrawHud(screen, float32(g.ScreenWidth), float32(g.ScreenHeight))
}
func (g *ChartRender) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
g.ScreenHeight = outsideHeight
g.ScreenWidth = outsideWidth
// Excluse header in useable size.
useableHeight := outsideHeight - HUD_SIZE - FOOTER_SIZE
// Set the chart size on resize.
g.Ctx.SetHeightPixels(float32(int(useableHeight/int(g.Ctx.PipsHeight))) * g.Ctx.PipsHeight)
g.Ctx.SetWidthPixels(float32(int(outsideWidth/int(g.Ctx.CandlesShown))) * g.Ctx.CandlesShown)
return outsideWidth, outsideHeight
}
func (g *ChartRender) drawHelp(screen *ebiten.Image) {
helpMessage :=
`[H] - Show this message
[D or →] Increment Current Candle
[A or ←] Decrement Current Candle
[W or ↑] Increment Current Pip
[S or ↓] Decrement Current Pip
[I] Decrease Candles Shown
[O] Increase Candles Shown
[J] Increase Price Per Pip
[K] Decrease Price Per Pip
[N] Increase Candles Per Move
[M] Decrease Candles Per Move
[Q] Close Chart
`
op := &text.DrawOptions{}
op.LineSpacing = 20
op.GeoM.Translate(10, 10)
op.ColorScale.ScaleWithColor(color.White)
text.Draw(screen, helpMessage, &text.GoTextFace{
Source: g.DefaultFont,
Size: 18,
}, op)
}
func CreateChartRender(chart *chart.Chart, candleIntervals []float32, startingInterval int, font *text.GoTextFaceSource) *ChartRender {
chartRenderer := &ChartRender{
Ctx: chart,
CandlesPerMove: 1,
CandleIntervals: candleIntervals,
CurrentInterval: startingInterval,
DefaultFont: font,
TickRate: 5,
}
// Hardcoded for now...
chart.ViewportY = HUD_SIZE
if chart.PipsHeight == 0 {
chart.PipsHeight = 24
}
if chart.CandlesShown == 0 {
chart.CandlesShown = candleIntervals[startingInterval]
}
return chartRenderer
}