1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-02 17:27:23 -04:00
OpenDiablo2/d2core/d2ui/textbox.go

229 lines
4.4 KiB
Go
Raw Normal View History

package d2ui
import (
"strings"
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
// TextBox represents a text input box
type TextBox struct {
textLabel Label
lineBar Label
text string
filter string
x int
y int
bgSprite *Sprite
visible bool
enabled bool
isFocused bool
}
2020-07-26 14:52:54 -04:00
// CreateTextbox creates a new instance of a text box
func CreateTextbox() TextBox {
animation, _ := d2asset.LoadAnimation(d2resource.TextBox2, d2resource.PaletteUnits)
bgSprite, _ := LoadSprite(animation)
tb := TextBox{
2020-06-18 14:11:04 -04:00
filter: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
bgSprite: bgSprite,
textLabel: CreateLabel(d2resource.FontFormal11, d2resource.PaletteUnits),
lineBar: CreateLabel(d2resource.FontFormal11, d2resource.PaletteUnits),
enabled: true,
visible: true,
}
tb.lineBar.SetText("_")
return tb
}
2020-07-26 14:52:54 -04:00
// SetFilter sets the text box filter
2020-06-18 14:11:04 -04:00
func (v *TextBox) SetFilter(filter string) {
v.filter = filter
}
2020-07-26 14:52:54 -04:00
// Render renders the text box
func (v *TextBox) Render(target d2interface.Surface) error {
if !v.visible {
2020-07-26 14:52:54 -04:00
return nil
}
2020-07-26 14:52:54 -04:00
if err := v.bgSprite.Render(target); err != nil {
return err
}
v.textLabel.Render(target)
2020-07-26 14:52:54 -04:00
if (time.Now().UnixNano()/1e6)&(1<<8) > 0 {
v.lineBar.Render(target)
}
2020-07-26 14:52:54 -04:00
return nil
}
2020-07-26 14:52:54 -04:00
// OnKeyChars handles key character events
func (v *TextBox) OnKeyChars(event d2interface.KeyCharsEvent) bool {
if !v.isFocused || !v.visible || !v.enabled {
return false
}
2020-07-26 14:52:54 -04:00
newText := string(event.Chars())
2020-07-26 14:52:54 -04:00
if len(newText) > 0 {
v.text += newText
2020-07-26 14:52:54 -04:00
v.SetText(v.text)
2020-07-26 14:52:54 -04:00
return true
}
2020-07-26 14:52:54 -04:00
return false
}
2020-07-26 14:52:54 -04:00
// OnKeyRepeat handles key repeat events
func (v *TextBox) OnKeyRepeat(event d2interface.KeyEvent) bool {
if event.Key() == d2enum.KeyBackspace && debounceEvents(event.Duration()) {
if len(v.text) >= 1 {
v.text = v.text[:len(v.text)-1]
}
2020-07-26 14:52:54 -04:00
v.SetText(v.text)
}
2020-07-26 14:52:54 -04:00
return false
}
func debounceEvents(numFrames int) bool {
const (
delay = 30
interval = 3
)
2020-07-26 14:52:54 -04:00
if numFrames == 1 {
return true
}
2020-07-26 14:52:54 -04:00
if numFrames >= delay && (numFrames-delay)%interval == 0 {
return true
}
2020-07-26 14:52:54 -04:00
return false
}
2020-07-26 14:52:54 -04:00
// Advance updates the text box
func (v *TextBox) Advance(_ float64) {
if !v.visible || !v.enabled {
return
}
}
2020-07-26 14:52:54 -04:00
// Update updates the textbox
func (v *TextBox) Update() {
}
2020-07-26 14:52:54 -04:00
// GetText returns the text box's text
func (v *TextBox) GetText() string {
return v.text
}
2020-07-26 14:52:54 -04:00
// SetText sets the text box's text
//nolint:gomnd // Built-in values
func (v *TextBox) SetText(newText string) {
result := ""
2020-07-26 14:52:54 -04:00
for _, c := range newText {
2020-06-18 14:11:04 -04:00
if !strings.Contains(v.filter, string(c)) {
continue
}
2020-07-26 14:52:54 -04:00
result += string(c)
}
2020-07-26 14:52:54 -04:00
if len(result) > 15 {
result = result[0:15]
}
2020-07-26 14:52:54 -04:00
v.text = result
2020-07-26 14:52:54 -04:00
for {
tw, _ := v.textLabel.GetTextMetrics(result)
2020-07-26 14:52:54 -04:00
if tw > 150 {
result = result[1:]
continue
}
2020-07-26 14:52:54 -04:00
v.lineBar.SetPosition(v.x+6+tw, v.y+3)
v.textLabel.SetText(result)
2020-07-26 14:52:54 -04:00
break
}
}
2020-07-26 14:52:54 -04:00
// GetSize returns the size of the text box
func (v *TextBox) GetSize() (width, height int) {
return v.bgSprite.GetCurrentFrameSize()
}
2020-07-26 14:52:54 -04:00
// SetPosition sets the position of the text box
//nolint:gomnd // Built-in values
func (v *TextBox) SetPosition(x, y int) {
lw, _ := v.textLabel.GetSize()
2020-07-26 14:52:54 -04:00
v.x = x
v.y = y
2020-07-26 14:52:54 -04:00
v.textLabel.SetPosition(v.x+6, v.y+3)
v.lineBar.SetPosition(v.x+6+lw, v.y+3)
v.bgSprite.SetPosition(v.x, v.y+26)
}
2020-07-26 14:52:54 -04:00
// GetPosition returns the position of the text box
func (v *TextBox) GetPosition() (x, y int) {
return v.x, v.y
}
2020-07-26 14:52:54 -04:00
// GetVisible returns the visibility of the text box
func (v *TextBox) GetVisible() bool {
return v.visible
}
2020-07-26 14:52:54 -04:00
// SetVisible sets the visibility of the text box
func (v *TextBox) SetVisible(visible bool) {
v.visible = visible
}
2020-07-26 14:52:54 -04:00
// GetEnabled returns the enabled state of the text box
func (v *TextBox) GetEnabled() bool {
return v.enabled
}
2020-07-26 14:52:54 -04:00
// SetEnabled sets the enabled state of the text box
func (v *TextBox) SetEnabled(enabled bool) {
v.enabled = enabled
}
2020-07-26 14:52:54 -04:00
// SetPressed does nothing for text boxes
func (v *TextBox) SetPressed(_ bool) {
// no op
}
2020-07-26 14:52:54 -04:00
// GetPressed does nothing for text boxes
func (v *TextBox) GetPressed() bool {
return false
}
2020-07-26 14:52:54 -04:00
// OnActivated handles activation events for the text box
func (v *TextBox) OnActivated(_ func()) {
// no op
}
2020-07-26 14:52:54 -04:00
// Activate activates the text box
func (v *TextBox) Activate() {
v.isFocused = true
}