-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
133 lines (107 loc) · 2.7 KB
/
window.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
package windeau
import (
"github.com/nsf/termbox-go"
)
type SimpleBorder struct {
edge, verticalBorder, horizontalBorder rune
}
func (sb SimpleBorder) Edge(x, y int, window Window) rune {
return sb.edge
}
func (sb SimpleBorder) VerticalBorder() rune {
return sb.verticalBorder
}
func (sb SimpleBorder) HorizontalBorder() rune {
return sb.horizontalBorder
}
func MakeSimpleBorder(edge, vertical, horizontal rune) SimpleBorder {
return SimpleBorder{edge: edge, verticalBorder: vertical, horizontalBorder: horizontal}
}
type PositionHandler func(x, y int)
func WalkRegion(x, y, w, h int, handler PositionHandler) {
for px := x; px < x+w; px++ {
for py := y; py < y+h; py++ {
handler(px, py)
}
}
}
type Window struct {
X, Y int
Width, Height int
Border WindowBorder
Title string
Fg, Bg termbox.Attribute
View *Drawable
}
func MakeBasicWindow(x, y, w, h int) *Window {
border := MakeSimpleBorder('+', '|', '-')
return &Window{X: x, Y: y, Width: w, Height: h, Border: border}
}
func (w *Window) Draw() {
w.drawBorder()
w.drawTitle()
if w.View != nil {
(*w.View).Draw()
}
}
func (w *Window) SetParent(parent Drawable) {}
func (w *Window) GetColors() (termbox.Attribute, termbox.Attribute) {
return w.Fg, w.Bg
}
func (w *Window) IsRoot() bool {
return true
}
func (w *Window) IsFocused() bool {
return true
}
func (w *Window) SetFocused(f bool) {}
func (w *Window) GetRect() Rect {
return Rect{X: w.X, Y: w.Y, Width: w.Width, Height: w.Height}
}
func (w *Window) WithinBox(x, y int) bool {
return w.GetRect().WithinRect(x, y)
}
func (w *Window) drawBorder() {
WalkRegion(w.X, w.Y, w.Width, w.Height, func(x, y int) {
var element rune
if w.isEdge(x, y) {
element = w.Border.Edge(x, y, *w)
} else if w.isTopOrBottom(x, y) {
element = w.Border.HorizontalBorder()
} else if w.isLeftOrRight(x, y) {
element = w.Border.VerticalBorder()
} else {
element = ' '
}
termbox.SetCell(x, y, element, w.Fg, w.Bg)
})
}
func (w *Window) drawTitle() {
title := w.Title
if len(title) > 0 {
if len(title) > w.titleWidth() {
title = title[0:w.titleWidth()]
}
for c := 0; c < len(title); c++ {
termbox.SetCell(c+w.X+w.titleStart(), w.Y, rune(title[c]), w.Fg, w.Bg)
}
}
}
func (w *Window) titleWidth() int {
return w.Width - w.titleStart() - w.titlePadding()
}
func (w *Window) titleStart() int {
return w.titlePadding()
}
func (w *Window) titlePadding() int {
return 2
}
func (w *Window) isEdge(x, y int) bool {
return w.isTopOrBottom(x, y) && w.isLeftOrRight(x, y)
}
func (w *Window) isTopOrBottom(x, y int) bool {
return y == w.Y || y == w.Y+w.Height-1
}
func (w *Window) isLeftOrRight(x, y int) bool {
return x == w.X || x == w.X+w.Width-1
}