-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
92 lines (76 loc) · 1.8 KB
/
ui.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
package ui
import (
"image"
termbox "github.com/nsf/termbox-go"
)
/* This code is slowly lapsing into an entire UI rendering framework
that is not my objective, so I'm putting on the brakes and getting back
to the features of the drawing program, for now... */
type UIComponent interface {
Render()
SetPosition(x, y int)
GetPosition() (int, int)
SetSize(width, height int)
Width() int
InBounds(x, y int) bool
Handle(event termbox.Event)
}
func RenderBar(r rune, y int, fg, bg termbox.Attribute) {
width, _ := termbox.Size()
for x := 0; x < width; x++ {
termbox.SetCell(x, y, r, fg, bg)
}
}
func RenderText(text string, x, y int, fg, bg termbox.Attribute) {
for i, rune := range text {
termbox.SetCell(x+i, y, rune, fg, bg)
}
}
func RenderRune(r rune, x, y int, fg, bg termbox.Attribute) {
// probably a pointless wrapper function..
termbox.SetCell(x, y, r, fg, bg)
}
func RenderRect(rect image.Rectangle, colour termbox.Attribute) {
min := rect.Min
max := rect.Max
for x := min.X; x < max.X; x++ {
for y := min.Y; y < max.Y; y++ {
termbox.SetCell(x, y, rune(' '), colour, colour)
}
}
}
type Component struct {
X, Y int
width, height int
//parent *Component
children []Component
}
func (c *Component) SetPosition(x, y int) {
c.X = x
c.Y = y
}
func (c *Component) GetPosition() (int, int) {
return c.X, c.Y
}
func (c *Component) SetSize(width, height int) {
c.width = width
c.height = height
}
func (c *Component) Width() int {
return c.width
}
func (c *Component) Height() int {
return c.height
}
func (c *Component) InBounds(x, y int) bool {
leftX := c.X
rightX := c.X + c.width
topY := c.Y
bottomY := c.Y + c.height
if x >= leftX && x < rightX && y >= topY && y < bottomY {
return true
}
return false
}
func (c *Component) Handle(event termbox.Event) {
}