-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelDisplayPane.go
106 lines (90 loc) · 2.49 KB
/
LabelDisplayPane.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
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 LabelDisplayPane struct {
thePane swtk.Pane
im draw.Image
fg image.Image
renderer swtk.Renderer
drawChannel chan int
sizeChannel chan swtk.ResizeEvent
closeChannel chan int
stateChannel chan int
ftContext *freetype.Context
label string
size image.Point
view image.Rectangle
ftim draw.Image
}
func NewLabelDisplayPane(l string, c color.Color) *LabelDisplayPane {
pn := new(LabelDisplayPane)
pn.fg = image.NewUniform(c)
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.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))
pn.label = l
return pn
}
func (pn *LabelDisplayPane) SetPane(p swtk.Pane) {
pn.thePane = p
}
func (dp *LabelDisplayPane) SetSize(size swtk.ResizeEvent) {
dp.sizeChannel <- size
}
func (dp *LabelDisplayPane) Close() {
dp.closeChannel <- 1
}
func (dp *LabelDisplayPane) setSize(s swtk.ResizeEvent) {
//set the correct size in the buffer
dp.size = s.Size
dp.view = s.View
if s.Size.X == 0 || s.Size.Y == 0 {
dp.im = nil
}
dp.im = image.NewRGBA(image.Rect(0, 0, s.View.Dx(), s.View.Dy()))
}
func (dp *LabelDisplayPane) DrawingHandler() {
for {
select {
case _ = <-dp.drawChannel:
dp.draw()
case re := <-dp.sizeChannel:
dp.setSize(re)
dp.draw()
case _ = <-dp.closeChannel:
break
}
}
}
func (pn *LabelDisplayPane) Draw() {
pn.drawChannel <- 1
}
func (dp *LabelDisplayPane) SetRenderer(r swtk.Renderer) {
dp.renderer = r
}
func (pn *LabelDisplayPane) draw() {
if pn.im != nil {
r := pn.im.Bounds()
draw.Draw(pn.im, r, image.Transparent, 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)
}
pn.renderer.SetAspect(swtk.PaneImage{pn.thePane, pn.im})
}