-
Notifications
You must be signed in to change notification settings - Fork 0
/
hud.go
206 lines (171 loc) · 5.65 KB
/
hud.go
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
196
197
198
199
200
201
202
203
204
205
206
package dcshmd
import (
"fmt"
"image"
"image/color"
"sync"
"github.com/hajimehoshi/ebiten/v2"
"github.com/dimchansky/dcs-hmd/aircraft/ka-50/devices/rotorpitch"
"github.com/dimchansky/dcs-hmd/aircraft/ka-50/devices/rotorrpm"
"github.com/dimchansky/dcs-hmd/aircraft/ka-50/devices/verticalvelocity"
"github.com/dimchansky/dcs-hmd/utils"
)
const (
ScreenWidth = 800
ScreenHeight = 600
fontBaseSize = 18
dpi = 72
rowWidth = 20
rowHeight = 20
)
var (
textColor = color.NRGBA{G: 0xff, A: 0xff}
shadowColor = color.NRGBA{A: 0xff}
)
func NewHUD() (*HUD, error) {
ebiten.SetScreenClearedEveryFrame(false)
ebiten.SetScreenFilterEnabled(false)
ebiten.SetRunnableOnUnfocused(true)
ebiten.SetInitFocused(false)
ebiten.SetScreenTransparent(true)
ebiten.SetWindowDecorated(false)
ebiten.SetWindowFloating(true)
ebiten.SetWindowSize(ScreenWidth, ScreenHeight)
const (
indicatorHeight = 400
xSpan = rowWidth / 2
ySpan = rowHeight
)
rotorPitchIndicator := rotorpitch.NewIndicator(&rotorpitch.IndicatorConfig{
Width: rowWidth * 3,
Height: indicatorHeight,
TickLength: rowWidth,
MinorTickLength: rowWidth / 2,
LineWidth: 2,
Color: textColor,
BorderColor: shadowColor,
Rect: image.Rectangle{
Min: image.Pt(xSpan, ySpan),
Max: image.Pt(rowWidth*2+xSpan, indicatorHeight-ySpan),
},
})
rotorRPMIndicator := rotorrpm.NewIndicator(&rotorrpm.IndicatorConfig{
Width: rowWidth * 3,
Height: indicatorHeight,
TickLength: rowWidth,
MinorTickLength: rowWidth * 3 / 4,
LineWidth: 2,
Color: textColor,
BorderColor: shadowColor,
Rect: image.Rectangle{
Min: image.Pt(xSpan, ySpan),
Max: image.Pt(rowWidth*2+xSpan, indicatorHeight-ySpan),
},
})
verticalVelocityIndicator := verticalvelocity.NewIndicator(&verticalvelocity.IndicatorConfig{
Width: rowWidth * 3,
Height: indicatorHeight,
TickLength: rowWidth,
MinorTickLength: rowWidth * 3 / 4,
LineWidth: 2,
Color: textColor,
BorderColor: shadowColor,
Rect: image.Rectangle{
Min: image.Pt(xSpan, ySpan),
Max: image.Pt(rowWidth*2+xSpan, indicatorHeight-ySpan),
},
})
ff, err := NewFontFace(fontBaseSize, dpi)
if err != nil {
return nil, err
}
hud := &HUD{
fontFace: ff,
rotorPitchIndicator: rotorPitchIndicator,
rotorRPMIndicator: rotorRPMIndicator,
verticalVelocityIndicator: verticalVelocityIndicator,
}
return hud, nil
}
type HUD struct {
once sync.Once
fontFace *FontFace
rotorPitchIndicator *rotorpitch.Indicator
rotorRPMIndicator *rotorrpm.Indicator
verticalVelocityIndicator *verticalvelocity.Indicator
rotorPitchImg redrawnImage
rotorRPMImg redrawnImage
verticalVelocityImg redrawnImage
}
func (h *HUD) Close() error {
return fmt.Errorf("failed to close font face: %w", h.fontFace.Close())
}
func (h *HUD) Update() error {
h.once.Do(enableCurrentProcessWindowClickThroughAsync)
h.rotorPitchImg.Update(h.rotorPitchIndicator.GetImage())
h.rotorRPMImg.Update(h.rotorRPMIndicator.GetImage())
h.verticalVelocityImg.Update(h.verticalVelocityIndicator.GetImage())
return nil
}
func (h *HUD) Draw(screen *ebiten.Image) {
rotorPitchImg := &h.rotorPitchImg
rotorRPMImg := &h.rotorRPMImg
verticalVelocityImg := &h.verticalVelocityImg
if !rotorPitchImg.NeedToDraw &&
!rotorRPMImg.NeedToDraw &&
!verticalVelocityImg.NeedToDraw {
return
}
op := &ebiten.DrawImageOptions{CompositeMode: ebiten.CompositeModeCopy}
op.GeoM.Translate(0, rowHeight)
rotorPitchImg.DrawOn(screen, op)
op.GeoM.Translate(float64(rotorPitchImg.Size().X), 0)
rotorRPMImg.DrawOn(screen, op)
op.GeoM.Reset()
op.GeoM.Translate(ScreenWidth-float64(verticalVelocityImg.Size().X)-1, rowHeight)
verticalVelocityImg.DrawOn(screen, op)
}
func (h *HUD) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenWidth, ScreenHeight
}
// SetRotorPitch is thread-safe to update rotor pitch.
func (h *HUD) SetRotorPitch(val float64) {
h.rotorPitchIndicator.SetRotorPitch(val)
}
// SetRotorRPM is thread-safe to update rotor RPM.
func (h *HUD) SetRotorRPM(val float64) {
h.rotorRPMIndicator.SetRotorRPM(val)
}
// SetVerticalVelocity is thread-safe to update vertical velocity.
func (h *HUD) SetVerticalVelocity(val float64) {
h.verticalVelocityIndicator.SetVerticalVelocity(val)
}
func enableCurrentProcessWindowClickThroughAsync() {
go utils.EnableCurrentProcessWindowClickThrough()
}
// redrawnImage represents an image that needs to be drawn again.
type redrawnImage struct {
img *ebiten.Image
NeedToDraw bool
}
// Update updates the image and sets the NeedToDraw flag based on whether the image has been redrawn or not,
// or whether NeedToDraw was already true. If img is nil, NeedToDraw will always be true.
// The NeedToDraw flag is used to indicate whether the image needs to be drawn on the screen or not.
// If the flag is true, the image will be redrawn on the screen in the next frame.
// This flag is set to false by the DrawOn method after the image is drawn on the screen.
func (i *redrawnImage) Update(img *ebiten.Image, isRedrawn bool) {
i.NeedToDraw = i.NeedToDraw || i.img == nil || isRedrawn
i.img = img
}
// DrawOn draws the image on the given screen with the given options if the NeedToDraw flag is true.
// After drawing, NeedToDraw is set to false.
func (i *redrawnImage) DrawOn(screen *ebiten.Image, op *ebiten.DrawImageOptions) {
if i.NeedToDraw {
screen.DrawImage(i.img, op)
i.NeedToDraw = false
}
}
// Size returns the size of the image.
func (i *redrawnImage) Size() image.Point {
return i.img.Bounds().Size()
}