Add Input.SetAutoResize

This commit is contained in:
Trevor Slocum 2024-11-18 22:00:13 -08:00
parent 9a6cafd7b8
commit 600f8d0720
3 changed files with 24 additions and 3 deletions

View file

@ -194,6 +194,15 @@ func (t *Input) SetFont(fnt *text.GoTextFaceSource, size int) {
t.field.SetFont(fnt, size, fontMutex)
}
// SetAutoResize sets whether the font is automatically scaled down when it is
// too large to fit the entire text buffer on one line.
func (t *Input) SetAutoResize(resize bool) {
t.Lock()
defer t.Unlock()
t.field.SetAutoResize(resize)
}
// Padding returns the amount of padding around the text within the field.
func (i *Input) Padding() int {
i.Lock()

View file

@ -85,12 +85,18 @@ func (f *InputField) HandleKeyboardEvent(key ebiten.Key, r rune) (handled bool,
// Handle rune event.
if r > 0 {
f.handleRunes([]rune{r})
ok := f.handleRunes([]rune{r})
if ok {
f.resizeFont()
}
return true, nil
}
// Handle key event.
f.handleKeys([]ebiten.Key{key})
ok := f.handleKeys([]ebiten.Key{key})
if ok {
f.resizeFont()
}
return true, nil
}
@ -207,6 +213,7 @@ func (f *InputField) Update() error {
f.rawKeyBuffer = f.rawKeyBuffer[:0]
if redraw {
f.resizeFont()
f.bufferModified()
}

View file

@ -267,6 +267,7 @@ func (f *TextField) SetRect(r image.Rectangle) {
func (f *TextField) text() string {
f.processIncoming()
f.resizeFont()
return string(bytes.Join(f.buffer, []byte("\n")))
}
@ -554,6 +555,7 @@ func (f *TextField) SetScrollBorderSize(size int) {
f.scrollBorderSize = size
f.redraw = true
f.resizeFont()
}
// SetScrollBorderColor sets the color of the top, right, bottom and left border
@ -582,6 +584,7 @@ func (f *TextField) SetScrollBarVisible(scrollVisible bool) {
f.needWrap = 0
f.wrapStart = 0
f.modified = true
f.resizeFont()
}
// SetAutoHideScrollBar sets whether the scroll bar is automatically hidden
@ -598,6 +601,7 @@ func (f *TextField) SetAutoHideScrollBar(autoHide bool) {
f.needWrap = 0
f.wrapStart = 0
f.modified = true
f.resizeFont()
}
// WordWrap returns the current text wrap mode.
@ -777,7 +781,7 @@ func (f *TextField) _handleMouseEvent(cursor image.Point, pressed bool, clicked
f.scrollDragOffset = f.offset
}
if f.scrollDragPoint.X != -1 {
delta := f.scrollDragPoint.Y - cursor.Y
delta := f.scrollDragPoint.Y - p.Y
f.offset = f.scrollDragOffset - delta
} else { // Handle dragging the scroll bar handle.
dragY := cursor.Y - f.r.Min.Y - f.scrollWidth/4
@ -1256,6 +1260,7 @@ func (f *TextField) processIncoming() {
func (f *TextField) bufferModified() {
f.processIncoming()
f.resizeFont()
f.drawImage()