/
cursor.go
67 lines (58 loc) · 2.3 KB
/
cursor.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
package render
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/liamg/darktile/internal/app/darktile/termutil"
)
func (r *Render) drawCursor() {
//draw cursor
if !r.buffer.IsCursorVisible() {
return
}
pixelX := float64(int(r.buffer.CursorColumn()) * r.font.CellSize.X)
pixelY := float64(int(r.buffer.CursorLine()) * r.font.CellSize.Y)
cell := r.buffer.GetCell(r.buffer.CursorColumn(), r.buffer.CursorLine())
useFace := r.font.Regular
if cell != nil {
if cell.Bold() && cell.Italic() {
useFace = r.font.BoldItalic
} else if cell.Bold() {
useFace = r.font.Bold
} else if cell.Italic() {
useFace = r.font.Italic
}
}
pixelW, pixelH := float64(r.font.CellSize.X), float64(r.font.CellSize.Y)
// empty rect without focus
if !ebiten.IsFocused() {
ebitenutil.DrawRect(r.frame, pixelX, pixelY, pixelW, pixelH, r.theme.CursorBackground())
ebitenutil.DrawRect(r.frame, pixelX+1, pixelY+1, pixelW-2, pixelH-2, r.theme.CursorForeground())
return
}
// draw the cursor shape
switch r.buffer.GetCursorShape() {
case termutil.CursorShapeBlinkingBar, termutil.CursorShapeSteadyBar:
ebitenutil.DrawRect(r.frame, pixelX, pixelY, 2, pixelH, r.theme.CursorBackground())
case termutil.CursorShapeBlinkingUnderline, termutil.CursorShapeSteadyUnderline:
ebitenutil.DrawRect(r.frame, pixelX, pixelY+pixelH-2, pixelW, 2, r.theme.CursorBackground())
default:
// draw a custom cursor if we have one and there are no characters in the way
if r.cursorImage != nil && (cell == nil || cell.Rune().Rune == 0) {
opt := &ebiten.DrawImageOptions{}
_, h := r.cursorImage.Size()
ratio := 1 / (float64(h) / float64(r.font.CellSize.Y))
actualHeight := float64(h) * ratio
offsetY := (float64(r.font.CellSize.Y) - actualHeight) / 2
opt.GeoM.Scale(ratio, ratio)
opt.GeoM.Translate(pixelX, pixelY+offsetY)
r.frame.DrawImage(r.cursorImage, opt)
return
}
ebitenutil.DrawRect(r.frame, pixelX, pixelY, pixelW, pixelH, r.theme.CursorBackground())
// we've drawn over the cell contents, so we need to draw it again in the cursor colours
if cell != nil && cell.Rune().Rune > 0 {
text.Draw(r.frame, string(cell.Rune().Rune), useFace, int(pixelX), int(pixelY)+r.font.DotDepth, r.theme.CursorForeground())
}
}
}