From e2e846a0e51d3b8c69c54b5fbf7c42a456718791 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 16 Feb 2023 14:39:51 -0500 Subject: [PATCH] AHHHHH!!! --- examples/list/main.go | 2 +- textdraw/setter.go | 17 ++++++++++++++--- textdraw/setter_test.go | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/examples/list/main.go b/examples/list/main.go index 68ab48d..151db82 100644 --- a/examples/list/main.go +++ b/examples/list/main.go @@ -38,7 +38,7 @@ func run () { popups.NewDialog(popups.DialogKindInfo, "", "Sike!") }) mouse := testing.NewMouse() - input := basicElements.NewTextBox("Write some text", "fkjasdklfja\ndjkfhadf") + input := basicElements.NewTextBox("Write some text", "") form := basicElements.NewContainer(basicLayouts.Vertical { true, false}) form.Adopt(basicElements.NewLabel("I have:", false), false) form.Adopt(basicElements.NewSpacer(true), false) diff --git a/textdraw/setter.go b/textdraw/setter.go index 55437ba..8867161 100644 --- a/textdraw/setter.go +++ b/textdraw/setter.go @@ -65,6 +65,7 @@ func (setter *TypeSetter) needLayout () { // add a null onto the end because the very end of the text should have // a valid layout position + // lastLine = lastLine lastWord := &lastLine.Words[len(lastLine.Words) - 1] lastWord.Runes = append (lastWord.Runes, RuneLayout { X: lastWord.Width + lastWord.SpaceAfter, @@ -203,8 +204,11 @@ func (setter *TypeSetter) For (iterator RuneIterator) { index := 0 for _, line := range setter.lines { + lastCharRightBound := fixed.Int26_6(0) + for _, word := range line.Words { for _, char := range word.Runes { + lastCharRightBound = word.X + char.X + char.Width keepGoing := iterator (index, char.Rune, fixed.Point26_6 { X: word.X + char.X, Y: line.Y, @@ -212,13 +216,20 @@ func (setter *TypeSetter) For (iterator RuneIterator) { if !keepGoing { return } index ++ }} - if line.BreakAfter { index ++ } + + if line.BreakAfter { + keepGoing := iterator (index, '\n', fixed.Point26_6 { + X: lastCharRightBound, + Y: line.Y, + }) + if !keepGoing { return } + index ++ + } } } // AtPosition returns the index of the rune at the specified position. func (setter *TypeSetter) AtPosition (position fixed.Point26_6) (index int) { - println("XXX", position.Y.Round()) setter.needAlignedLayout() if setter.lines == nil { return } @@ -250,12 +261,12 @@ func (setter *TypeSetter) AtPosition (position fixed.Point26_6) (index int) { for _, curWord := range line.Words { for _, curChar := range curWord.Runes { x := curWord.X + curChar.X + curChar.Width - println(index, x.Round(), position.X.Round()) if x > position.X { goto foundRune } index ++ } } foundRune: + println(index) return } diff --git a/textdraw/setter_test.go b/textdraw/setter_test.go index 555440d..94b1f8c 100644 --- a/textdraw/setter_test.go +++ b/textdraw/setter_test.go @@ -110,6 +110,24 @@ func TestSetterIndex (test *testing.T) { pos = fixed.P(-59, 230) index = setter.AtPosition(pos) + expect = 20 + if index != expect { + test.Fatalf ( + `setter index at (%d, %d): %d, expected: %d`, + pos.X.Round(), pos.Y.Round(), index, expect) + } + + pos = fixed.P(-500, -500) + index = setter.AtPosition(pos) + expect = 0 + if index != expect { + test.Fatalf ( + `setter index at (%d, %d): %d, expected: %d`, + pos.X.Round(), pos.Y.Round(), index, expect) + } + + pos = fixed.P(500, -500) + index = setter.AtPosition(pos) expect = 19 if index != expect { test.Fatalf ( @@ -119,7 +137,7 @@ func TestSetterIndex (test *testing.T) { pos = fixed.P(500, 500) index = setter.AtPosition(pos) - expect = 45 + expect = setter.Length() if index != expect { test.Fatalf ( `setter index at (%d, %d): %d, expected: %d`,