summaryrefslogtreecommitdiff
path: root/bar/x.go
blob: 4d77bfd7b1cb7fd2d3e72fbde236596565d8f491 (plain)
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
package bar

import (
	"os"
	"image"

	"hhvn.uk/hbspbar/common"

	"github.com/jezek/xgbutil/xgraphics"
	"github.com/BurntSushi/freetype-go/freetype/truetype"
)

func getFont(path string) (*truetype.Font, error) {
	read, err := os.Open(path)
	if err != nil {
		return nil, common.Perror("os.Open", err)
	}

	font, err := xgraphics.ParseFont(read)
	if err != nil {
		return nil, common.Perror("xgraphics.ParseFont", err)
	}

	return font, nil
}

func int2BGRA(argb uint32) (xgraphics.BGRA) {
	return xgraphics.BGRA{
		B: uint8( argb & 0x000000ff),
		G: uint8((argb & 0x0000ff00) >> 8),
		R: uint8((argb & 0x00ff0000) >> 16),
		A: uint8((argb & 0xff000000) >> 24) }
}

func (b *bar) rect() image.Rectangle {
	return image.Rect( 0, 0,
		int(b.Mon.Rectangle.Width), int(conf.H))
}

func (b *bar) drawReInit() {
	if b.i != nil {
		b.i.Destroy()
	}

	b.i = xgraphics.New(b.Xu, b.rect())
}

func (b *bar) drawText(x int, col uint32, text string) (int, error) {
	nx, _, err := b.i.Text(x, conf.FontYPad,
		int2BGRA(col), conf.FontSize, font, text)
	return nx - x, err
}

func (b *bar) textWidth(str string) int {
	return len(str) * b.tw
}

func pointInRect(px, py, x, y, w, h int) bool {
	if px >= x && px <= x + w && py >= x && py <= y + h {
		return true
	} else {
		return false
	}
}

func (b *bar) drawRect(x, y, w, h int, c uint32, fill bool) {
	col := int2BGRA(c)

	var ix, iy int

	for ix = x; ix < x + w; ix++ {
		for iy = y; iy < y + h; iy++ {
			if fill || ix == x || ix == x + w - 1 ||
					iy == y || iy == y + h - 1 {
				b.i.Set(ix, iy, col)
			}
		}
	}
}