diff options
Diffstat (limited to 'bar/x.go')
| -rw-r--r-- | bar/x.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/bar/x.go b/bar/x.go new file mode 100644 index 0000000..4d77bfd --- /dev/null +++ b/bar/x.go @@ -0,0 +1,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) + } + } + } +} |