TextBox cleanup + backspace repeat
- Switched textbox to helpers, - Added code to implement a repeat delay on backspace, default 300ms to start repeating, 60ms between repeats
This commit is contained in:
parent
97c0449bbd
commit
578b342040
1 changed files with 28 additions and 25 deletions
|
@ -3,22 +3,30 @@ package raygui
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gen2brain/raylib-go/raylib"
|
"github.com/gen2brain/raylib-go/raylib"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var backspaceHeld = false
|
||||||
|
var nextBackspace = time.Now()
|
||||||
|
|
||||||
|
// BackspaceRepeatDelay controls the time backspace must be held down before it will repeat.
|
||||||
|
var BackspaceRepeatDelay = 300 * time.Millisecond
|
||||||
|
// BackspaceRepeatInterval controls how frequently backspace registers after the initial delay.
|
||||||
|
var BackspaceRepeatInterval = 60 * time.Millisecond
|
||||||
|
|
||||||
// TextBox - Text Box element, updates input text
|
// TextBox - Text Box element, updates input text
|
||||||
func TextBox(bounds rl.Rectangle, text string) string {
|
func TextBox(bounds rl.Rectangle, text string) string {
|
||||||
b := bounds.ToInt32()
|
b := bounds.ToInt32()
|
||||||
state := Normal
|
|
||||||
|
|
||||||
mousePoint := rl.GetMousePosition()
|
|
||||||
letter := int32(-1)
|
letter := int32(-1)
|
||||||
|
|
||||||
// Update control
|
// Update control
|
||||||
if rl.CheckCollisionPointRec(mousePoint, bounds) {
|
state := GetInteractionState(bounds)
|
||||||
state = Focused // NOTE: PRESSED state is not used on this control
|
borderColor := TextboxBorderColor
|
||||||
|
if state == Pressed || state == Focused {
|
||||||
|
borderColor = ToggleActiveBorderColor
|
||||||
|
|
||||||
framesCounter2++
|
framesCounter2++
|
||||||
|
|
||||||
letter = rl.GetKeyPressed()
|
letter = rl.GetKeyPressed()
|
||||||
if letter != -1 {
|
if letter != -1 {
|
||||||
if letter >= 32 && letter < 127 {
|
if letter >= 32 && letter < 127 {
|
||||||
|
@ -26,33 +34,28 @@ func TextBox(bounds rl.Rectangle, text string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rl.IsKeyPressed(rl.KeyBackspace) {
|
backspacing := rl.IsKeyPressed(rl.KeyBackspace)
|
||||||
if len(text) > 0 {
|
if backspacing {
|
||||||
text = text[:len(text)-1]
|
nextBackspace = time.Now().Add(BackspaceRepeatDelay)
|
||||||
|
} else if rl.IsKeyDown(rl.KeyBackspace) {
|
||||||
|
backspacing = time.Since(nextBackspace) >= 0
|
||||||
|
if backspacing {
|
||||||
|
nextBackspace = time.Now().Add(BackspaceRepeatInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if backspacing && len(text) > 0 {
|
||||||
|
text = text[:len(text)-1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw control
|
DrawBorderedRectangle(b, GetStyle32(TextboxBorderWidth), GetStyleColor(borderColor), GetStyleColor(TextboxInsideColor))
|
||||||
switch state {
|
rl.DrawText(text, b.X+2, b.Y+int32(style[TextboxBorderWidth])+b.Height/2-int32(style[TextboxTextFontsize])/2, int32(style[TextboxTextFontsize]), GetStyleColor(TextboxTextColor))
|
||||||
case Normal:
|
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[TextboxBorderColor])))
|
|
||||||
rl.DrawRectangle(b.X+int32(style[TextboxBorderWidth]), b.Y+int32(style[TextboxBorderWidth]), b.Width-(int32(style[TextboxBorderWidth])*2), b.Height-(int32(style[TextboxBorderWidth])*2), rl.GetColor(int32(style[TextboxInsideColor])))
|
|
||||||
rl.DrawText(text, b.X+2, b.Y+int32(style[TextboxBorderWidth])+b.Height/2-int32(style[TextboxTextFontsize])/2, int32(style[TextboxTextFontsize]), rl.GetColor(int32(style[TextboxTextColor])))
|
|
||||||
break
|
|
||||||
case Focused:
|
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ToggleActiveBorderColor])))
|
|
||||||
rl.DrawRectangle(b.X+int32(style[TextboxBorderWidth]), b.Y+int32(style[TextboxBorderWidth]), b.Width-(int32(style[TextboxBorderWidth])*2), b.Height-(int32(style[TextboxBorderWidth])*2), rl.GetColor(int32(style[TextboxInsideColor])))
|
|
||||||
rl.DrawText(text, b.X+2, b.Y+int32(style[TextboxBorderWidth])+b.Height/2-int32(style[TextboxTextFontsize])/2, int32(style[TextboxTextFontsize]), rl.GetColor(int32(style[TextboxTextColor])))
|
|
||||||
|
|
||||||
if (framesCounter2/20)%2 == 0 && rl.CheckCollisionPointRec(mousePoint, bounds) {
|
if state == Focused || state == Pressed {
|
||||||
|
// Draw a cursor, when focused.
|
||||||
|
if (framesCounter2/20)%2 == 0 {
|
||||||
rl.DrawRectangle(b.X+4+rl.MeasureText(text, int32(style[GlobalTextFontsize])), b.Y+2, 1, b.Height-4, rl.GetColor(int32(style[TextboxLineColor])))
|
rl.DrawRectangle(b.X+4+rl.MeasureText(text, int32(style[GlobalTextFontsize])), b.Y+2, 1, b.Height-4, rl.GetColor(int32(style[TextboxLineColor])))
|
||||||
}
|
}
|
||||||
break
|
|
||||||
case Pressed:
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue