From a3d87f7dad8feb5e607a64cf67dfd40c8b3528b3 Mon Sep 17 00:00:00 2001 From: lleyton Date: Thu, 2 Mar 2023 13:05:33 -0800 Subject: [PATCH] Fix caret issues (#3) --- design/textbox.go | 1 - examples/main.go | 5 +++++ framework/uiLibTextInput.go | 35 +++++++++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/design/textbox.go b/design/textbox.go index b1a5649..cc9684a 100644 --- a/design/textbox.go +++ b/design/textbox.go @@ -34,7 +34,6 @@ func mkTxtBox(hint string, str *string, text []string, newline bool) framework.U if confirm && newline { // handle multiline textboxes fwTextbox.FyTInput("\n") - fwTextbox.AdditionalCaretPosY += 15 } // The reason why we wait for stall is because this reduces the lag. diff --git a/examples/main.go b/examples/main.go index 7f5f429..19d2540 100644 --- a/examples/main.go +++ b/examples/main.go @@ -34,6 +34,8 @@ func main() { TeleportSettings: framework.SlideTransition{}, }) + thought := "" + // Start an instant transition to our main screen. app.Teleport( // A 'document', with a title header and body. @@ -45,6 +47,9 @@ func main() { framework.NewUIFlexboxContainerPtr(framework.FlexboxContainer{ DirVertical: true, Slots: []framework.FlexboxSlot{ + { + Element: design.NewUITextareaPtr("Think of something...", &thought), + }, { Element: framework.NewUILabelPtr(integration.NewTextTypeChunk("Hello World!", design.GlobalFont), design.ThemeText, 0, frenyard.Alignment2i{}), }, diff --git a/framework/uiLibTextInput.go b/framework/uiLibTextInput.go index 3adcd45..52563c9 100644 --- a/framework/uiLibTextInput.go +++ b/framework/uiLibTextInput.go @@ -2,9 +2,10 @@ package framework import ( "fmt" - "golang.org/x/image/math/fixed" "strings" + "golang.org/x/image/math/fixed" + "github.com/uwu/frenyard" "github.com/uwu/frenyard/integration" "github.com/veandco/go-sdl2/sdl" @@ -22,8 +23,6 @@ type UITextbox struct { // Called on enter OnConfirm func() - AdditionalCaretPosY int32 - _open bool _caretBlinker float64 _stallTimer float64 @@ -106,10 +105,29 @@ func (tb *UITextbox) rebuild() { // see comment in NewUITextboxPtr - empty textboxes misbehave safeTextPre := tb._textPre + safeTextPost := tb._textPost if safeTextPre == "" && tb._textPost == "" { safeTextPre = " " } + // this is messy, but it works and I'd rather not spend time on it + // feel free to simplify it if you can + if len(safeTextPre) == 0 && len(safeTextPost) > 0 && safeTextPost[0] == '\n' { + safeTextPre = " " + } + + if len(safeTextPre) != 0 && safeTextPre[0] == '\n' { + safeTextPre = " \n" + safeTextPre[1:] + } + + if (len(safeTextPre) != 0 && safeTextPre[len(safeTextPre)-1] == '\n') && (len(safeTextPost) == 0 || safeTextPost[0] == '\n') { + safeTextPost = " " + safeTextPost + } + + if len(safeTextPost) != 0 && safeTextPost[len(safeTextPost)-1] == '\n' { + safeTextPost = safeTextPost + " " + } + if !tb._open { if tb._textPre == "" && tb._textPost == "" { tb._label.SetText(integration.NewCompoundTypeChunk([]integration.TypeChunk{ @@ -117,13 +135,13 @@ func (tb *UITextbox) rebuild() { })) } else { tb._label.SetText(integration.NewCompoundTypeChunk([]integration.TypeChunk{ - integration.NewColouredTextTypeChunk(safeTextPre+tb._textPost, tb._face, tb._primaryColour), + integration.NewColouredTextTypeChunk(safeTextPre+safeTextPost, tb._face, tb._primaryColour), })) } } else if !tb._suggesting { tb._label.SetText(integration.NewCompoundTypeChunk([]integration.TypeChunk{ integration.NewColouredTextTypeChunk(safeTextPre, tb._face, tb._primaryColour), - integration.NewColouredTextTypeChunk(tb._textPost, tb._face, tb._primaryColour), + integration.NewColouredTextTypeChunk(safeTextPost, tb._face, tb._primaryColour), })) } else { tb._label.SetText(integration.NewCompoundTypeChunk([]integration.TypeChunk{ @@ -131,7 +149,7 @@ func (tb *UITextbox) rebuild() { integration.NewColouredTextTypeChunk(tb._textSuggestion1, tb._face, tb._suggestionColour), integration.NewUnderlineTypeChunk(integration.NewColouredTextTypeChunk(tb._textSuggestion2, tb._face, tb._suggestionColour), tb._suggestionColour), integration.NewColouredTextTypeChunk(tb._textSuggestion3, tb._face, tb._suggestionColour), - integration.NewColouredTextTypeChunk(tb._textPost, tb._face, tb._primaryColour), + integration.NewColouredTextTypeChunk(safeTextPost, tb._face, tb._primaryColour), })) } if tb.OnRebuild != nil { @@ -146,7 +164,8 @@ func (tb *UITextbox) FyEDraw(target frenyard.Renderer, under bool) { // TODO: For the love of god improve this currentLineSlice := strings.Split(tb._textPre, "\n") - currentLine := currentLineSlice[len(currentLineSlice)-1] + currentLineNum := len(currentLineSlice) - 1 + currentLine := currentLineSlice[currentLineNum] // all between here and the end of the func is caret drawing code preChunk := integration.NewColouredTextTypeChunk(currentLine, tb._face, tb._primaryColour) @@ -154,7 +173,7 @@ func (tb *UITextbox) FyEDraw(target frenyard.Renderer, under bool) { caretPos := frenyard.Vec2i{ X: int32(preDot.X.Ceil()), - Y: int32(preDot.Y.Ceil()) + 2 + tb.AdditionalCaretPosY, // lil visual offset + Y: int32(preDot.Y.Ceil()+preChunk.FyCHeight()*currentLineNum) + 2, // lil visual offset } caretSize := frenyard.Vec2i{ X: 1,