-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonDisplayPane.go
139 lines (122 loc) · 3.75 KB
/
ButtonDisplayPane.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
134
135
136
137
138
139
package displays
import "image"
import "image/draw"
import "image/color"
import "code.google.com/p/freetype-go/freetype"
import "github.com/ctlod/go.swtk"
type ButtonDisplayPane struct {
thePane swtk.Pane
im draw.Image
ftim draw.Image
renderer swtk.Renderer
fg image.Image
bg image.Image
mask image.Image
highlightWidth int
shadowWidth int
drawChannel chan int
sizeChannel chan swtk.ResizeEvent
closeChannel chan int
stateChannel chan int
state int
ftContext *freetype.Context
label string
size image.Point
view image.Rectangle
}
func NewButtonDisplayPane(bgc, fgc color.Color, label string) *ButtonDisplayPane {
pn := new(ButtonDisplayPane)
pn.bg = image.NewUniform(bgc)
pn.fg = image.NewUniform(fgc)
pn.mask = image.NewUniform(color.Alpha{128})
pn.highlightWidth = 2
pn.sizeChannel = make(chan swtk.ResizeEvent, 1)
pn.drawChannel = make(chan int, 1)
pn.closeChannel = make(chan int, 1)
pn.stateChannel = make(chan int, 1)
pn.state = 0
pn.label = label
pn.ftContext = freetype.NewContext()
pn.ftContext.SetDPI(swtk.Dpi)
pn.ftContext.SetFont(swtk.Font)
pn.ftContext.SetFontSize(swtk.FontSize)
pn.ftContext.SetSrc(pn.fg)
pn.ftim = image.NewRGBA(image.Rect(0,0,0,0))
return pn
}
func (pn *ButtonDisplayPane) SetPane(p swtk.Pane) {
pn.thePane = p
}
func (dp *ButtonDisplayPane) SetState(s int) {
dp.stateChannel <- s
}
func (dp *ButtonDisplayPane) SetSize(size swtk.ResizeEvent) {
dp.sizeChannel <- size
}
func (dp *ButtonDisplayPane) Close() {
dp.closeChannel <- 1
}
func (dp *ButtonDisplayPane) setSize(s swtk.ResizeEvent) {
//set the correct size in the buffer
dp.size = s.Size
dp.view = s.View
if s.View.Dx() == 0 || s.View.Dy() == 0 {
dp.im = nil
}
dp.im = image.NewRGBA(image.Rect(0, 0, s.View.Dx(), s.View.Dy()))
}
func (dp *ButtonDisplayPane) DrawingHandler() {
for {
select {
case _ = <-dp.drawChannel:
dp.draw()
case re := <-dp.sizeChannel:
dp.setSize(re)
dp.draw()
case st := <-dp.stateChannel:
dp.state = st
dp.draw()
case _ = <-dp.closeChannel:
break
}
}
}
func (pn *ButtonDisplayPane) Draw() {
pn.drawChannel <- 1
}
func (dp *ButtonDisplayPane) SetRenderer(r swtk.Renderer) {
dp.renderer = r
}
func (pn *ButtonDisplayPane) draw() {
if pn.im != nil {
r := pn.im.Bounds()
draw.Draw(pn.im, r, pn.bg, image.ZP, draw.Src)
pn.ftContext.SetClip(image.Rect(0,0,pn.size.X,pn.size.Y))
pn.ftContext.SetDst(pn.ftim)
textHeight := int(pn.ftContext.PointToFix32(swtk.FontSize)>>8)
pt := freetype.Pt(0, textHeight)
pt, _ = pn.ftContext.DrawString(pn.label, pt)
labelLength := int(pt.X >> 8)
pt = freetype.Pt((pn.size.X - labelLength) / 2 - pn.view.Min.X, (pn.size.Y + textHeight) / 2 - 1 - pn.view.Min.Y)
pn.ftContext.SetDst(pn.im)
pn.ftContext.DrawString(pn.label, pt)
if pn.state > 0 {
r0 := image.Rect(0, 0, pn.size.X, pn.highlightWidth).Intersect(pn.view).Sub(pn.view.Min)
draw.Draw(pn.im, r0, image.Black, image.ZP, draw.Src)
//left
r0 = image.Rect(0, pn.highlightWidth, pn.highlightWidth, pn.size.Y - pn.highlightWidth).Intersect(pn.view).Sub(pn.view.Min)
draw.Draw(pn.im, r0, image.Black, image.ZP, draw.Src)
//bottom
r0 = image.Rect(0, pn.size.Y - pn.highlightWidth, pn.size.X, pn.size.Y).Intersect(pn.view).Sub(pn.view.Min)
draw.Draw(pn.im, r0, image.Black, image.ZP, draw.Src)
//right
r0 = image.Rect(pn.size.X - pn.highlightWidth, pn.highlightWidth, pn.size.X, pn.size.Y - pn.highlightWidth).Intersect(pn.view).Sub(pn.view.Min)
draw.Draw(pn.im, r0, image.Black, image.ZP, draw.Src)
}
if pn.state == 2 {
//shade if pressed
draw.DrawMask(pn.im, r, image.Black, image.ZP, pn.mask, image.ZP, draw.Over)
}
}
pn.renderer.SetAspect(swtk.PaneImage{pn.thePane, pn.im})
}