Add SetMask to messeji and Text widget

This commit is contained in:
Trevor Slocum 2024-02-10 12:38:13 -08:00
parent b1b41487c2
commit 7e5208a9f6
7 changed files with 55 additions and 15 deletions

View File

@ -113,8 +113,10 @@ func (b *Button) HandleKeyboard(ebiten.Key, rune) (handled bool, err error) {
func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
if !clicked {
if b.pressed && !pressed {
b.Lock()
b.pressed = false
b.SetBackground(Style.ButtonBgColor)
b.background = Style.ButtonBgColor
b.Unlock()
}
return true, nil
}

4
doc.go
View File

@ -54,7 +54,7 @@ in the order they are returned.
# Subpackages
There are two subpackages in etk: messeji and kibodo. These are available for
use without requiring etk. Usually you will not reference any subpackages, as
etk wraps them to provide widgets with additional features.
use without requiring etk. Usually you will not need to reference any
subpackages, as etk wraps them to provide widgets with additional features.
*/
package etk

8
go.mod
View File

@ -3,18 +3,18 @@ module code.rocket9labs.com/tslocum/etk
go 1.18
require (
github.com/hajimehoshi/ebiten/v2 v2.6.4
github.com/hajimehoshi/ebiten/v2 v2.6.5
github.com/llgcode/draw2d v0.0.0-20231212091825-f55e0c776b44
golang.org/x/image v0.15.0
)
require (
github.com/ebitengine/purego v0.5.2 // indirect
github.com/ebitengine/purego v0.6.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
golang.org/x/exp/shiny v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/exp/shiny v0.0.0-20240205201215-2c58cdc269a3 // indirect
golang.org/x/mobile v0.0.0-20240112133503-c713f31d574b // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

16
go.sum
View File

@ -1,24 +1,24 @@
github.com/ebitengine/purego v0.5.2 h1:r2MQEtkGzZ4LRtFZVAg5bjYKnUbxxloaeuGxH0t7qfs=
github.com/ebitengine/purego v0.5.2/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/ebitengine/purego v0.6.0 h1:Yo9uBc1x+ETQbfEaf6wcBsjrQfCEnh/gaGUg7lguEJY=
github.com/ebitengine/purego v0.6.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
github.com/hajimehoshi/ebiten/v2 v2.6.4 h1:G6tABZ4/njmi8Qn/l4Bqq49UrONrWW7TKcMMOSjPcpk=
github.com/hajimehoshi/ebiten/v2 v2.6.4/go.mod h1:TZtorL713an00UW4LyvMeKD8uXWnuIuCPtlH11b0pgI=
github.com/hajimehoshi/ebiten/v2 v2.6.5 h1:lALv+qhEK3CBWViyiGpz4YcR6slVJEjCiS7sExKZ9OE=
github.com/hajimehoshi/ebiten/v2 v2.6.5/go.mod h1:TZtorL713an00UW4LyvMeKD8uXWnuIuCPtlH11b0pgI=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/llgcode/draw2d v0.0.0-20231212091825-f55e0c776b44 h1:1ad70i0s40IpMtRm2ST+Nvr03X7mlHWtdALYkFNrlxk=
github.com/llgcode/draw2d v0.0.0-20231212091825-f55e0c776b44/go.mod h1:muweRyJCZ1mZSMiCgYbAicfnwZFoeHpNr6A6QBu+rBg=
github.com/llgcode/ps v0.0.0-20210114104736-f4b0c5d1e02e h1:ZAvbj5hI/G/EbAYAcj4yCXUNiFKefEhH0qfImDDD0/8=
golang.org/x/exp/shiny v0.0.0-20240119083558-1b970713d09a h1:NZ9mAQhIcCceDZKqQX3JJVIz7nn3QLDuC+nXedsViBM=
golang.org/x/exp/shiny v0.0.0-20240119083558-1b970713d09a/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o=
golang.org/x/exp/shiny v0.0.0-20240205201215-2c58cdc269a3 h1:tImqKNm/Iclm3Rqb6GffLiURSp3m1iRx/C4mturH8Ys=
golang.org/x/exp/shiny v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/mobile v0.0.0-20240112133503-c713f31d574b h1:kfWLZgb8iUBHdE9WydD5V5dHIS/F6HjlBZNyJfn2bs4=
golang.org/x/mobile v0.0.0-20240112133503-c713f31d574b/go.mod h1:4efzQnuA1nICq6h4kmZRMGzbPiP06lZvgADUu1VpJCE=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

View File

@ -217,6 +217,14 @@ func (i *Input) SetVertical(h Alignment) {
i.field.SetVertical(messeji.Alignment(h))
}
// SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
func (i *Input) SetMask(r rune) {
i.Lock()
defer i.Unlock()
i.field.SetMask(r)
}
// Write writes to the text buffer.
func (i *Input) Write(p []byte) (n int, err error) {
return i.field.Write(p)

View File

@ -169,6 +169,9 @@ type TextField struct {
// scrollDrag is whether the scroll bar is currently being dragged.
scrollDrag bool
// maskRune is the rune shown instead of the actual buffer contents.
maskRune rune
// img is the image of the field.
img *ebiten.Image
@ -565,6 +568,19 @@ func (f *TextField) SetHandleKeyboard(handle bool) {
f.handleKeyboard = handle
}
// SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
func (f *TextField) SetMask(r rune) {
f.Lock()
defer f.Unlock()
if f.maskRune == r {
return
}
f.maskRune = r
f.modified = true
}
// Write writes to the field's buffer.
func (f *TextField) Write(p []byte) (n int, err error) {
f.Lock()
@ -945,6 +961,12 @@ func (f *TextField) drawContent() (overflow bool) {
}
for i := firstVisible; i <= lastVisible; i++ {
line := f.bufferWrapped[i]
if f.maskRune != 0 {
line = strings.Repeat(string(f.maskRune), len(line))
if i == lastVisible && len(line) > 0 && len(line) >= len(f.suffix) {
line = line[:len(line)-len(f.suffix)] + f.suffix
}
}
lineX := f.padding
lineY := 1 + f.padding + f.lineOffset + lineHeight*i

View File

@ -197,6 +197,14 @@ func (t *Text) SetSingleLine(single bool) {
t.field.SetSingleLine(single)
}
// SetMask sets the rune used to mask the text buffer contents. Set to 0 to disable.
func (t *Text) SetMask(r rune) {
t.Lock()
defer t.Unlock()
t.field.SetMask(r)
}
// HandleKeyboard is called when a keyboard event occurs.
func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
return t.field.HandleKeyboardEvent(key, r)