Basic cursor

This commit is contained in:
bloeys
2022-02-26 09:09:55 +04:00
parent ceda79cffb
commit d99b122ee5
2 changed files with 43 additions and 11 deletions

View File

@ -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{

View File

@ -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)