this just j
This commit is contained in:
parent
7235c86e22
commit
56dc9ba54c
@ -69,12 +69,7 @@ func (element *TextBox) HandleMouseDown (x, y int, button input.Button) {
|
||||
if !element.Focused() { element.Focus() }
|
||||
|
||||
if button == input.ButtonLeft {
|
||||
point := image.Pt(x, y)
|
||||
offset := element.Bounds().Min.Add (image.Pt (
|
||||
element.config.Padding() - element.scroll,
|
||||
element.config.Padding()))
|
||||
runeIndex := element.valueDrawer.AtPosition (
|
||||
fixedutil.Pt(point.Sub(offset)))
|
||||
runeIndex := element.atPosition(image.Pt(x, y))
|
||||
element.dragging = true
|
||||
if runeIndex > -1 {
|
||||
element.dot = textmanip.EmptyDot(runeIndex)
|
||||
@ -88,12 +83,7 @@ func (element *TextBox) HandleMouseMove (x, y int) {
|
||||
if !element.Focused() { element.Focus() }
|
||||
|
||||
if element.dragging {
|
||||
point := image.Pt(x, y)
|
||||
offset := element.Bounds().Min.Add (image.Pt (
|
||||
element.config.Padding() - element.scroll,
|
||||
element.config.Padding()))
|
||||
runeIndex := element.valueDrawer.AtPosition (
|
||||
fixedutil.Pt(point.Sub(offset)))
|
||||
runeIndex := element.atPosition(image.Pt(x, y))
|
||||
if runeIndex > -1 {
|
||||
element.dot.End = runeIndex
|
||||
element.redo()
|
||||
@ -101,6 +91,15 @@ func (element *TextBox) HandleMouseMove (x, y int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (element *TextBox) atPosition (position image.Point) int {
|
||||
offset := element.Bounds().Min.Add (image.Pt (
|
||||
element.config.Padding() - element.scroll,
|
||||
element.config.Padding()))
|
||||
textBoundsMin := element.valueDrawer.LayoutBounds().Min
|
||||
return element.valueDrawer.AtPosition (
|
||||
fixedutil.Pt(position.Sub(offset).Add(textBoundsMin)))
|
||||
}
|
||||
|
||||
func (element *TextBox) HandleMouseUp (x, y int, button input.Button) {
|
||||
if button == input.ButtonLeft {
|
||||
element.dragging = false
|
||||
|
@ -6,6 +6,7 @@ import "image"
|
||||
import "image/color"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/defaultfont"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||
|
||||
@ -159,7 +160,7 @@ func (element *Artist) draw () {
|
||||
|
||||
// how long did that take to render?
|
||||
drawTime := time.Since(drawStart)
|
||||
textDrawer := artist.TextDrawer { }
|
||||
textDrawer := textdraw.Drawer { }
|
||||
textDrawer.SetFace(defaultfont.FaceRegular)
|
||||
textDrawer.SetText ([]rune (fmt.Sprintf (
|
||||
"%dms\n%dus",
|
||||
|
@ -38,7 +38,7 @@ func run () {
|
||||
popups.NewDialog(popups.DialogKindInfo, "", "Sike!")
|
||||
})
|
||||
mouse := testing.NewMouse()
|
||||
input := basicElements.NewTextBox("Write some text", "")
|
||||
input := basicElements.NewTextBox("Write some text", "fkjasdklfja\ndjkfhadf")
|
||||
form := basicElements.NewContainer(basicLayouts.Vertical { true, false})
|
||||
form.Adopt(basicElements.NewLabel("I have:", false), false)
|
||||
form.Adopt(basicElements.NewSpacer(true), false)
|
||||
|
@ -218,39 +218,44 @@ func (setter *TypeSetter) For (iterator RuneIterator) {
|
||||
|
||||
// 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 }
|
||||
if setter.face == nil { return }
|
||||
metrics := setter.face.Metrics()
|
||||
|
||||
// find the first line who's bottom bound is greater than position.Y. if
|
||||
// we haven't found it, then dont set the line variable (defaults to the
|
||||
// last line)
|
||||
metrics := setter.face.Metrics()
|
||||
line := setter.lines[len(setter.lines) - 1]
|
||||
lineSize := 0
|
||||
for _, curLine := range setter.lines {
|
||||
for _, curWord := range curLine.Words {
|
||||
index += len(curWord.Runes)
|
||||
lineSize += len(curWord.Runes)
|
||||
}
|
||||
if curLine.BreakAfter { lineSize ++ }
|
||||
index += lineSize
|
||||
|
||||
if curLine.Y + metrics.Descent > position.Y {
|
||||
line = curLine
|
||||
break
|
||||
}
|
||||
}
|
||||
index -= lineSize
|
||||
|
||||
if line.Words == nil { return }
|
||||
|
||||
// find the first rune who's right bound is greater than position.X.
|
||||
for _, curWord := range line.Words {
|
||||
for _, curChar := range curWord.Runes {
|
||||
if curWord.X + curChar.X + curChar.Width > position.X {
|
||||
break
|
||||
}
|
||||
x := curWord.X + curChar.X + curChar.Width
|
||||
println(index, x.Round(), position.X.Round())
|
||||
if x > position.X { goto foundRune }
|
||||
index ++
|
||||
}
|
||||
if line.BreakAfter { index ++ }
|
||||
}
|
||||
|
||||
foundRune:
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ func testLargeRecHeight (test *testing.T, width int) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetterIndex (test *testing.T) {
|
||||
func TestSetterPosition (test *testing.T) {
|
||||
setter := TypeSetter { }
|
||||
setter.SetText([]rune("The quick brown fox\njumped over the lazy dog."))
|
||||
setter.SetFace(defaultfont.FaceRegular)
|
||||
@ -94,4 +94,37 @@ func TestSetterIndex (test *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetterIndex (test *testing.T) {
|
||||
setter := TypeSetter { }
|
||||
setter.SetText([]rune("The quick brown fox\njumped over the lazy dog."))
|
||||
setter.SetFace(defaultfont.FaceRegular)
|
||||
|
||||
pos := fixed.P(3, 8)
|
||||
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(-59, 230)
|
||||
index = setter.AtPosition(pos)
|
||||
expect = 19
|
||||
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 = 45
|
||||
if index != expect {
|
||||
test.Fatalf (
|
||||
`setter index at (%d, %d): %d, expected: %d`,
|
||||
pos.X.Round(), pos.Y.Round(), index, expect)
|
||||
}
|
||||
}
|
||||
|
||||
const lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Fermentum et sollicitudin ac orci phasellus egestas tellus rutrum. Aliquam vestibulum morbi blandit cursus risus at ultrices mi. Gravida dictum fusce ut placerat. Cursus metus aliquam eleifend mi in nulla posuere. Sit amet nulla facilisi morbi tempus iaculis urna id. Amet volutpat consequat mauris nunc congue nisi vitae. Varius duis at consectetur lorem donec massa sapien faucibus et. Vitae elementum curabitur vitae nunc sed velit dignissim. In hac habitasse platea dictumst quisque sagittis purus. Enim nulla aliquet porttitor lacus luctus accumsan tortor. Lectus magna fringilla urna porttitor rhoncus dolor purus non.\n\nNon pulvinar neque laoreet suspendisse. Viverra adipiscing at in tellus integer. Vulputate dignissim suspendisse in est ante. Purus in mollis nunc sed id semper. In est ante in nibh mauris cursus. Risus pretium quam vulputate dignissim suspendisse in est. Blandit aliquam etiam erat velit scelerisque in dictum. Lectus quam id leo in. Odio tempor orci dapibus ultrices in iaculis. Pharetra sit amet aliquam id. Elit ut aliquam purus sit. Egestas dui id ornare arcu odio ut sem nulla pharetra. Massa tempor nec feugiat nisl pretium fusce id. Dui accumsan sit amet nulla facilisi morbi. A lacus vestibulum sed arcu non odio euismod. Nam libero justo laoreet sit amet cursus. Mattis rhoncus urna neque viverra justo nec. Mauris augue neque gravida in fermentum et sollicitudin ac. Vulputate mi sit amet mauris. Ut sem nulla pharetra diam sit amet."
|
||||
|
Reference in New Issue
Block a user