mirror of
https://github.com/bloeys/gopad.git
synced 2025-12-29 15:08:21 +00:00
Basic cursor
This commit is contained in:
49
editor.go
49
editor.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -9,7 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
linesPerNode int = 100
|
linesPerNode = 100
|
||||||
|
textPadding = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
type Line struct {
|
type Line struct {
|
||||||
@ -26,8 +29,8 @@ type Editor struct {
|
|||||||
FilePath string
|
FilePath string
|
||||||
FileContents string
|
FileContents string
|
||||||
|
|
||||||
CursorX int
|
MouseX int
|
||||||
CursorY int
|
MouseY int
|
||||||
|
|
||||||
SelectionStart int
|
SelectionStart int
|
||||||
SelectionLength int
|
SelectionLength int
|
||||||
@ -39,14 +42,13 @@ type Editor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) SetCursorPos(x, y int) {
|
func (e *Editor) SetCursorPos(x, y int) {
|
||||||
e.CursorX = x
|
e.MouseX = x
|
||||||
e.CursorY = y
|
e.MouseY = y
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
||||||
|
|
||||||
e.CursorY = clampInt(e.CursorY, 0, e.LineCount)
|
origDrawStartPos := *drawStartPos
|
||||||
e.CursorX = clampInt(e.CursorX, 0, e.GetLineCharCount(e.CursorY))
|
|
||||||
|
|
||||||
//Draw window
|
//Draw window
|
||||||
imgui.SetNextWindowPos(*drawStartPos)
|
imgui.SetNextWindowPos(*drawStartPos)
|
||||||
@ -57,19 +59,40 @@ func (e *Editor) Render(drawStartPos, winSize *imgui.Vec2) {
|
|||||||
imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, settings.TextSelectionColor)
|
imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, settings.TextSelectionColor)
|
||||||
|
|
||||||
//Add padding to text
|
//Add padding to text
|
||||||
drawStartPos.X += 10
|
drawStartPos.X += textPadding
|
||||||
drawStartPos.Y += 10
|
drawStartPos.Y += textPadding
|
||||||
|
|
||||||
//Draw lines
|
//Draw lines
|
||||||
lineHeight := imgui.TextLineHeightWithSpacing()
|
lineHeight := imgui.TextLineHeightWithSpacing()
|
||||||
|
charWidth := imgui.CalcTextSize("a", false, 1000).X
|
||||||
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(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
|
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 {
|
func (e *Editor) GetLine(lineNum int) *Line {
|
||||||
@ -163,6 +186,10 @@ func clampInt(x, min, max int) int {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func roundF32(x float32) float32 {
|
||||||
|
return float32(math.Round(float64(x)))
|
||||||
|
}
|
||||||
|
|
||||||
func NewScratchEditor() *Editor {
|
func NewScratchEditor() *Editor {
|
||||||
|
|
||||||
e := &Editor{
|
e := &Editor{
|
||||||
|
|||||||
5
main.go
5
main.go
@ -166,6 +166,11 @@ func (g *Gopad) Update() {
|
|||||||
g.showErrorPopup()
|
g.showErrorPopup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if input.MouseClicked(sdl.BUTTON_LEFT) {
|
||||||
|
x, y := input.GetMousePos()
|
||||||
|
g.getActiveEditor().SetCursorPos(int(x), int(y))
|
||||||
|
}
|
||||||
|
|
||||||
//Close editor if needed
|
//Close editor if needed
|
||||||
if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) {
|
if input.KeyDown(sdl.K_LCTRL) && input.KeyClicked(sdl.K_w) {
|
||||||
g.closeEditor(g.activeEditor)
|
g.closeEditor(g.activeEditor)
|
||||||
|
|||||||
Reference in New Issue
Block a user