mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 15:08:21 +00:00
Working on tab support and cursor
This commit is contained in:
60
editor.go
60
editor.go
@ -48,8 +48,6 @@ func (e *Editor) SetCursorPos(x, y int) {
|
|||||||
|
|
||||||
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
||||||
|
|
||||||
origDrawStartPos := *drawStartPos
|
|
||||||
|
|
||||||
//Draw window
|
//Draw window
|
||||||
imgui.SetNextWindowPos(*drawStartPos)
|
imgui.SetNextWindowPos(*drawStartPos)
|
||||||
imgui.SetNextWindowSize(*winSize)
|
imgui.SetNextWindowSize(*winSize)
|
||||||
@ -61,6 +59,7 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
|||||||
//Add padding to text
|
//Add padding to text
|
||||||
drawStartPos.X += textPadding
|
drawStartPos.X += textPadding
|
||||||
drawStartPos.Y += textPadding
|
drawStartPos.Y += textPadding
|
||||||
|
paddedDrawStartPos := *drawStartPos
|
||||||
|
|
||||||
//Draw lines
|
//Draw lines
|
||||||
lineHeight := imgui.TextLineHeightWithSpacing()
|
lineHeight := imgui.TextLineHeightWithSpacing()
|
||||||
@ -68,31 +67,62 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
|||||||
linesToDraw := int(winSize.Y / lineHeight)
|
linesToDraw := int(winSize.Y / lineHeight)
|
||||||
// println("Lines to draw:", linesToDraw)
|
// println("Lines to draw:", linesToDraw)
|
||||||
|
|
||||||
cx := clampInt(e.MouseX-int(drawStartPos.X), 0, int(winSize.X))
|
|
||||||
cy := clampInt(e.MouseY-int(drawStartPos.Y), 0, int(winSize.Y))
|
|
||||||
|
|
||||||
clickedLine := clampInt(cy/int(lineHeight), 0, e.LineCount)
|
|
||||||
clickedCol := clampInt(cx/int(charWidth), 0, e.GetLineCharCount(clickedLine))
|
|
||||||
|
|
||||||
dl := imgui.WindowDrawList()
|
dl := imgui.WindowDrawList()
|
||||||
for i := 0; i < linesToDraw; i++ {
|
for i := 0; i < linesToDraw; i++ {
|
||||||
dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(0+i).chars))
|
dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(0+i).chars))
|
||||||
drawStartPos.Y += lineHeight
|
drawStartPos.Y += lineHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cx := clampInt(e.MouseX-int(paddedDrawStartPos.X), 0, int(winSize.X))
|
||||||
|
cy := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, int(winSize.Y))
|
||||||
|
|
||||||
|
clickedLine := clampInt(cy/int(lineHeight), 0, e.LineCount)
|
||||||
|
clickedCol := cx / int(charWidth)
|
||||||
fmt.Printf("line,col: %v,%v\n", clickedLine, clickedCol)
|
fmt.Printf("line,col: %v,%v\n", clickedLine, clickedCol)
|
||||||
|
|
||||||
origDrawStartPos.X += textPadding
|
eee := e.GetLine(clickedLine)
|
||||||
origDrawStartPos.Y += textPadding
|
tabCount, tabChars := getTabs(eee, clickedCol)
|
||||||
|
|
||||||
|
maxCol := len(eee.chars) - 1
|
||||||
|
if tabCount > 0 {
|
||||||
|
maxCol += clampInt(tabCount*settings.TabSize-1, 0, math.MaxInt)
|
||||||
|
}
|
||||||
|
finalCol := clampInt(clickedCol+tabChars, 0, maxCol)
|
||||||
|
if len(eee.chars) > 0 && finalCol > 0 {
|
||||||
|
x := finalCol - tabCount*settings.TabSize
|
||||||
|
println("!!!!", len(string(eee.chars)), "; C:", string(eee.chars[x]))
|
||||||
|
}
|
||||||
|
|
||||||
|
lineX := paddedDrawStartPos.X + float32(finalCol)*charWidth
|
||||||
lineStart := imgui.Vec2{
|
lineStart := imgui.Vec2{
|
||||||
X: origDrawStartPos.X + float32(clickedCol)*charWidth,
|
X: lineX,
|
||||||
Y: origDrawStartPos.Y + float32(clickedLine)*lineHeight - lineHeight*0.25,
|
Y: paddedDrawStartPos.Y + float32(clickedLine)*lineHeight - lineHeight*0.25,
|
||||||
}
|
}
|
||||||
lineEnd := imgui.Vec2{
|
lineEnd := imgui.Vec2{
|
||||||
X: origDrawStartPos.X + float32(clickedCol)*charWidth,
|
X: lineX,
|
||||||
Y: origDrawStartPos.Y + float32(clickedLine)*lineHeight + lineHeight*0.75,
|
Y: paddedDrawStartPos.Y + float32(clickedLine)*lineHeight + lineHeight*0.75,
|
||||||
}
|
}
|
||||||
dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(imgui.Vec4{Z: 0.7, W: 1}), 5)
|
|
||||||
|
thickness := 0.2 * charWidth
|
||||||
|
dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(imgui.Vec4{Z: 0.7, W: 1}), thickness)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTabs(l *Line, col int) (tabCount, charsToOffsetBy int) {
|
||||||
|
|
||||||
|
for i := 0; i < len(l.chars) && i < col; i++ {
|
||||||
|
if l.chars[i] == '\t' {
|
||||||
|
tabCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
charsToOffsetBy = tabCount * settings.TabSize
|
||||||
|
if tabCount == 0 {
|
||||||
|
return tabCount, charsToOffsetBy
|
||||||
|
}
|
||||||
|
|
||||||
|
charsToRemove := col / charsToOffsetBy * settings.TabSize
|
||||||
|
charsToRemove += col % charsToOffsetBy
|
||||||
|
return tabCount, clampInt(charsToOffsetBy-charsToRemove, 0, charsToOffsetBy)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) GetLine(lineNum int) *Line {
|
func (e *Editor) GetLine(lineNum int) *Line {
|
||||||
|
|||||||
@ -5,4 +5,5 @@ import "github.com/inkyblackness/imgui-go/v4"
|
|||||||
var (
|
var (
|
||||||
TextSelectionColor imgui.Vec4 = imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4}
|
TextSelectionColor imgui.Vec4 = imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4}
|
||||||
EditorBgColor imgui.Vec4 = imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}
|
EditorBgColor imgui.Vec4 = imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}
|
||||||
|
TabSize int = 4
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user