2019-11-10 12:28:41 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-06-23 18:12:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-11-10 12:28:41 -05:00
|
|
|
)
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
// TextBox with cursor focus
|
|
|
|
var focusedTextBox *TextBox
|
|
|
|
|
2019-11-10 12:28:41 -05:00
|
|
|
// TextBox represents a text input box
|
|
|
|
type TextBox struct {
|
|
|
|
text string
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
visible bool
|
|
|
|
enabled bool
|
2020-02-01 18:55:56 -05:00
|
|
|
bgSprite *Sprite
|
2019-11-10 12:28:41 -05:00
|
|
|
textLabel Label
|
|
|
|
lineBar Label
|
2020-06-18 14:11:04 -04:00
|
|
|
filter string
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateTextbox() TextBox {
|
2020-02-01 18:55:56 -05:00
|
|
|
animation, _ := d2asset.LoadAnimation(d2resource.TextBox2, d2resource.PaletteUnits)
|
|
|
|
bgSprite, _ := LoadSprite(animation)
|
2020-06-23 18:12:08 -04:00
|
|
|
tb := TextBox{
|
2020-06-18 14:11:04 -04:00
|
|
|
filter: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
2019-12-21 20:53:18 -05:00
|
|
|
bgSprite: bgSprite,
|
|
|
|
textLabel: CreateLabel(d2resource.FontFormal11, d2resource.PaletteUnits),
|
|
|
|
lineBar: CreateLabel(d2resource.FontFormal11, d2resource.PaletteUnits),
|
2019-11-10 12:28:41 -05:00
|
|
|
enabled: true,
|
|
|
|
visible: true,
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
tb.lineBar.SetText("_")
|
|
|
|
|
|
|
|
return tb
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
func (v *TextBox) SetFilter(filter string) {
|
|
|
|
v.filter = filter
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (v *TextBox) Render(target d2render.Surface) {
|
2019-11-10 12:28:41 -05:00
|
|
|
if !v.visible {
|
|
|
|
return
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
v.bgSprite.Render(target)
|
|
|
|
v.textLabel.Render(target)
|
2019-11-10 12:28:41 -05:00
|
|
|
if (time.Now().UnixNano()/1e6)&(1<<8) > 0 {
|
2019-12-21 20:53:18 -05:00
|
|
|
v.lineBar.Render(target)
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:12:08 -04:00
|
|
|
func (v *TextBox) OnKeyChars(event d2input.KeyCharsEvent) bool {
|
|
|
|
if !(focusedTextBox == v) || !v.visible || !v.enabled {
|
|
|
|
return false
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
newText := string(event.Chars)
|
2019-11-10 12:28:41 -05:00
|
|
|
if len(newText) > 0 {
|
|
|
|
v.text += newText
|
|
|
|
v.SetText(v.text)
|
2020-06-23 18:12:08 -04:00
|
|
|
return true
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) OnKeyRepeat(event d2input.KeyEvent) bool {
|
|
|
|
if event.Key == d2input.KeyBackspace && debounceEvents(event.Duration) {
|
2019-11-10 12:28:41 -05:00
|
|
|
if len(v.text) >= 1 {
|
|
|
|
v.text = v.text[:len(v.text)-1]
|
|
|
|
}
|
|
|
|
v.SetText(v.text)
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func debounceEvents(numFrames int) bool {
|
|
|
|
const (
|
|
|
|
delay = 30
|
|
|
|
interval = 3
|
|
|
|
)
|
|
|
|
if numFrames == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if numFrames >= delay && (numFrames-delay)%interval == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) Advance(_ float64) {
|
|
|
|
if !v.visible || !v.enabled {
|
|
|
|
return
|
|
|
|
}
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:51:23 -04:00
|
|
|
func (v *TextBox) Update() {
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetText() string {
|
2019-11-10 12:28:41 -05:00
|
|
|
return v.text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) SetText(newText string) {
|
|
|
|
result := ""
|
|
|
|
for _, c := range newText {
|
2020-06-18 14:11:04 -04:00
|
|
|
if !strings.Contains(v.filter, string(c)) {
|
2019-11-10 12:28:41 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result += string(c)
|
|
|
|
}
|
|
|
|
if len(result) > 15 {
|
|
|
|
result = result[0:15]
|
|
|
|
}
|
|
|
|
v.text = result
|
|
|
|
for {
|
|
|
|
tw, _ := v.textLabel.GetTextMetrics(result)
|
|
|
|
if tw > 150 {
|
|
|
|
result = result[1:]
|
|
|
|
continue
|
|
|
|
}
|
2020-02-01 21:51:49 -05:00
|
|
|
v.lineBar.SetPosition(v.x+6+tw, v.y+3)
|
2019-11-10 12:28:41 -05:00
|
|
|
v.textLabel.SetText(result)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetSize() (width, height int) {
|
2019-12-21 20:53:18 -05:00
|
|
|
return v.bgSprite.GetCurrentFrameSize()
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *TextBox) SetPosition(x, y int) {
|
2019-11-10 12:28:41 -05:00
|
|
|
v.x = x
|
|
|
|
v.y = y
|
2019-12-21 20:53:18 -05:00
|
|
|
v.textLabel.SetPosition(v.x+6, v.y+3)
|
2020-02-01 21:51:49 -05:00
|
|
|
v.lineBar.SetPosition(v.x+6+v.textLabel.Width, v.y+3)
|
2019-12-21 20:53:18 -05:00
|
|
|
v.bgSprite.SetPosition(v.x, v.y+26)
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetPosition() (x, y int) {
|
2019-11-10 12:28:41 -05:00
|
|
|
return v.x, v.y
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetVisible() bool {
|
2019-11-10 12:28:41 -05:00
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetEnabled() bool {
|
2019-11-10 12:28:41 -05:00
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) SetPressed(pressed bool) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) GetPressed() bool {
|
2019-11-10 12:28:41 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TextBox) OnActivated(callback func()) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *TextBox) Activate() {
|
2020-06-23 18:12:08 -04:00
|
|
|
focusedTextBox = v
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|