Skip to content

Commit

Permalink
Fix caret issues (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Mar 2, 2023
1 parent 5143bc8 commit a3d87f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
1 change: 0 additions & 1 deletion design/textbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{}),
},
Expand Down
35 changes: 27 additions & 8 deletions framework/uiLibTextInput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,8 +23,6 @@ type UITextbox struct {
// Called on enter
OnConfirm func()

AdditionalCaretPosY int32

_open bool
_caretBlinker float64
_stallTimer float64
Expand Down Expand Up @@ -106,32 +105,51 @@ 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{
integration.NewColouredTextTypeChunk(tb._hint, tb._face, tb._hintColour),
}))
} 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{
integration.NewColouredTextTypeChunk(safeTextPre, tb._face, tb._primaryColour),
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 {
Expand All @@ -146,15 +164,16 @@ 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)
preDot, _ := preChunk.FyCBounds(fixed.Point26_6{})

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,
Expand Down

0 comments on commit a3d87f7

Please sign in to comment.