diff --git a/editor.go b/editor.go index 983115d..8176f52 100755 --- a/editor.go +++ b/editor.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "math" "os" "path/filepath" @@ -9,7 +11,8 @@ import ( ) const ( - linesPerNode int = 100 + linesPerNode = 100 + textPadding = 10 ) type Line struct { @@ -26,8 +29,8 @@ type Editor struct { FilePath string FileContents string - CursorX int - CursorY int + MouseX int + MouseY int SelectionStart int SelectionLength int @@ -39,14 +42,13 @@ type Editor struct { } func (e *Editor) SetCursorPos(x, y int) { - e.CursorX = x - e.CursorY = y + e.MouseX = x + e.MouseY = y } func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) { - e.CursorY = clampInt(e.CursorY, 0, e.LineCount) - e.CursorX = clampInt(e.CursorX, 0, e.GetLineCharCount(e.CursorY)) + origDrawStartPos := *drawStartPos //Draw window imgui.SetNextWindowPos(*drawStartPos) @@ -57,19 +59,40 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) { imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, settings.TextSelectionColor) //Add padding to text - drawStartPos.X += 10 - drawStartPos.Y += 10 + drawStartPos.X += textPadding + drawStartPos.Y += textPadding //Draw lines lineHeight := imgui.TextLineHeightWithSpacing() + charWidth := imgui.CalcTextSize("a", false, 1000).X 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() for i := 0; i < linesToDraw; i++ { - dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(e.CursorY+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 } + + fmt.Printf("line,col: %v,%v\n", clickedLine, clickedCol) + + origDrawStartPos.X += textPadding + origDrawStartPos.Y += textPadding + lineStart := imgui.Vec2{ + X: origDrawStartPos.X + float32(clickedCol)*charWidth, + Y: origDrawStartPos.Y + float32(clickedLine)*lineHeight - lineHeight*0.25, + } + lineEnd := imgui.Vec2{ + X: origDrawStartPos.X + float32(clickedCol)*charWidth, + Y: origDrawStartPos.Y + float32(clickedLine)*lineHeight + lineHeight*0.75, + } + dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(imgui.Vec4{Z: 0.7, W: 1}), 5) } func (e *Editor) GetLine(lineNum int) *Line { @@ -163,6 +186,10 @@ func clampInt(x, min, max int) int { return x } +func roundF32(x float32) float32 { + return float32(math.Round(float64(x))) +} + func NewScratchEditor() *Editor { e := &Editor{ diff --git a/main.go b/main.go index d7dc075..679271f 100755 --- a/main.go +++ b/main.go @@ -166,6 +166,11 @@ func (g *Gopad) Update() { g.showErrorPopup() } + if input.MouseClicked(sdl.BUTTON_LEFT) { + x, y := input.GetMousePos() + g.getActiveEditor().SetCursorPos(int(x), int(y)) + } + //Close editor if needed if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) { g.closeEditor(g.activeEditor)